-
Notifications
You must be signed in to change notification settings - Fork 1
/
Acceptor.cpp
42 lines (34 loc) · 1.33 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
#include "Acceptor.h"
#include "Connection.h"
Acceptor::Acceptor(EventLoop *eloop, const std::string &ip, const uint16_t port)
:eloop_(eloop),servsock_(createnonblocking()),acceptchannel_(eloop, servsock_.fd())
{
// servsock_ = new Socket(createnonblocking());
InetAddress servaddr(ip,port);
servsock_.setreuseaddr(true);
servsock_.setkeepalive(true);
servsock_.setreuseport(true);
servsock_.settcpnodelay(true);
servsock_.bind(servaddr);
servsock_.listen();
// acceptchannel_ = new Channel(eloop_, servsock_.fd());
// acceptchannel_->setreadcallback(std::bind(&Channel::newconnection, acceptchannel_,servsock_));
acceptchannel_.setreadcallback(std::bind(&Acceptor::newconnection, this));
acceptchannel_.enablereading(); //让epoll_wait() 监视servchannel读事件
}
Acceptor::~Acceptor()
{
// delete servsock_;
// delete acceptchannel_;
}
void Acceptor::setnewconnectioncb(std::function<void(std::unique_ptr<Socket>)> fn)
{
newconnectioncb_ = fn;
}
void Acceptor::newconnection() //处理新客服连接请求
{
InetAddress clientaddr; // 客户端的地址和协议。
std::unique_ptr<Socket> clientsock(new Socket(servsock_.accept(clientaddr)));
clientsock->setipport(clientaddr.ip(), clientaddr.port());
newconnectioncb_(std::move(clientsock));
}