-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinetstream.hpp
More file actions
817 lines (807 loc) · 24.1 KB
/
inetstream.hpp
File metadata and controls
817 lines (807 loc) · 24.1 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
#ifndef INETSTREAM_HPP_
#define INETSTREAM_HPP_
// C++
#include <sstream>
#include <iostream>
#include <optional>
#include <vector>
#include <array>
#include <type_traits>
#include <chrono>
// C
#include <cstring>
#include <cerrno>
// System
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <signal.h>
#include <fcntl.h>
// users may override these
#ifndef INET_MAX_CONNECTIONS
#define INET_MAX_CONNECTIONS 10
#endif
#ifndef INET_MAX_SEND_TIMEOUT_MS
#define INET_MAX_SEND_TIMEOUT_MS 1000
#endif
#ifndef INET_MAX_RECV_TIMEOUT_MS
#define INET_MAX_RECV_TIMEOUT_MS 1000
#endif
// set to 6 to use IPv6 instead of IPv4
#ifndef INET_IPV
#define INET_IPV 4
#endif
#ifndef INET_USE_DEFAULT_SIGUSR1_HANDLER
#define INET_USE_DEFAULT_SIGUSR1_HANDLER false
#endif
#ifndef INET_PRINT_SIGUSR1
#define INET_PRINT_SIGUSR1 false
#endif
namespace inet {
inline void sigusr1_handler(int signal) {
if (INET_PRINT_SIGUSR1) {
std::cout << "inet::sigusr1_handler() was called (signal=" << signal << ")" << std::endl;
}
}
inline void* get_in_addr(sockaddr* sa) {
if (sa->sa_family == AF_INET) {
return &((reinterpret_cast<sockaddr_in*>(sa))->sin_addr);
}
return &((reinterpret_cast<sockaddr_in6*>(sa))->sin6_addr);
}
typedef unsigned char byte;
enum class protocol {
TCP, UDP
};
// "type" traits
template <protocol P> struct is_tcp_prot { static constexpr const bool value = std::false_type::value; };
template <> struct is_tcp_prot<protocol::TCP> { static constexpr const bool value = std::true_type::value; };
template <protocol P> struct is_udp_prot { static constexpr const bool value = std::false_type::value; };
template <> struct is_udp_prot<protocol::UDP> { static constexpr const bool value = std::true_type::value; };
// forward decl
struct addrinfos {
struct addrinfo* infos, *p;
};
template <protocol P> class server;
template <protocol P> class client;
template <protocol P>
class inetstream {
public:
inetstream() = delete;
inetstream(const inetstream<P>&) = delete;
inetstream(inetstream<P>&& other)
: _socket_fd {other._socket_fd}, _owns {other._owns}
{
other._socket_fd = -1;
_addrinfos.infos = other._addrinfos.infos;
_addrinfos.p = other._addrinfos.p;
other._addrinfos.infos = other._addrinfos.p = nullptr;
auto pos = other.size() - 1;
_send_buf = std::move(other._send_buf);
_recv_buf = std::move(other._recv_buf);
_read_pos = _recv_buf.begin() + pos;
}
~inetstream() {
if (_owns) {
if (_socket_fd != -1) {
close(_socket_fd);
}
freeaddrinfo(_addrinfos.infos);
_addrinfos.infos = _addrinfos.p = nullptr;
}
}
/**
* push data onto the stream
*
*/
template <typename T>
typename std::enable_if<!std::is_same<T, uint16_t>::value &&
!std::is_same<T, uint32_t>::value &&
!std::is_same<T, uint64_t>::value &&
!std::is_same<T, int16_t>::value &&
!std::is_same<T, int32_t>::value &&
!std::is_same<T, int64_t>::value &&
!std::is_same<T, float>::value &&
!std::is_same<T, double>::value &&
!std::is_same<T, std::string>::value &&
!std::is_same<typename std::remove_const<T>::type, char[]>::value &&
!std::is_same<typename std::remove_const<T>::type, char*>::value,
inetstream<P>&>::type
operator<<(T t) {
unsigned char chars[sizeof(T)];
std::memcpy(&chars[0], &t, sizeof(T));
for (std::size_t s {0}; s < sizeof t; ++s) {
this->_send_buf.push_back(chars[s]);
}
return *this;
}
/**
* retrieve data from the stream
*
*/
template <typename T>
typename std::enable_if<!std::is_same<T, uint16_t>::value &&
!std::is_same<T, uint32_t>::value &&
!std::is_same<T, uint64_t>::value &&
!std::is_same<T, int16_t>::value &&
!std::is_same<T, int32_t>::value &&
!std::is_same<T, int64_t>::value &&
!std::is_same<T, float>::value &&
!std::is_same<T, double>::value &&
!std::is_same<T, std::string>::value &&
!std::is_same<typename std::remove_const<T>::type, char[]>::value &&
!std::is_same<typename std::remove_const<T>::type, char*>::value, void>::type
operator>>(T& t) {
if (this->size() < sizeof(T)) {
std::stringstream ss;
ss << "tried to read " << sizeof(T) << " bytes into variable but only got "
<< this->size() << " bytes";
throw std::runtime_error {ss.str()};
}
unsigned char chars[sizeof(T)];
for (std::size_t i {0}; i < sizeof(T); ++i) {
chars[i] = *(this->_read_pos)++;
}
std::memcpy(&t, &chars[0], sizeof(T));
}
inetstream<P>& operator<< (uint16_t us) {
constexpr std::size_t sz = sizeof us;
// no undefined behaviour, since aliased type is unsigned char
union {
uint16_t i;
byte b[sz];
} u {0};
u.i = htons(us);
for (std::size_t s {0}; s < sz; ++s) {
this->_send_buf.push_back(u.b[s]);
}
return *this;
}
inetstream<P>& operator<< (int16_t s) {
return this->operator<<(static_cast<uint16_t>(s));
}
inetstream<P>& operator<< (uint32_t ul) {
constexpr std::size_t sz = sizeof ul;
union {
uint32_t i;
byte b[sz];
} u {0};
u.i = htonl(ul);
for (std::size_t s {0}; s < sz; ++s) {
this->_send_buf.push_back(u.b[s]);
}
return *this;
}
inetstream<P>& operator<< (int32_t l) {
return this->operator<<(static_cast<uint32_t>(l));
}
inetstream<P>& operator<< (uint64_t ull) {
constexpr std::size_t sz = sizeof ull;
union {
uint64_t i;
byte b[sz];
} u {0};
u.i = static_cast<uint64_t>(htonl((ull & 0xffffffff00000000) >> 32));
u.i |= static_cast<uint64_t>(htonl(ull & 0x00000000ffffffff)) << 32;
for (std::size_t s {0}; s < sz; ++s) {
this->_send_buf.push_back(u.b[s]);
}
return *this;
}
inetstream<P>& operator<< (int64_t ll) {
return this->operator<<(static_cast<uint64_t>(ll));
}
inetstream<P>& operator<< (float f) {
static_assert(sizeof(float) == sizeof(uint32_t), "sizeof float not supported");
uint32_t ul {};
std::memcpy(&ul, &f, sizeof(float));
*this << ul;
return *this;
}
inetstream<P>& operator<< (double d) {
static_assert(sizeof(double) == sizeof(uint64_t), "sizeof double not supported");
uint64_t ull {};
std::memcpy(&ull, &d, sizeof(double));
*this << ull;
return *this;
}
inetstream<P>& operator<< (std::string s) {
for (const char c : s) {
this->_send_buf.push_back(c);
}
return *this;
}
inetstream<P>& operator<< (const char* p) {
std::size_t sz = std::strlen(p);
for (std::size_t i {0}; i < sz; ++i) {
this->_send_buf.push_back(p[i]);
}
return *this;
}
void operator>> (uint16_t& us) {
constexpr std::size_t sz = sizeof us;
if (this->size() < sz) {
std::stringstream ss;
ss << "tried to read " << sz << " bytes into variable but only got "
<< this->size() << " bytes";
throw std::runtime_error {ss.str()};
}
union {
uint16_t i;
byte b[sz];
} u {0};
for (std::size_t s = 0; s < sz; ++s) {
u.b[s] = *this->_read_pos++;
}
us = ntohs(u.i);
}
void operator>> (int16_t& s) {
uint16_t us {};
this->operator>>(us);
s = static_cast<int16_t>(us);
}
void operator>> (uint32_t& ul) {
constexpr std::size_t sz = sizeof ul;
if (this->size() < sz) {
std::stringstream ss;
ss << "tried to read " << sz << " bytes into variable but only got "
<< this->size() << " bytes";
throw std::runtime_error {ss.str()};
}
union {
uint32_t i;
byte b[sz];
} u {0};
for (std::size_t s = 0; s < sz; ++s) {
u.b[s] = *this->_read_pos++;
}
ul = ntohl(u.i);
}
void operator>> (int32_t& l) {
uint32_t ul {};
this->operator>>(ul);
l = static_cast<int32_t>(ul);
}
void operator>> (uint64_t& ull) {
constexpr std::size_t sz = sizeof ull;
if (this->size() < sz) {
std::stringstream ss;
ss << "tried to read " << sz << " bytes into variable but only got "
<< this->size() << " bytes";
throw std::runtime_error {ss.str()};
}
uint32_t tmp {0};
uint64_t utmp {0};
// msb
*this >> tmp;
utmp = static_cast<uint64_t>(tmp) << 32;
// lsb
*this >> tmp;
// bitshift operators already take care of ntohl stuff
ull = utmp | tmp;
}
void operator>> (int64_t& ll) {
uint64_t ull {};
this->operator>>(ull);
ll = static_cast<int64_t>(ull);
}
void operator>> (float& f) {
static_assert(sizeof(float) == sizeof(uint32_t), "sizeof float not supported");
uint32_t ul {};
*this >> ul;
std::memcpy(&f, &ul, sizeof(float));
}
void operator>> (double& d) {
static_assert(sizeof(double) == sizeof(uint64_t), "sizeof double not supported");
uint64_t ull {};
*this >> ull;
std::memcpy(&d, &ull, sizeof(double));
}
void operator>> (std::string& s) {
s.clear();
while (this->size() > 0) {
byte b = *(this->_read_pos)++;
if (b != 0) {
s += b;
}
else {
break;
}
}
}
/**
* sends data pushed into the stream over the network to remote
*
*/
template <protocol T = P>
typename std::enable_if<is_tcp_prot<T>::value, void>::type
send() {
std::chrono::milliseconds timeout {INET_MAX_SEND_TIMEOUT_MS};
auto t_end = std::chrono::system_clock::now() + timeout;
auto total = _send_buf.size();
do {
int sent_this_iter = 0;
sent_this_iter = ::send(_socket_fd, &_send_buf[_send_buf.size() - total], total, 0);
if (sent_this_iter == -1) {
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
if (std::chrono::system_clock::now() > t_end) {
throw std::runtime_error {"timeout reached"};
}
total -= sent_this_iter;
} while (total > 0);
this->clear();
}
/**
* sends data pushed into the stream over the network to remote
*
*/
template <protocol T = P>
typename std::enable_if<is_udp_prot<T>::value, void>::type
send() {
std::chrono::milliseconds timeout {INET_MAX_SEND_TIMEOUT_MS};
auto t_end = std::chrono::system_clock::now() + timeout;
auto total = _send_buf.size();
do {
int sent_this_iter =
::sendto(_socket_fd, &_send_buf[_send_buf.size() - total], total, 0,
_addrinfos.p->ai_addr, _addrinfos.p->ai_addrlen);
if (sent_this_iter == -1) {
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
if (std::chrono::system_clock::now() > t_end) {
throw std::runtime_error {"timeout reached"};
}
total -= sent_this_iter;
} while (total > 0);
}
/**
* receives network data, populating the stream with data
*
* @param s number of bytes to try and receive
* @return the number of bytes received
*/
template <protocol T = P>
typename std::enable_if<is_tcp_prot<T>::value, std::size_t>::type
recv(std::size_t sz) {
std::chrono::milliseconds timeout {INET_MAX_RECV_TIMEOUT_MS};
auto t_end = std::chrono::system_clock::now() + timeout;
// cache read_pos pointer, since _recv_buf may realloc
auto read_offset_ = _read_pos - _recv_buf.begin();
auto tmp_size = _recv_buf.size();
constexpr const std::size_t SZ {1024};
int read {0};
std::array<unsigned char, SZ> buf;
do {
if (sz < SZ) {
read = ::recv(_socket_fd, &buf[0], sz, 0);
}
else {
read = ::recv(_socket_fd, &buf[0], SZ - 1, 0);
}
if (read == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
if (std::chrono::system_clock::now() < t_end) {
// wait until timeout is reached
continue;
}
break;
}
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
if (read == 0) {
break;
}
if (read > 0) {
sz -= read;
}
_recv_buf.insert(_recv_buf.end(), buf.begin(), buf.begin() + read);
} while (sz && std::chrono::system_clock::now() < t_end);
_read_pos = _recv_buf.begin() + read_offset_;
return _recv_buf.size() - tmp_size;
}
/**
* receives data from network and stores it in stream
*
* if data is available to receive, will return early after receiving
*
* @throws std::system_error if ::recv() encountered an error
*
* @return number of bytes received
*/
template <protocol T = P>
typename std::enable_if<is_udp_prot<T>::value, std::size_t>::type
recv() {
std::chrono::milliseconds timeout {INET_MAX_RECV_TIMEOUT_MS};
// cache offset because recv may cause _recv_buf to realloc
auto read_offset_ = _read_pos - _recv_buf.begin();
struct sockaddr_storage remote_addr;
socklen_t addr_len = sizeof(remote_addr);
constexpr const std::size_t SZ {1024};
std::array<unsigned char, SZ> buf;
int num_recv {0};
if (this->select(timeout)) {
num_recv = ::recvfrom(_socket_fd, &buf[0], SZ - 1, 0,
reinterpret_cast<struct sockaddr*>(&remote_addr), &addr_len);
}
if (num_recv == -1) {
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
if (num_recv == 0)
return 0;
_recv_buf.insert(_recv_buf.end(), buf.begin(), buf.begin() + num_recv);
_read_pos = _recv_buf.begin() + read_offset_;
return num_recv;
}
bool empty() const { return size() == 0; }
void clear() { _send_buf.clear(); _recv_buf.clear(); _read_pos = _recv_buf.begin(); }
std::size_t size() const { return _recv_buf.end() - _read_pos; }
/**
* blocks while now() < time_of_call + timeout and checks if data can be
* recv()-ed
*
* @param timeout duration for which to wait for data to arrive
*
* @return early true if data can be recv()-ed or false after timeout
* expired otherwise
*
* @throws std::system_error if ::select() encounters an error
*
*/
bool select(std::chrono::milliseconds timeout) const {
struct timeval t {};
t.tv_sec = timeout.count() / 1000;
t.tv_usec = (timeout.count() - t.tv_sec * 1000) * 1000;
fd_set rfds {};
FD_ZERO(&rfds);
FD_SET(_socket_fd, &rfds);
int rv = ::select(_socket_fd + 1, &rfds, nullptr, nullptr, &t);
if (rv < 0) {
// error occured
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
// 0 on timeout, 1 if socket is ready to recv
return static_cast<bool>(rv);
}
private:
inetstream(int socket_fd, addrinfos addrinfos, bool owns)
: _socket_fd {socket_fd}, _addrinfos {addrinfos}, _read_pos {_recv_buf.begin()}, _owns {owns}
{
}
friend class server<P>;
friend class client<P>;
int _socket_fd;
addrinfos _addrinfos;
std::vector<byte> _send_buf;
std::vector<byte> _recv_buf;
std::vector<byte>::iterator _read_pos;
bool _owns;
};
template <protocol P>
class server {
public:
template <protocol T = P, typename std::enable_if<is_tcp_prot<T>::value, int>::type* = nullptr>
server(unsigned short Port) : _port {Port}, _addrinfos {nullptr, nullptr} {
if (INET_USE_DEFAULT_SIGUSR1_HANDLER) {
struct sigaction sa;
sa.sa_handler = sigusr1_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGUSR1, &sa, NULL) != 0) {
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
}
addrinfo hints;
std::memset(&hints, 0, sizeof hints);
if (INET_IPV == 4) {
hints.ai_family = AF_INET;
}
else if (INET_IPV == 6) {
hints.ai_family = AF_INET6;
}
else {
hints.ai_family = AF_UNSPEC;
}
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
char port[6] = {0};
std::stringstream ss;
ss << _port;
ss >> port;
port[5] = 0;
int rv = getaddrinfo(NULL, port, &hints, &_addrinfos.infos);
if (rv != 0) {
throw std::system_error {rv, std::system_category(), gai_strerror(rv)};
}
for (_addrinfos.p = _addrinfos.infos; _addrinfos.p != NULL; _addrinfos.p = _addrinfos.p->ai_next) {
_socket_fd = socket(_addrinfos.p->ai_family, _addrinfos.p->ai_socktype, _addrinfos.p->ai_protocol);
if (_socket_fd == -1) {
// swallow error
continue;
}
int yes = 1; rv = 0;
rv = setsockopt(_socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (rv == -1) {
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
if (bind(_socket_fd, _addrinfos.p->ai_addr, _addrinfos.p->ai_addrlen) == -1) {
close(_socket_fd);
// swallow error
continue;
}
break;
}
if (_addrinfos.p == NULL) {
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
if (listen(_socket_fd, INET_MAX_CONNECTIONS) == -1) {
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
}
template <protocol T = P, typename std::enable_if<is_udp_prot<T>::value, int>::type* = nullptr>
server(unsigned short Port) : _port {Port}, _addrinfos {nullptr, nullptr} {
addrinfo hints;
std::memset(&hints, 0, sizeof hints);
if (INET_IPV == 4) {
hints.ai_family = AF_INET;
}
else if (INET_IPV == 6) {
hints.ai_family = AF_INET6;
}
else {
hints.ai_family = AF_UNSPEC;
}
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
char port[6] = {0};
std::stringstream ss;
ss << _port;
ss >> port;
port[5] = 0;
int rv = getaddrinfo(NULL, port, &hints, &_addrinfos.infos);
if (rv != 0) {
throw std::system_error {rv, std::system_category(), gai_strerror(rv)};
}
for (_addrinfos.p = _addrinfos.infos; _addrinfos.p != NULL; _addrinfos.p = _addrinfos.p->ai_next) {
_socket_fd = socket(_addrinfos.p->ai_family, _addrinfos.p->ai_socktype, _addrinfos.p->ai_protocol);
if (_socket_fd == -1) {
// swallow error
continue;
}
if (bind(_socket_fd, _addrinfos.p->ai_addr, _addrinfos.p->ai_addrlen) == -1) {
close(_socket_fd);
// swallow error
continue;
}
break;
}
if (_addrinfos.p == NULL) {
freeaddrinfo(_addrinfos.infos);
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
}
~server() {
close(_socket_fd); _socket_fd = -1;
freeaddrinfo(_addrinfos.infos);
_addrinfos.infos = _addrinfos.p = nullptr;
}
/**
* set the underlying socket to non-blocking mode.
* @throws std::system_error if an error occurs
*/
template <protocol T = P>
typename std::enable_if<is_tcp_prot<T>::value>::type set_nonblocking() {
if (fcntl(_socket_fd, F_SETFD, O_NONBLOCK)) {
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
}
/**
* if using select() before set_nonblocking() has been called, the
* behaviour is undefined.
*
* blocks while now() < time_of_call + timeout and checks if a client
* can be accept()-ed without blocking.
*
* @param timeout duration for which to wait for client to connect()
*
* @return early true if client can be accept()-ed or false after
* timeout expired otherwise
*
* @throws std::system_error if ::select() encounters an error
*
*/
template <protocol T = P>
typename std::enable_if<is_tcp_prot<T>::value, bool>::type
select(std::chrono::milliseconds timeout) const {
struct timeval t {};
t.tv_sec = timeout.count() / 1000;
t.tv_usec = (timeout.count() - t.tv_sec * 1000) * 1000;
fd_set rfds {};
FD_ZERO(&rfds);
FD_SET(_socket_fd, &rfds);
int rv = ::select(_socket_fd + 1, &rfds, nullptr, nullptr, &t);
if (rv == 0) {
return false;
}
if (rv > 0) {
return true;
}
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
/**
* blocks until a client connects.
*
* @throws std::system_error if ::accept() was interrupted by a signal
* or the connection could not established cleanly
*
* @return inetstream to the connected client
*/
// enables "accept" if protocol is TCP
template <protocol T = P>
typename std::enable_if<is_tcp_prot<T>::value, inetstream<protocol::TCP>>::type accept() {
sockaddr_storage client_addr;
socklen_t sin_sz = sizeof client_addr;
int new_fd = ::accept(_socket_fd, reinterpret_cast<sockaddr*>(&client_addr), &sin_sz);
if (new_fd == -1) {
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
char s[INET6_ADDRSTRLEN];
const char* rv = inet_ntop(client_addr.ss_family,
get_in_addr(reinterpret_cast<sockaddr*>(&client_addr)),
s, sizeof s);
if (rv == NULL) {
close(new_fd);
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
if (fcntl(new_fd, F_SETFL, O_NONBLOCK) != 0) {
close(new_fd);
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
// connected to s
return inetstream<protocol::TCP> {new_fd, {nullptr, nullptr}, /*owns*/true};
}
/**
*
* @return inetstream to this end of the communication
*/
// enables "get_inetstream" if protocol is UDP
template <protocol T = P>
typename std::enable_if<is_udp_prot<T>::value, inetstream<protocol::UDP>>::type
get_inetstream() {
// don't transfer ownership
return inetstream<protocol::UDP> {_socket_fd, _addrinfos, /*owns*/false};
}
private:
unsigned short _port;
int _socket_fd;
addrinfos _addrinfos;
};
template <protocol P>
class client {
public:
template <protocol T = P, typename std::enable_if<is_tcp_prot<T>::value, int>::type* = nullptr>
client(const std::string& Host, unsigned short Port)
: _host {Host}, _port {Port} {}
template <protocol T = P, typename std::enable_if<is_udp_prot<T>::value, int>::type* = nullptr>
client (const std::string& Host, unsigned short Port)
: _host {Host}, _port {Port} {}
/**
* connect the client to the server specified via the constructor
*
* @throws std::system_error if connection was not possible for some
* reason
*
* @return inetstream to the connected server
*/
// enables "connect" if protocol is TCP
template <protocol T = P>
typename std::enable_if<is_tcp_prot<T>::value, inetstream<protocol::TCP>>::type
connect() {
addrinfo hints;
std::memset(&hints, 0, sizeof hints);
if (INET_IPV == 4) {
hints.ai_family = AF_INET;
}
else if (INET_IPV == 6) {
hints.ai_family = AF_INET6;
}
else {
hints.ai_family = AF_UNSPEC;
}
hints.ai_socktype = SOCK_STREAM;
char port[6] = {0};
std::stringstream ss;
ss << _port;
ss >> port;
port[5] = 0;
addrinfo* infos, *p;
int rv = getaddrinfo(_host.c_str(), port, &hints, &infos);
if (rv != 0) {
throw std::system_error {rv, std::system_category(), gai_strerror(rv)};
}
for (p = infos; p != NULL; p = p->ai_next) {
_socket_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (_socket_fd == -1) {
// swallow error
continue;
}
if (::connect(_socket_fd, p->ai_addr, p->ai_addrlen) == -1) {
close(_socket_fd);
// swallow error
continue;
}
break;
}
if (p == NULL) {
freeaddrinfo(infos);
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
char s[INET6_ADDRSTRLEN];
const char* rvp = inet_ntop(
p->ai_family,
get_in_addr(reinterpret_cast<sockaddr*>(p->ai_addr)),
s, sizeof s);
if (rvp == NULL) {
freeaddrinfo(infos);
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
if (fcntl(_socket_fd, F_SETFL, O_NONBLOCK) != 0) {
freeaddrinfo(infos);
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
return inetstream<protocol::TCP> {_socket_fd, addrinfos {infos, p}, /*owns*/true};
}
/**
* @throws std::system_error if communication could not be established
* @return inetstream to this end of the communication
*/
// enables "get_inetstream" if protocol is UDP
template <protocol T = P>
typename std::enable_if<is_udp_prot<T>::value, inetstream<protocol::UDP>>::type
get_inetstream() {
addrinfo hints;
std::memset(&hints, 0, sizeof hints);
if (INET_IPV == 4) {
hints.ai_family = AF_INET;
}
else if (INET_IPV == 6) {
hints.ai_family = AF_INET6;
}
else {
hints.ai_family = AF_UNSPEC;
}
hints.ai_socktype = SOCK_DGRAM;
char port[6] = {0};
std::stringstream ss;
ss << _port;
ss >> port;
port[5] = 0;
addrinfo* infos, *p;
int rv = getaddrinfo(_host.c_str(), port, &hints, &infos);
if (rv != 0) {
throw std::system_error {rv, std::system_category(), gai_strerror(rv)};
}
for (p = infos; p != NULL; p = p->ai_next) {
_socket_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (_socket_fd == -1) {
// swallow error
continue;
}
break;
}
if (p == NULL) {
freeaddrinfo(infos);
throw std::system_error {errno, std::system_category(), strerror(errno)};
}
return inetstream<protocol::UDP> {_socket_fd, addrinfos {infos, p}, /*owns*/true};
}
private:
std::string _host;
unsigned short _port;
int _socket_fd;
};
} // namespace inet
#endif