-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockets.cpp
1073 lines (928 loc) · 31.3 KB
/
sockets.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
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 "sockets.h"
#include <sstream>
#include <string.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <unistd.h>
using namespace std;
using namespace gdlib;
/**
* @brief ...sock d_tor
* @ note virtual functions are not to be used
*
*/
network::Sockets::~Sockets()
{
close();
if( nullptr != m_pszHostname )
{
delete[] m_pszHostname;
m_pszHostname = nullptr;
}
}
/**
* @brief ...set std socket options, see man setsockopt
*
* @param a_fd ...
* @param a_level ...prtocol number
* @param a_optName ...option name
* @param a_nValue ...option value
* @return bool
*/
bool network::Sockets::setOption(const network::socketfd_t a_fd, const int a_level, const int a_optName, const int a_nValue )
{
if( 0 == setsockopt( a_fd, a_level, a_optName, &a_nValue, sizeof(int) ) )
{
return true;
} else
{
return false;
}
}
/**
* @brief ...get socket option see man getsocketopt
*
* @param a_fd ...
* @param a_level ...
* @param a_optName ...
* @param a_nValue ...
* @param a_len ...
* @return int
*/
int network::Sockets::getOption(const network::socketfd_t a_fd, const int a_level, const int a_optName, int& a_nValue, socklen_t& a_len )
{
if( 0 == getsockopt( a_fd, a_level, a_optName, &a_nValue, &a_len ) )
{
return true;
} else
{
return false;
}
}
/**
* @brief ...set fd to not blocking
*
* @param a_fd ...descriptor
* @return bool
*/
bool network::Sockets::makeNonBlocking( socketfd_t& a_fd )
{
int32_t nFlags = fcntl( a_fd, F_GETFL, 0 );
if( -1 == nFlags )
{
return false;
}
nFlags |= O_NONBLOCK;
nFlags = fcntl( a_fd, F_SETFL, nFlags );
return 0 == nFlags? true: false;
}
/**
* @brief ...return ture if descriptor is non-blocking
*
* @param a_fd ...fd
* @return bool
*/
bool network::Sockets::isNonBlocking(network::socketfd_t& a_fd)
{
int val;
if( (val= fcntl( a_fd, F_GETFL, 0 )) >= 0 )
{
//bIsblk = !(val & O_NONBLOCK);
return (val & O_NONBLOCK);
}
return false;
}
/**
* @brief ...no nageling so packets are sent right away inproving latency
* @details see https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_MRG/1.2/html/Realtime_Tuning_Guide/sect-Realtime_Tuning_Guide-Application_Tuning_and_Deployment-TCP_NODELAY_and_Small_Buffer_Writes.html
* For this to be used effectively, applications must avoid doing small, logically related buffer writes. Because TCP_NODELAY is enabled, these small writes will make TCP send these multiple buffers as individual packets, which can result in poor overall performance.
* I f applications have several buffers that are logically related and that should be sent as one packet it could be po*ssible to build a contiguous packet in memory and then send the logical packet to TCP, on a socket configured with TCP_NODELAY.
* Alternatively, create an I/O vector and pass it to the kernel using writev on a socket configured with TCP_NODELAY.
*
* @return bool
*/
bool network::Sockets::setNoDelay()
{
int nOptValue = 1;
if( 0 == setsockopt( m_fdSocket, IPPROTO_TCP, TCP_NODELAY, &nOptValue, sizeof(int) ) )
{
return true;
} else
{
return false;
}
}
/**
* @brief ...get a local socket (internal)
* CLIENT: SOCK_STREAM, AF_INET, AI_NUMERICSERV
* SERVER: SOCK_STREAM, AF_INET, AI_PASSIVE | AI_NUMERICSERV
*
* @param a_nType ...we are using SOCK_STREAM
* @param a_nFamily ...We are using AF_INET
* @param a_nFlags ...See above for client and server
* @return bool retrun true if ok
*/
bool network::Sockets::setLocalSocketProperties( const int32_t a_nFlags, const int32_t a_nType, const int32_t a_nFamily )
{
m_nSocketType = a_nType;
m_nSocketFamily = a_nFamily;
m_nSocketFlags = a_nFlags;
return true;
}
/**
* @brief open a server or a client
* server: open local port , bind and listen
* client open local port and connect to foreign port
*
* @param a_nType ...CLIENT or SERVER
* @param a_strHostname ...hostname or IP
* @param a_strPort ...port as a string, ex "5000"
* @param a_nProtocol ...TCP, only option
* @return bool
*/
bool network::Sockets::open( const sockType_t a_nType, const protocol_t a_nProtocol, const string a_strHostname, const string a_strPort )
{
if( a_strPort.length() > PORT_DIGIT_COUNT_INT32 )
{
// error port string more than 2^16 - 1
return false;
}
strcpy( m_szPort, a_strPort.c_str() );
m_protocol = a_nProtocol;
m_pszHostname = new char[a_strHostname.length() + 1];
if( nullptr == m_pszHostname )
{
// log error
return false;
}
strcpy( m_pszHostname, a_strHostname.c_str() );
m_type = a_nType;
struct addrinfo hints;
struct addrinfo *pActualAddress = nullptr;
memset( &hints, 0, sizeof( struct addrinfo ) );
hints.ai_canonname = nullptr;
hints.ai_addr = nullptr;
hints.ai_next = nullptr;
hints.ai_socktype = m_nSocketType;
hints.ai_family = m_nSocketFamily;
// set for client or server -> listener or active connector
switch( a_nType )
{
case sockType_t::CLIENT:
hints.ai_flags = AI_NUMERICSERV;
break;
case sockType_t::SERVER:
hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
break;
default:
return false;
}
if( getaddrinfo( m_pszHostname, m_szPort, &hints, &pActualAddress ) != 0 )
{
// log error
return false;
}
struct addrinfo *pLocalAddess;
int32_t fdLocalSock;
// find a local socket to use
int nOptValue = 1;
for( pLocalAddess=pActualAddress; pLocalAddess != nullptr; pLocalAddess = pLocalAddess->ai_next )
{
fdLocalSock = ::socket( pLocalAddess->ai_family, pLocalAddess->ai_socktype, pLocalAddess->ai_protocol );
if( -1 == fdLocalSock )
{
continue;
}
switch( a_nType )
{
case sockType_t::CLIENT:
if( ::connect( fdLocalSock, pLocalAddess->ai_addr, pLocalAddess->ai_addrlen ) == 0 )
{
// we have conection to server
m_fdSocket = fdLocalSock;
freeaddrinfo( pActualAddress );
pActualAddress = nullptr;
return true;
} else
{
std::cerr << "connect falilure:" << strerror( errno ) << std::endl;
}
break;
case sockType_t::SERVER:
setsockopt( fdLocalSock, SOL_SOCKET, SO_REUSEADDR, &nOptValue, sizeof(int) );
if( ::bind( fdLocalSock, pLocalAddess->ai_addr, pLocalAddess->ai_addrlen ) == 0 )
{
// we have an active listener
m_fdSocket = fdLocalSock;
freeaddrinfo( pActualAddress );
pActualAddress = nullptr;
listen( m_fdSocket, m_nBacklog );
return true;
} else
{
std::cerr << "bind falilure:" << strerror( errno ) << std::endl;
}
break;
default:
break;
}
// bind or connect failed, try next address
::close( fdLocalSock );
}
freeaddrinfo( pActualAddress );
return false;
}
/**
* @brief ...non-blocking fd: read from fd, will return bytes count read and block if no bytes avail
* nonblocking
* level trigger = read. on data ready from epoll, call once then call epoll, if additional calls, it will return data until fd is empty, then 0
* edge trigger - call read getting data until 0 is returned, then process and call epoll
*
* For level trigger it is best to call receive once and then epol while edge, you must call receive untill receive returns 0 else you will loose data
*
* @param a_pBuffer ...buffer to hold data
* @param a_nBufferSize ...size of your buffer (note this is no a null term string)
* @return ssize_t - bytes read, error -1
* nonblocking edge - return 0 when no bytes left on fd
* nonblocking level- return 0 when no bytes, better not to call repeatedly but to level epoll signal again
*/
ssize_t network::Sockets::receive( const socketfd_t& a_fd, void* a_pBuffer, const ssize_t& a_nBufferSize )
{
if( (nullptr == a_pBuffer) || (a_nBufferSize <= 0) )
{
return false;
}
ssize_t nBytesRead( 0 );
ssize_t nBytesReadPerCall( 0 );
uint8_t* pBuffer = reinterpret_cast<uint8_t*>( a_pBuffer ); // uchar
while( nBytesRead < a_nBufferSize )
{
nBytesReadPerCall = ::read( a_fd, pBuffer, static_cast<size_t>(a_nBufferSize-nBytesRead) );
switch( nBytesReadPerCall )
{
case -1:
switch( errno )
{
case EINTR:
continue;
case EAGAIN:
return nBytesRead;
default:
return -1;
}
case 0:
// reached EOF - done
return nBytesRead;
default:
nBytesRead += nBytesReadPerCall;
pBuffer = pBuffer + sizeof(uint8_t)* static_cast<decltype(sizeof(uint8_t))>( nBytesReadPerCall );
}
}
// buffer too small
return nBytesRead;
}
/**
* @brief ...read using s blocking fd
*
* @param a_fd ...
* @param a_pBuffer ...buffer to hold data
* @param a_nBufferSize ...size of your buffer (note this is not a null term string)
* @return ssize_t
*/
ssize_t network::Sockets::receive_blocking( const socketfd_t& a_fd, void* a_pBuffer, const ssize_t& a_nBufferSize )
{
if( (nullptr == a_pBuffer) || (a_nBufferSize <= 0) )
{
return false;
}
uint8_t* pBuffer = reinterpret_cast<uint8_t*>( a_pBuffer ); // uchar
return ::read( a_fd, pBuffer, static_cast<size_t>( a_nBufferSize ) );
}
/**
* @brief ...write to socket until buffer send empty, can be used with blocking or non blocking
* with blocking the loop will only execute once
*
* @param a_fs ...
* @param a_pBuffer ...
* @param a_nBufferSize ...
* @return ssize_t
* 0 < n <= a_nBufferSize write sucessfull, bytes written
* -1 write failed
*/
ssize_t network::Sockets::send( const socketfd_t& a_fd, const void* a_pBuffer, const ssize_t& a_nBufferSize )
{
if( (nullptr == a_pBuffer) || (a_nBufferSize <= 0) )
{
return -1;
}
ssize_t nBytesWrittenPerCall( 0 );
ssize_t nBytesWritten( 0 );
const uint8_t* pBufferPos = reinterpret_cast<const uint8_t*>( a_pBuffer );
while( nBytesWritten < a_nBufferSize )
{
nBytesWrittenPerCall = ::write( a_fd, pBufferPos, static_cast<size_t>( a_nBufferSize-nBytesWritten ) );
switch( nBytesWrittenPerCall )
{
case -1:
switch( errno )
{
case EAGAIN:
case EINTR:
continue;
default:
return -1;
}
default:
nBytesWritten += nBytesWrittenPerCall;
pBufferPos = pBufferPos + sizeof( uint8_t )* static_cast< decltype(sizeof(uint8_t) )>(nBytesWrittenPerCall);
}
}
return nBytesWritten;
}
/**
* @brief ...close the socket
*
* @return bool
*/
bool network::Sockets::close()
{
if( m_fdSocket > 0 )
{
::close( m_fdSocket );
m_fdSocket = 0;
}
return false;
}
// blocking client
//
/**
* @brief ...connect to host a=on a port
*
* @param a_strHostname ...host to connect to on port
* @param a_strPort ...port
* @param a_proto ... TCP
* @return bool
*/
bool network::Client::connect( const string& a_strHostname, string a_strPort, protocol_t a_proto )
{
return open( sockType_t::CLIENT, a_proto, a_strHostname, a_strPort );
}
ssize_t network::Client::send( const void* a_pBuffer, const ssize_t& a_nBufferSize )
{
return network::Sockets::send( m_fdSocket, a_pBuffer, a_nBufferSize );
}
/**
* @brief ...blocking receive
*
* @param a_pBuffer ...
* @param a_nBufferSize ...
* @return ssize_t
*/
ssize_t network::Client::receive( void* a_pBuffer, const ssize_t& a_nBufferSize )
{
return network::Sockets::receive_blocking( m_fdSocket, a_pBuffer, a_nBufferSize );
}
// non-blocking client
//
/**
* @brief ...receive non-blockeding reply
*
* @param a_pBuffer ...
* @param a_nBufferSize ...
* @return ssize_t
*/
ssize_t network::ClientAsync::receive( void* a_pBuffer, const ssize_t& a_nBufferSize )
{
return network::Sockets::receive( m_fdSocket, a_pBuffer, a_nBufferSize );
}
/**
* @brief ...start async receiver for replies. starts a thread with epolling to callback on events and errors
* @example see testing/async/client.cpp
*
* @param a_cbMessage ...call back on connect HUP, and data ready
* @param a_pData ...void pointer that will be returned in call back
* @param a_cbError ...call back on error, fn( errno, null terminaled string, a_pData )
* @param a_bEdgeTrigger ...level or edge trigger (level default)
* edge -> triggered once when data is present, must read all data on this event
* level -> triggeered whenever data is present, can read some or all data per even
* @return bool
*/
bool network::ClientAsync::startAsync( const network::socketCallback_t a_cbMessage, void* const a_pData, const network::errorCallBack_t a_cbError, const bool a_bEdgeTrigger )
{
m_bEdgeTriggered = a_bEdgeTrigger;
thread thd( &ClientAsync::startAsync_, this, a_cbMessage, a_cbError, a_pData );
m_thdReceiver = std::move( thd );
return true;
}
/**
* @brief ...async private worker
*
* @param a_onSocketEvent ...
* @param a_error ...
* @param a_pThis ...
* @return bool
*/
bool network::ClientAsync::startAsync_( const network::socketCallback_t a_onSocketEvent, const errorCallBack_t a_error, void* const a_pThis )
{
{
lock_guard<std::mutex> lock( m_muxReady ); // used for conditional to signal async is ready
}
if( nullptr == a_onSocketEvent )
{
// error
return false;
}
// alloc on stack events for all connections
if( true == m_bUseMalloc )
{
m_pEvents = reinterpret_cast<epoll_event*>( malloc( static_cast< decltype(sizeof(epoll_event)) >(m_nMaximumEpollEvents)*sizeof(epoll_event) ) );
} else
{
m_pEvents = reinterpret_cast<epoll_event*>( alloca( static_cast< decltype(sizeof(epoll_event)) >(m_nMaximumEpollEvents)*sizeof(epoll_event) ) );
}
if( nullptr == m_pEvents )
{
// try malloc if stack alloc fails, but keep track of who did the alloc, for now. fail
if( nullptr != a_error )
{
a_error( errno, strerror( errno ), a_pThis );
}
return false;
}
m_fdEpoll = epoll_create1( 0 );
if( -1 == m_fdEpoll )
{
// log error
if( nullptr != a_error )
{
a_error( errno, strerror( errno ), a_pThis );
}
return false;
}
epoll_event epEventMainSocket;
epEventMainSocket.data.fd = m_fdSocket;
epEventMainSocket.events = EPOLLIN | EPOLLRDHUP; // this is a socket from a connection, so listen for HUP
if( true == m_bEdgeTriggered )
{
epEventMainSocket.events |= EPOLLET;
}
makeNonBlocking( m_fdSocket );
if( -1 == epoll_ctl( m_fdEpoll, EPOLL_CTL_ADD, m_fdSocket, &epEventMainSocket ) )
{
// log error
if( nullptr != a_error )
{
a_error( errno, strerror( errno ), a_pThis ); // investigate if we needno use strerror_r
}
return false;
}
int32_t fdCount;
int64_t lIndex;
// listener side ready
bool bNotified = false;
while( m_bAsyncRunFlag )
{
fdCount = epoll_wait( m_fdEpoll, m_pEvents, m_nMaximumEpollEvents, m_nEpollTimeout_ms );
if( false == bNotified )
{
// notify that connection is ready
m_cvReady.notify_one();
bNotified = true;
//std::cout<<pthread_self()<<"notified"<<std::endl;
}
switch( fdCount )
{
case 0:
// epoll timeout
continue;
case -1:
if( EINTR == errno )
{
continue;
} else
{
if( nullptr != a_error )
{
string str( "epoll error: " );
str.append( strerror( errno ) );
a_error( errno, str.c_str(), nullptr );
m_bAsyncRunFlag = false;
continue;
}
}
continue;
default:
int32_t fd;
for( lIndex=0; lIndex<fdCount; ++lIndex )
{
fd = m_pEvents[lIndex].data.fd;
if( (m_pEvents[lIndex].events & EPOLLERR) || (m_pEvents[lIndex].events & EPOLLRDHUP) )
{
// HUP: here
std::cerr << "hup (server dropped connection):" << strerror( errno ) << std::endl;
// handle connection closed by either hangup or network error
//m_bAsyncRunFlag = false;
::close( fd );
if( nullptr != a_onSocketEvent )
{
a_onSocketEvent( fd, network::callBack_t::SESSION_CLOSE, a_pThis );
//m_bAsyncRunFlag = false;
}
}
if( m_pEvents[lIndex].events & EPOLLIN )
{
// data is ready on a fd
if( nullptr != a_onSocketEvent )
{
a_onSocketEvent( fd, network::callBack_t::MESSAGE, a_pThis );
} else
{
a_error( 0, "no callback event:", nullptr );
}
} else
{
if( nullptr != a_error )
{
a_error( 0, "unhandled socket event", nullptr );
}
}
}
}
}
::close( m_fdSocket );
::close( m_fdEpoll );
//sem_post( &m_semReconnect );
//cout << " end sock startAsync" << endl;
return true;
}
/**
* @brief ...see TWPriceFeed for details
*
* @param a_nRetryCount ...
* @param a_nRetryWait ...
* @param a_cbLog ...
* @return bool
*/
bool network::ClientAsync::reconnect( const int32_t a_nRetryCount, const int32_t a_nRetryWait, logCallBack_t a_cbLog )
{
std::cout << "m_thdReceiver:" << m_thdReceiver.native_handle() << std::endl;
cout << "waiting for Client::startAsync to end" << endl;
cout << "Client::startAsync ended" << endl;
cout << "network::Client::reconnect" << endl;
if( (a_nRetryCount == 0) || (a_nRetryWait == 0) )
{
if( nullptr != a_cbLog )
{
stringstream sstr;
sstr << "retry disabled at least one retry var is zero. MddReconnectRetryCount:" << a_nRetryCount << ", MddReconnectWait:" << a_nRetryWait;
a_cbLog( network::LogLevel::EERR, sstr.str().c_str() );
}
return false;
}
int32_t nRetryCount = a_nRetryCount;
while( nRetryCount-- > 0 )
{
if( false == m_bAsyncRunFlag )
{
if( nullptr != a_cbLog )
{
a_cbLog( network::LogLevel::EINF, "retry, stop issued:" );
}
cout << "stop issued" << endl;
break;
}
usleep( static_cast<__useconds_t>(a_nRetryWait)* 1000000 );
if( true == open( sockType_t::CLIENT, m_protocol, m_pszHostname, m_szPort ) ) // dev-idb27.nyoffice.tradeweb.com.
{
if( nullptr != a_cbLog )
{
a_cbLog( network::LogLevel::EINF, "connection suceeded" );
}
epoll_event epEventMainSocket;
epEventMainSocket.data.fd = m_fdSocket;
epEventMainSocket.events = EPOLLIN | EPOLLRDHUP; // this is a socket from a connection, so listen for HUP
if( true == m_bEdgeTriggered )
{
epEventMainSocket.events |= EPOLLET;
}
makeNonBlocking( m_fdSocket );
if( -1 == epoll_ctl( m_fdEpoll, EPOLL_CTL_ADD, m_fdSocket, &epEventMainSocket ) )
{
if( nullptr != a_cbLog )
{
stringstream sstr;
sstr << "failed to add connection to poll, no data will be received";
a_cbLog( network::LogLevel::EERR, sstr.str().c_str() );
}
return false;
}
return true;
} else
{
if( nullptr != a_cbLog )
{
a_cbLog( network::LogLevel::EWRN, "connection failed" );
}
cout << "retry failed" << endl;
}
}
if( nullptr != a_cbLog )
{
stringstream sstr;
a_cbLog( network::LogLevel::EERR, "mdd reconnect failed" );
}
cout << "mdd reconnect failed nRetryCount:" << a_nRetryCount << ", nRetryWait:" << a_nRetryWait << endl;
return false;
}
// blocking server
//
/**
* @brief ...
*
*/
network::Server::~Server()
{
network::Sockets::close();
}
/**
* @brief ...return descriptor when there is a connections
*
* @return falcon::core::network::socketfd_t
*/
network::socketfd_t network::Server::waitForConnection( )
{
if( sockType_t::SERVER != m_type )
{
return -1;
}
return accept( m_fdSocket, nullptr, nullptr );
}
// non-blocking server
//
/**
* @brief ...
*
*/
network::ServerAsync::~ServerAsync()
{
network::Sockets::close();
if( (true == m_bUseMalloc) && (m_pEvents != nullptr ) )
{
free( m_pEvents );
m_pEvents = nullptr;
}
}
/**
* @brief ...listens and calls back on data ready connection or HUP.
*
* edge trigger: you must read data from socket while receive returns > 0, data available
* level trigger: you must make one receive call per callback and ten return
*
* @param a_socketEvent ...callback on good socket events (conection, HUP and data ready
* @param a_bEdgeTrigger ...true if edge trigger, you must consume all data on event, else level and make mult calls
* @param a_error ...error callback handler
* @param a_pData ...pointer to pass back to callbacks
*
* edge -> triggered once when data is present, must read all data on this event
* level -> triggeered whenever data is present, can read some or all data per even
*
*
*
* @return bool
*/
bool network::ServerAsync::nonblockingListener( const socketCallback_t a_socketEvent, const bool a_bEdgeTrigger, const errorCallBack_t a_error, void *a_pData )
{
if( nullptr == a_socketEvent )
{
return false;
}
if( sockType_t::SERVER != m_type )
{
return false;
}
if( nullptr == a_error )
{
cerr << "Error handler not set" << endl;
}
// alloc on stack events for all connections
if( true == m_bUseMalloc )
{
m_pEvents = reinterpret_cast<epoll_event*>( malloc( static_cast< decltype(sizeof(epoll_event)) >(m_nMaximumEpollEvents)*sizeof(epoll_event) ) );
} else
{
m_pEvents = reinterpret_cast<epoll_event*>( alloca( static_cast< decltype(sizeof(epoll_event)) >(m_nMaximumEpollEvents)*sizeof(epoll_event) ) );
}
for( int32_t nIndex=0; nIndex<m_nMaximumEpollEvents; nIndex++ )
{
(m_pEvents+nIndex)->data.fd = 0;
}
if( nullptr == m_pEvents )
{
// try malloc if stack alloc fails, but keep track of who did the alloc, for now. fail
if( nullptr != a_error )
{
a_error( errno, strerror( errno ), nullptr );
}
return false;
}
m_fdEpoll = epoll_create1( 0 );
if( -1 == m_fdEpoll )
{
// log error
if( nullptr != a_error )
{
a_error( errno, strerror( errno ), nullptr );
}
return false;
}
epoll_event epEventMainSocket;
epEventMainSocket.data.fd = m_fdSocket;
epEventMainSocket.events = EPOLLIN;
// in above case we are watching READ events for fd
// here is snippet
// available events
// EPOLLIN
// The associated file is available for read(2) operations.
//
// EPOLLOUT
// The associated file is available for write(2) operations.
//
// EPOLLRDHUP (since Linux 2.6.17)
// Stream socket peer closed connection, or shut down writing half of connection.
// (This flag is especially useful for writing simple code to detect peer shutdown
// when using Edge Triggered monitoring.)
//
// EPOLLPRI
// There is urgent data available for read(2) operations.
//
// EPOLLERR
// Error condition happened on the associated file descriptor. epoll_wait(2) will
// always wait for this event; it is not necessary to set it in events.
//
// EPOLLHUP
// Hang up happened on the associated file descriptor. epoll_wait(2) will always
// wait for this event; it is not necessary to set it in events.
//
// EPOLLET
// Sets the Edge Triggered behavior for the associated file descriptor. The
// default behavior for epoll is Level Triggered. See epoll(7) for more detailed
// information about Edge and Level Triggered event distribution architectures.
//
// EPOLLONESHOT (since Linux 2.6.2)
// Sets the one-shot behavior for the associated file descriptor. This means that
// after an event is pulled out with epoll_wait(2) the associated file descriptor
// is internally disabled and no other events will be reported by the epoll inter-
// face. The user must call epoll_ctl() with EPOLL_CTL_MOD to re-arm the file
// descriptor with a new event mask.
// add listener socket to events watched by epoll
if( -1 == epoll_ctl( m_fdEpoll, EPOLL_CTL_ADD, m_fdSocket, &epEventMainSocket ) )
{
// log error
if( nullptr != a_error )
{
a_error( errno, strerror( errno ), nullptr ); // investigate if we needno use strerror_r
}
return false;
}
// connection vars
struct sockaddr remoteAddress;
socklen_t remoteAddressLength = sizeof( remoteAddress );
::listen( m_fdSocket, m_nBacklog );
int32_t fdCount;
int64_t lIndex;
while( m_bAsyncRunFlag )
{
fdCount = epoll_wait( m_fdEpoll, m_pEvents, m_nMaximumEpollEvents, m_nEpollTimeout_ms );
switch( fdCount )
{
//cerr << "fd:" << fdCount << endl;
case 0:
continue;
case -1:
// epoll error
// EBADF epfd is not a valid file descriptor.
//
// EFAULT The memory area pointed to by events is not accessible with write permissions.
//
// EINTR The call was interrupted by a signal handler before any of the requested events
// occurred or the timeout expired; see signal(7).
//
// EINVAL epfd is not an epoll file descriptor, or maxevents is less than or equal to
// zero.
if( EINTR == errno )
{
continue;
} else
{
if( nullptr != a_error )
{
string str( "epoll error: " );
str.append( strerror( errno ) );
a_error( errno, str.c_str(), nullptr );
m_bAsyncRunFlag = false;
continue;
}
}
continue;
default:
// process all descripters
int32_t fd;
for( lIndex=0; lIndex<fdCount; ++lIndex )
{
fd = m_pEvents[lIndex].data.fd;
// socket error or close
// ---------------------------
if( (m_pEvents[lIndex].events & EPOLLERR) || (m_pEvents[lIndex].events & EPOLLRDHUP) )
{
// handle connection closed by either hangup or network error
if( nullptr != a_socketEvent )
{
a_socketEvent( fd, network::callBack_t::SESSION_CLOSE, a_pData );
}
::close( fd );
//::shutdown( fd, SHUT_RDWR );
m_pEvents[lIndex].data.fd = 0;
// new connection
// ---------------------------
} else if( m_fdSocket == fd )
{
// if a valid descriptor, then we have a new connection
//makeNonBlocking( fd );
socketfd_t fdRemote = accept( m_fdSocket, dynamic_cast<struct sockaddr*>( &remoteAddress ), &remoteAddressLength );
if( -1 == fdRemote )
{
// log failue
if( nullptr != a_error )
{
a_error( errno, strerror( errno ), nullptr ); // investigate if we needno use strerror_r
}
continue;
} else
{
makeNonBlocking( fdRemote );