Skip to content

Commit

Permalink
add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozhengcoder committed Nov 4, 2018
1 parent 8df56a1 commit 6743060
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
50 changes: 24 additions & 26 deletions muduo_tutorial/recipes/reactor/s04/Acceptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,44 @@

#include "Acceptor.h"

#include "logging/Logging.h"
#include "EventLoop.h"
#include "InetAddress.h"
#include "SocketsOps.h"
#include "logging/Logging.h"

#include <boost/bind.hpp>

using namespace muduo;

Acceptor::Acceptor(EventLoop* loop, const InetAddress& listenAddr)
: loop_(loop),
acceptSocket_(sockets::createNonblockingOrDie()),
acceptChannel_(loop, acceptSocket_.fd()), //创建一个channel的对象
listenning_(false)
Acceptor::Acceptor(EventLoop *loop, const InetAddress &listenAddr)
: loop_(loop), acceptSocket_(sockets::createNonblockingOrDie()),
acceptChannel_(loop, acceptSocket_.fd()), //创建一个channel的对象
listenning_(false)
{
acceptSocket_.setReuseAddr(true);
acceptSocket_.bindAddress(listenAddr);
acceptChannel_.setReadCallback(boost::bind(&Acceptor::handleRead, this));
acceptSocket_.setReuseAddr(true);
acceptSocket_.bindAddress(listenAddr);
acceptChannel_.setReadCallback(boost::bind(&Acceptor::handleRead, this));
}

void Acceptor::listen()
void Acceptor::listen()
{
loop_->assertInLoopThread();
listenning_ = true;
acceptSocket_.listen();
acceptChannel_.enableReading();
loop_->assertInLoopThread();
listenning_ = true;
acceptSocket_.listen();
acceptChannel_.enableReading();
}

void Acceptor::handleRead()
void Acceptor::handleRead()
{
loop_->assertInLoopThread();
InetAddress peerAddr(0);
//FIXME loop until no more
int connfd = acceptSocket_.accept(&peerAddr);
if (connfd >= 0) {
if (newConnectionCallback_) {
newConnectionCallback_(connfd, peerAddr);
} else {
sockets::close(connfd);
loop_->assertInLoopThread();
InetAddress peerAddr(0);
// FIXME loop until no more
int connfd = acceptSocket_.accept(&peerAddr);
if (connfd >= 0) {
if (newConnectionCallback_) {
newConnectionCallback_(connfd, peerAddr);
} else {
sockets::close(connfd);
}
}
}
}

4 changes: 4 additions & 0 deletions muduo_tutorial/recipes/reactor/s04/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 添加了acceptor 类



0 comments on commit 6743060

Please sign in to comment.