boost::corosio::udp_socket

An asynchronous UDP socket for coroutine I/O.

Synopsis

class udp_socket
    : public io_object

Description

This class provides asynchronous UDP datagram operations that return awaitable types. Each operation participates in the affine awaitable protocol, ensuring coroutines resume on the correct executor.

Supports two modes of operation:

*Connectionless mode**: each send_to specifies a destination endpoint, and each recv_from captures the source endpoint. The socket must be opened (and optionally bound) before I/O.

*Connected mode**: call connect() to set a default peer, then use send()/`recv()` without endpoint arguments. The kernel filters incoming datagrams to those from the connected peer.

Thread Safety

Distinct objects: Safe. Shared objects: Unsafe. A socket must not have concurrent operations of the same type (e.g., two simultaneous recv_from). One send_to and one recv_from may be in flight simultaneously.

Example

// Connectionless mode
io_context ioc;
udp_socket sock( ioc );
sock.open( udp::v4() );
sock.bind( endpoint( ipv4_address::any(), 9000 ) );

char buf[1024];
endpoint sender;
auto [ec, n] = co_await sock.recv_from(
    capy::mutable_buffer( buf, sizeof( buf ) ), sender );
if ( !ec )
    co_await sock.send_to(
        capy::const_buffer( buf, n ), sender );

// Connected mode
udp_socket csock( ioc );
auto [cec] = co_await csock.connect(
    endpoint( ipv4_address::loopback(), 9000 ) );
if ( !cec )
    co_await csock.send(
        capy::const_buffer( buf, n ) );

Base Classes

Name

Description

io_object

Base class for platform I/O objects.

Types

Name

Description

connect_awaitable

Represent the awaitable returned by connect.

handle

RAII wrapper for I/O object implementation lifetime.

implementation

Define backend hooks for UDP socket operations.

io_service

Service interface for I/O object lifecycle management.

recv_awaitable

Represent the awaitable returned by recv.

recv_from_awaitable

Represent the awaitable returned by recv_from.

send_awaitable

Represent the awaitable returned by send.

send_to_awaitable

Represent the awaitable returned by send_to.

wait_awaitable

Represent the awaitable returned by wait.

Member Functions

Name

Description

udp_socket [constructor] [deleted]

Constructors

~udp_socket [destructor] [virtual]

Destructor.

operator= [deleted]

Move assignment operator.

bind

Bind the socket to a local endpoint.

cancel

Cancel any pending asynchronous operations.

close

Close the socket.

connect

Initiate an asynchronous connect to set the default peer.

context

Return the execution context.

get_option

Get a socket option.

is_open

Check if the socket is open.

local_endpoint

Get the local endpoint of the socket.

native_handle

Get the native socket handle.

open

Open the socket.

recv

Receive a datagram from the connected peer.

recv_from

Receive a datagram and capture the sender's endpoint.

remote_endpoint

Get the remote endpoint of the socket.

send

Send a datagram to the connected peer.

send_to

Send a datagram to the specified destination.

set_option

Set a socket option.

wait

Wait for the socket to become ready in a given direction.

Protected Member Functions

Name

Description

udp_socket [constructor]

Construct from a pre‐built handle (for native_udp_socket).

operator= [deleted]

Move assign from another I/O object.

Protected Static Member Functions

Name

Description

create_handle

Create a handle bound to a service found in the context.

Protected Data Members

Name

Description

h_

The platform I/O handle owned by this object.

Derived Classes

Name

Description

native_udp_socket

An asynchronous UDP socket with devirtualized I/O operations.

Created with MrDocs