-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtcpserver.cpp
344 lines (263 loc) · 6.61 KB
/
tcpserver.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "tcpserver.h"
#define WRITE_QUEUE_MAX_SIZE 1000000
static void __SetNonblock(int fd)
{
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
}
static void __SetReuseaddr(int fd)
{
int reuse = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
}
static void __SetNodelay(int fd)
{
int nodelay = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));
}
//====================CTcpConnection================================
CTcpConnection::CTcpConnection(CTcpServer * pTcpServer, const SocketClientData_t & sClient)
{
m_pTcpServer = pTcpServer;
m_SocketClient = sClient;
m_Socket = sClient.Socket;
__SetNonblock(m_SocketClient.Socket);
__SetReuseaddr(m_SocketClient.Socket);
__SetNodelay(m_SocketClient.Socket);
m_Io.set(m_pTcpServer->GetLoop());
m_Timer.set(m_pTcpServer->GetLoop());
m_Io.set<CTcpConnection, &CTcpConnection::__IoCallback>(this);
m_Io.start(m_SocketClient.Socket, ev::READ);
m_Timer.set<CTcpConnection, &CTcpConnection::__TimerCallback>(this);
m_Timer.start(ev::tstamp(m_pTcpServer->GetConnectionTimeout()), m_pTcpServer->GetConnectionTimeout());
m_nLasttime = (int)time(0);
}
CTcpConnection::~CTcpConnection()
{
Close();
m_Io.stop();
m_Timer.stop();
}
void CTcpConnection::Send(COutputBuffer::Pointer pOutputBuffer)
{
if (m_WriteQueue.size() > WRITE_QUEUE_MAX_SIZE)
{
m_pTcpServer->OnClientSendError(m_SocketClient, 0);
return;
}
m_WriteQueue.push(pOutputBuffer);
m_Io.set(ev::READ|ev::WRITE);
}
void CTcpConnection::Close()
{
if (m_Socket > 0)
{
shutdown(m_Socket, SHUT_RDWR);
close(m_Socket);
m_Socket = 0;
}
}
void CTcpConnection::__IoCallback(ev::io &watcher, int revents)
{
if (EV_ERROR & revents)
{
__ErrorCallback();
return;
}
if (revents & EV_READ)
{
char szBuffer[4096];
ssize_t nRead = recv(watcher.fd, szBuffer, sizeof(szBuffer), 0);
if (nRead == 0)
{
m_pTcpServer->OnClientDisconnected(m_SocketClient, 0);
return;
}
else if (nRead < 0)
{
if (errno == EAGAIN)
{
// ignore
}
else
{
m_pTcpServer->OnClientRecvError(m_SocketClient, errno);
return;
}
}
else
{
m_nLasttime = (int)time(0);
m_pTcpServer->OnClientDataReceived(m_SocketClient, szBuffer, nRead);
}
}
if (revents & EV_WRITE)
{
if (m_WriteQueue.empty())
{
m_Io.set(ev::READ);
return;
}
COutputBuffer::Pointer pBuffer = m_WriteQueue.front();
ssize_t nWritten = write(watcher.fd, pBuffer->GetDataPos(), pBuffer->GetBytes());
if (nWritten < 0)
{
if (errno == EAGAIN)
{
// ignore
return;
}
else
{
m_pTcpServer->OnClientSendError(m_SocketClient, errno);
return;
}
}
m_nLasttime = (int)time(0);
pBuffer->m_nPos += nWritten;
if (pBuffer->GetBytes() == 0)
{
m_WriteQueue.pop();
}
if (m_WriteQueue.empty())
{
m_Io.set(ev::READ);
}
}
}
void CTcpConnection::__TimerCallback(ev::timer &watcher, int revents)
{
if (EV_ERROR & revents)
{
__ErrorCallback();
return;
}
if (revents & EV_TIMER)
{
if((int)time(0) - m_nLasttime >= TCP_CONNECTION_TIMEOUT)
{
m_pTcpServer->OnClientTimeout(m_SocketClient);
}
}
}
void CTcpConnection::__ErrorCallback()
{
m_pTcpServer->OnClientDisconnected(m_SocketClient, errno);
}
//====================CTcpServer================================
bool CTcpServer::Start(unsigned int IP, unsigned short Port)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(Port);
addr.sin_addr.s_addr = htonl(IP);
m_Socket = socket(PF_INET, SOCK_STREAM, 0);
__SetNonblock(m_Socket);
__SetReuseaddr(m_Socket);
if (bind(m_Socket, (struct sockaddr *)&addr, sizeof(addr)) != 0)
{
// bind error
return false;
}
listen(m_Socket, SOMAXCONN);
m_ListenIo.set(m_Loop);
m_Async.set(m_Loop);
m_ListenIo.set<CTcpServer, &CTcpServer::__Accept>(this);
m_ListenIo.start(m_Socket, ev::READ);
m_Async.set<CTcpServer, &CTcpServer::__AsyncCallback>(this);
m_Async.start();
CSigHandle::GetInstance(m_Loop);
m_Loop.loop();
return true;
}
bool CTcpServer::Stop()
{
shutdown(m_Socket, SHUT_RDWR);
close(m_Socket);
m_ListenIo.stop();
m_Async.stop();
m_Loop.unloop();
return true;
}
bool CTcpServer::Send(SocketClientData_t sClient, const char *pData, int nDataLen)
{
pthread_spin_lock(&m_Spinlock);
COutputBuffer::Pointer pOutputBuffer(new COutputBuffer(pData, nDataLen));
m_Functions.push_back(std::tr1::bind(&CTcpServer::__Send, this, sClient, pOutputBuffer));
m_Async.send();
pthread_spin_unlock(&m_Spinlock);
return true;
}
bool CTcpServer::CloseClient(SocketClientData_t sClient)
{
CTcpConnection::Pointer pTcpConnection = m_SocketInfoManager.Get(sClient);
if (pTcpConnection)
{
pTcpConnection->Close();
}
return true;
}
void CTcpServer::OnClientDisconnected(SocketClientData_t sClient, int nErrorCode)
{
m_pDataHandle->OnClientDisconnected(sClient, nErrorCode);
m_SocketInfoManager.Remove(sClient);
}
void CTcpServer::OnClientDataReceived(SocketClientData_t sClient, const char * pData, int nDataLen)
{
m_pDataHandle->OnClientDataReceived(sClient, pData, nDataLen);
}
void CTcpServer::OnClientRecvError(SocketClientData_t sClient, int nErrorCode)
{
m_pDataHandle->OnClientDisconnected(sClient, nErrorCode);
m_SocketInfoManager.Remove(sClient);
}
void CTcpServer::OnClientSendError(SocketClientData_t sClient, int nErrorCode)
{
m_pDataHandle->OnClientDisconnected(sClient, nErrorCode);
m_SocketInfoManager.Remove(sClient);
}
void CTcpServer::OnClientTimeout(SocketClientData_t sClient)
{
m_pDataHandle->OnClientDisconnected(sClient, 0);
m_SocketInfoManager.Remove(sClient);
}
bool CTcpServer::__Send(SocketClientData_t sClient, COutputBuffer::Pointer pOutputBuffer)
{
CTcpConnection::Pointer pTcpConnection = m_SocketInfoManager.Get(sClient);
if (pTcpConnection)
{
pTcpConnection->Send(pOutputBuffer);
return true;
}
return false;
}
void CTcpServer::__Accept(ev::io &watcher, int revents)
{
if (EV_ERROR & revents)
{
return;
}
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
int client_fd = accept(watcher.fd, (struct sockaddr *)&addr, &addrlen);
if (client_fd < 0)
{
return;
}
SocketClientData_t sClient = {ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port), client_fd};
CTcpConnection::Pointer pTcpConnection(new CTcpConnection(this, sClient));
m_SocketInfoManager.Add(sClient, pTcpConnection);
m_pDataHandle->OnClientConnected(sClient);
}
void CTcpServer::__AsyncCallback(ev::async &watcher, int revents)
{
FunctionList_t functions;
{
pthread_spin_lock(&m_Spinlock);
functions.swap(m_Functions);
pthread_spin_unlock(&m_Spinlock);
}
for (size_t i = 0; i < functions.size(); ++i)
{
functions[i]();
}
}