-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcp_acceptor.h
125 lines (94 loc) · 2.4 KB
/
tcp_acceptor.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* tcp_acceptor.h
*
* Created on: 2011-10-31
* Author: gxl2007@hotmail.com
*/
#ifndef TCP_ACCEPTOR_H_
#define TCP_ACCEPTOR_H_
#include <errno.h>
#include <unistd.h>
#include "io_handler.h"
#include "network_util.h"
#include "epoll_reactor.h"
namespace xlnet
{
class tcp_acceptor : public io_handler
{
public:
enum
{
MAX_ACCEPT_ONCE = 32 ,
};
public:
tcp_acceptor();
virtual ~tcp_acceptor();
/*
* @brief create listen socket and add to reactor
* @return 0 if success
*/
int init(epoll_reactor* reactor,const char* host,int port) ;
void fini() ;
int fd() const { return m_fd ; } ;
protected:
/*
* @brief call when accept new fd , implemented by inherent class
* @return 0 if success
*/
virtual int on_connection(int fd,sa_in_t* addr) = 0 ;
protected:
virtual void on_read(int fd) ;
virtual void on_write(int fd) ;
virtual void on_error(int fd) ;
protected:
epoll_reactor* m_reactor ;
int m_fd ;
};
template<typename T,int (T::*callback)(int fd,sa_in_t* addr)=&T::on_connection>
class tcp_service_handler : public io_handler
{
public:
enum
{
MAX_ACCEPT_ONCE = 256 ,
};
public:
explicit tcp_service_handler():m_owner(NULL){ } ;
virtual ~tcp_service_handler() { } ;
/*
* @brief owner must implement below interface
* int on_connection(int cfd,sa_in_t* caddr)
*/
void set_owner(T* owner)
{
m_owner = owner ;
} ;
protected:
virtual void on_read(int fd)
{
socklen_t addr_len;
sa_in_t caddr = {0};
addr_len = sizeof(caddr);
int cfd = 0 ;
for(int i = 0 ; i< MAX_ACCEPT_ONCE ; ++i )
{
cfd = accept(fd,(sa_t*)&caddr,&addr_len) ;
if(cfd >= 0 )
{
if ( (m_owner->*callback)(cfd,&caddr) != 0 ) close(cfd) ;
}
else
{
if(errno == EAGAIN || errno ==EMFILE || errno ==ENFILE ) break ;
else if ( errno == EINTR || errno ==ECONNABORTED ) continue ;
else on_error(fd);
}
}
}
virtual void on_write(int fd) { } ;
virtual void on_error(int fd) { close(fd) ; } ;
protected:
T* m_owner ;
};
}
#endif /* TCP_ACCEPTOR_H_ */