boost::corosio::tcp_socket
An asynchronous TCP socket for coroutine I/O.
Synopsis
Declared in <boost/corosio/tcp_socket.hpp>
class tcp_socket
: public io_stream
Description
This class provides asynchronous TCP socket operations that return awaitable types. Each operation participates in the affine awaitable protocol, ensuring coroutines resume on the correct executor.
The socket must be opened before performing I/O operations. Operations support cancellation through std::stop_token via the affine protocol, or explicitly through the cancel() member function.
Thread Safety
Distinct objects: Safe. Shared objects: Unsafe. A socket must not have concurrent operations of the same type (e.g., two simultaneous reads). One read and one write may be in flight simultaneously.
Semantics
Wraps the platform TCP/IP stack. Operations dispatch to OS socket APIs via the io_context reactor (epoll, IOCP, kqueue). Satisfies capy::Stream.
Example
io_context ioc;
tcp_socket s(ioc);
s.open();
// Using structured bindings
auto [ec] = co_await s.connect(
endpoint(ipv4_address::loopback(), 8080));
if (ec)
co_return;
char buf[1024];
auto [read_ec, n] = co_await s.read_some(
capy::mutable_buffer(buf, sizeof(buf)));
Types
Name |
Description |
RAII wrapper for I/O object implementation lifetime. |
|
Forward declaration for platform‐specific implementation. |
|
Base interface for platform I/O implementations. |
|
Service interface for I/O object lifecycle management. |
|
Platform‐specific stream implementation interface. |
|
Options for SO_LINGER socket option. |
|
Member Functions
Name |
Description |
|
Constructors |
|
Destructor. |
|
Move assignment operator. |
Cancel any pending asynchronous operations. |
|
Close the socket. |
|
Initiate an asynchronous connect operation. |
|
Return the execution context. |
|
Check if the socket is open. |
|
Get the current SO_KEEPALIVE setting. |
|
Get the current SO_LINGER setting. |
|
Get the local endpoint of the socket. |
|
Get the native socket handle. |
|
Get the current TCP_NODELAY setting. |
|
Open the socket. |
|
Asynchronously read data from the stream. |
|
Get the receive buffer size (SO_RCVBUF). |
|
Get the remote endpoint of the socket. |
|
Get the send buffer size (SO_SNDBUF). |
|
Enable or disable SO_KEEPALIVE. |
|
Set the SO_LINGER option. |
|
Enable or disable TCP_NODELAY (disable Nagle's algorithm). |
|
Set the receive buffer size (SO_RCVBUF). |
|
Set the send buffer size (SO_SNDBUF). |
|
Disable sends or receives on the socket. |
|
Asynchronously write data to the stream. |
Protected Types
Name |
Description |
Awaitable for async read operations. |
|
Awaitable for async write operations. |
Friends
| Name | Description |
|---|---|
An asynchronous TCP acceptor for coroutine I/O. |
Created with MrDocs