-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathcp_server.cpp
551 lines (438 loc) · 13.5 KB
/
cp_server.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/***********************************************************************
* Copyright (c) 2012, Baidu Inc. All rights reserved.
*
* Licensed under the BSD License
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* license.txt
*********************************************************************/
#ifdef _WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include "time_util.h"
#include "cp_server.h"
#include "binary_protocol.h"
#include "server_task.h"
#include "log.h"
#include "connection_manager.h"
#include "sema.h"
namespace bgcc {
class CClientContext {
private:
OVERLAPPED *m_pol;
WSABUF *m_pwbuf;
int m_nTotalBytes;
int m_nOKBytes;
SOCKET m_Socket;
int m_nOpCode;
char* m_szBuffer;
int m_nBufferLen;
public:
enum {
S_READ_HRAD,
S_READ_BODY
};
void SetOpCode(int n) {
m_nOpCode = n;
}
int GetOpCode() {
return m_nOpCode;
}
void SetTotalBytes(int n) {
m_nTotalBytes = n;
}
int GetTotalBytes() {
return m_nTotalBytes;
}
void SetOKBytes(int n) {
m_nOKBytes = n;
}
void IncrOKBytes(int n) {
m_nOKBytes += n;
}
int GetOKBytes() {
return m_nOKBytes;
}
void SetSocket(SOCKET s) {
m_Socket = s;
}
SOCKET GetSocket() {
return m_Socket;
}
void SetBuffer(int size) {
if (m_szBuffer) {
delete[] m_szBuffer;
}
m_szBuffer = new char[size];
m_nBufferLen = size;
ZeroBuffer();
}
void GetBuffer(char *szBuffer) {
strcpy(szBuffer, m_szBuffer);
}
char* GetBuffer() {
return m_szBuffer;
}
void ZeroBuffer() {
ZeroMemory(m_szBuffer, m_nBufferLen);
}
void SetWSABUFLength(int nLength) {
m_pwbuf->len = nLength;
}
int GetWSABUFLength() {
return m_pwbuf->len;
}
WSABUF* GetWSABUFPtr() {
return m_pwbuf;
}
OVERLAPPED* GetOVERLAPPEDPtr() {
return m_pol;
}
void ResetWSABUF() {
m_pwbuf->buf = m_szBuffer;
m_pwbuf->len = m_nBufferLen;
}
int GetBufferLen() {
return m_nBufferLen;
}
CClientContext() {
m_pol = new OVERLAPPED;
m_pwbuf = new WSABUF;
ZeroMemory(m_pol, sizeof(OVERLAPPED));
m_Socket = SOCKET_ERROR;
m_szBuffer = NULL;
m_nBufferLen = 0;
m_pwbuf->buf = NULL;
m_pwbuf->len = 0;
m_nOpCode = S_READ_HRAD;
m_nTotalBytes = 0;
m_nOKBytes = 0;
}
~CClientContext()
{
delete m_pol;
delete m_pwbuf;
if (m_szBuffer) {
delete[] m_szBuffer;
}
}
};
static bool associate_with_iocp(CClientContext* Contex, CpServer* server) {
HANDLE hTemp = CreateIoCompletionPort((HANDLE)Contex->GetSocket(),
server->get_iocp(), (ULONG_PTR)Contex, 0);
if (NULL == hTemp) {
return false;
}
else {
return true;
}
}
static void accept_connection(SOCKET ListenSocket, CpServer* server) {
sockaddr_in ClientAddress;
int nClientLength = sizeof(ClientAddress);
SOCKET Socket = accept(ListenSocket, (sockaddr*)&ClientAddress, &nClientLength);
int32_t tv = 5000;
setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(int32_t));
if (INVALID_SOCKET == Socket) {
return;
}
CClientContext* context = new CClientContext();
context->SetSocket(Socket);
if (true == associate_with_iocp(context, server))
{
WSABUF *p_wbuf = context->GetWSABUFPtr();
OVERLAPPED *p_ol = context->GetOVERLAPPEDPtr();
context->SetBuffer(20);
context->SetTotalBytes(20);
context->ResetWSABUF();
//Get data.
DWORD dwFlags = 0;
DWORD dwBytes = 0;
int nBytesRecv = WSARecv(context->GetSocket(), p_wbuf, 1,
&dwBytes, &dwFlags, p_ol, NULL);
}
}
static DWORD WINAPI accept_thread_func(LPVOID lParam)
{
CpServer* server = (CpServer*)lParam;
SOCKET ListenSocket = server->get_listen_socket();
HANDLE shutdown_event = server->get_shutdown_event();
WSAEVENT accept_event = server->get_accept_event();
WSANETWORKEVENTS WSAEvents;
while(WAIT_OBJECT_0 != WaitForSingleObject(shutdown_event, 0))
{
if (WSA_WAIT_TIMEOUT != WSAWaitForMultipleEvents(1, &accept_event, FALSE, 100, FALSE))
{
WSAEnumNetworkEvents(ListenSocket, accept_event, &WSAEvents);
if ((WSAEvents.lNetworkEvents & FD_ACCEPT) && (0 == WSAEvents.iErrorCode[FD_ACCEPT_BIT]))
{
accept_connection(ListenSocket, server);
}
}
}
return 0;
}
static void enroll_proxy(const std::string& proxy_name, socket_t fd) {
BGCC_TRACE("bgcc", "enroll_proxy name %s, %d", proxy_name.c_str(), fd);
bgcc::ConnectionManager::get_instance()->enroll(proxy_name, fd);
}
static DWORD WINAPI worker_thread(LPVOID lpParam) {
CpServer* server = (CpServer*)lpParam;
void *lpContext = NULL;
OVERLAPPED *pOverlapped = NULL;
CClientContext *pClientContext = NULL;
DWORD dwBytesTransfered = 0;
int nBytesRecv = 0;
int nBytesSent = 0;
DWORD dwBytes = 0, dwFlags = 0;
while (WAIT_OBJECT_0 != WaitForSingleObject(server->get_shutdown_event(), 0)) {
BOOL bReturn = GetQueuedCompletionStatus(
server->get_iocp(),
&dwBytesTransfered,
(LPDWORD)&lpContext,
&pOverlapped,
INFINITE);
BGCC_TRACE("bgcc", "got a status");
if (NULL == lpContext) { //Òì³£´¦Àí
BGCC_DEBUG("bgcc", "contest is null");
break;
}
pClientContext = (CClientContext *)lpContext;
WSABUF *p_wbuf = pClientContext->GetWSABUFPtr();
OVERLAPPED *p_ol = pClientContext->GetOVERLAPPEDPtr();
if ((FALSE == bReturn) || ((TRUE == bReturn) && (0 == dwBytesTransfered))) {
closesocket(pClientContext->GetSocket());
delete pClientContext;
BGCC_DEBUG("bgcc", "GetQueuedCompletionStatus end");
continue;
}
BGCC_DEBUG("bgcc", "dwBytesTransfered : %d", dwBytesTransfered);
pClientContext->IncrOKBytes(dwBytesTransfered);
if (pClientContext->GetOKBytes() < pClientContext->GetTotalBytes()) {
p_wbuf->buf = pClientContext->GetBuffer() + pClientContext->GetOKBytes();
p_wbuf->len = pClientContext->GetBufferLen() - pClientContext->GetOKBytes();
BGCC_DEBUG("bgcc", "read %s again, ok: %d, total: %d", (pClientContext->GetOpCode() == CClientContext::S_READ_HRAD ?
"head" : "body"), pClientContext->GetOKBytes(), pClientContext->GetTotalBytes());
dwFlags = 0;
nBytesRecv = WSARecv(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, &dwFlags, p_ol, NULL);
if ((SOCKET_ERROR == nBytesRecv) && (WSA_IO_PENDING != WSAGetLastError()))
{
closesocket(pClientContext->GetSocket());
delete pClientContext;
}
continue;
}
if (pClientContext->GetOpCode() == CClientContext::S_READ_HRAD) {
pClientContext->SetOpCode(CClientContext::S_READ_BODY);
char* phead = pClientContext->GetBuffer();
int32_t ndata = (int32_t)ntohl(*(uint32_t*)(phead + 16));
BGCC_DEBUG("bgcc", "ndata: %d", ndata);
pClientContext->SetBuffer(ndata);
pClientContext->SetTotalBytes(ndata);
pClientContext->SetOKBytes(0);
pClientContext->ResetWSABUF();
dwFlags = 0;
nBytesRecv = WSARecv(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, &dwFlags, p_ol, NULL);
if ((SOCKET_ERROR == nBytesRecv) && (WSA_IO_PENDING != WSAGetLastError()))
{
closesocket(pClientContext->GetSocket());
delete pClientContext;
}
continue;
}
BGCC_DEBUG("bgcc", "op code :%d", pClientContext->GetOpCode());
char* szBuffer = pClientContext->GetBuffer();
char* p_item_data = szBuffer;
int32_t processor_name_len = (int32_t)ntohl(*(uint32_t*)(p_item_data));
BGCC_DEBUG("bgcc", "processor_name_len: %d", processor_name_len);
std::string processor_name(p_item_data + 4, processor_name_len);
if (processor_name == "bgcc::BaseProcessor_enroll") {
int32_t func_name_len = (int32_t)ntohl(*(uint32_t*)(
p_item_data + 4 + processor_name_len + 8));
int32_t proxy_name_len = (int32_t)ntohl(*(uint32_t*)(
p_item_data + 4 + processor_name_len + 8
+ 4 + func_name_len));
std::string proxy_name(p_item_data + 4 + processor_name_len + 8
+ 4 + func_name_len + 4, proxy_name_len);
enroll_proxy(proxy_name, pClientContext->GetSocket());
continue;
}
SharedPointer<IProcessor> processor = server->get_service_manager()->get_service(processor_name);
if (processor.is_valid()) {
BGCC_TRACE("bgcc", "create a task. processor name %s", processor_name.c_str());
SharedPointer<ITransport> trans(new ServerPeerSocket(pClientContext->GetSocket()));
SharedPointer<IProtocol> proto(new BinaryProtocol(trans));
int32_t body_size = pClientContext->GetTotalBytes();
char* p = (char*)bgcc::Mempool::get_instance()->get_mem_block(body_size);
memmove(p, p_item_data, body_size);
BGCC_DEBUG("bgcc", "server body size %d", body_size);
SharedPointer<Runnable> task(new ServerTask(processor, p, body_size, proto));
server->get_thread_pool()->addTask(task);
}
else {
BGCC_TRACE("bgcc", "Cannot find processor `%s`. Try to find ._baseservice_2012", processor_name.c_str());
processor = server->get_service_manager()->get_service("._baseservice_2012");
if (processor.is_valid()) {
BGCC_TRACE("bgcc", "find ._baseservice_2012");
SharedPointer<BinaryProtocol> bp(new BinaryProtocol(SharedPointer<ITransport>(NULL)));
bp->writeMessageBegin("xx", "__service_not_found", bgcc::CALL, 0);
bp->writeInt32(0);
bp->writeString("__service_not_found");
bp->writeBool(false);
NBDataBuffer* db = bp->get_data_buffer();
void* data = NULL;
int32_t size;
db->get_data(&data, size);
char* p = (char*)bgcc::Mempool::get_instance()->get_mem_block(size - 20);
memmove(p, ((char*)data) + 20, size - 20);
SharedPointer<ITransport> trans(new ServerPeerSocket(pClientContext->GetSocket()));
SharedPointer<IProtocol> proto(new BinaryProtocol(trans));
SharedPointer<Runnable> task(new ServerTask(processor, p, size - 20, proto));
server->get_thread_pool()->addTask(task);
}
}
pClientContext->SetBuffer(20);
pClientContext->SetTotalBytes(20);
pClientContext->ResetWSABUF();
pClientContext->SetOKBytes(0);
pClientContext->SetOpCode(CClientContext::S_READ_HRAD);
DWORD dwFlags = 0;
DWORD dwBytes = 0;
int nBytesRecv = WSARecv(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, &dwFlags, p_ol, NULL);
if ((SOCKET_ERROR == nBytesRecv) && (WSA_IO_PENDING != WSAGetLastError()))
{
closesocket(pClientContext->GetSocket());
delete pClientContext;
continue;
}
}
return 0;
}
CpServer::CpServer(ServiceManager* service_manager,
ThreadPool* thread_pool,
uint16_t port,
const std::string& node)
:_service_manager(service_manager),
_thread_pool(thread_pool),
_port(port),
_state(S_UNINIT),
_hiocp(NULL),
_haccept_thread(NULL),
_hshutdown_event(NULL),
_node(node)
{
}
int32_t CpServer::init() {
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
#endif
if (S_UNINIT != _state) {
return E_BGCC_SERVER_ALREADY_INIT;
}
WSADATA wsaData;
int ret;
ret = WSAStartup(MAKEWORD(2,2), &wsaData);
if (NO_ERROR != ret) {
return -1;
}
ret = init_iocp();
if (0 != ret) {
return -1;
}
_hshutdown_event = CreateEvent(NULL, TRUE, FALSE, NULL);
_state = S_INIT;
return 0;
}
int32_t CpServer::init_iocp() {
_hiocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0 );
if ( NULL == _hiocp) {
return -1;
}
DWORD nThreadID;
CreateThread(0, 0, worker_thread, (void *)this, 0, &nThreadID);
return 0;
}
int32_t CpServer::socket_init() {
struct sockaddr_in ServerAddress;
_listen_socket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
if (INVALID_SOCKET == _listen_socket) {
return -1;
}
ZeroMemory((char *)&ServerAddress, sizeof(ServerAddress));
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_addr.s_addr = inet_addr(_node.c_str());
ServerAddress.sin_port = htons(_port);
if (SOCKET_ERROR == bind(_listen_socket, (struct sockaddr *) &ServerAddress, sizeof(ServerAddress))) {
closesocket(_listen_socket);
return -1;
}
if (SOCKET_ERROR == listen(_listen_socket, SOMAXCONN)) {
closesocket(_listen_socket);
return -1;
}
_haccept_event = WSACreateEvent();
if (WSA_INVALID_EVENT == _haccept_event) {
return -1;
}
if (SOCKET_ERROR == WSAEventSelect(_listen_socket, _haccept_event, FD_ACCEPT)) {
WSACloseEvent(_haccept_event);
return -1;
}
DWORD nThreadID;
_haccept_thread = CreateThread(0, 0, accept_thread_func, (void *)this, 0, &nThreadID);
return 0;
}
int32_t CpServer::serve() {
int32_t ret;
if (0 != (ret = init())) {
return ret;
}
if (S_INIT != _state) {
return E_BGCC_SERVER_NEED_INIT;
}
ret = socket_init();
if (0 != ret) {
return E_BGCC_SERVER_CREATE_LISTENFD_FAILED;
}
WaitForSingleObject(_haccept_thread, INFINITE);
_state = S_SERVE;
return 0;
}
SOCKET CpServer::get_listen_socket() {
return _listen_socket;
}
WSAEVENT CpServer::get_accept_event() {
return _haccept_event;
}
HANDLE CpServer::get_shutdown_event(){
return _hshutdown_event;
}
int32_t CpServer::stop() {
if (S_SERVE != _state) {
return -1;
}
SetEvent(_hshutdown_event);
if (INVALID_SOCKET != _listen_socket) {
closesocket(_listen_socket);
_listen_socket = INVALID_SOCKET;
}
return 0;
}
HANDLE CpServer::get_iocp() {
return _hiocp;
}
ServiceManager* CpServer::get_service_manager() {
return _service_manager;
}
ThreadPool* CpServer::get_thread_pool() {
return _thread_pool;
}
}
#endif //_WIN32