-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptor_basic.h
57 lines (44 loc) · 883 Bytes
/
acceptor_basic.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
//acceptor_basic.h
#ifndef ACCEPTOR_BASIC_H
#define ACCEPTOR_BASIC_H
#include "addr.h"
#include "fddef.h"
#include "sockopt.h"
#include "namespdef.h"
#include <memory>
NAMESP_BEGIN
namespace net
{
template<class Socket, class Event, class AcceptCb>
class AcceptorBasic
{
public:
AcceptorBasic(const AddrPair& local_addr)
:_socket(local_addr)
{
open();
}
AcceptorBasic(unsigned short port)
:AcceptorBasic(AddrPair{"",port})
{
}
virtual ~AcceptorBasic(){
}
void listenOnAccept(const AcceptCb& cb){ _accept_cb = cb; }
fd_t fd()const{ return _socket.fd(); }
private:
void open(){
_socket.setOption(ReuseAddrOpt());
if(_socket.bind()){
_socket.listen();
}
}
protected:
using event_t = Event;
std::unique_ptr<event_t> _evt;
AcceptCb _accept_cb;
Socket _socket;
};
}//net
NAMESP_END
#endif /*ACCEPTOR_BASIC_H*/