This repository was archived by the owner on Apr 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsocket.hpp
More file actions
881 lines (763 loc) · 23.9 KB
/
socket.hpp
File metadata and controls
881 lines (763 loc) · 23.9 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
#ifndef SOCKET_HPP
#define SOCKET_HPP
#include "socket_family.hpp"
#include <memory>
#include <stdexcept>
#include <string>
namespace net {
/**
* @class net::Socket
* Socket class to create Berkeley sockets.
* Uses socket domains from domain enum in SF namespace from socket_family.hpp
* Uses socket types from type enum in SF namespace from socket_family.hpp
*/
class Socket final {
private:
union {
AddrStore store;
AddrIPv4 ipv4;
AddrIPv6 ipv6;
AddrUnix unix;
};
int sockfd;
Domain sock_domain;
Type sock_type;
bool isClosed = false;
/**
* @method low_write
* @access private
* Writes given _msg using _sockfd by calling _fn with args having flags and
* destination socket address.
*
* @param {callable} _fn Some callable that writes using socket descriptor.
* @param {string} _msg Msg to write on sockfd.
* @param {parameter_pack} args Flags, destination sockaddr objects and their
* lengths.
* @returns {ssize_t} Status of writing _msg using socket descriptor / Number
* of bytes written using socket descriptor.
*/
template <typename Fn, typename... Args>
auto low_write(Fn &&_fn, const std::string &_msg, Args &&... args) const
{
ssize_t written = 0;
std::string::size_type count = 0;
do {
written = std::forward<Fn>(_fn)(sockfd, _msg.c_str() + count,
_msg.length() - count,
std::forward<Args>(args)...);
count += written;
} while (count < _msg.length() && written > 0);
return written;
}
/**
* @method low_read
* @access private
* Reads using sockfd by calling _fn with args having flags and destination
* socket address.
*
* @param {callable} _fn Some callable that reads using socket descriptor.
* @param {string} _str String to store the data.
* @param {parameter_pack} args Flags, destination sockaddr objects and their
* lengths.
* @returns {ssize_t} Status of reading data using socket descriptor / Number
* of bytes read using socket descriptor.
*/
template <typename Fn, typename... Args>
auto low_read(Fn &&_fn, std::string &_str, Args &&... args) const
{
const auto bufSize = _str.capacity();
const auto buffer = std::make_unique<char[]>(bufSize);
const auto recvd = std::forward<Fn>(_fn)(sockfd, buffer.get(), bufSize,
std::forward<Args>(args)...);
_str.append(buffer.get(), (recvd > 0) ? recvd : 0);
return recvd;
}
/**
* @construct Socket
* @access private
* @param {int} _sockfd Descriptor representing a socket.
* @param {Domain} _domain Socket domain.
* @param {Type} _domain Socket type.
* @param {void *} _addr Pointer to initialize appropriate member of Union of
* net::Socket.
*/
Socket(const int, Domain, Type, const void *);
Socket(const Socket &) = delete;
Socket &operator=(const Socket &) = delete;
public:
/**
* @construct net::Socket
* @access public
* @param {domain} _domain Socket domain.
* @param {type} _type Socket type.
* @param {int} _proto Socket protocol.
*/
Socket(Domain _domain, Type _type, const int _proto = 0)
: sock_domain(_domain), sock_type(_type)
{
const auto d = static_cast<int>(sock_domain);
const auto t = static_cast<int>(sock_type);
sockfd = socket(d, t, _proto);
if (sockfd < 0) {
const auto currErrno = errno;
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
switch (sock_domain) {
case Domain::IPv4: ipv4.sin_family = AF_INET; break;
case Domain::IPv6: ipv6.sin6_family = AF_INET6; break;
case Domain::UNIX: unix.sun_family = AF_UNIX; break;
default: store.ss_family = d;
}
}
/**
* @construct net::Socket using another net::Socket.
* @access public
* @param {Socket} s Rvalue of type socket.
*/
Socket(Socket &&s)
{
sockfd = s.sockfd;
sock_domain = s.sock_domain;
sock_type = s.sock_type;
switch (s.sock_domain) {
case Domain::IPv4: ipv4 = s.ipv4; break;
case Domain::IPv6: ipv6 = s.ipv6; break;
case Domain::UNIX: unix = s.unix; break;
default: store = s.store;
}
}
/**
* @method getSocket
* @access public
* Get the socket descriptor in net::Socket.
*
* @returns {int} Socket descriptor for net::Socket.
*/
auto getSocket() const noexcept { return sockfd; }
/**
* @method getDomain
* @access public
* Get the Domain type of Socket.
*
* @returns {Domain} for net::Socket.
*/
auto getDomain() const noexcept { return sock_domain; }
/**
* @method getType
* @access public
* Get the Protocol Type of Socket.
*
* @returns {Type} for net::Socket.
*/
auto getType() const noexcept { return sock_type; }
/**
* @method bind
* @access public
* Binds net::Socket to local address if successful else if Address argument
* is invalid then throws invalid_argument exception
* else throws runtime_error exception signalling that bind failed. Invokes
* the callable provided to fill AddrIPv4 object.
*
* @param {callable} _fn Some callable that takes arg of type AddrIPv4.
*/
template <typename F>
auto bind(F _fn) -> decltype(_fn(std::declval<AddrIPv4 &>()), void()) const
{
AddrIPv4 addr;
auto res = _fn(addr);
if (res >= 1) {
res = ::bind(sockfd, (sockaddr *) &addr, sizeof(addr));
res = (res == 0) ? 1 : res;
}
const auto currErrno = errno;
if (res == -1) {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
} else if (res == 0) {
throw std::invalid_argument("Address argument invalid");
}
ipv4 = addr;
}
/**
* @method bind
* @access public
* Binds net::Socket to local address if successful else if Address argument
* is invalid then throws invalid_argument exception
* else throws runtime_error exception signalling that bind failed. Invokes
* the callable provided to fill AddrIPv6 object.
*
* @param {callable} _fn Some callable that takes arg of type AddrIPv6.
*/
template <typename F>
auto bind(F _fn) -> decltype(_fn(std::declval<AddrIPv6 &>()), void()) const
{
AddrIPv6 addr;
auto res = _fn(addr);
if (res >= 1) {
res = ::bind(sockfd, (sockaddr *) &addr, sizeof(addr));
res = (res == 0) ? 1 : res;
}
const auto currErrno = errno;
if (res == -1) {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
} else if (res == 0) {
throw std::invalid_argument("Address argument invalid");
}
ipv6 = addr;
}
/**
* @method bind
* @access public
* Binds net::Socket to local address if successful else if Address argument
* is invalid then throws invalid_argument exception
* else throws runtime_error exception signalling that bind failed. Invokes
* the callable provided to fill AddrUnix object.
*
* @param {callable} _fn Some callable that takes arg of type AddrUnix.
*/
template <typename F>
auto bind(F _fn) -> decltype(_fn(std::declval<AddrUnix &>()), void()) const
{
AddrUnix addr;
auto res = _fn(addr);
if (res >= 1) {
res = ::bind(sockfd, (sockaddr *) &addr, sizeof(addr));
res = (res == 0) ? 1 : res;
}
const auto currErrno = errno;
if (res == -1) {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
} else if (res == 0) {
throw std::invalid_argument("Address argument invalid");
}
unix = addr;
}
/**
* @method connect
* @access public
* Connects net::Socket to address _addr:_port if successful else throws
* invalid_argument exception.
*
* @param {char []} _addr Ip address in case of ipv4 or ipv6 domain, and Path
* in case of unix domain.
* @param {int} _port Port number in case of AddrIPv4 or AddrIPv6.
* @param {bool *} _errorNB To signal error in case of non-blocking connect.
*/
void connect(const char[], const int = 0, bool * = nullptr);
/**
* @method connect
* @access public
* Connects net::Socket to ipv4 peer if successful else throws runtime_error
* exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
* Throws invalid_argument exception if destination address given is invalid.
*
* @param {callable} _fn Some callable that takes arg of type AddrIPv4.
* @param {bool *} _errorNB To signal error in case of non-blocking connect.
*/
template <typename F>
auto connect(F _fn, bool *_errorNB = nullptr)
-> decltype(_fn(std::declval<AddrIPv4 &>()), void()) const
{
AddrIPv4 addr;
auto res = _fn(addr);
if (res >= 1) {
res = ::connect(sockfd, (sockaddr *) &addr, sizeof(addr));
res = (res == 0) ? 1 : res;
}
const auto currErrno = errno;
if (res == -1) {
if (currErrno == EINPROGRESS) {
if (_errorNB != nullptr) {
*_errorNB = true;
} else {
throw std::invalid_argument("errorNB argument missing");
}
} else {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
} else if (res == 0) {
throw std::invalid_argument("Address argument invalid");
}
}
/**
* @method connect
* @access public
* Connects net::Socket to ipv6 peer if successful else throws runtime_error
* exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
* Throws invalid_argument exception if destination address given is invalid.
* Invokes the callable provided to fill AddrIPv4 object.
*
* @param {callable} _fn Some callable that takes arg of type AddrIPv6.
* @param {bool *} _errorNB To signal error in case of non-blocking connect.
*/
template <typename F>
auto connect(F _fn, bool *_errorNB = nullptr)
-> decltype(_fn(std::declval<AddrIPv6 &>()), void()) const
{
AddrIPv6 addr;
auto res = _fn(addr);
if (res >= 1) {
res = ::connect(sockfd, (sockaddr *) &addr, sizeof(addr));
res = (res == 0) ? 1 : res;
}
const auto currErrno = errno;
if (res == -1) {
if (currErrno == EINPROGRESS) {
if (_errorNB != nullptr) {
*_errorNB = true;
} else {
throw std::invalid_argument("errorNB argument missing");
}
} else {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
} else if (res == 0) {
throw std::invalid_argument("Address argument invalid");
}
}
/**
* @method connect
* @access public
* Connects net::Socket to unix socket peer if successful else throws
* runtime_error exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
* Throws invalid_argument exception if destination address given is invalid.
* Invokes the callable provided to fill AddrIPv4 object.
*
* @param {callable} _fn Some callable that takes arg of type AddrUnix.
* @param {bool *} _errorNB To signal error in case of non-blocking connect.
*/
template <typename F>
auto connect(F _fn, bool *_errorNB = nullptr)
-> decltype(_fn(std::declval<AddrUnix &>()), void()) const
{
AddrUnix addr;
auto res = _fn(addr);
if (res >= 1) {
res = ::connect(sockfd, (sockaddr *) &addr, sizeof(addr));
res = (res == 0) ? 1 : res;
}
const auto currErrno = errno;
if (res == -1) {
if (currErrno == EINPROGRESS) {
if (_errorNB != nullptr) {
*_errorNB = true;
} else {
throw std::invalid_argument("errorNB argument missing");
}
} else {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
} else if (res == 0) {
throw std::invalid_argument("Address argument invalid");
}
}
/**
* @method start
* @access public
* Starts the net::Socket in listen mode on given ip address and given port
* with given backlog if successful
* else throws runtime_error exception.
* Throws invalid_argument exception if given ip address or port are not
* valid.
*
* @param {char []} _addr Ip address in case of ipv4 or ipv6 domain, and Path
* in case of unix domain.
* @param {int} _port Port number in case of ipv4 or ipv6.
* @param {int} _q Size of backlog of listening socket.
*/
void start(const char[], const int = 0, const int = SOMAXCONN);
/**
* @method accept
* @access public
* Returns Socket object from connected sockets queue if successful else
* throws runtime_error exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
*
* @param {bool *} _errorNB To signal error in case of non-blocking accept.
* @returns {net::Socket}
*/
Socket accept(bool * = nullptr) const;
/**
* @method write
* @access public
* Writes given string to Socket if successful else throws runtime_error
* exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
*
* @param {string} _msg String to be written to Socket.
* @param {bool *} _errorNB To signal error in case of non-blocking write.
*/
void write(const std::string &, bool * = nullptr) const;
/**
* @method read
* @access public
* Reads given number of bytes using Socket if successful else throws
* runtime_error exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
*
* @param {int} _bufSize Number of bytes to be read using Socket.
* @param {bool *} _errorNB To signal error in case of non-blocking read.
* @returns {string} String of _bufSize bytes read using Socket.
*/
std::string read(const int, bool * = nullptr) const;
/**
* @method send
* @access public
* Sends given string using Socket if successful else throws runtime_error
* exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
*
* @param {string} _msg String to be sent using Socket.
* @param {send} _flags Modify default behaviour of send.
* @param {bool *} _errorNB To signal error in case of non-blocking send.
*/
void send(const std::string &, Send = Send::NONE, bool * = nullptr) const;
/**
* @method send
* @access public
* Sends given string using Socket if successful else throws runtime_error
* exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
* Throws invalid_argument exception if destination address given is invalid.
* Invokes the callable provided to fill AddrIPv4 object.
*
* @param {string} _msg String to be sent using Socket.
* @param {callable} _fn Some callable that takes arg of type AddrIPv4 or
* void.
* @param {send} _flags Modify default behaviour of send.
* @param {bool *} _errorNB To signal error in case of non-blocking send.
*/
template <typename F>
auto send(const std::string &_msg, F _fn, Send _flags = Send::NONE,
bool *_errorNB = nullptr) const
-> decltype(_fn(std::declval<AddrIPv4 &>()), void()) const
{
AddrIPv4 addr;
const auto flags = static_cast<int>(_flags);
const auto res = _fn(addr);
if (res == 0) {
throw std::invalid_argument("Address argument invalid");
}
ssize_t sent = -1;
if (res >= 1) {
sent = low_write(::sendto, _msg, flags, (sockaddr *) &addr,
sizeof(addr));
}
const auto currErrno = errno;
if (res == -1 || sent == -1) {
if (currErrno == EAGAIN || currErrno == EWOULDBLOCK) {
if (_errorNB != nullptr) {
*_errorNB = true;
} else {
throw std::invalid_argument("errorNB argument missing");
}
} else {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
}
}
/**
* @method send
* @access public
* Sends given string using Socket if successful else throws runtime_error
* exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
* Throws invalid_argument exception if destination address given is invalid.
* Invokes the callable provided to fill AddrIPv6 object.
*
* @param {string} _msg String to be sent using Socket.
* @param {callable} _fn Some callable that takes arg of type AddrIPv6 or
* void.
* @param {send} _flags Modify default behaviour of send.
* @param {bool *} _errorNB To signal error in case of non-blocking send.
*/
template <typename F>
auto send(const std::string &_msg, F _fn, Send _flags = Send::NONE,
bool *_errorNB = nullptr) const
-> decltype(_fn(std::declval<AddrIPv6 &>()), void()) const
{
AddrIPv6 addr;
const auto flags = static_cast<int>(_flags);
const auto res = _fn(addr);
if (res == 0) {
throw std::invalid_argument("Address argument invalid");
}
ssize_t sent = -1;
if (res >= 1) {
sent = low_write(::sendto, _msg, flags, (sockaddr *) &addr,
sizeof(addr));
}
const auto currErrno = errno;
if (res == -1 || sent == -1) {
if (currErrno == EAGAIN || currErrno == EWOULDBLOCK) {
if (_errorNB != nullptr) {
*_errorNB = true;
} else {
throw std::invalid_argument("errorNB argument missing");
}
} else {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
}
}
/**
* @method send
* @access public
* Sends given string using Socket if successful else throws runtime_error
* exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
* Throws invalid_argument exception if destination address given is invalid.
* Invokes the callable provided to fill AddrUnix object.
*
* @param {string} _msg String to be sent using Socket.
* @param {callable} _fn Some callable that takes arg of type AddrUnix or
* void.
* @param {send} _flags Modify default behaviour of send.
* @param {bool *} _errorNB To signal error in case of non-blocking send.
*/
template <typename F>
auto send(const std::string &_msg, F _fn, Send _flags = Send::NONE,
bool *_errorNB = nullptr) const
-> decltype(_fn(std::declval<AddrUnix &>()), void()) const
{
AddrUnix addr;
const auto flags = static_cast<int>(_flags);
const auto res = _fn(addr);
if (res == 0) {
throw std::invalid_argument("Address argument invalid");
}
ssize_t sent = -1;
if (res >= 1) {
sent = low_write(::sendto, _msg, flags, (sockaddr *) &addr,
sizeof(addr));
}
const auto currErrno = errno;
if (res == -1 || sent == -1) {
if (currErrno == EAGAIN || currErrno == EWOULDBLOCK) {
if (_errorNB != nullptr) {
*_errorNB = true;
} else {
throw std::invalid_argument("errorNB argument missing");
}
} else {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
}
}
/**
* @method recv
* @access public
* Reads given number of bytes using Socket if successful else throws
* runtime_error exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
*
* @param {int} _bufSize Number of bytes to be read using Socket.
* @param {recv} _flags Modify default behaviour of recv.
* @param {bool *} _errorNB To signal error in case of non-blocking recv.
* @returns {string} String of _bufSize bytes read from socket.
*/
std::string recv(const int, Recv = Recv::NONE, bool * = nullptr) const;
/**
* @method recv
* @access public
* Reads given number of bytes using Socket if successful else throws
* runtime_error exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
* Throws invalid_argument exception if destination address given is invalid.
* Invokes the callable provided to return AddrIPv4 object from where msg has
* been received.
*
* @param {int} _numBytes Number of bytes to read.
* @param {callable} _fn Some callable that takes arg of type AddrIPv4 or
* void.
* @param {recv} _flags Modify default behaviour of recv.
* @param {bool *} _errorNB To signal error in case of non-blocking recv.
*/
template <typename F>
auto recv(const int _numBytes, F _fn, Recv _flags = Recv::NONE,
bool *_errorNB = nullptr) const
-> decltype(_fn(std::declval<AddrIPv4 &>()), std::string()) const
{
AddrIPv4 addr;
std::string str;
str.reserve(_numBytes);
const auto flags = static_cast<int>(_flags);
socklen_t length = sizeof(addr);
const auto recvd
= low_read(::recvfrom, str, flags, (sockaddr *) &addr, &length);
const auto currErrno = errno;
if (recvd == -1) {
if (currErrno == EAGAIN || currErrno == EWOULDBLOCK) {
if (_errorNB != nullptr) {
*_errorNB = true;
} else {
throw std::invalid_argument("errorNB argument missing");
}
} else {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
}
_fn(addr);
return str;
}
/**
* @method recv
* @access public
* Reads given number of bytes using Socket if successful else throws
* runtime_error exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
* Throws invalid_argument exception if destination address given is invalid.
* Invokes the callable provided to return AddrIPv6 object from where msg has
* been received.
*
* @param {int} _numBytes Number of bytes to read.
* @param {callable} _fn Some callable that takes arg of type AddrIPv6 or
* void.
* @param {recv} _flags Modify default behaviour of recv.
* @param {bool *} _errorNB To signal error in case of non-blocking recv.
*/
template <typename F>
auto recv(const int _numBytes, F _fn, Recv _flags = Recv::NONE,
bool *_errorNB = nullptr) const
-> decltype(_fn(std::declval<AddrIPv6 &>()), std::string()) const
{
AddrIPv6 addr;
std::string str;
str.reserve(_numBytes);
const auto flags = static_cast<int>(_flags);
socklen_t length = sizeof(addr);
const auto recvd
= low_read(::recvfrom, str, flags, (sockaddr *) &addr, &length);
const auto currErrno = errno;
if (recvd == -1) {
if (currErrno == EAGAIN || currErrno == EWOULDBLOCK) {
if (_errorNB != nullptr) {
*_errorNB = true;
} else {
throw std::invalid_argument("errorNB argument missing");
}
} else {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
}
_fn(addr);
return str;
}
/**
* @method recv
* @access public
* Reads given number of bytes using Socket if successful else throws
* runtime_error exception.
* Throws invalid_argument exception in case of non-blocking net::Socket if
* _errorNB is missing.
* Throws invalid_argument exception if destination address given is invalid.
* Invokes the callable provided to return AddrUnix object from where msg has
* been received.
*
* @param {int} _numBytes Number of bytes to read.
* @param {callable} _fn Some callable that takes arg of type AddrUnix or
* void.
* @param {recv} _flags Modify default behaviour of recv.
* @param {bool *} _errorNB To signal error in case of non-blocking recv.
*/
template <typename F>
auto recv(const int _numBytes, F _fn, Recv _flags = Recv::NONE,
bool *_errorNB = nullptr) const
-> decltype(_fn(std::declval<AddrUnix &>()), std::string()) const
{
AddrUnix addr;
std::string str;
str.reserve(_numBytes);
const auto flags = static_cast<int>(_flags);
socklen_t length = sizeof(addr);
const auto recvd
= low_read(::recvfrom, str, flags, (sockaddr *) &addr, &length);
const auto currErrno = errno;
if (recvd == -1) {
if (currErrno == EAGAIN || currErrno == EWOULDBLOCK) {
if (_errorNB != nullptr) {
*_errorNB = true;
} else {
throw std::invalid_argument("errorNB argument missing");
}
} else {
throw std::runtime_error(net::methods::getErrorMsg(currErrno));
}
}
_fn(addr);
return str;
}
/**
* @method setOpt
* @access public
* Set a socket option from net::Opt for Socket using object of type
* net::SockOpt.
*
* @param {net::Opt} _opType Option to set for Socket.
* @param {net::SockOpt} _opValue socket option structure. Present inside
* socket_family.hpp.
*/
void setOpt(Opt, SockOpt) const;
/**
* @method getOpt
* @access public
* Get value of some socket option for Socket.
*
* @param {net::Opt} _opType Option of Socket whose value to get.
*/
SockOpt getOpt(Opt) const;
/**
* @method stop
* @access public
* Shutdown Socket using net::shut.
*
* @param {net::shut} _s Option specifying which side of connection to
* shutdown for Socket.
*/
void stop(Shut _s) const noexcept
{
shutdown(sockfd, static_cast<int>(_s));
}
/**
* method unlink
* @access public
* Unlinks the unix socket path.
*/
bool unlink() const noexcept
{
return (sock_domain == Domain::UNIX && !isClosed)
? ::unlink(unix.sun_path) == 0
: false;
}
/**
* method close
* @access public
* Closes the Socket for terminating connection.
*/
bool close() const noexcept
{
return (!isClosed) ? ::close(sockfd) == 0 : false;
}
~Socket() noexcept
{
unlink();
close();
}
};
}
#endif