1
1
2
- #ifdef LINUX
2
+ #ifdef __GNUC__
3
3
4
4
#include < arpa/inet.h>
5
5
#include < netinet/in.h>
@@ -46,7 +46,7 @@ namespace com::github::socket
46
46
Socket (Socket&) = delete ;
47
47
48
48
Socket () noexcept (false )
49
- #ifdef LINUX
49
+ #ifdef __GNUC__
50
50
: m_fd(-1 )
51
51
#else
52
52
: m_fd(INVALID_SOCKET)
@@ -55,7 +55,7 @@ namespace com::github::socket
55
55
init ();
56
56
}
57
57
58
- #ifdef LINUX
58
+ #ifdef __GNUC__
59
59
explicit Socket (const int filedescriptor) noexcept
60
60
#else
61
61
explicit Socket (SOCKET filedescriptor) noexcept
@@ -72,7 +72,7 @@ namespace com::github::socket
72
72
73
73
virtual ~Socket ()
74
74
{
75
- #ifdef LINUX
75
+ #ifdef __GNUC__
76
76
static_cast <void >(::shutdown (m_fd, SHUT_RDWR));
77
77
static_cast <void >(::close (m_fd));
78
78
#else
@@ -84,7 +84,7 @@ namespace com::github::socket
84
84
void initSocket (const int domain, const int type, const int protocol) noexcept (false )
85
85
{
86
86
m_fd = ::socket (domain, type, protocol);
87
- #ifdef LINUX
87
+ #ifdef __GNUC__
88
88
if (m_fd < 0 )
89
89
#else
90
90
if (m_fd == INVALID_SOCKET)
@@ -94,7 +94,7 @@ namespace com::github::socket
94
94
}
95
95
}
96
96
97
- #ifdef LINUX
97
+ #ifdef __GNUC__
98
98
auto accept (sockaddr* address, socklen_t * addrlen) const noexcept -> int
99
99
#else
100
100
auto accept (sockaddr* address, socklen_t * addrlen) const noexcept -> SOCKET
@@ -103,7 +103,7 @@ namespace com::github::socket
103
103
return ::accept (m_fd, address, addrlen);
104
104
}
105
105
106
- #ifdef LINUX
106
+ #ifdef __GNUC__
107
107
[[nodiscard]] auto accept (std::string& ipAddr, uint16_t & port) const noexcept (false ) -> int
108
108
#else
109
109
[[nodiscard]] auto accept (std::string& ipAddr, uint16_t & port) const noexcept (false ) -> SOCKET
@@ -112,7 +112,7 @@ namespace com::github::socket
112
112
sockaddr_in addr = {};
113
113
socklen_t length = sizeof (addr);
114
114
auto retval = accept (reinterpret_cast <sockaddr*>(&addr), &length);
115
- #ifdef LINUX
115
+ #ifdef __GNUC__
116
116
if (retval > 0 )
117
117
#else
118
118
if (retval != INVALID_SOCKET)
@@ -169,7 +169,7 @@ namespace com::github::socket
169
169
170
170
[[nodiscard]] auto read (void * buffer, size_t length) const noexcept -> ssize_t
171
171
{
172
- #ifdef LINUX
172
+ #ifdef __GNUC__
173
173
return ::read (m_fd, buffer, length);
174
174
#else
175
175
return ::recv (m_fd, reinterpret_cast <char *>(buffer), length, 0 );
@@ -178,7 +178,7 @@ namespace com::github::socket
178
178
179
179
[[nodiscard]] auto readfrom (void * buffer, size_t length, sockaddr* from, socklen_t * fromlength) const noexcept -> ssize_t
180
180
{
181
- #ifdef LINUX
181
+ #ifdef __GNUC__
182
182
return ::recvfrom (m_fd, buffer, length, 0 , from, fromlength);
183
183
#else
184
184
return ::recvfrom (m_fd, reinterpret_cast <char *>(buffer), length, 0 , from, fromlength);
@@ -201,7 +201,7 @@ namespace com::github::socket
201
201
202
202
auto send (const void * buffer, size_t length) const noexcept -> ssize_t
203
203
{
204
- #ifdef LINUX
204
+ #ifdef __GNUC__
205
205
return ::send (m_fd, buffer, length, 0 );
206
206
#else
207
207
return ::send (m_fd, reinterpret_cast <const char *>(buffer), length, 0 );
@@ -210,7 +210,7 @@ namespace com::github::socket
210
210
211
211
auto sendto (const void * buffer, size_t length, const sockaddr* toaddr, int tolength) const noexcept -> ssize_t
212
212
{
213
- #ifdef LINUX
213
+ #ifdef __GNUC__
214
214
return ::sendto (m_fd, buffer, length, 0 , toaddr, tolength);
215
215
#else
216
216
return ::sendto (m_fd, reinterpret_cast <const char *>(buffer), length, 0 , toaddr, tolength);
@@ -224,40 +224,25 @@ namespace com::github::socket
224
224
return sendto (buffer, length, reinterpret_cast <sockaddr*>(&addr), sizeof (addr));
225
225
}
226
226
227
- [[nodiscard]] auto select (fd_set* readfds, fd_set* writefds, fd_set* exceptfds, timeval* timeout) const noexcept -> ssize_t
227
+ [[nodiscard]] auto selectWrite ( timeval& timeout) const noexcept -> bool
228
228
{
229
- return ::select (m_fd, readfds, writefds, exceptfds, timeout);
229
+ fd_set set;
230
+ FD_ZERO (&set);
231
+ FD_SET (m_fd, &set);
232
+ return (::select (m_fd+1 , nullptr , &set, nullptr , &timeout) == 1 );
230
233
}
231
234
232
- [[nodiscard]] auto select (fd_set& readfds, fd_set& writefds, fd_set& exceptfds, timeval& timeout) const noexcept -> ssize_t
235
+ [[nodiscard]] auto selectRead ( timeval& timeout) const noexcept -> bool
233
236
{
234
- return ::select (m_fd, &readfds, &writefds, &exceptfds, &timeout);
235
- }
236
-
237
- [[nodiscard]] auto select (fd_set& readfds, fd_set& writefds, fd_set& exceptfds, const int milliseconds) const noexcept -> ssize_t
238
- {
239
- ssize_t retval;
240
- if (milliseconds < 0 )
241
- {
242
- retval = ::select (m_fd, &readfds, &writefds, &exceptfds, nullptr );
243
- }
244
- else
245
- {
246
- timeval timeout
247
- {
248
- .tv_sec = static_cast <time_t >(milliseconds * MILLISECONDS_PER_SECOND),
249
- .tv_usec = static_cast <time_t >(milliseconds / MILLISECONDS_PER_SECOND)
250
- };
251
-
252
- retval = select (readfds, writefds, exceptfds, timeout);
253
- }
254
-
255
- return retval;
237
+ fd_set set;
238
+ FD_ZERO (&set);
239
+ FD_SET (m_fd, &set);
240
+ return (::select (m_fd+1 , &set, nullptr , nullptr , &timeout) == 1 );
256
241
}
257
242
258
243
auto getsockopt (const int level, const int optname, void *optval, socklen_t * optlen) const noexcept -> ssize_t
259
244
{
260
- #ifdef LINUX
245
+ #ifdef __GNUC__
261
246
return ::getsockopt (m_fd, level, optname, optval, optlen);
262
247
#else
263
248
return ::getsockopt (m_fd, level, optname, reinterpret_cast <char *>(optval), optlen);
@@ -266,13 +251,22 @@ namespace com::github::socket
266
251
267
252
auto setsockopt (const int level, const int optname, const void *optval, const int optlen) const noexcept -> ssize_t
268
253
{
269
- #ifdef LINUX
254
+ #ifdef __GNUC__
270
255
return ::setsockopt (m_fd, level, optname, optval, optlen);
271
256
#else
272
257
return ::setsockopt (m_fd, level, optname, reinterpret_cast <const char *>(optval), optlen);
273
258
#endif
274
259
}
275
260
261
+ #ifdef __GNUC__
262
+ auto getFd () const noexcept -> int
263
+ #else
264
+ auto getFd () const noexcept -> SOCKET
265
+ #endif
266
+ {
267
+ return m_fd;
268
+ }
269
+
276
270
protected:
277
271
278
272
[[nodiscard]] static auto getIpAddress (const in_addr inaddr) noexcept (false ) -> std::string
@@ -292,7 +286,7 @@ namespace com::github::socket
292
286
*/
293
287
void init () noexcept (false )
294
288
{
295
- #ifdef WINDOWS
289
+ #ifdef _WIN32
296
290
std::call_once (m_onetime, [this ]()
297
291
{
298
292
if (::WSAStartup (MAKEWORD (2 , 2 ), &m_wsaData) != 0 )
@@ -414,7 +408,7 @@ namespace com::github::socket
414
408
constexpr static unsigned int FIVE_SECONDS = 5 ; // NOLINT
415
409
416
410
static std::array<unsigned char , COOKIE_LEN> m_cookie; // NOLINT
417
- #ifdef LINUX
411
+ #ifdef __GNUC__
418
412
int m_fd; // NOLINT
419
413
#else
420
414
SOCKET m_fd; // NOLINT
@@ -456,7 +450,7 @@ namespace com::github::socket
456
450
initAddr (ipAddr, port, addr);
457
451
458
452
auto ret = ::connect (m_fd, reinterpret_cast <sockaddr*>(&addr), sizeof (addr));
459
- #ifdef LINUX
453
+ #ifdef __GNUC__
460
454
if (ret < 0 )
461
455
#else
462
456
if (ret == SOCKET_ERROR)
@@ -471,7 +465,7 @@ namespace com::github::socket
471
465
void init () noexcept (false )
472
466
{
473
467
int flag = 1 ;
474
- #ifdef LINUX
468
+ #ifdef __GNUC__
475
469
if (setsockopt (IPPROTO_TCP, TCP_NODELAY, &flag, sizeof (flag)) != 0 )
476
470
#else
477
471
if (setsockopt (IPPROTO_TCP, TCP_NODELAY, reinterpret_cast <const char *>(&flag), sizeof (flag)) == SOCKET_ERROR)
@@ -524,7 +518,7 @@ namespace com::github::socket
524
518
sockaddr_in client = {};
525
519
socklen_t clientLen = sizeof (client);
526
520
auto fileD = Socket::accept (reinterpret_cast <sockaddr*>(&client), &clientLen);
527
- #ifdef LINUX
521
+ #ifdef __GNUC__
528
522
if (fileD < 0 )
529
523
#else
530
524
if (fileD == INVALID_SOCKET)
@@ -541,7 +535,7 @@ namespace com::github::socket
541
535
void init () noexcept (false )
542
536
{
543
537
int opt = 1 ;
544
- #ifdef LINUX
538
+ #ifdef __GNUC__
545
539
if (setsockopt (SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof (opt)) != 0 )
546
540
#else
547
541
if (setsockopt (SOL_SOCKET, SO_REUSEADDR, reinterpret_cast <const char *>(&opt), sizeof (opt)) == SOCKET_ERROR)
@@ -590,7 +584,7 @@ namespace com::github::socket
590
584
[[nodiscard]] auto read (void * buffer, size_t length) noexcept -> ssize_t
591
585
{
592
586
socklen_t fromlength = sizeof (m_serverAddr);
593
- #ifdef LINUX
587
+ #ifdef __GNUC__
594
588
return ::recvfrom (m_fd, buffer, length, 0 , reinterpret_cast <sockaddr*>(&m_serverAddr), &fromlength);
595
589
#else
596
590
return ::recvfrom (m_fd, reinterpret_cast <char *>(buffer), length, 0 , reinterpret_cast <sockaddr*>(&m_serverAddr), &fromlength);
@@ -599,7 +593,7 @@ namespace com::github::socket
599
593
600
594
auto send (const void * buffer, size_t length) noexcept -> ssize_t
601
595
{
602
- #ifdef LINUX
596
+ #ifdef __GNUC__
603
597
return ::sendto (m_fd, buffer, length, 0 , reinterpret_cast <sockaddr*>(&m_serverAddr), sizeof (m_serverAddr));
604
598
#else
605
599
return ::sendto (m_fd, reinterpret_cast <const char *>(buffer), length, 0 , reinterpret_cast <sockaddr*>(&m_serverAddr), sizeof (m_serverAddr));
@@ -637,7 +631,7 @@ namespace com::github::socket
637
631
[[nodiscard]] auto read (void * buffer, size_t length) noexcept -> ssize_t
638
632
{
639
633
socklen_t fromlength = sizeof (m_clientAddr);
640
- #ifdef LINUX
634
+ #ifdef __GNUC__
641
635
return ::recvfrom (m_fd, buffer, length, 0 , reinterpret_cast <sockaddr*>(&m_clientAddr), &fromlength);
642
636
#else
643
637
return ::recvfrom (m_fd, reinterpret_cast <char *>(buffer), length, 0 , reinterpret_cast <sockaddr*>(&m_clientAddr), &fromlength);
@@ -646,7 +640,7 @@ namespace com::github::socket
646
640
647
641
auto send (const void * buffer, size_t length) noexcept -> ssize_t
648
642
{
649
- #ifdef LINUX
643
+ #ifdef __GNUC__
650
644
return ::sendto (m_fd, buffer, length, 0 , reinterpret_cast <sockaddr*>(&m_clientAddr), sizeof (m_clientAddr));
651
645
#else
652
646
return ::sendto (m_fd, reinterpret_cast <const char *>(buffer), length, 0 , reinterpret_cast <sockaddr*>(&m_clientAddr), sizeof (m_clientAddr));
@@ -658,7 +652,7 @@ namespace com::github::socket
658
652
void init () noexcept (false )
659
653
{
660
654
int opt = 1 ;
661
- #ifdef LINUX
655
+ #ifdef __GNUC__
662
656
if (setsockopt (SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof (opt)) != 0 )
663
657
#else
664
658
if (setsockopt (SOL_SOCKET, SO_REUSEADDR, reinterpret_cast <const char *>(&opt), sizeof (opt)) == SOCKET_ERROR)
@@ -679,7 +673,7 @@ namespace com::github::socket
679
673
auto operator =(SecureTcpClient&&) -> SecureTcpClient& = delete ;
680
674
SecureTcpClient (SecureTcpClient&) = delete ;
681
675
682
- #ifdef LINUX
676
+ #ifdef __GNUC__
683
677
SecureTcpClient (const int filedescriptor, SSL_CTX* sslctx) noexcept (false )
684
678
#else
685
679
SecureTcpClient (SOCKET filedescriptor, SSL_CTX *sslctx) noexcept (false )
@@ -717,7 +711,7 @@ namespace com::github::socket
717
711
initAddr (ipAddr, port, addr);
718
712
719
713
auto ret = connect (reinterpret_cast <sockaddr*>(&addr), sizeof (addr));
720
- #ifdef LINUX
714
+ #ifdef __GNUC__
721
715
if (ret < 0 )
722
716
#else
723
717
if (ret == SOCKET_ERROR)
@@ -765,7 +759,7 @@ namespace com::github::socket
765
759
void init () noexcept (false )
766
760
{
767
761
m_fd = ::socket (AF_INET, SOCK_STREAM, 0 );
768
- #ifdef LINUX
762
+ #ifdef __GNUC__
769
763
if (m_fd < 0 )
770
764
#else
771
765
if (m_fd == INVALID_SOCKET)
@@ -775,7 +769,7 @@ namespace com::github::socket
775
769
}
776
770
777
771
int flag = 1 ;
778
- #ifdef LINUX
772
+ #ifdef __GNUC__
779
773
if (setsockopt (IPPROTO_TCP, TCP_NODELAY, &flag, sizeof (flag)) != 0 )
780
774
#else
781
775
if (setsockopt (IPPROTO_TCP, TCP_NODELAY, reinterpret_cast <const char *>(&flag), sizeof (flag)) == SOCKET_ERROR)
@@ -861,7 +855,7 @@ namespace com::github::socket
861
855
[[nodiscard]] auto accept (std::string& ipAddr, uint16_t & port) noexcept (false ) -> SecureTcpClient
862
856
{
863
857
auto fileD = Socket::accept (ipAddr, port);
864
- #ifdef LINUX
858
+ #ifdef __GNUC__
865
859
if (fileD < 0 )
866
860
#else
867
861
if (fileD == INVALID_SOCKET)
@@ -885,7 +879,7 @@ namespace com::github::socket
885
879
sockaddr_in client = {};
886
880
socklen_t clientLen = sizeof (client);
887
881
auto fileD = Socket::accept (reinterpret_cast <sockaddr*>(&client), &clientLen);
888
- #ifdef LINUX
882
+ #ifdef __GNUC__
889
883
if (fileD < 0 )
890
884
#else
891
885
if (fileD == INVALID_SOCKET)
@@ -906,7 +900,7 @@ namespace com::github::socket
906
900
Socket::init ();
907
901
908
902
m_fd = ::socket (AF_INET, SOCK_STREAM, 0 );
909
- #ifdef LINUX
903
+ #ifdef __GNUC__
910
904
if (m_fd < 0 )
911
905
#else
912
906
if (m_fd == INVALID_SOCKET)
@@ -916,7 +910,7 @@ namespace com::github::socket
916
910
}
917
911
918
912
int opt = 1 ;
919
- #ifdef LINUX
913
+ #ifdef __GNUC__
920
914
if (setsockopt (SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof (opt)) != 0 )
921
915
#else
922
916
if (setsockopt (SOL_SOCKET, SO_REUSEADDR, reinterpret_cast <const char *>(&opt), sizeof (opt)) == SOCKET_ERROR)
@@ -1034,7 +1028,7 @@ namespace com::github::socket
1034
1028
void init (const std::string& keyFile, const std::string& certFile) noexcept (false )
1035
1029
{
1036
1030
m_fd = ::socket (AF_INET, SOCK_DGRAM, 0 );
1037
- #ifdef LINUX
1031
+ #ifdef __GNUC__
1038
1032
if (m_fd < 0 )
1039
1033
#else
1040
1034
if (m_fd == INVALID_SOCKET)
@@ -1185,7 +1179,7 @@ namespace com::github::socket
1185
1179
SSL_CTX_set_cookie_verify_cb (m_sslctx, &Socket::verifyCookie);
1186
1180
1187
1181
m_fd = ::socket (AF_INET, SOCK_DGRAM, 0 );
1188
- #ifdef LINUX
1182
+ #ifdef __GNUC__
1189
1183
if (m_fd < 0 )
1190
1184
#else
1191
1185
if (m_fd == INVALID_SOCKET)
@@ -1195,7 +1189,7 @@ namespace com::github::socket
1195
1189
}
1196
1190
1197
1191
int opt = 1 ;
1198
- #ifdef LINUX
1192
+ #ifdef __GNUC__
1199
1193
if (setsockopt (SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof (opt)) != 0 )
1200
1194
#else
1201
1195
if (setsockopt (SOL_SOCKET, SO_REUSEADDR, reinterpret_cast <const char *>(&opt), sizeof (opt)) == SOCKET_ERROR)
0 commit comments