-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiocp.cpp
More file actions
736 lines (578 loc) · 24.7 KB
/
iocp.cpp
File metadata and controls
736 lines (578 loc) · 24.7 KB
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include "iocp.h"
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::PostAcceptEx
Summary: accept the connection, and allocate I/O info structure
Args: PPER_IO_INFO p_acce_io_info
I/O info structure that descripes the connection
Modifies: [p_acce_io_info, link_pool]
Returns: OPSTATUS
operation status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
OPSTATUS IOCP::PostAcceptEx( PPER_IO_INFO p_acce_io_info )
{
ULONG u_bytes_ret = 0;
if ( (*(PPER_LINK_INFO *)p_acce_io_info->buffer = link_pool.LinkPoolAlloc()) == NULL )
{
return OP_FAILED;
}
ZeroMemory( &p_acce_io_info->overlapped, sizeof(OVERLAPPED) );
BOOL bRet = p_AcceptEx( p_ser_link_info->socket, ( *(PPER_LINK_INFO *)p_acce_io_info->buffer )->socket, &p_acce_io_info->buffer[sizeof(PPER_LINK_INFO)], 0, sizeof(SOCKADDR_IN) + 16, sizeof(SOCKADDR_IN) + 16, &u_bytes_ret, &p_acce_io_info->overlapped );
if ( !bRet && WSAGetLastError() != WSA_IO_PENDING )
{
printf( "#Err: post AcceptEx failed\n" );
return OP_FAILED;
}
return OP_SUCCESS;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::AgingThread
Summary: manage connections status, close overtimed connections
Args: LPVOID arg_list
contain the "this" pointer of IOCP instance
Modifies: [link_pool]
Returns: UINT
thread termination status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
UINT WINAPI IOCP::AgingThread( LPVOID arg_list )
{
IOCP* p_this = static_cast<IOCP *>(arg_list);
while( TRUE )
{
Sleep( 1000 );
p_this->link_pool.LinkPoolCheck(p_this->p_DisconnectEx);
}
return 0;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::PostRecv
Summary: post receive on the socket
return only after receive the size of package header
Args: PPER_IO_INFO p_per_link_info
I/O info structure that descripes the connection
ULONG buff_offset
indicate where the newly received data should be written to
ULONG buff_len
indicate the length of data that suppoused to receive,
function can return without receiving enough data
Modifies: [p_per_link_info]
Returns: OPSTATUS
operation status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
OPSTATUS IOCP::PostRecv( PPER_LINK_INFO p_per_link_info, ULONG buff_offset, ULONG buff_len )
{
ULONG u_flag = 0;
ULONG u_recv = 0;
ZeroMemory( &p_per_link_info->p_per_io_info[0].overlapped, sizeof(OVERLAPPED) );
p_per_link_info->p_per_io_info[0].w_buf.buf = &p_per_link_info->p_per_io_info[0].buffer[buff_offset];
p_per_link_info->p_per_io_info[0].w_buf.len = buff_len;
if( WSARecv( p_per_link_info->socket, &p_per_link_info->p_per_io_info[0].w_buf, 1, &u_recv, &u_flag, &p_per_link_info->p_per_io_info[0].overlapped, NULL ) == SOCKET_ERROR &&
WSAGetLastError() != WSA_IO_PENDING )
{
printf( "#Err: post receive failed, discard connection\n" );
return OP_FAILED;
}
return OP_SUCCESS;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::IsRecvFinish
Summary: check whether data is received completely
if not, post receive on the socket
Args: PPER_IO_INFO p_per_link_info
I/O info structure that descripes the connection
ULONG actual_trans
indicate the amount of data has been received so far
Modifies: [p_per_link_info]
Returns: OPSTATUS
operation status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
OPSTATUS IOCP::IsRecvFinish( PPER_LINK_INFO p_per_link_info, ULONG actual_trans )
{
// package length cannot bigger than buffer
if ( p_per_link_info->p_per_io_info[0].w_buf.len != actual_trans )
{
// not completely, post receive
p_per_link_info->p_per_io_info[0].curr_data_len += actual_trans;
PostRecv( p_per_link_info, actual_trans, p_per_link_info->p_per_io_info[0].w_buf.len - actual_trans );
p_per_link_info->p_per_io_info[0].post_recv_times ++;
return OP_FAILED;
}
else
{
// received completely, change reveived length
p_per_link_info->p_per_io_info[0].curr_data_len += actual_trans;
}
// package length bigger than buffer, discard the link directly
if ( ((PPACKET_HEADER)( p_per_link_info->p_per_io_info[0].buffer ))->packet_len >= sizeof(PACKET_HEADER) && ((PPACKET_HEADER)( p_per_link_info->p_per_io_info[0].buffer ))->packet_len > p_per_link_info->p_per_io_info[0].curr_data_len)
{
// not completely, post receive
PostRecv( p_per_link_info, p_per_link_info->p_per_io_info[0].curr_data_len, ((PPACKET_HEADER)( p_per_link_info->p_per_io_info[0].buffer ))->packet_len - p_per_link_info->p_per_io_info[0].curr_data_len );
p_per_link_info->p_per_io_info[0].post_recv_times ++;
return OP_FAILED;
}
return OP_SUCCESS;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::AcceptClient
Summary: accept new connection
post receive on the new connection
Args: PPER_IO_INFO p_per_io_Info
I/O info structure that descripes the connection
Modifies: [p_per_io_Info]
Returns: OPSTATUS
operation status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
OPSTATUS IOCP::AcceptClient(PPER_IO_INFO p_per_io_Info)
{
PPER_LINK_INFO p_per_link_info = *(PPER_LINK_INFO *)p_per_io_Info->buffer;
if ( CreateIoCompletionPort( (HANDLE)p_per_link_info->socket, h_iocp, (ULONG_PTR)p_per_link_info, 0 ) == NULL )
{
printf( "#Err: accept client failed\n" );
p_DisconnectEx( p_per_link_info->socket, NULL, TF_REUSE_SOCKET, 0 );
return OP_FAILED;
}
p_per_link_info->state_machine = SM_FULL;
PostAcceptEx(p_per_io_Info);
PostRecv( p_per_link_info, 0, sizeof(PACKET_HEADER) );
return OP_SUCCESS;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::PacketSend
Summary: asynchronously send data that stored in the send buffer
Args: PPER_IO_INFO p_per_io_Info
I/O info structure that descripes the connection
Modifies: [p_per_link_info]
Returns: BOOL
operation status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
BOOL IOCP::PacketSend(PPER_LINK_INFO p_per_link_info)
{
ULONG u_send = 0;
ULONG u_flag = 0;
ZeroMemory(&p_per_link_info->p_per_io_info[1].overlapped, sizeof(OVERLAPPED));
EnterCriticalSection(&SendCriticalSection);
ULONG uRet = WSASend(p_per_link_info->socket, &p_per_link_info->p_per_io_info[1].w_buf, 1, &u_send, u_flag, &p_per_link_info->p_per_io_info[1].overlapped, NULL);
if (uRet == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING)
{
printf("#Err: send data package asynchronously failed\n");
LeaveCriticalSection(&SendCriticalSection);
return FALSE;
}
LeaveCriticalSection(&SendCriticalSection);
return TRUE;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::LogonStatus
Summary: nodify client the logon status
Args: PPER_IO_INFO p_per_link_info
I/O info structure that descripes the connection
ULONG status
logon status
Modifies: [p_per_link_info]
Returns: BOOL
operation status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
BOOL IOCP::LogonStatus(PPER_LINK_INFO p_per_link_info, ULONG status)
{
ZeroMemory(p_per_link_info->p_per_io_info[1].buffer, MAX_BUF_LEN);
((PPACKET_HEADER)p_per_link_info->p_per_io_info[1].buffer)->comm_code = status;
((PPACKET_HEADER)p_per_link_info->p_per_io_info[1].buffer)->packet_len = sizeof(PACKET_HEADER);
p_per_link_info->p_per_io_info[1].w_buf.len = sizeof(PACKET_HEADER);
return PacketSend(p_per_link_info);
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::DealThread
Summary: thread used to accept connection and process messages
Args: LPVOID arg_list
contain the "this" pointer of IOCP instance
Modifies: [link_pool, p_redis]
Returns: UINT
thread termination status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
UINT WINAPI IOCP::DealThread( LPVOID arg_list )
{
IOCP* p_this = static_cast<IOCP *>(arg_list);
ULONG actual_trans = 0;
OVERLAPPED *p_overlapped = NULL;
PPER_IO_INFO p_per_io_Info = NULL;
PPER_LINK_INFO p_per_link_info = NULL;
// exclusive redis instance
RedisConnector* p_redis = new RedisConnector(p_this->redis_addr.c_str(), p_this->redis_port);
p_redis->Connect();
while( TRUE )
{
GetQueuedCompletionStatus( p_this->h_iocp, &actual_trans, (PULONG_PTR)&p_per_link_info, &p_overlapped, INFINITE );
p_per_io_Info = (PPER_IO_INFO)CONTAINING_RECORD(p_overlapped, PER_IO_INFO, overlapped );
if(p_per_io_Info->op_type == IO_ACCE )
{
printf("Received Client Connection Request\n");
p_this->AcceptClient(p_per_io_Info);
}
else if(p_per_io_Info->op_type == IO_RECV )
{
// break, if the package received is not complete
if ( p_this->IsRecvFinish( p_per_link_info, actual_trans ) == OP_FAILED ) continue;
// receive completed. prepare for next package receiving
p_per_link_info->p_per_io_info[0].w_buf.len = sizeof(PACKET_HEADER);
p_per_link_info->p_per_io_info[0].w_buf.buf = p_per_link_info->p_per_io_info[0].buffer;
p_per_link_info->p_per_io_info[0].curr_data_len = 0;
// do different thing for different communication code
switch( ((PPACKET_HEADER)p_per_io_Info->buffer)->comm_code )
{
case MSG_LOGON:
{
if (p_this->CheckDeviceID(((PPACKET_LOGON)p_per_io_Info->buffer)->account))
{
printf("#Log: logon succeed\nDevice: %s\n", ((PPACKET_LOGON)p_per_io_Info->buffer)->account);
strcpy_s(p_per_link_info->client_info.account, ((PPACKET_LOGON)p_per_io_Info->buffer)->account);
p_this->LogonStatus(p_per_link_info, MSG_LOGON_SUCCESS);
}
else
{
printf("#Log: logon failed\nDevice: %s\n", ((PPACKET_LOGON)p_per_io_Info->buffer)->account);
p_this->LogonStatus(p_per_link_info, MSG_LOGON_FAILURE);
}
break;
}
case MSG_LOGOUT:
{
printf("#Log: Device: %s logged out\n", p_per_link_info->client_info.account);
p_this->p_DisconnectEx(p_per_link_info->socket, NULL, TF_REUSE_SOCKET, 0);
p_per_link_info->free_flag = LINK_FREE;
p_per_link_info->state_machine = SM_IDLE;
p_per_link_info->heartbeat_info.hold_time = 0;
continue;
}
case MSG_GEO_LOCATION:
{
Value& detection = p_this->config["detection"];
string range = detection["radius"].GetString();
stringstream location;
location << detection["longitude"].GetString() << " " << detection["latitude"].GetString();
BOOL prev_status = p_redis->DetectGeofence(location.str(), range, string(p_per_link_info->client_info.account));
PPACKET_GEO_LOCATION p_location = (PPACKET_GEO_LOCATION)p_per_io_Info->buffer;
p_redis->InsertGeospatial(p_location, string(p_per_link_info->client_info.account));
BOOL curr_status = p_redis->DetectGeofence(location.str(), range, string(p_per_link_info->client_info.account));
if (prev_status && !curr_status)
{
printf("Device %s is leaving the geofence\n", p_per_link_info->client_info.account);
}
else if (!prev_status && curr_status)
{
printf("Device %s is entering the geofence\n", p_per_link_info->client_info.account);
}
break;
}
case MSG_EVENT:
{
PPACKET_EVENT p_event = (PPACKET_EVENT)p_per_io_Info->buffer;
p_redis->InsertEvent(p_event, string(p_per_link_info->client_info.account));
break;
}
case MSG_HEART_BEAT:
{
break;
}
default:
{
break;
}
}
if (p_per_link_info->state_machine == SM_FULL || p_per_link_info->state_machine == SM_OVER)
{
p_per_link_info->state_machine = SM_FULL;
p_per_link_info->heartbeat_info.hold_time = 240;
}
p_this->PostRecv(p_per_link_info, 0, sizeof(PACKET_HEADER));
}
}
return 0;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::GenerateGeospatialReportThread
Summary: thread used to constantly check redis and generate report
Args: LPVOID arg_list
contain the "this" pointer of IOCP instance
Modifies: [link_pool, p_redis]
Returns: UINT
thread termination status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
UINT WINAPI IOCP::GenerateGeospatialReportThread( LPVOID arg_list )
{
IOCP* p_this = static_cast<IOCP*>(arg_list);
RedisConnector* p_redis = new RedisConnector(p_this->redis_addr.c_str(), p_this->redis_port);
p_redis->Connect();
while (TRUE)
{
Value& detection = p_this->config["detection"];
Value& device_ids = p_this->config["device_ids"];
string range = detection["radius"].GetString();
stringstream location;
location << detection["longitude"].GetString() << " " << detection["latitude"].GetString();
// Detect n objects within x feet of each other
// here we iterate all devices and find all devices that within x feet, which include itself.
string result_a;
p_redis->DetectObjectInRange(device_ids, range, result_a);
// Count objects passing a radius from a given position
string result_b;
p_redis->CountObjectInRange(location.str(), range, result_b);
// save report locally
ofstream ofs("geospatial_report.txt", ios::out | ios::trunc);
ofs << result_a << endl << endl << result_b;
ofs.close();
Sleep(1000);
}
return 0;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::GenerateEventReportThread
Summary: thread used to constantly check redis and generate report
Args: LPVOID arg_list
contain the "this" pointer of IOCP instance
Modifies: [link_pool, p_redis]
Returns: UINT
thread termination status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
UINT WINAPI IOCP::GenerateEventReportThread( LPVOID arg_list )
{
IOCP* p_this = static_cast<IOCP*>(arg_list);
RedisConnector* p_redis = new RedisConnector(p_this->redis_addr.c_str(), p_this->redis_port);
p_redis->Connect();
while (TRUE)
{
Value& tem = p_this->config["temporal"];
Value& seq = p_this->config["sequence"];
Value& eva = p_this->config["evaluation"];
Value& devs = p_this->config["device_ids"];
string result_a;
p_redis->Temporal(tem, devs, result_a);
string result_b;
p_redis->Sequence(seq, devs, result_b);
string result_c;
p_redis->Evaluation(eva, devs, result_c);
ofstream ofs("event_report.txt", ios::out | ios::trunc);
ofs << "---------------Temporal---------------" << endl;
ofs << result_a << endl << endl;
ofs << "---------------Sequence---------------" << endl;
ofs << result_b << endl << endl;
ofs << "---------------Evaluation---------------" << endl;
ofs << result_c;
ofs.close();
Sleep(1000);
}
return 0;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::InitialEnvironment
Summary: initial linkpool for I/O info structure allocation
initial server I/O info structure
dynamically load socket related functions from library
Modifies: [link_pool, p_ser_link_info, p_acce_io_info, SendCriticalSection
p_AcceptEx, p_DisconnectEx, p_GetAcceptExSockAddrs]
Returns: OPSTATUS
operation status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
OPSTATUS IOCP::InitialEnvironment()
{
// initial link pool
link_pool.LinkPoolBuild();
// allocate link info for server
p_ser_link_info = (PPER_LINK_INFO)VirtualAlloc(NULL, sizeof(PER_LINK_INFO), MEM_COMMIT, PAGE_READWRITE);
p_acce_io_info = (PPER_IO_INFO)VirtualAlloc(NULL, 10 * sizeof(PER_IO_INFO), MEM_COMMIT, PAGE_READWRITE);
if ( p_ser_link_info == NULL || p_acce_io_info == NULL )
{
printf( "#Err: allocating server io info failed\n" );
return OP_FAILED;
}
ZeroMemory( p_ser_link_info, sizeof(PER_LINK_INFO) );
ZeroMemory( p_acce_io_info, 10 * sizeof(PER_IO_INFO) );
p_ser_link_info->p_per_io_info = p_acce_io_info;
// initial socket for server
p_ser_link_info->socket = WSASocket( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
if ( p_ser_link_info->socket == NULL )
{
printf( "#Err: creating server socket failed\n" );
return OP_FAILED;
}
// load lib functions
ULONG uBytesRet = 0;
GUID GuidAcceptEx = WSAID_ACCEPTEX;
GUID GuidGetAcceptExSockAddrs = WSAID_GETACCEPTEXSOCKADDRS;
GUID GuidDisconnectEx = WSAID_DISCONNECTEX;
WSAIoctl(p_ser_link_info->socket, SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidAcceptEx, sizeof(GuidAcceptEx), &p_AcceptEx, sizeof(p_AcceptEx), &uBytesRet, NULL, NULL);
WSAIoctl(p_ser_link_info->socket, SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidDisconnectEx, sizeof(GuidDisconnectEx), &p_DisconnectEx, sizeof(p_DisconnectEx), &uBytesRet, NULL, NULL);
WSAIoctl(p_ser_link_info->socket, SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidGetAcceptExSockAddrs, sizeof(GuidGetAcceptExSockAddrs), &p_GetAcceptExSockAddrs, sizeof(p_GetAcceptExSockAddrs), &uBytesRet, NULL, NULL);
if (p_AcceptEx == NULL || p_DisconnectEx == NULL || p_GetAcceptExSockAddrs == NULL)
{
printf("#Err: unable to load lib functions <AcceptEx, DisconnectEx, GetAcceptExSockAddrs>\n");
return OP_FAILED;
}
InitializeCriticalSection(&SendCriticalSection);
// load config file
ifstream ifs("config.json");
if (ifs.is_open())
{
stringstream sstr;
sstr << ifs.rdbuf();
ifs.close();
config.Parse(sstr.str().c_str());
}
else
{
printf("#Err: unable to load config file <config.json>\n");
return OP_FAILED;
}
this->server_addr = this->config["server_address"].GetString();
this->server_port = this->config["server_port"].GetInt();
this->redis_addr = this->config["redis_address"].GetString();
this->redis_port = this->config["redis_port"].GetInt();
return OP_SUCCESS;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::CompletePortStart
Summary: start listening on the given address and port
start 10 IOCP threads for network events processing
Modifies: [link_pool, p_redis, h_iocp]
Returns: OPSTATUS
operation status
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
OPSTATUS IOCP::CompletePortStart()
{
SOCKADDR_IN sock_addr = {0};
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = htons(this->server_port);
sock_addr.sin_addr.S_un.S_addr = inet_addr(this->server_addr.c_str() );
// bind socket to address & port
if ( bind( p_ser_link_info->socket, (PSOCKADDR)&sock_addr, sizeof(SOCKADDR_IN) ) != 0 )
{
printf( "#Err: binding server socket failed\n" );
return OP_FAILED;
}
// create compltion port
h_iocp = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 );
if ( h_iocp == NULL )
{
printf( "#Err: create IOCP failed\n" );
return OP_FAILED;
}
// bind socket to compltion port
if ( CreateIoCompletionPort( (HANDLE)p_ser_link_info->socket, h_iocp, (ULONG)p_ser_link_info, 0 ) == NULL )
{
printf( "#Err: binding IOCP failed\n" );
return OP_FAILED;
}
// start listen
if ( listen( p_ser_link_info->socket, SOMAXCONN ) == SOCKET_ERROR )
{
printf( "#Err: start listening socket failed\n" );
return OP_FAILED;
}
// create 10 deal threads
for (ULONG i = 0; i < 10; i ++)
{
printf("#Log: start IOCP deal thread #%u\n", i + 1);
if ( ( h_thread[i] = (HANDLE)_beginthreadex( NULL, 0, IOCP::DealThread, this, 0, NULL ) ) == NULL )
{
printf( "#Err: start IOCP thread failed\n" );
return OP_FAILED;
}
}
// create aging thread
if ( ( h_thread[10] = (HANDLE)_beginthreadex( NULL, 0, IOCP::AgingThread, this, 0, NULL ) ) == NULL )
{
printf( "#Err: start IOCP aging thread failed\n" );
return OP_FAILED;
}
printf("#Log: start IOCP aging thread\n");
for ( ULONG i = 0; i < 10; ++ i )
{
p_acce_io_info[i].op_type = IO_ACCE;
p_acce_io_info[i].w_buf.len = MAX_BUF_LEN;
p_acce_io_info[i].w_buf.buf = p_acce_io_info[i].buffer;
if( PostAcceptEx( &p_acce_io_info[i] ) != OP_SUCCESS )
{
return OP_FAILED;
}
}
// create geospatial report thread
if ((h_thread[11] = (HANDLE)_beginthreadex(NULL, 0, IOCP::GenerateGeospatialReportThread, this, 0, NULL)) == NULL)
{
printf("#Err: start Geospatial Report thread failed\n");
return OP_FAILED;
}
printf("#Log: start Geospatial Report thread\n");
// create event report thread
if ((h_thread[12] = (HANDLE)_beginthreadex(NULL, 0, IOCP::GenerateEventReportThread, this, 0, NULL)) == NULL)
{
printf("#Err: start Event Report thread failed\n");
return OP_FAILED;
}
printf("#Log: start Event Report thread\n");
while (TRUE)
{
Sleep(1000);
}
return OP_SUCCESS;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::InitialRedis
Summary: based on the device_ids, create corresponding tables
Modifies: [p_redis]
Returns: BOOL
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
BOOL IOCP::InitialRedis()
{
// TS.CREATE event:A:AB1345ED79 RETENTION 60000 DUPLICATE_POLICY MAX LABELS event_type A device_id AB1345ED79
Value& device_ids = config["device_ids"];
Value& event_types = config["event_types"];
for (SizeType i = 0; i < device_ids.Size(); i++)
{
for (SizeType j = 0; j < event_types.Size(); j++)
{
stringstream commands;
commands << "TS.CREATE event:" << event_types[j].GetString() << ":" << device_ids[i].GetString() << " RETENTION 0 LABELS event_type " << event_types[j].GetString() << " device_id " << device_ids[i].GetString();
p_redis->ExecuteCommand(commands.str());
}
}
return TRUE;
}
/*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
Method: IOCP::CheckDeviceID
Summary: check whether the incoming device is registered
Args: const char* device_id
Modifies: []
Returns: BOOL
M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
BOOL IOCP::CheckDeviceID(const char* device_id)
{
Value& device_ids = config["device_ids"];
for (SizeType i = 0; i < device_ids.Size(); i++)
{
if (strcmp(device_ids[i].GetString(), device_id) == 0)
{
return TRUE;
}
}
return FALSE;
}
IOCP::IOCP()
{
InitialEnvironment();
printf("------------------- Test Redis -------------------\n");
p_redis = new RedisConnector(this->redis_addr.c_str(), this->redis_port);
p_redis->Connect();
p_redis->TestRedis();
}
IOCP::~IOCP()
{
link_pool.LinkPoolDestroy();
VirtualFree(p_acce_io_info, 0, MEM_RELEASE);
VirtualFree(p_ser_link_info, 0, MEM_RELEASE);
}
int main(int argc, char const *argv[])
{
IOCP server;
printf("\n\n------------------- Start IOCP -------------------\n");
server.InitialRedis();
server.CompletePortStart();
return 0;
}