-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlev.cpp
More file actions
1427 lines (1012 loc) · 28.5 KB
/
lev.cpp
File metadata and controls
1427 lines (1012 loc) · 28.5 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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <atomic>
#include <vector>
#include <list>
#include <map>
#include "lev.h"
#ifdef _WIN32
#include <chrono>
#include <thread>
#else
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#endif
#ifdef _WIN32
#pragma comment( lib, "ws2_32" )
#endif
//#define LEV_CHECK_VALID_FD 1
#define LEV_EPOLL_EVT_SIZE 10
#define LEV_OBJ_BUF_SIZE 100
#define LEV_CUST_FUNC_SIZE 10
#define LEV_CUST_TASK_SIZE 100
#define LEV_MAX_WAIT_TIME 1000000 //1 second
#define LEV_MIN_TIMER_ID 1
#define LEV_MAX_TIMER_ID 100000000
static bool LevLoopRun = true;
std::atomic_int LevInstance(0);
#define find_timer_buf() ( timer_cnt_ <= LEV_OBJ_BUF_SIZE ? timer_buf_ : ext_timer_buf_ )
typedef struct FdCtx{
int ctx_idx; //ctx idx in vector
uint32_t idx; //fd version idx
}FdCtx;
typedef struct LevIoCtx{
int flag;
lev_sock_t fd;
uint32_t idx;
int WatchRead;
LevIoCallback ReadCB;
void* ReadData;
int WatchWrite;
LevIoCallback WriteCB;
void* WriteData;
}LevIoCtx;
typedef struct LevTimerCtx{
int TimerID;
uint64_t TrigTime;
uint64_t Interval;
LevTimerCallback TimerCB;
void* Data;
}LevTimerCtx;
typedef struct LevCustFuncCtx{
LevCustFuncCallback cb;
void* data;
}LevCustFuncCtx;
static int CompTimer( const void* data1, const void* data2 );
static uint64_t UsecNow();
static void UsecSleep( uint64_t usec );
#ifdef LEV_CHECK_VALID_FD
static bool IsValidFd( lev_sock_t fd );
#endif
#ifdef _WIN32
static void Usec2Tmv( uint64_t usec, struct timeval &tmv );
#endif
class LevEventLoopImpl: public LevEventLoop{
public:
LevEventLoopImpl( bool low_latency );
~LevEventLoopImpl();
bool Init();
void Run() override;
void Stop() override;
void SetSleepTime( int miliseoncds ) override;
bool AddIoWatcher( lev_sock_t fd, int event, LevIoCallback cb, void* data ) override;
bool DeleteIoWatcher( lev_sock_t fd, int event ) override;
bool DeleteIoWatcher( lev_sock_t fd ) override;
bool AddTimerWatcher( double start, double interval, LevTimerCallback cb, void* data, int& id ) override;
bool DeleteTimerWatcher( int id ) override;
bool AddCustFunc( LevCustFuncCallback cb, void* data ) override;
bool DeleteCustFunc( LevCustFuncCallback cb, void* data ) override;
bool AddCustTask( LevCustFuncCallback cb, void* data ) override;
void Close( lev_sock_t fd ) override;
void CloseAll() override;
private:
void LoadFds( int start_idx, int end_idx, FdCtx fd_list[], LevIoCtx ctx_list[] );
int FindFreePos();
bool AddNewTimer( LevTimerCtx ctx);
void ProcFdEvents();
void ProcCustFunc();
void ProcCustTask();
void ProcTimerEvents();
void DeleteTimer( int id );
uint64_t CalcSleepTime( bool poll );
LevTimerCtx* FindTimer( int id );
int NewTimerID();
int timer_id_;
uint32_t fd_idx_;
bool low_latency_;
bool run_;
int epoll_fd_;
int timer_cnt_;
int timer_ext_buf_size_;
uint64_t timer_trig_time_;
int sleep_time_;
int cust_func_cnt_;
LevTimerCtx* ext_timer_buf_;
std::map<lev_sock_t, int> fd_map_; //map of fd/ctx_idx(ctx_vec_)
std::vector<LevIoCtx> ctx_vec_;
std::list<LevCustFuncCtx> cust_task_;
LevTimerCtx timer_buf_[LEV_OBJ_BUF_SIZE];
LevCustFuncCtx cust_func_[LEV_CUST_FUNC_SIZE];
};
LevEventLoopImpl::LevEventLoopImpl( bool low_latency )
{
low_latency_ = low_latency;
if( low_latency )
sleep_time_ = 0;
else
sleep_time_ = LEV_MAX_WAIT_TIME;
run_ = true;
timer_id_ = LEV_MIN_TIMER_ID;
fd_idx_ = 0;
epoll_fd_ = -1;
timer_cnt_ = 0;
timer_ext_buf_size_ = 0;
timer_trig_time_ = 0;
ext_timer_buf_ = NULL;
cust_func_cnt_ = 0;
}
LevEventLoopImpl::~LevEventLoopImpl()
{
if( ext_timer_buf_ )
free( ext_timer_buf_ );
#ifdef __linux__
if( epoll_fd_ != -1 )
{
close( epoll_fd_ );
}
#endif
}
void LevEventLoopImpl::LoadFds( int start_idx, int end_idx, FdCtx fd_list[], LevIoCtx ctx_list[] )
{
int idx = 0;
int cnt = 0;
for( auto it = fd_map_.begin(); it != fd_map_.end(); it++ )
{
if( idx >= start_idx && idx <= end_idx )
{
fd_list[cnt].ctx_idx = it->second;
ctx_list[cnt] = ctx_vec_[it->second];
fd_list[cnt].idx = ctx_list[cnt].idx;
cnt++;
}
idx++;
}
}
int LevEventLoopImpl::FindFreePos()
{
int i;
for( i = 0; i < (int)ctx_vec_.size(); i++ )
{
LevIoCtx& ctx = ctx_vec_[i];
if( ctx.flag == 0 )
return i;
}
return -1;
}
void LevEventLoopImpl::SetSleepTime( int miliseconds )
{
if( miliseconds <= 0 || miliseconds > LEV_MAX_WAIT_TIME/1000 )
return ;
sleep_time_ = miliseconds * 1000;
}
bool LevEventLoopImpl::Init()
{
#ifdef __linux__
epoll_fd_ = epoll_create( 100 );
if( epoll_fd_ == -1 )
{
//write_log( "[ERR] create epoll failed! err:%s\n", strerror( errno ) );
return false;
}
#endif
return true;
}
LevTimerCtx* LevEventLoopImpl::FindTimer( int id )
{
int i;
LevTimerCtx *pt;
if( timer_cnt_ == 0 )
return NULL;
pt = find_timer_buf();
for( i = 0; i < timer_cnt_; i++ )
{
if( pt[i].TimerID == id )
return pt+i;
}
return NULL;
}
uint64_t LevEventLoopImpl::CalcSleepTime( bool poll )
{
int64_t diff;
uint64_t now,sleep_time;
if( low_latency_ )
{
sleep_time = 0;
}
else
{
now = UsecNow();
if( timer_trig_time_ )
{
diff = timer_trig_time_ - now;
if( diff > 0 )
{
sleep_time = diff > sleep_time_ ? sleep_time_ : diff;
if( !poll )
sleep_time = sleep_time % 1000;
}
else
sleep_time = 0;
}
else
{
sleep_time = sleep_time_;
if( !poll )
sleep_time = sleep_time % 1000;
}
}
return sleep_time;
}
int LevEventLoopImpl::NewTimerID()
{
int id;
LevTimerCtx *pt;
while( 1 )
{
id = timer_id_++;
if( timer_id_ > LEV_MAX_TIMER_ID )
timer_id_ = LEV_MIN_TIMER_ID;
pt = FindTimer( id );
if( !pt )
break;
}
return id;
}
void LevEventLoopImpl::ProcFdEvents()
{
int i, rc, cnt;
uint32_t idx,ctx_idx;
lev_sock_t fd;
uint64_t sleep_time;
LevIoCtx ctx;
#ifdef _WIN32
int fd_cnt;
int batch_idx;
int batch_size;
bool last_batch;
FdCtx fds[FD_SETSIZE];
LevIoCtx ctx_list[FD_SETSIZE];
FD_SET rd_set;
FD_SET wr_set;
struct timeval tmv;
bool event_trig;
#else
FdCtx fdctx;
struct epoll_event events[LEV_EPOLL_EVT_SIZE];
#endif
while( fd_map_.size() )
{
#ifdef _WIN32
event_trig = false;
batch_idx = 0;
while( batch_idx < ( fd_cnt = (int)fd_map_.size() ) )
{
batch_size = ( fd_cnt - batch_idx ) > FD_SETSIZE ? FD_SETSIZE : ( fd_cnt - batch_idx );
last_batch = ( batch_idx + batch_size ) >= fd_cnt ? true : false;
if( last_batch )
{
if( event_trig )
sleep_time = 0;
else
sleep_time = CalcSleepTime( true );
}
else
sleep_time = 0;
FD_ZERO( &rd_set );
FD_ZERO( &wr_set );
cnt = batch_size;
LoadFds(batch_idx, batch_idx + batch_size -1, fds, ctx_list);
batch_idx += batch_size;
for( i = 0; i < cnt ; i++ )
{
ctx = ctx_list[i];
fd = ctx.fd;
if( ctx.WatchRead )
FD_SET( fd, &rd_set );
if(ctx.WatchWrite )
FD_SET( fd, &wr_set );
}
Usec2Tmv( sleep_time, tmv );
rc = select( 0, &rd_set, &wr_set, NULL, &tmv );
if( rc <= 0 )
continue;
event_trig = true;
cnt = batch_size;
for( i = 0; i < cnt; i++ )
{
ctx = ctx_list[i];
idx = ctx.idx;
fd = ctx.fd;
ctx_idx = fds[i].ctx_idx;
if( FD_ISSET( fd, &rd_set ) )
{
ctx = ctx_vec_[ctx_idx];
if( !ctx.flag )
continue;
if( ctx.idx != idx )
continue;
if( ctx.WatchRead )
ctx.ReadCB( this, fd, ctx.ReadData );
}
//it's possible fd watcher already delete on read event call back,
//so it's need to check if fd in watch list.
if( FD_ISSET( fd, &wr_set ) )
{
ctx = ctx_vec_[ctx_idx];
if( !ctx.flag )
continue;
if( ctx.idx != idx )
continue;
if( ctx.WatchWrite )
ctx.WriteCB( this, fd, ctx.WriteData );
}
}
}
ProcCustTask();
if( !event_trig )
break;
#else //linux, epoll
sleep_time = CalcSleepTime( true );
rc = epoll_wait( epoll_fd_, events, LEV_EPOLL_EVT_SIZE, sleep_time / 1000 );
if( rc <= 0 )
break;
cnt = rc;
for( i = 0; i < cnt; i++ )
{
memcpy( &fdctx, &events[i].data, sizeof(fdctx) );
idx = fdctx.idx;
ctx_idx = fdctx.ctx_idx;
if( events[i].events & EPOLLIN )
{
ctx = ctx_vec_[ctx_idx];
fd = ctx.fd;
if( !ctx.flag )
continue;
if( ctx.idx != idx )
continue;
if( ctx.WatchRead )
ctx.ReadCB( this, fd, ctx.ReadData );
}
//it's possible fd watcher already delete on read event call back,
//so it's need to check if fd in watch list.
if( events[i].events & EPOLLOUT )
{
ctx = ctx_vec_[ctx_idx];
fd = ctx.fd;
if( !ctx.flag )
continue;
if( ctx.idx != idx )
continue;
if( ctx.WatchWrite )
ctx.WriteCB( this, fd, ctx.WriteData );
}
}
ProcCustTask();
#endif
if( !LevLoopRun )
break;
}
}
void LevEventLoopImpl::ProcCustFunc()
{
int i,cnt;
LevCustFuncCallback cb;
LevCustFuncCtx cust_func[LEV_CUST_FUNC_SIZE];
void* data;
cnt = cust_func_cnt_;
memcpy( cust_func, cust_func_, cnt*sizeof(LevCustFuncCtx) );
for( i = 0; i < cnt; i++ )
{
cb = cust_func[i].cb;
data = cust_func[i].data;
if( cb )
cb( this, data );
}
}
void LevEventLoopImpl::ProcCustTask()
{
LevCustFuncCtx task;
LevCustFuncCallback cb;
while( cust_task_.size() )
{
auto it = cust_task_.begin();
task = *it;
cb = task.cb;
if( cb )
cb( this, task.data );
cust_task_.pop_front();
}
}
void LevEventLoopImpl::Run()
{
int fd_cnt;
uint64_t sleep_time;
LevInstance ++;
while( LevLoopRun && run_ )
{
fd_cnt = (int)fd_map_.size();
if( fd_cnt + timer_cnt_ + cust_func_cnt_ == 0 )
{
UsecSleep( LEV_MAX_WAIT_TIME );
continue;
}
//process fd io events
ProcFdEvents();
//process customer functions
if( cust_func_cnt_ )
ProcCustFunc();
ProcCustTask();
//sleep only on high latency mode
if( !low_latency_ )
{
//epoll_wait wait with milliseconds,
//so it's possible to sleep after call of epoll_wait
sleep_time = CalcSleepTime( false );
if( sleep_time )
UsecSleep( sleep_time );
}
//process timer
ProcTimerEvents();
ProcCustTask();
}
LevInstance --;
}
void LevEventLoopImpl::ProcTimerEvents()
{
int id;
uint64_t now;
LevTimerCtx *ctx;
while( timer_cnt_ )
{
now = UsecNow();
if( now < timer_trig_time_ )
break;
ctx = find_timer_buf();
id = ctx->TimerID;
ctx->TimerCB( this, id, ctx->Data );
//in the timer callback, it's possible the timer already delete
if( timer_cnt_ == 0 )
break;
ctx = find_timer_buf();
if( id != ctx->TimerID )
continue;
if( ctx->Interval == 0 ) //timer Interval is 0, only trig once
{
DeleteTimer( id );
}
else
{
ctx->TrigTime += ctx->Interval;
qsort( ctx, timer_cnt_, sizeof(LevTimerCtx), CompTimer );
timer_trig_time_ = ctx->TrigTime;
}
}
}
void LevEventLoopImpl::Stop()
{
run_ = false;
}
bool LevEventLoopImpl::AddNewTimer( LevTimerCtx ctx )
{
LevTimerCtx *pt;
if( timer_cnt_ == LEV_OBJ_BUF_SIZE )
{
if( timer_ext_buf_size_ == 0 )
{
pt = (LevTimerCtx*)malloc( sizeof(LevTimerCtx) * LEV_OBJ_BUF_SIZE * 2 );
if( !pt )
return false;
timer_ext_buf_size_ = LEV_OBJ_BUF_SIZE * 2;
ext_timer_buf_ = pt;
}
memcpy( ext_timer_buf_, timer_buf_, LEV_OBJ_BUF_SIZE * sizeof(LevTimerCtx) );
}
if( timer_cnt_ > LEV_OBJ_BUF_SIZE && timer_cnt_ == timer_ext_buf_size_ )
{
pt = (LevTimerCtx*)malloc( sizeof(LevTimerCtx) * timer_ext_buf_size_ * 2 );
if( !pt )
return false;
memcpy( pt, ext_timer_buf_, sizeof(LevTimerCtx) * timer_ext_buf_size_ );
timer_ext_buf_size_ = timer_ext_buf_size_ * 2;
free( ext_timer_buf_ );
ext_timer_buf_ = pt;
}
if( timer_cnt_ < LEV_OBJ_BUF_SIZE )
pt = timer_buf_;
else
pt = ext_timer_buf_;
pt[timer_cnt_] = ctx;
timer_cnt_++;
qsort( pt, timer_cnt_, sizeof(LevTimerCtx), CompTimer );
return true;
}
bool LevEventLoopImpl::AddIoWatcher( lev_sock_t fd, int event, LevIoCallback cb, void* data )
{
LevIoCtx ctx;
int ctx_idx;
#ifdef __linux__
int rc;
struct epoll_event evt;
FdCtx fdctx;
#endif
if( !cb )
return false;
switch( event )
{
case LEV_IO_EVENT_READ:
case LEV_IO_EVENT_WRITE:
break;
default:
return false;
}
auto it = fd_map_.find( fd );
if( it != fd_map_.end() )
{
ctx_idx = it->second;
ctx = ctx_vec_[ctx_idx];
if( event == LEV_IO_EVENT_READ )
{
if( ctx.WatchRead )
return false;
ctx.WatchRead = 1;
ctx.ReadCB = cb;
ctx.ReadData = data;
}
if( event == LEV_IO_EVENT_WRITE )
{
if( ctx.WatchWrite )
return false;
ctx.WatchWrite = 1;
ctx.WriteCB = cb;
ctx.WriteData = data;
}
#ifdef __linux__
fdctx.ctx_idx = ctx_idx;
fdctx.idx = ctx.idx;
memcpy( &evt.data, &fdctx, sizeof(fdctx) );
evt.events = 0;
if( ctx.WatchRead )
evt.events = evt.events | EPOLLIN;
if( ctx.WatchWrite )
evt.events = evt.events | EPOLLOUT;
rc = epoll_ctl( epoll_fd_, EPOLL_CTL_MOD, fd, &evt );
if( rc == -1 )
{
//write_log( "[WRN] epoll op failed! line: %d, fd: %d, errno: %d err: %s", __LINE__, fd, errno, strerror( errno ) );
return false;
}
#endif
ctx_vec_[ctx_idx] = ctx;
}
else
{
memset( &ctx, 0, sizeof(ctx) );
ctx_idx = FindFreePos();
if( ctx_idx == -1 )
{
try{
ctx_vec_.push_back( ctx );
}
catch(...)
{
return false;
}
ctx_idx = (int)ctx_vec_.size() - 1;
}
ctx.flag = 1;
ctx.fd = fd;
ctx.idx = fd_idx_++;
if( event == LEV_IO_EVENT_READ )
{
ctx.WatchRead = 1;
ctx.ReadCB = cb;
ctx.ReadData = data;
}
if( event == LEV_IO_EVENT_WRITE )
{
ctx.WatchWrite = 1;
ctx.WriteCB = cb;
ctx.WriteData = data;
}
fd_map_[fd] = ctx_idx;
ctx_vec_[ctx_idx] = ctx;
#ifdef __linux__
fdctx.ctx_idx = ctx_idx;
fdctx.idx = ctx.idx;
memcpy( &evt.data, &fdctx, sizeof(fdctx) );
evt.events = 0;
if( ctx.WatchRead )
evt.events = evt.events | EPOLLIN;
if( ctx.WatchWrite )
evt.events = evt.events | EPOLLOUT;
rc = epoll_ctl( epoll_fd_, EPOLL_CTL_ADD, fd, &evt );
if( rc == -1 )
{
//write_log( "[WRN] epoll op failed! line: %d, fd: %d, errno: %d err: %s", __LINE__, fd, errno, strerror( errno ) );
fd_map_.erase( fd );
ctx.flag = 0;
ctx_vec_[ctx_idx] = ctx;
return false;
}
#endif
}
return true;
}
bool LevEventLoopImpl::DeleteIoWatcher( lev_sock_t fd, int event )
{
LevIoCtx ctx;
int ctx_idx;
#ifdef __linux__
int rc, op;
FdCtx fdctx;
struct epoll_event evt;
#endif
switch( event )
{
case LEV_IO_EVENT_READ:
case LEV_IO_EVENT_WRITE:
break;
default:
return false;
}
auto it = fd_map_.find( fd );
if( it == fd_map_.end() )
return false;
ctx_idx = it->second;
ctx = ctx_vec_[ctx_idx];
#ifdef LEV_CHECK_VALID_FD
if( !IsValidFd( fd ) )
{
fd_map_.erase( fd );
return false;
}
#endif
if( event == LEV_IO_EVENT_READ )
{
if( !ctx.WatchRead )
return false;
ctx.WatchRead = 0;
}
if( event == LEV_IO_EVENT_WRITE )
{
if( !ctx.WatchWrite )
return false;
ctx.WatchWrite = 0;
}
#ifdef __linux__
evt.events = 0;
if( ctx.WatchRead )
evt.events = evt.events | EPOLLIN;
if( ctx.WatchWrite )
evt.events = evt.events | EPOLLOUT;
fdctx.ctx_idx = ctx_idx;
fdctx.idx = ctx.idx;
memcpy( &evt.data, &fdctx, sizeof(fdctx) );
if( evt.events )
op = EPOLL_CTL_MOD;
else
op = EPOLL_CTL_DEL;
rc = epoll_ctl( epoll_fd_, op, fd, &evt );
if( rc == -1 )
{
//write_log( "[WRN] epoll op failed! line: %d, fd: %d, errno: %d err: %s", __LINE__, fd, errno, strerror( errno ) );
return false;
}
#endif
//delete fd from watch list
if( !ctx.WatchRead && !ctx.WatchWrite )
{
fd_map_.erase( fd );
ctx.flag = 0;
ctx_vec_[ctx_idx] = ctx;
}
else
ctx_vec_[ctx_idx] = ctx;
return true;
}
bool LevEventLoopImpl::DeleteIoWatcher( lev_sock_t fd )
{
int ctx_idx;
#ifdef __linux__
struct epoll_event evt;
#endif
auto it = fd_map_.find( fd );
if( it == fd_map_.end() )
return false;