COROIO: coroio/socket.hpp Source File
COROIO
Loading...
Searching...
No Matches
socket.hpp
1#pragma once
2
3#ifdef _WIN32
4#include <winsock2.h>
5#include <ws2tcpip.h>
6#include <io.h>
7#else
8#include <arpa/inet.h>
9#include <sys/stat.h>
10#include <sys/socket.h>
11#include <sys/types.h>
12#include <netinet/in.h>
13#include <unistd.h>
14#include <fcntl.h>
15#endif
16
17#include <expected>
18#include <optional>
19#include <variant>
20
21#include "poller.hpp"
22#include "address.hpp"
23#include "corochain.hpp"
24
25namespace NNet {
26
27template<typename T> class TSocketBase;
28
42template<>
43class TSocketBase<void> {
44public:
49 TPollerBase* Poller() { return Poller_; }
50
51protected:
52 TSocketBase(TPollerBase& poller, int domain, int type);
53 TSocketBase(int fd, TPollerBase& poller);
54 TSocketBase() = default;
55
63 int Create(int domain, int type);
72 int Setup(int s);
74 static void SetCloseOnExec(int s);
75
76 TPollerBase* Poller_ = nullptr;
77 int Fd_ = -1;
78};
79
102template<typename TSockOps>
103class TSocketBase: public TSocketBase<void> {
104protected:
112 TSocketBase(TPollerBase& poller, int domain, int type): TSocketBase<void>(poller, domain, type)
113 { }
114
120 TSocketBase(int fd, TPollerBase& poller): TSocketBase<void>(fd, poller)
121 { }
122
123 TSocketBase() = default;
124 TSocketBase(const TSocketBase& other) = delete;
125 TSocketBase& operator=(TSocketBase& other) const = delete;
126
127 ~TSocketBase() {
128 Close();
129 }
130
131public:
143 auto ReadSome(void* buf, size_t size) {
144 struct TAwaitableRead: public TAwaitable<TAwaitableRead> {
145 void run() {
146 this->ret = TSockOps::read(this->fd, this->b, this->s);
147 }
148
149 void await_suspend(std::coroutine_handle<> h) {
150 this->handle = h;
151 this->poller->AddRead(this->fd, h);
152 }
153 };
154 return TAwaitableRead{Poller_,Fd_,buf,size};
155 }
156
167 auto ReadSomeYield(void* buf, size_t size) {
168 struct TAwaitableRead: public TAwaitable<TAwaitableRead> {
169 bool await_ready() {
170 return (this->ready = false);
171 }
172
173 void run() {
174 this->ret = TSockOps::read(this->fd, this->b, this->s);
175 }
176
177 void await_suspend(std::coroutine_handle<> h) {
178 this->handle = h;
179 this->poller->AddRead(this->fd, h);
180 }
181 };
182 return TAwaitableRead{Poller_,Fd_,buf,size};
183 }
184
195 auto WriteSome(const void* buf, size_t size) {
196 struct TAwaitableWrite: public TAwaitable<TAwaitableWrite> {
197 void run() {
198 this->ret = TSockOps::write(this->fd, this->b, this->s);
199 }
200
201 void await_suspend(std::coroutine_handle<> h) {
202 this->handle = h;
203 this->poller->AddWrite(this->fd, h);
204 }
205 };
206 return TAwaitableWrite{Poller_,Fd_,const_cast<void*>(buf),size};
207 }
208
218 auto WriteSomeYield(const void* buf, size_t size) {
219 struct TAwaitableWrite: public TAwaitable<TAwaitableWrite> {
220 bool await_ready() {
221 return (this->ready = false);
222 }
223
224 void run() {
225 this->ret = TSockOps::write(this->fd, this->b, this->s);
226 }
227
228 void await_suspend(std::coroutine_handle<> h) {
229 this->handle = h;
230 this->poller->AddWrite(this->fd, h);
231 }
232 };
233 return TAwaitableWrite{Poller_,Fd_,const_cast<void*>(buf),size};
234 }
235
252 auto Monitor() {
253 if (!Poller_->SupportsRemoteHup()) {
254 throw std::system_error(std::make_error_code(std::errc::operation_not_supported),
255 "remote hang-up is not reported by this poller");
256 }
257 struct TAwaitableClose: public TAwaitable<TAwaitableClose> {
258 bool await_ready() {
259 return (this->ready = false);
260 }
261
262 void run() {
263 this->ret = true;
264 }
265
266 void await_suspend(std::coroutine_handle<> h) {
267 this->handle = h;
268 this->poller->AddRemoteHup(this->fd, h);
269 }
270 };
271 return TAwaitableClose{Poller_,Fd_};
272 }
273
280 void Close()
281 {
282 if (Fd_ >= 0) {
283 Poller_->Close(Fd_, TSockOps::CloseType);
284 Fd_ = -1;
285 }
286 }
287
288protected:
289 template<typename T>
290 struct TAwaitable {
291 ~TAwaitable() {
292 if (handle) {
293 // The coroutine is destroyed while waiting.
294 poller->RemoveEvent(fd, handle);
295 }
296 }
297
298 bool await_ready() {
299 SafeRun();
300 return (ready = (ret >= 0));
301 }
302
303 int await_resume() {
304 handle = {};
305 if (!ready) {
306 SafeRun();
307 }
308 return ret;
309 }
310
311 void SafeRun() {
312 ((T*)this)->run();
313#ifdef _WIN32
314 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) {
315 throw std::system_error(WSAGetLastError(), std::generic_category());
316 }
317#else
318 if (ret < 0 && !(errno==EINTR||errno==EAGAIN||errno==EINPROGRESS)) {
319 throw std::system_error(errno, std::generic_category());
320 }
321#endif
322 }
323
324 TPollerBase* poller = nullptr;
325 int fd = -1;
326 void* b = nullptr; size_t s = 0;
327 int ret = -1;
328 bool ready = false;
329 THandle handle = {};
330 };
331};
332
333class TFileOps {
334public:
335 static constexpr int CloseType = TEvent::CLOSE_FILE;
336
337 static auto read(int fd, void* buf, size_t count) {
338 return ::read(fd, buf, count);
339 }
340
341 static auto write(int fd, const void* buf, size_t count) {
342 return ::write(fd, buf, count);
343 }
344
345 static auto close(int fd) {
346 return ::close(fd);
347 }
348};
349
357class TFileHandle: public TSocketBase<TFileOps> {
358public:
365 TFileHandle(int fd, TPollerBase& poller)
366 : TSocketBase(fd, poller)
367 { }
368
369 TFileHandle(TFileHandle&& other);
370 TFileHandle& operator=(TFileHandle&& other);
371
372 TFileHandle() = default;
373};
374
375class TSockOps {
376public:
377 static constexpr int CloseType = TEvent::CLOSE_SOCKET;
378
379 static auto read(int fd, void* buf, size_t count) {
380 return ::recv(fd, static_cast<char*>(buf), count, 0);
381 }
382
383 static auto write(int fd, const void* buf, size_t count) {
384 return ::send(fd, static_cast<const char*>(buf), count, 0);
385 }
386
387 static auto close(int fd) {
388 if (fd >= 0) {
389#ifdef _WIN32
390 ::closesocket(fd);
391#else
392 ::close(fd);
393#endif
394 }
395 }
396};
397
406class TSocket: public TSocketBase<TSockOps> {
407public:
408 using TPoller = TPollerBase;
409
410 TSocket() = default;
411
419 TSocket(TPollerBase& poller, int domain, int type = SOCK_STREAM);
427 TSocket(const TAddress& addr, int fd, TPollerBase& poller);
428
429 TSocket(TSocket&& other);
430 TSocket& operator=(TSocket&& other);
431
446 auto Connect(const TAddress& addr, TTime deadline = TTime::max()) {
447 if (RemoteAddr_.has_value()) {
448 throw std::runtime_error("Already connected");
449 }
450 RemoteAddr_ = addr;
451 struct TAwaitable {
452 ~TAwaitable() {
453 if (handle) {
454 poller->RemoveEvent(fd, handle);
455 if (deadline != TTime::max()) {
456 poller->RemoveTimer(timerId, deadline);
457 }
458 }
459 }
460
461 bool await_ready() {
462 int ret = connect(fd, addr.first, addr.second);
463#ifdef _WIN32
464 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
465 throw std::system_error(WSAGetLastError(), std::generic_category(), "connect");
466 }
467#else
468 if (ret < 0 && !(errno == EINTR||errno==EAGAIN||errno==EINPROGRESS)) {
469 throw std::system_error(errno, std::generic_category(), "connect");
470 }
471#endif
472 return (ready = (ret >= 0));
473 }
474
475 void await_suspend(std::coroutine_handle<> h) {
476 handle = h;
477 poller->AddWrite(fd, h);
478 if (deadline != TTime::max()) {
479 timerId = poller->AddTimer(deadline, h);
480 }
481 }
482
483 void await_resume() {
484 handle = {};
485 if (ready) {
486 return;
487 }
488 if (deadline != TTime::max() && poller->RemoveTimer(timerId, deadline)) {
489 poller->RemoveEvent(fd);
490 throw std::system_error(std::make_error_code(std::errc::timed_out));
491 }
492 // Writability only means the attempt has finished, SO_ERROR tells how.
493 int err = 0;
494 socklen_t len = sizeof(err);
495 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&err), &len) < 0) {
496#ifdef _WIN32
497 err = WSAGetLastError();
498#else
499 err = errno;
500#endif
501 }
502 if (err != 0) {
503 throw std::system_error(err, std::generic_category(), "connect");
504 }
505 }
506
507 TPollerBase* poller;
508 int fd;
509 std::pair<const sockaddr*, int> addr;
510 TTime deadline;
511 unsigned timerId = 0;
512 bool ready = false;
513 THandle handle = {};
514 };
515 return TAwaitable{Poller_, Fd_, RemoteAddr_->RawAddr(), deadline};
516 }
517
532 auto Accept() {
533 struct TAwaitable {
534 ~TAwaitable() {
535 if (handle) {
536 poller->RemoveEvent(fd, handle);
537 }
538 }
539 bool await_ready() const {
540 if (fd < 0) {
541 throw std::system_error(EBADF, std::generic_category(), "accept");
542 }
543 return false;
544 }
545 void await_suspend(std::coroutine_handle<> h) {
546 handle = h;
547 poller->AddRead(fd, h);
548 }
549 std::expected<TSocket, int> await_resume() {
550 handle = {};
551 char clientaddr[sizeof(sockaddr_in6)];
552 socklen_t len = static_cast<socklen_t>(sizeof(sockaddr_in6));
553
554#ifdef SOCK_CLOEXEC
555 int clientfd = accept4(fd, reinterpret_cast<sockaddr*>(&clientaddr[0]), &len, SOCK_CLOEXEC);
556#else
557 int clientfd = accept(fd, reinterpret_cast<sockaddr*>(&clientaddr[0]), &len);
558#endif
559 if (clientfd < 0) {
560#ifdef _WIN32
561 // A closed socket is caught in await_ready.
562 int err = WSAGetLastError();
563#else
564 int err = errno;
565 if (err == EBADF) {
566 throw std::system_error(err, std::generic_category(), "accept");
567 }
568#endif
569 return std::unexpected(err);
570 }
571 SetCloseOnExec(clientfd);
572
573 return TSocket{TAddress{reinterpret_cast<sockaddr*>(&clientaddr[0]), len}, clientfd, *poller};
574 }
575
576 TPollerBase* poller;
577 int fd;
578 THandle handle = {};
579 };
580
581 return TAwaitable{Poller_, Fd_};
582 }
583
585 void Bind(const TAddress& addr);
587 void Listen(int backlog = 128);
593 const std::optional<TAddress>& RemoteAddr() const;
599 const std::optional<TAddress>& LocalAddr() const;
601 int Fd() const;
608 bool SetNoDelay();
609
610
611 void Reassign(TPollerBase* poller) {
612 Poller_ = poller;
613 }
614
615protected:
616 std::optional<TAddress> LocalAddr_;
617 std::optional<TAddress> RemoteAddr_;
618};
619
620namespace NDetail {
621
623inline int CheckDrivenResult(int ret) {
624 if (ret < 0) {
625 int err = -ret;
626#ifdef _WIN32
627 if (err == WSAEWOULDBLOCK || err == WSAEINTR || err == WSAEINPROGRESS) {
628#else
629 if (err == EINTR || err == EAGAIN || err == EINPROGRESS) {
630#endif
631 return ret; // retry hint
632 }
633 throw std::system_error(err, std::generic_category());
634 }
635 return ret;
636}
637
638} // namespace NDetail
639
663template<typename T>
664class TPollerDrivenSocket: public TSocket
665{
666public:
667 using TPoller = T;
668
678 TPollerDrivenSocket(T& poller, int domain, int type = SOCK_STREAM)
679 : TSocket(poller, domain, type)
680 , Poller_(&poller)
681 {
682 Poller_->Register(Fd_);
683 }
684
691 TPollerDrivenSocket(const TAddress& addr, int fd, T& poller)
692 : TSocket(addr, fd, poller)
693 , Poller_(&poller)
694 {
695 Poller_->Register(Fd_);
696 }
697
698 TPollerDrivenSocket(int fd, T& poller)
699 : TSocket({}, fd, poller)
700 , Poller_(&poller)
701 {
702 Poller_->Register(Fd_);
703 }
704
705 TPollerDrivenSocket() = default;
706
719 auto Accept() {
720 struct TAwaitable {
721 ~TAwaitable() {
722 if (op.Handle) {
723 // The coroutine is destroyed while waiting. Close a connection accepted meanwhile.
724 TSockOps::close(poller->Cancel(op));
725 }
726 }
727
728 bool await_ready() const { return false; }
729 void await_suspend(std::coroutine_handle<> h) {
730 op.Handle = h;
731 poller->Accept(fd, reinterpret_cast<sockaddr*>(&addr[0]), &len, op);
732 }
733
734 std::expected<TPollerDrivenSocket<T>, int> await_resume() {
735 int clientfd = op.Result;
736 if (clientfd == -EBADF) {
737 throw std::system_error(EBADF, std::generic_category(), "accept");
738 }
739 if (clientfd < 0) {
740 return std::unexpected(-clientfd);
741 }
742
743 return TPollerDrivenSocket<T>{TAddress{reinterpret_cast<sockaddr*>(&addr[0]), len}, clientfd, *poller};
744 }
745
746 T* poller;
747 int fd;
748
749 char addr[2*(sizeof(sockaddr_in6)+16)] = {0}; // use additional memory for windows
750 socklen_t len = static_cast<socklen_t>(sizeof(addr));
751 typename T::TOperation op = {};
752 };
753
754 return TAwaitable{Poller_, Fd_};
755 }
756
771 TFuture<void> Connect(const TAddress& addr, TTime deadline = TTime::max()) {
772 if (RemoteAddr_.has_value()) {
773 throw std::runtime_error("Already connected");
774 }
775 RemoteAddr_ = addr;
776 struct TAwaitable {
777 ~TAwaitable() {
778 if (op.Handle) {
779 poller->Cancel(op);
780 if (deadline != TTime::max()) {
781 poller->RemoveTimer(timerId, deadline);
782 }
783 }
784 }
785
786 bool await_ready() const { return false; }
787
788 void await_suspend(std::coroutine_handle<> h) {
789 op.Handle = h;
790 poller->Connect(fd, addr.first, addr.second, op);
791 if (deadline != TTime::max()) {
792 timerId = poller->AddTimer(deadline, h);
793 }
794 }
795
796 int await_resume() {
797 if (deadline != TTime::max() && poller->RemoveTimer(timerId, deadline)) {
798 // The deadline fired first: the connect is still pending, or its completion is not delivered yet.
799 poller->Cancel(op);
800 throw std::system_error(std::make_error_code(std::errc::timed_out));
801 }
802 return op.Result;
803 }
804
805 T* poller;
806 int fd;
807 std::pair<const sockaddr*, int> addr;
808 TTime deadline;
809 unsigned timerId = 0;
810 typename T::TOperation op = {};
811 };
812 int ret = co_await TAwaitable{Poller_, Fd_, RemoteAddr()->RawAddr(), deadline};
813 if (ret < 0) {
814 throw std::system_error(-ret, std::generic_category(), "connect");
815 }
816 }
817
828 auto ReadSome(void* buf, size_t size) {
829 struct TAwaitable {
830 ~TAwaitable() {
831 if (op.Handle) {
832 poller->Cancel(op);
833 }
834 }
835
836 bool await_ready() const { return false; }
837 void await_suspend(std::coroutine_handle<> h) {
838 op.Handle = h;
839 poller->Recv(fd, buf, size, op);
840 }
841
842 auto await_resume() {
843 return NDetail::CheckDrivenResult(op.Result);
844 }
845
846 T* poller;
847 int fd;
848
849 void* buf;
850 size_t size;
851 typename T::TOperation op = {};
852 };
853
854 return TAwaitable{Poller_, Fd_, buf, size};
855 }
856
867 auto WriteSome(const void* buf, size_t size) {
868 struct TAwaitable {
869 ~TAwaitable() {
870 if (op.Handle) {
871 poller->Cancel(op);
872 }
873 }
874
875 bool await_ready() const { return false; }
876 void await_suspend(std::coroutine_handle<> h) {
877 op.Handle = h;
878 poller->Send(fd, buf, size, op);
879 }
880
881 auto await_resume() {
882 return NDetail::CheckDrivenResult(op.Result);
883 }
884
885 T* poller;
886 int fd;
887
888 const void* buf;
889 size_t size;
890 typename T::TOperation op = {};
891 };
892
893 return TAwaitable{Poller_, Fd_, buf, size};
894 }
895
897 auto WriteSomeYield(const void* buf, size_t size) {
898 return WriteSome(buf, size);
899 }
900
902 auto ReadSomeYield(void* buf, size_t size) {
903 return ReadSome(buf, size);
904 }
905
906 // TODO: implement for TFileHandle
907 void Reassign(T* poller) {
908 Poller_ = poller;
909 }
910
911private:
912 T* Poller_;
913};
914
935template<typename T>
937{
938public:
939 using TPoller = T;
940
950 TPollerDrivenFileHandle(int fd, T& poller)
951 : TFileHandle(fd, poller)
952 , Poller_(&poller)
953 { }
954
965 auto ReadSome(void* buf, size_t size) {
966 struct TAwaitable {
967 ~TAwaitable() {
968 if (op.Handle) {
969 poller->Cancel(op);
970 }
971 }
972
973 bool await_ready() const { return false; }
974 void await_suspend(std::coroutine_handle<> h) {
975 op.Handle = h;
976 poller->Read(fd, buf, size, op);
977 }
978
979 auto await_resume() {
980 return NDetail::CheckDrivenResult(op.Result);
981 }
982
983 T* poller;
984 int fd;
985
986 void* buf;
987 size_t size;
988 typename T::TOperation op = {};
989 };
990
991 return TAwaitable{Poller_, Fd_, buf, size};
992 }
993
1004 auto WriteSome(const void* buf, size_t size) {
1005 struct TAwaitable {
1006 ~TAwaitable() {
1007 if (op.Handle) {
1008 poller->Cancel(op);
1009 }
1010 }
1011
1012 bool await_ready() const { return false; }
1013 void await_suspend(std::coroutine_handle<> h) {
1014 op.Handle = h;
1015 poller->Write(fd, buf, size, op);
1016 }
1017
1018 auto await_resume() {
1019 return NDetail::CheckDrivenResult(op.Result);
1020 }
1021
1022 T* poller;
1023 int fd;
1024
1025 const void* buf;
1026 size_t size;
1027 typename T::TOperation op = {};
1028 };
1029
1030 return TAwaitable{Poller_, Fd_, buf, size};
1031 }
1032
1034 auto WriteSomeYield(const void* buf, size_t size) {
1035 return WriteSome(buf, size);
1036 }
1037
1039 auto ReadSomeYield(void* buf, size_t size) {
1040 return ReadSome(buf, size);
1041 }
1042
1043private:
1044 T* Poller_;
1045};
1046
1047} // namespace NNet
A class representing an IPv4 or IPv6 address (with port).
Definition address.hpp:38
Asynchronous file handle that owns its file descriptor.
Definition socket.hpp:357
TFileHandle(int fd, TPollerBase &poller)
Constructs a TFileHandle from an existing file descriptor.
Definition socket.hpp:365
Definition socket.hpp:333
static constexpr int CloseType
Passed to TPollerBase::Close().
Definition socket.hpp:335
Backend-independent base for I/O pollers.
Definition poller.hpp:49
unsigned AddTimer(TTime deadline, THandle h)
Schedules a timer.
Definition poller.hpp:83
void AddWrite(int fd, THandle h)
Registers a write interest on a file descriptor.
Definition poller.hpp:128
bool RemoveTimer(unsigned timerId, TTime deadline)
Removes or cancels a timer.
Definition poller.hpp:98
void RemoveEvent(int fd)
Removes registered events for a specific file descriptor.
Definition poller.hpp:174
void AddRead(int fd, THandle h)
Registers a read interest on a file descriptor.
Definition poller.hpp:115
auto WriteSomeYield(const void *buf, size_t size)
The WriteSomeYield and ReadSomeYield variants behave similarly to WriteSome/ReadSome.
Definition socket.hpp:1034
auto ReadSomeYield(void *buf, size_t size)
The WriteSomeYield and ReadSomeYield variants behave similarly to WriteSome/ReadSome.
Definition socket.hpp:1039
auto WriteSome(const void *buf, size_t size)
Asynchronously writes data from the provided buffer to the file.
Definition socket.hpp:1004
TPollerDrivenFileHandle(int fd, T &poller)
Constructs a TPollerDrivenFileHandle from an existing file descriptor.
Definition socket.hpp:950
auto ReadSome(void *buf, size_t size)
Asynchronously reads data from the file into the provided buffer.
Definition socket.hpp:965
Socket type driven by the poller's implementation.
Definition socket.hpp:665
TFuture< void > Connect(const TAddress &addr, TTime deadline=TTime::max())
Asynchronously connects to the specified address with an optional deadline.
Definition socket.hpp:771
auto ReadSomeYield(void *buf, size_t size)
The WriteSomeYield and ReadSomeYield variants behave similarly to WriteSome/ReadSome.
Definition socket.hpp:902
auto ReadSome(void *buf, size_t size)
Asynchronously reads data from the socket.
Definition socket.hpp:828
auto WriteSome(const void *buf, size_t size)
Asynchronously writes data to the socket.
Definition socket.hpp:867
auto Accept()
Asynchronously accepts an incoming connection.
Definition socket.hpp:719
TPollerDrivenSocket(T &poller, int domain, int type=SOCK_STREAM)
Constructs a TPollerDrivenSocket from a poller, domain, and socket type.
Definition socket.hpp:678
auto WriteSomeYield(const void *buf, size_t size)
The WriteSomeYield and ReadSomeYield variants behave similarly to WriteSome/ReadSome.
Definition socket.hpp:897
TPollerDrivenSocket(const TAddress &addr, int fd, T &poller)
Constructs a TPollerDrivenSocket from an existing file descriptor.
Definition socket.hpp:691
Definition socket.hpp:375
static constexpr int CloseType
Passed to TPollerBase::Close().
Definition socket.hpp:377
TPollerBase * Poller()
Returns the poller associated with this socket.
Definition socket.hpp:49
Template base class implementing asynchronous socket I/O operations.
Definition socket.hpp:103
TSocketBase(TPollerBase &poller, int domain, int type)
Constructs a TSocketBase with a new socket descriptor.
Definition socket.hpp:112
auto Monitor()
Monitors the socket for remote hang-up (closure).
Definition socket.hpp:252
auto WriteSomeYield(const void *buf, size_t size)
Forces a write operation on the next event loop iteration.
Definition socket.hpp:218
auto WriteSome(const void *buf, size_t size)
Asynchronously writes data from the provided buffer to the socket.
Definition socket.hpp:195
auto ReadSome(void *buf, size_t size)
Asynchronously reads data from the socket into the provided buffer.
Definition socket.hpp:143
void Close()
Closes the socket.
Definition socket.hpp:280
TSocketBase(int fd, TPollerBase &poller)
Constructs a TSocketBase from an existing socket descriptor.
Definition socket.hpp:120
auto ReadSomeYield(void *buf, size_t size)
Forces a read operation on the next event loop iteration.
Definition socket.hpp:167
High-level asynchronous socket for network communication.
Definition socket.hpp:406
bool SetNoDelay()
Disables Nagle's algorithm (TCP_NODELAY) on a TCP socket.
Definition socket.cpp:149
const std::optional< TAddress > & RemoteAddr() const
Returns the remote address of the connected peer.
Definition socket.cpp:141
auto Connect(const TAddress &addr, TTime deadline=TTime::max())
Asynchronously connects to the specified address.
Definition socket.hpp:446
int Fd() const
Returns the underlying socket descriptor.
Definition socket.cpp:145
void Bind(const TAddress &addr)
Binds the socket to the specified local address.
Definition socket.cpp:115
auto Accept()
Asynchronously accepts an incoming connection.
Definition socket.hpp:532
const std::optional< TAddress > & LocalAddr() const
Returns the local address to which the socket is bound.
Definition socket.cpp:137
void Listen(int backlog=128)
Puts the socket in a listening state with an optional backlog (default is 128).
Definition socket.cpp:131
Implementation of a promise/future system for coroutines.
Definition corochain.hpp:74
Definition socket.hpp:290