-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreactor_impl_iocp.cc
1501 lines (1385 loc) · 61.8 KB
/
reactor_impl_iocp.cc
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
Copyright (c) 2013-2014 King Lee
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifdef WIN32
#include <process.h>
#include <stdio.h>
#include "reactor_impl_iocp.h"
#include "event_handle.h"
#include "easy_util.h"
#define TIME_OVERTIME 60*1000
// fix #4
#define PRE_POST_RECV_NUM 5
#define PRE_POST_ACCEPT_NUM 5
#define MAX_FREE_OVERLAPPED_PLUS_NUM 5000
#define MAX_FREE_CLIENT_CONTEXT_NUM 5000
#define MAX_CONNECT_NUM 50000
struct Overlapped_Puls {
// the overlapped struct
OVERLAPPED overLapped_;
// a global WSABUF data,
WSABUF wsa_buf_;
// the buffer's start address
easy_char* buffer_;
// the used buffer size
easy_int32 used_size_;
// total size of buffer context
easy_int32 buffer_length_;
// the sequence of buffer,include post send and post recv sequence.
easy_long sequence_num_;
// socket of client
SOCKET sock_client_;
// operator type
kOPType op_type_;
struct Overlapped_Puls* next_;
public:
// packet data interface
// add data to buffer
BOOL add_data(easy_char* __data,easy_int32 __length) {
if (used_size_ + __length > DATA_BUFSIZE) {
return FALSE;
}
memcpy_s(buffer_ + used_size_,__length,__data,__length);
used_size_ += __length;
return TRUE;
}
// add a bool to buffer
BOOL add_data(easy_bool __data) {
return add_data((easy_char*)&__data,sizeof(easy_bool));
}
// add a easy_uint8 to buffer
BOOL add_data( easy_uint8 __data ) {
return add_data((easy_char*)&__data,sizeof(easy_uint8));
}
// add a easy_int16 to buffer
BOOL add_data( easy_int16 __data ) {
return add_data((easy_char*)&__data,sizeof(easy_int16));
}
// add a easy_int32 to buffer
BOOL add_data( easy_int32 __data ) {
return add_data((easy_char*)&__data,sizeof(easy_int32));
}
// read data from buffer
void read_data( easy_char* __data,const easy_int32 __bytes ) {
memcpy_s(__data,__bytes,buffer_ + used_size_,__bytes);
}
// read easy_int32 from buffer
void read_int( easy_int32& __val ) {
read_data((easy_char*)&__val,sizeof(easy_int32));
}
// flush buffer
BOOL flush_buffer( Overlapped_Puls* __next_overlap_puls,easy_int32 __flush_size ) {
if (!__next_overlap_puls) {
return FALSE;
}
easy_int32 __not_read_bytes = left_size();
// may be the __next_overlap_puls 's buffer have all used.
easy_int32 __can_flush_bytes = __next_overlap_puls->left_size();
if (__can_flush_bytes < __flush_size) {
// fix #2
// to the contrary, add this->buffer_ to __next_overlap_puls->buffer_
// first, copy __next_overlap_puls->buffer_ to this->buffer_
if(0 != used_size_) {
memmove_s(buffer_,__not_read_bytes,buffer_ + used_size_,__not_read_bytes);
}
memmove_s(buffer_ + __not_read_bytes,__can_flush_bytes,__next_overlap_puls->buffer_ + __next_overlap_puls->used_size_,__can_flush_bytes);
buffer_length_ = __not_read_bytes + __can_flush_bytes;
used_size_ = 0;
memset(buffer_ + buffer_length_,0,DATA_BUFSIZE - buffer_length_);
// second, copy this->buffer_ to __next_overlap_puls->buffer_
memmove_s(__next_overlap_puls->buffer_,this->buffer_length_,this->buffer_,this->buffer_length_);
__next_overlap_puls->buffer_length_ = this->buffer_length_;
__next_overlap_puls->used_size_ = 0;
used_size_ = buffer_length_;
return FALSE;
}
if(0 != used_size_) {
memmove_s(buffer_,__not_read_bytes,buffer_ + used_size_,__not_read_bytes);
}
memmove_s(buffer_ + __not_read_bytes,__flush_size,__next_overlap_puls->buffer_ + __next_overlap_puls->used_size_,__flush_size);
buffer_length_ = __not_read_bytes + __flush_size;
used_size_ = 0;
memset(buffer_ + buffer_length_,0,DATA_BUFSIZE - buffer_length_);
__next_overlap_puls->setp_used_size(__flush_size);
return TRUE;
}
BOOL is_enough( const easy_int32 __read_bytes ) {
easy_int32 __not_read_bytes = buffer_length_ - used_size_;
if (__not_read_bytes < __read_bytes) {
return FALSE;
}
return TRUE;
}
void setp_used_size( const easy_int32 __step_bytes ) {
if (used_size_ + __step_bytes <= buffer_length_) {
used_size_ += __step_bytes;
}
}
easy_int32 left_size() {
return buffer_length_ - used_size_;
}
};
// desc : implement SOCKET pool instead of PER_HANDLE_DATA
struct Client_Context {
// accept socket
SOCKET socket_;
// the sockaddr_in to store client address
sockaddr_in sockaddr_client_;
// if socket close or not
BOOL close_;
// number of WSARecv posted
easy_long num_post_recv_;
// number of WSASend posted
easy_long num_post_send_;
// the inc dequeue in the Client_Context,if a read or read overlapped operator occur in the client socket ,read_sequence_ increase by one,
// that is the value is the total number of post recv.
easy_long read_sequence_;
// current sequence to read,if lReadSequence decrease by by one,get rid of a Overlapped_Puls from pOutOrderReads
easy_long cur_read_sequence_;
// the inc dequeue of waiting send buffer
easy_long write_sequence_;
// current sequence to write data
easy_long cur_write_sequence_;
// lock to protect the struct
CRITICAL_SECTION lock_;
// record the current Overlapped_Puls I/O pending but we do not known if completion or not
Overlapped_Puls* cur_pending_send_;
// current waiting send number
easy_long waiting_send_count_;
// record the OVERLAPPED_PLUS which waiting send
Overlapped_Puls* waiting_send_;
// record the OVERLAPPED_PLUS which out of order
Overlapped_Puls* out_order_reads_;
struct Client_Context* next_;
};
Reactor_Impl_Iocp::Reactor_Impl_Iocp() {
handle_ = NULL;
fd_ = -1;
memset(event_array_,0,sizeof(WSAEVENT)*WSA_MAXIMUM_WAIT_EVENTS);
event_total_ = 0;
pre_post_accept_num_ = PRE_POST_ACCEPT_NUM;
pre_post_recv_num_ = PRE_POST_RECV_NUM;
free_overlap_puls_ = NULL;
free_overlap_puls_count_ = 0;
max_free_overlap_puls_count_ = MAX_FREE_OVERLAPPED_PLUS_NUM;
free_client_context_ = NULL;
free_cleint_context_count_ = 0;
max_free_client_context_count_ = MAX_FREE_CLIENT_CONTEXT_NUM;
active_cleint_context_ = NULL;
cur_connection_ = 0;
max_connection_ = MAX_CONNECT_NUM;
penging_accept_overlap_puls_ = NULL;
pending_accept_count_ = 0;
work_thread_cur_ = 0;
}
easy_int32 Reactor_Impl_Iocp::register_handle(Event_Handle* __handle,easy_int32 __fd,easy_int32 __mask,easy_int32 __connect) {
if(kMaskAccept ==__mask) {
fd_ = __fd;
handle_ = __handle;
_ready();
} else {
if(1 == __connect) {
}
}
return -1;
}
easy_int32 Reactor_Impl_Iocp::remove_handle(Event_Handle* __handle,easy_int32 __mask) {
return -1;
}
easy_int32 Reactor_Impl_Iocp::handle_event(easy_ulong __millisecond) {
return -1;
}
easy_int32 Reactor_Impl_Iocp::event_loop(easy_ulong __millisecond) {
easy_int32 __sleep_time = 1000*1000*30;
while(true) {
send_all_pending_send();
easy::Util::sleep(__sleep_time);
}
return -1;
}
void Reactor_Impl_Iocp::_create_completeion_port() {
// if the NumberOfConcurrentThreads is 0,that means io completion port will use default value,the number of cpu ' thread.
completeion_port_ = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if(NULL == completeion_port_) {
printf("_create_completeion_port failed with error: %d\n", GetLastError());
}
}
void Reactor_Impl_Iocp::_associate_completeion_port(HANDLE __completion_port,HANDLE __device,ULONG_PTR __completion_key) {
HANDLE __completeion_port = CreateIoCompletionPort(__device, completeion_port_, __completion_key, 0);
if(NULL == __completeion_port) {
printf("_associate_completeion_port failed with error: %d\n", GetLastError());
}
}
void Reactor_Impl_Iocp::_ready() {
_create_completeion_port();
// but when use sleep,Waitforsingelobject,waitformultinobjects,singleobjectandwait and so on,
// it will be make thread unusable,so the moment create another thread for use
// Create worker threads to service the overlapped I/O requests.
// The decision to create 2 worker threads per CPU in the system is a heuristic.
// Also,note that thread handles are closed right away, because we will not need them and the worker threads will continue to execute.
easy_int32 __number_work_thread = _get_cpu_number();
work_thread_cur_ = __number_work_thread;
for(easy_int32 __i = 0; __i < work_thread_cur_; ++__i) {
_begin_thread(&work_thread_function,this);
}
if ((event_array_[event_total_] = WSACreateEvent()) == WSA_INVALID_EVENT) {
printf("WSACreateEvent() failed with error %d\n", WSAGetLastError());
exit(1);
}
++event_total_;
if (WSAEventSelect(fd_, event_array_[event_total_ - 1], FD_ACCEPT) == SOCKET_ERROR) {
printf("WSAEventSelect() failed with error %d\n", WSAGetLastError());
exit(1);
}
_associate_completeion_port(completeion_port_,(HANDLE)fd_,(easy_ulong)0);
// Load the AcceptEx function into memory using WSAIoctl.
easy_ulong __bytes = 0;
GUID __guid_accept_ex = WSAID_ACCEPTEX;
if(SOCKET_ERROR == WSAIoctl(fd_,
SIO_GET_EXTENSION_FUNCTION_POINTER,
&__guid_accept_ex,
sizeof(__guid_accept_ex),
&lpfn_acceptex_,
sizeof(lpfn_acceptex_),
&__bytes,
NULL,
NULL)) {
printf("WSAIoctl() failed with error %d\n", WSAGetLastError());
exit(1);
}
// Load the GetAcceptExSockaddrs function into memory using WSAIoctl.
GUID __guid_get_acceptex_sockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
WSAIoctl(fd_,
SIO_GET_EXTENSION_FUNCTION_POINTER,
&__guid_get_acceptex_sockaddrs,
sizeof(__guid_get_acceptex_sockaddrs),
&lpfn_get_acceptex_sockaddrs_,
sizeof(lpfn_get_acceptex_sockaddrs_),
&__bytes,
NULL,
NULL
);
// start listen thread
_begin_thread(&listen_thread,this);
set_sock_opt();
}
easy_int32 Reactor_Impl_Iocp::_get_cpu_number() {
SYSTEM_INFO __systemInfo;
ZeroMemory(&__systemInfo,sizeof(__systemInfo));
// determine how many processors are on the system.
GetSystemInfo(&__systemInfo);
return __systemInfo.dwNumberOfProcessors;
}
void Reactor_Impl_Iocp::_begin_thread(unsigned (__stdcall * __start_address) (void *),void* __pv) {
easy_uint32 __thread_id = 0;
uintptr_t __res = _beginthreadex( NULL, 0, __start_address, __pv, 0, &__thread_id );
if (0 == __res) {
printf("_beginthreadex exception!");
return;
}
HANDLE __work_thread = (HANDLE)__res;
try {
if (__work_thread) {
CloseHandle(__work_thread);
(__work_thread) = NULL;
}
} catch(...) {
printf("CloseHandle error\n");
}
}
easy_uint32 __stdcall Reactor_Impl_Iocp::work_thread_function( void* __pv ) {
Reactor_Impl_Iocp* __this = (Reactor_Impl_Iocp*)__pv;
easy_ulong __bytes_transferred = 0;
easy_ulong __per_handle = 0;
LPOVERLAPPED __overlapped = NULL;
Overlapped_Puls* __overlapped_puls = NULL;
while (true) {
#ifndef _WIN64
BOOL __res = GetQueuedCompletionStatus(__this->completeion_port_, &__bytes_transferred,(LPDWORD)&__per_handle,(LPOVERLAPPED*)&__overlapped, TIME_OVERTIME/*INFINITE*/);
#else
BOOL __res = GetQueuedCompletionStatus(__this->completeion_port_, &__bytes_transferred,(PULONG_PTR)&__per_handle,(LPOVERLAPPED*)&__overlapped, TIME_OVERTIME/*INFINITE*/);
#endif //_WIN64
easy_ulong __io_error = ::WSAGetLastError();
if(!__res && __io_error == WAIT_TIMEOUT) {
// there is not much for server to do,and this thread can die even if it still outstanding I/O request
// to be continue ...
}
// thread exit,thought call post PostQueuedCompletionStatus and set dwCompletionKey = -1
if(-1 == __per_handle) {
_endthreadex(0);
}
__overlapped_puls = CONTAINING_RECORD(__overlapped, Overlapped_Puls, overLapped_);
if(__overlapped_puls) {
__io_error = NO_ERROR;
easy_ulong __flags = 0;
if (!__res) {
// specify the socket for WSAGetOverlappedResult
SOCKET __sock = INVALID_SOCKET;
if(__overlapped_puls->op_type_ == OP_ACCEPT) {
__sock = __this->fd_;
} else {
if(0 == __per_handle) {
break;
}
__sock = ((Client_Context*)__per_handle)->socket_;
}
easy_ulong dwFlags = 0;
if(!::WSAGetOverlappedResult(__sock, &__overlapped_puls->overLapped_, &__bytes_transferred, FALSE, &dwFlags)) {
__io_error = WSAGetLastError();
}
}
__this->_process_io(__per_handle,__overlapped_puls,__bytes_transferred,__io_error);
}
}
return 0;
}
easy_int32 Reactor_Impl_Iocp::handle_close( easy_int32 __fd ) {
return -1;
}
easy_int32 Reactor_Impl_Iocp::handle_packet( easy_int32 __fd,const easy_char* __packet,easy_uint32 __length ) {
Client_Context* __client_context = _get_client_context(__fd);
if (__client_context) {
send_2_client(__client_context,__packet,__length);
}
return -1;
}
easy_uint32 __stdcall Reactor_Impl_Iocp::listen_thread( void* __pv ) {
easy_int32 __error = 0;
Reactor_Impl_Iocp* __this = (Reactor_Impl_Iocp*)__pv;
while(TRUE) {
Overlapped_Puls* __overlapped_puls = NULL;
easy_ulong __events = 0;
// Wait for one of the sockets to receive I/O notification and
if (((__events = WSAWaitForMultipleEvents(__this->event_total_, __this->event_array_, FALSE,
/*WSA_INFINITE*/TIME_OVERTIME, FALSE)) == WSA_WAIT_FAILED)) {
printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
__this->destoryt_net();
return 0;
}
if(WSA_WAIT_TIMEOUT == __events) {
__this->check_all_connection_timeout();
// if the client connect server for a long time but not recv or send any data,disconnect it
__this->pending_accept_lock_.acquire_lock();
__overlapped_puls = __this->penging_accept_overlap_puls_;
while(NULL != __overlapped_puls) {
// fix 6, clear the data which __overlapped_puls->sock_client_ is 0
if (0 == __overlapped_puls->sock_client_) {
__this->remove_pending_accept(__overlapped_puls);
break;
}
easy_int32 __seconds = 0;
easy_int32 __bytes = sizeof(__seconds);
// check all AcceptEx is timeout
__error = getsockopt(__overlapped_puls->sock_client_, SOL_SOCKET, SO_CONNECT_TIME,(easy_char*)&__seconds, (easy_int32*)&__bytes );
if ( NO_ERROR != (__error = WSAGetLastError())) {
printf("getsockopt(SO_CONNECT_TIME) failed: %ld\n", __error);
}
if(-1 != __seconds && __seconds >= TIME_OVERTIME/1000) {
__this->_close_socket(__overlapped_puls->sock_client_);
}
__overlapped_puls = __overlapped_puls->next_;
}
__this->pending_accept_lock_.release_lock();
} else {
WSANETWORKEVENTS __network_events;
__events = __events - WAIT_OBJECT_0;
if (WSAEnumNetworkEvents(__this->fd_, __this->event_array_[__events - WSA_WAIT_EVENT_0], &__network_events) == SOCKET_ERROR) {
printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
return 0;
}
if (__network_events.lNetworkEvents & FD_ACCEPT) {
if (__network_events.iErrorCode[FD_ACCEPT_BIT] != 0) {
printf("FD_ACCEPT failed with error %d\n", __network_events.iErrorCode[FD_ACCEPT_BIT]);
break;
}
for(easy_int32 i = 0; i < __this->pre_post_accept_num_; ++i) {
__overlapped_puls = __this->allocate_overlapped_puls(DATA_BUFSIZE);
if(NULL != __overlapped_puls) {
__this->post_accept(__overlapped_puls);
__this->insert_pending_accept(__overlapped_puls);
}
}
if (__this->event_total_ > WSA_MAXIMUM_WAIT_EVENTS) {
printf("Too many connections - closing socket.\n");
__this->_close_socket(__this->fd_);
break;
}
}
}
}
return 0;
}
void Reactor_Impl_Iocp::post_accept(Overlapped_Puls* __overlapped_plus) {
easy_int32 __error_code = 0;
easy_ulong __bytes = 0;
// Create per I/O socket information structure to associate with the WSARecv call below.
if(SOCKET_ERROR == (__overlapped_plus->sock_client_ = WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL, 0,WSA_FLAG_OVERLAPPED))) {
printf("WSASocket() failed with error %d\n", WSAGetLastError());
return ;
}
// Empty our overlapped structure and accept connections.
memset(&__overlapped_plus->overLapped_,0,sizeof(OVERLAPPED));
__overlapped_plus->op_type_ = OP_ACCEPT;
if(!lpfn_acceptex_(fd_,
__overlapped_plus->sock_client_,
__overlapped_plus->buffer_,
__overlapped_plus->buffer_length_- ((sizeof(sockaddr_in) + 16) * 2),
sizeof(sockaddr_in) + 16,
sizeof(sockaddr_in) + 16,
&__bytes,
&__overlapped_plus->overLapped_)) {
if(ERROR_IO_PENDING != (__error_code = WSAGetLastError())) {
printf("AcceptEx() failed with error %d\n", __error_code);
return ;
}
}
}
Overlapped_Puls* Reactor_Impl_Iocp::allocate_overlapped_puls( easy_int32 __buffer_len ) {
if(__buffer_len > DATA_BUFSIZE) {
return NULL;
}
Overlapped_Puls* __overlapped_plus = NULL;
overlap_puls_lock_.acquire_lock();
// if free overlap puls list is NULL,new a buffer,else get a block from overlappuls list
if(NULL == free_overlap_puls_) {
if ((__overlapped_plus = (Overlapped_Puls*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(Overlapped_Puls) + DATA_BUFSIZE)) == NULL) {
printf("HeapAlloc() failed with error %d\n", GetLastError());
}
} else {
__overlapped_plus = free_overlap_puls_;
free_overlap_puls_ = free_overlap_puls_->next_;
__overlapped_plus->next_ = NULL;
InterlockedDecrement(&free_overlap_puls_count_);
}
if(NULL != __overlapped_plus) {
__overlapped_plus->buffer_ = (easy_char*)(__overlapped_plus + 1);
__overlapped_plus->buffer_length_ = __buffer_len;
}
overlap_puls_lock_.release_lock();
return __overlapped_plus;
}
Client_Context* Reactor_Impl_Iocp::allocate_client_context(SOCKET __sock) {
if(INVALID_SOCKET == __sock) {
return NULL;
}
Client_Context* __client_context = NULL;
client_context_lock_.acquire_lock();
if(NULL == free_client_context_) {
if ((__client_context = (Client_Context*) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(Client_Context))) == NULL) {
printf("HeapAlloc() failed with error %d\n", GetLastError());
} else {
InterlockedIncrement(&cur_connection_);
::InitializeCriticalSection(&__client_context->lock_);
}
} else {
__client_context = free_client_context_;
free_client_context_ = free_client_context_->next_;
__client_context->next_ = NULL;
InterlockedIncrement(&cur_connection_);
InterlockedDecrement(&free_cleint_context_count_);
}
if(NULL != __client_context) {
__client_context->socket_ = __sock;
// fix #1
__client_context->read_sequence_ = 1;
}
client_context_lock_.release_lock();
return __client_context;
}
void Reactor_Impl_Iocp::release_client_context( Client_Context* __client_context ) {
if (!__client_context) {
return;
}
if(INVALID_SOCKET != __client_context->socket_) {
_close_socket(__client_context->socket_);
}
client_context_lock_.acquire_lock();
if(0 == __client_context->socket_) {
client_context_lock_.release_lock();
return ;
}
if(free_client_context_ != __client_context) {
// first release the overlappuls in which the socket have not read yet
Overlapped_Puls* __next_overlap_plus = NULL;
while(NULL != __client_context->out_order_reads_) {
__next_overlap_plus = __client_context->out_order_reads_->next_;
release_overlapped_puls(__client_context->out_order_reads_);
__client_context->out_order_reads_ = __next_overlap_plus;
}
__next_overlap_plus = NULL;
while(NULL != __client_context->waiting_send_) {
__next_overlap_plus = __client_context->waiting_send_->next_;
release_overlapped_puls(__client_context->waiting_send_);
__client_context->waiting_send_ = __next_overlap_plus;
}
if(free_cleint_context_count_ < max_free_client_context_count_) {
__client_context->next_ = free_client_context_;
__client_context->socket_ = INVALID_SOCKET;
memset( &__client_context->sockaddr_client_, 0, sizeof(sockaddr_in) );
__client_context->num_post_recv_ = 0;
__client_context->num_post_send_ = 0;
__client_context->read_sequence_ = 0;
__client_context->cur_read_sequence_ = 0;
__client_context->write_sequence_ = 0;
__client_context->cur_write_sequence_ = 0;
__client_context->waiting_send_count_ = 0;
__client_context->close_ = TRUE;
__client_context->waiting_send_ = NULL;
__client_context->cur_pending_send_ = NULL;
__client_context->out_order_reads_ = NULL;
__client_context->next_ = NULL;
free_client_context_ = __client_context;
InterlockedDecrement(&cur_connection_);
InterlockedIncrement(&free_cleint_context_count_);
} else {
::DeleteCriticalSection(&__client_context->lock_);
HeapFree(GetProcessHeap(),0,__client_context);
__client_context = NULL;
InterlockedDecrement(&cur_connection_);
}
}
on_connection_closing(__client_context, NULL);
client_context_lock_.release_lock();
// to be continue ...
}
void Reactor_Impl_Iocp::_process_io( easy_ulong __per_handle,Overlapped_Puls* __overlapped_puls,easy_ulong __bytes_transferred,easy_int32 __error ) {
// check if __bytes_transferred is 0.if so, a new client connection is coming,sub a pening acceptex count first
Client_Context* __client_context = (Client_Context*)__per_handle;
if(NULL != __client_context) {
if( TRUE == __client_context->close_ ) {
// no use, it will lead to client not release corrrctly.i do not known the reason! 2011-06-16
// return ;
}
if(OP_READ == __overlapped_puls->op_type_) {
// client socket overlapped recv count sub by one
InterlockedDecrement(&__client_context->num_post_recv_);
} else if(OP_WRITE == __overlapped_puls->op_type_) {
// client socket overlapped send count sub by one
InterlockedDecrement(&__client_context->num_post_send_);
}
// check the client close or not
if( TRUE == __client_context->close_ ) {
if(0 == __client_context->num_post_recv_ && 0 == __client_context->num_post_send_) {
release_client_context(__client_context);
}
release_overlapped_puls(__overlapped_puls);
return ;
}
} else {
// remove pending accept after get a accept status from queue
remove_pending_accept(__overlapped_puls);
}
if(NO_ERROR != __error) {
// do with errors
//...
if(__overlapped_puls->op_type_ != OP_ACCEPT) {
// call virtual function-----------------------------------------
on_connection_error(__client_context, __overlapped_puls, __error);
// call virtual function-----------------------------------------
close_connection(__client_context);
if(0 == __client_context->num_post_recv_ && 0 == __client_context->num_post_send_) {
release_client_context(__client_context);
}
} else {
if(INVALID_SOCKET != __overlapped_puls->sock_client_) {
_close_socket(__overlapped_puls->sock_client_);
}
}
release_overlapped_puls(__overlapped_puls);
return ;
}
switch(__overlapped_puls->op_type_) {
case OP_ACCEPT: {
on_accept_completed( __overlapped_puls, __bytes_transferred );
}
break;
case OP_READ: {
on_read_completed( __client_context, __overlapped_puls, __bytes_transferred );
}
break;
case OP_ZERO_READ: {
on_zero_read_completed( __client_context, __overlapped_puls, __bytes_transferred );
}
break;
case OP_WRITE: {
on_write_completed( __client_context, __overlapped_puls, __bytes_transferred );
}
break;
}
}
void Reactor_Impl_Iocp::release_overlapped_puls( Overlapped_Puls* __overlapped_puls ) {
if(!__overlapped_puls) {
return ;
}
overlap_puls_lock_.acquire_lock();
if(__overlapped_puls != free_overlap_puls_) {
if(free_overlap_puls_count_ < max_free_overlap_puls_count_) {
memset(__overlapped_puls,0,sizeof(Overlapped_Puls) + DATA_BUFSIZE);
__overlapped_puls->next_ = free_overlap_puls_;
free_overlap_puls_ = __overlapped_puls;
InterlockedIncrement(&free_overlap_puls_count_);
} else {
HeapFree(GetProcessHeap(),0,__overlapped_puls);
}
}
overlap_puls_lock_.release_lock();
}
BOOL Reactor_Impl_Iocp::remove_pending_accept( Overlapped_Puls* __overlapped_puls ) {
BOOL __res = FALSE;
pending_accept_lock_.acquire_lock();
Overlapped_Puls* __temp_overLap_plus = penging_accept_overlap_puls_;
// if the next overlapped plus just we want to find
if(__overlapped_puls == __temp_overLap_plus) {
penging_accept_overlap_puls_ = __overlapped_puls->next_;
__res = TRUE;
} else {
// travel all element until find the des
while(NULL != __temp_overLap_plus && __overlapped_puls != __temp_overLap_plus->next_) {
__temp_overLap_plus = __temp_overLap_plus->next_;
}
// find it
if(NULL != __temp_overLap_plus) {
__temp_overLap_plus->next_ = __overlapped_puls->next_;
__res = TRUE;
} else {
/*
if the the __overlapped_puls have not add to pending accept and accept event have trigged. that will cause
__overlapped_puls will not found at penging_accept_overlap_puls_.error code 10038 will happed when getsockopt
called at listen_thread.
*/
}
}
if(__res) {
InterlockedDecrement(&pending_accept_count_);
}
pending_accept_lock_.release_lock();
return FALSE;
}
void Reactor_Impl_Iocp::on_connection_error( Client_Context* __client_context,Overlapped_Puls* __overlapped_puls, easy_int32 __error ) {
}
void Reactor_Impl_Iocp::on_accept_completed( Overlapped_Puls* __overlapped_puls,easy_ulong __bytes_transferred ) {
if(0 == __bytes_transferred) {
if(INVALID_SOCKET != __overlapped_puls->sock_client_) {
_close_socket(__overlapped_puls->sock_client_);
}
}
easy_int32 __local_len = 0;
easy_int32 __rmote_len = 0;
LPSOCKADDR __localaddr, __remoteaddr;
Client_Context* __client_context = allocate_client_context(__overlapped_puls->sock_client_);
if(NULL != __client_context) {
if(add_connection(__client_context)) {
lpfn_get_acceptex_sockaddrs_(
__overlapped_puls->buffer_,
__overlapped_puls->buffer_length_ - ((sizeof(sockaddr_in) + 16) * 2),
sizeof(sockaddr_in) + 16,
sizeof(sockaddr_in) + 16,
(SOCKADDR **)&__localaddr,
&__local_len,
(SOCKADDR **)&__remoteaddr,
&__rmote_len);
memcpy(&__client_context->sockaddr_client_, __localaddr, __local_len);
__client_context->close_ = FALSE;
// Associate the accept socket with the completion port
_associate_completeion_port(completeion_port_,(HANDLE)__client_context->socket_,(easy_ulong)__client_context);
__overlapped_puls->buffer_length_ = __bytes_transferred;
// call virtual function-----------------------------------------
on_connection_established(__client_context,__overlapped_puls);
// call virtual function-----------------------------------------
//post a few WSARecv quest
for(easy_int32 i = 0; i < PRE_POST_RECV_NUM; ++i) {
Overlapped_Puls* __temp_overLap_plus = allocate_overlapped_puls(DATA_BUFSIZE);
if(NULL != __temp_overLap_plus) {
if(!post_recv(__client_context,__temp_overLap_plus)) {
close_connection(__client_context);
release_client_context(__client_context);
break;
}
}
}
} else {
close_connection(__client_context);
release_client_context(__client_context);
}
} else {
_close_socket(__overlapped_puls->sock_client_);
}
// fix #1
process_packet(__client_context,__overlapped_puls);
}
void Reactor_Impl_Iocp::on_read_completed( Client_Context* __client_context,Overlapped_Puls* __overlapped_puls,easy_ulong __bytes_transferred ) {
// check to see if an error has occured on the socket and if so then close the socket and cleanup the SOCKET_INFORMATION structure
// associated with the socket.
if(0 == __bytes_transferred) {
__overlapped_puls->buffer_length_ = 0;
on_connection_closing(__client_context,__overlapped_puls);
// call virtual function-----------------------------------------
close_connection(__client_context);
// call virtual function-----------------------------------------
if(0 == __client_context->num_post_recv_ && 0 == __client_context->num_post_send_) {
release_client_context(__client_context);
}
release_overlapped_puls(__overlapped_puls);
} else {
__overlapped_puls->buffer_length_ = __bytes_transferred;
process_packet(__client_context,__overlapped_puls);
if(TRUE == __client_context->close_) {
return;
}
easy_int32 __posr_recv_left = PRE_POST_RECV_NUM - __client_context->num_post_recv_;
for(easy_int32 i = 0; i < __posr_recv_left; ++i) {
Overlapped_Puls* __temp_overLap_puls = allocate_overlapped_puls(DATA_BUFSIZE);
if(NULL != __temp_overLap_puls) {
if(!post_recv(__client_context,__temp_overLap_puls)) {
close_connection(__client_context);
// this part will come out memory leak, how to work it out?
// add 2011-04-08
if(0 == __client_context->num_post_recv_ && 0 == __client_context->num_post_send_) {
release_client_context(__client_context);
}
break;
}
}
}
}
}
void Reactor_Impl_Iocp::on_zero_read_completed( Client_Context* __client_context,Overlapped_Puls* __overlapped_puls,easy_ulong __bytes_transferred ) {
}
void Reactor_Impl_Iocp::on_write_completed( Client_Context* __client_context,Overlapped_Puls* __overlapped_puls,easy_ulong __bytes_transferred ) {
if(0 == __bytes_transferred) {
__overlapped_puls->buffer_length_ = 0;
on_connection_closing(__client_context, __overlapped_puls);
// call virtual function-----------------------------------------
close_connection(__client_context);
// call virtual function-----------------------------------------
if(0 == __client_context->num_post_recv_ && 0 == __client_context->num_post_send_) {
release_client_context(__client_context);
}
} else {
/************************************************************************
reference from msdn:
For non-overlapped sockets, the last two parameters (lpOverlapped, lpCompletionRoutine) are ignored and WSASend adopts
the same blocking semantics as send. Data is copied from the buffer(s) into the transport's buffer. If the socket is
non-blocking and stream-oriented, and there is not sufficient space in the transport's buffer, WSASend will return with
only part of the application's buffers having been consumed. Given the same buffer situation and a blocking socket,
WSASend will block until all of the application buffer contents have been consumed.
/************************************************************************/
if(__overlapped_puls->buffer_length_ != __bytes_transferred) {
// not write complete, usual it happened when not sufficient space in the transport 's buffer
printf("on_write_completed, not complete,__overlapped_puls->buffer_length_ = %d, \
__bytes_transferred = %d \n",__overlapped_puls->buffer_length_,__bytes_transferred);
}
if ( __client_context->cur_pending_send_ == __overlapped_puls ) {
__client_context->cur_pending_send_ = NULL;
}
// update 2011-06-22
// if there any buffer waiting send
if ( __client_context->waiting_send_ ) {
send_pending_send( __client_context );
}
}
::InterlockedIncrement(&__client_context->cur_write_sequence_);
release_overlapped_puls(__overlapped_puls);
}
void Reactor_Impl_Iocp::close_connection( Client_Context* __client_context ) {
active_clienk_context_lock_.acquire_lock();
if(0 == __client_context->socket_ || INVALID_SOCKET == __client_context->socket_) {
active_clienk_context_lock_.release_lock();
return ;
}
Client_Context* __temp_client_context = active_cleint_context_;
if(__temp_client_context == __client_context) {
active_cleint_context_ = __client_context->next_;
} else {
while(NULL != __temp_client_context && __temp_client_context->next_ != __client_context) {
__temp_client_context = __temp_client_context->next_;
}
if(NULL != __temp_client_context) {
// update 2011-04-05
__temp_client_context->next_ = __client_context->next_;
}
}
// close socket
::EnterCriticalSection(&__client_context->lock_);
if(INVALID_SOCKET != __client_context->socket_) {
// add 2011-04-13
// force the subsequent closesocket to be abortative.
LINGER lingerStruct;
lingerStruct.l_onoff = 1;
lingerStruct.l_linger = 0;
setsockopt(__client_context->socket_, SOL_SOCKET, SO_LINGER,(easy_char*)&lingerStruct, sizeof(lingerStruct));
// add 2011-04-21
// now close the socket handle.this will do an abortive or graceful close, as requested.
CancelIo((HANDLE)__client_context->socket_);
_close_socket(__client_context->socket_);
}
__client_context->close_ = TRUE;
::LeaveCriticalSection(&__client_context->lock_);
active_clienk_context_lock_.release_lock();
}
void Reactor_Impl_Iocp::_close_socket( SOCKET __socket ) {
closesocket(__socket);
__socket = INVALID_SOCKET;
}
Client_Context* Reactor_Impl_Iocp::_get_client_context( easy_int32 __fd ) {
active_clienk_context_lock_.acquire_lock();
Client_Context* __first_client_context = active_cleint_context_;
while(__first_client_context) {
if(__fd == __first_client_context->socket_) {
active_clienk_context_lock_.release_lock();
return __first_client_context;
}
__first_client_context = __first_client_context->next_;
}
active_clienk_context_lock_.release_lock();
return NULL;
}
BOOL Reactor_Impl_Iocp::add_connection( Client_Context* __client_context ) {
active_clienk_context_lock_.acquire_lock();
if(cur_connection_ < max_connection_) {
__client_context->next_ = active_cleint_context_;
active_cleint_context_ = __client_context;
active_clienk_context_lock_.release_lock();
return TRUE;
}
active_clienk_context_lock_.release_lock();
return FALSE;
}
void Reactor_Impl_Iocp::on_connection_established( Client_Context* __client_context,Overlapped_Puls* __overlapped_puls ) {
handle_->handle_input(__client_context->socket_);
}
BOOL Reactor_Impl_Iocp::post_recv( Client_Context* __client_context,Overlapped_Puls* __overlapped_puls ) {
::EnterCriticalSection(&__client_context->lock_);
__overlapped_puls->sequence_num_ = __client_context->read_sequence_;
easy_int32 __error = 0;
easy_ulong __bytes = 0;
easy_ulong __flags = 0;
__overlapped_puls->op_type_ = OP_READ;
__overlapped_puls->wsa_buf_.buf = __overlapped_puls->buffer_;
__overlapped_puls->wsa_buf_.len = __overlapped_puls->buffer_length_;
easy_int32 __res = WSARecv(__client_context->socket_, &__overlapped_puls->wsa_buf_, 1, &__bytes, &__flags, &__overlapped_puls->overLapped_, NULL);
if ((__res == SOCKET_ERROR) && (WSA_IO_PENDING != (__error = WSAGetLastError()))) {
printf("WSARecv failed: %d\n", __error);
::LeaveCriticalSection(&__client_context->lock_);
return FALSE;
}
InterlockedIncrement(&__client_context->read_sequence_);
InterlockedIncrement(&__client_context->num_post_recv_);
::LeaveCriticalSection(&__client_context->lock_);
return TRUE;
}
void Reactor_Impl_Iocp::process_packet(Client_Context* __client_context,Overlapped_Puls* __overlapped_puls) {
if(!__client_context) {
return;
}
out_read_overlap_puls_lock_.acquire_lock();
if(NULL != __overlapped_puls) {
__overlapped_puls->next_ = NULL;
Overlapped_Puls* __temp_overlap_plus = __client_context->out_order_reads_;
Overlapped_Puls* __pre_overlap_plus = NULL;
// traverse all client order reads until the end,and record the last overlappuls.
// and make sure the out_order_reads_ in the order
while(NULL != __temp_overlap_plus) {
if(__overlapped_puls->sequence_num_ < __temp_overlap_plus->sequence_num_) {
break;
}
__pre_overlap_plus = __temp_overlap_plus;
__temp_overlap_plus = __temp_overlap_plus->next_;
}
// insert the head of list
if(NULL == __pre_overlap_plus) {
__overlapped_puls->next_ = __client_context->out_order_reads_;
__client_context->out_order_reads_ = __overlapped_puls;
}
// insert the mid of list
else {
__overlapped_puls->next_ = __pre_overlap_plus->next_;
__pre_overlap_plus->next_ = __overlapped_puls;
}
}
// process packet really