-
Notifications
You must be signed in to change notification settings - Fork 0
/
Acceptor.cpp
63 lines (57 loc) · 1.55 KB
/
Acceptor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include"Acceptor.h"
#include"base/Logging.h"
Acceptor::Acceptor(EventLoop *loop,const InetAddress &listenAddr)
: loop_(loop),
acceptChannel_(loop,sockets::createNonblocking(AF_INET)),
listenSocket_(acceptChannel_.fd()),
listenning_(false),
idleFd_(::open("/dev/null",O_RDONLY|O_CLOEXEC))
{
assert(idleFd_>=0);
assert(listenSocket_.fd()>=0);
listenSocket_.setReuseAddr(true);
assert(acceptChannel_.fd()==listenSocket_.fd());
listenSocket_.bindAddress(listenAddr);
acceptChannel_.setReadCallback(std::bind(&Acceptor::handleRead,this));
}
void Acceptor::listen()
{
loop_->assertInLoopThread();
listenning_=true;
assert(listenSocket_.fd()>=0);
listenSocket_.listen();
acceptChannel_.enableReading();
}
void Acceptor::handleRead()
{
loop_->assertInLoopThread();
InetAddress peeraddress(0);
Log<<"accepting in Acceptor::handleRead\n";
int connfd=listenSocket_.accept(&peeraddress);
if(connfd>=0)
{
//fix me
if(newConnectionCallback_)
{
Log<<"accepted a new conn in Acceptor::handleRead\n";
newConnectionCallback_(connfd,peeraddress);
}
}
else
{
if(errno==EMFILE)
{
::close(idleFd_),
::accept(listenSocket_.fd(),NULL,NULL);
::close(idleFd_);
idleFd_=::open("/dev/null",O_RDONLY|O_CLOEXEC);
}
sockets::socket_close(connfd);
}
}
Acceptor::~Acceptor()
{
acceptChannel_.disableAll();
acceptChannel_.remove();
::close(idleFd_);
}