-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptor_completed.h
73 lines (59 loc) · 1.56 KB
/
acceptor_completed.h
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
64
65
66
67
68
69
70
71
72
73
//acceptor_completed.h
#ifndef ACCEPTOR_COMPLETED_H
#define ACCEPTOR_COMPLETED_H
#include "addr.h"
#include "acceptor_basic.h"
#include "netevents.h"
#include "event_listener.h"
#include "namespdef.h"
#include <functional>
NAMESP_BEGIN
namespace net
{
///call back when accpet event was fired
///@param 1 netpeer ptr was accepted.
template<class NetPeerPtr>
using AcceptCb_c = std::function<void(NetPeerPtr)>;
//@param EventQueueT
//@param NetPeer
template<class Socket, class EventQueueT
,class NetPeer, class NetPeerPtr
>
class AcceptorCompleted: public EventListener<EventQueueT>
,public AcceptorBasic<Socket, NetInputEvent, AcceptCb_c<NetPeerPtr>>
{
public:
using netpeer_ptr = NetPeerPtr;
private:
using AcceptorBase=AcceptorBasic<Socket, NetInputEvent, AcceptCb_c<NetPeerPtr>>;
public:
AcceptorCompleted(EventQueueT* q, const AddrPair& local_addr)
:EventListener<EventQueueT>(q)
,AcceptorBase(local_addr)
{
this->_evt.reset( new typename AcceptorBase::event_t([this](){this->onInput();}) );
this->template listen(this->template fd(), this->_evt.get());
}
AcceptorCompleted(EventQueueT* q, unsigned short port)
:AcceptorCompleted(q, AddrPair{"", port})
{
}
~AcceptorCompleted(){
this->template unlisten(this->template fd());
}
private:
void onInput(){
Socket cli = this->_socket.accept();
if(cli.invalid()){
//log
return;
}
NetPeerPtr peer( new NetPeer(this->_evt_queue, cli) );
if(this->_accept_cb != nullptr){
this->_accept_cb(peer);
}
}
};
}//net
NAMESP_END
#endif /*ACCEPTOR_COMPLETED_H*/