boost::corosio::tcp_socket

An asynchronous TCP socket for coroutine I/O.

Synopsis

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)));

Base Classes

Name Description

io_stream

Platform stream with read/write operations.

Types

Name

Description

connect_awaitable

handle

RAII wrapper for I/O object implementation lifetime.

implementation

Forward declaration for platform‐specific implementation.

io_object_impl

Base interface for platform I/O implementations.

io_service

Service interface for I/O object lifecycle management.

io_stream_impl

Platform‐specific stream implementation interface.

linger_options

Options for SO_LINGER socket option.

socket_impl

Enums

Name

Description

shutdown_type

Different ways a socket may be shutdown.

Member Functions

Name

Description

tcp_socket [constructor] [deleted]

Constructors

~tcp_socket [destructor] [virtual]

Destructor.

operator= [deleted]

Move assignment operator.

cancel

Cancel any pending asynchronous operations.

close

Close the socket.

connect

Initiate an asynchronous connect operation.

context

Return the execution context.

is_open

Check if the socket is open.

keep_alive

Get the current SO_KEEPALIVE setting.

linger

Get the current SO_LINGER setting.

local_endpoint

Get the local endpoint of the socket.

native_handle

Get the native socket handle.

no_delay

Get the current TCP_NODELAY setting.

open

Open the socket.

read_some

Asynchronously read data from the stream.

receive_buffer_size

Get the receive buffer size (SO_RCVBUF).

remote_endpoint

Get the remote endpoint of the socket.

send_buffer_size

Get the send buffer size (SO_SNDBUF).

set_keep_alive

Enable or disable SO_KEEPALIVE.

set_linger

Set the SO_LINGER option.

set_no_delay

Enable or disable TCP_NODELAY (disable Nagle's algorithm).

set_receive_buffer_size

Set the receive buffer size (SO_RCVBUF).

set_send_buffer_size

Set the send buffer size (SO_SNDBUF).

shutdown

Disable sends or receives on the socket.

write_some

Asynchronously write data to the stream.

Protected Types

Name

Description

read_some_awaitable

Awaitable for async read operations.

write_some_awaitable

Awaitable for async write operations.

Protected Data Members

Name

ctx_

impl_

Friends

Name Description

boost::corosio::tcp_acceptor

An asynchronous TCP acceptor for coroutine I/O.

Created with MrDocs