boost::corosio::local_datagram_socket

An asynchronous Unix datagram socket for coroutine I/O.

Synopsis

class local_datagram_socket
    : public io_object

Description

This class provides asynchronous Unix domain datagram socket 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: each send_to() specifies a destination endpoint, and each recv_from() captures the source. The socket must be opened (and optionally bound) before I/O.

  • Connected: 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.

Not available on Windows. Windows does not support AF_UNIX datagram sockets (SOCK_DGRAM). Attempting to open this socket on Windows will fail.

Cancellation

All asynchronous operations support cancellation through std::stop_token via the affine protocol, or explicitly through cancel(). Cancelled operations complete with capy::cond::canceled. Datagram sends and receives are atomic — there is no partial progress on cancellation.

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 and one recv may be in flight simultaneously. Note that recv and recv_from share the same internal read slot, so they must not overlap; likewise send and send_to share the write slot.

Example

// Connectionless
local_datagram_socket sender(ioc);
sender.open();
sender.bind(local_endpoint("/tmp/sender.sock"));
auto [ec, n] = co_await sender.send_to(
    capy::const_buffer("hello", 5),
    local_endpoint("/tmp/receiver.sock"));

// Connected
local_datagram_socket sock(ioc);
co_await sock.connect(local_endpoint("/tmp/peer.sock"));
auto [ec2, n2] = co_await sock.send(
    capy::const_buffer("hi", 2));

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 local datagram 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.

Type Aliases

Name

Description

shutdown_type

The shutdown direction type used by shutdown().

Member Functions

Name

Description

local_datagram_socket [constructor] [deleted]

Constructors

~local_datagram_socket [destructor] [virtual]

Destructor.

operator= [deleted]

Move assignment operator.

assign

Assign an existing file descriptor to this socket.

available

Query the number of bytes available for reading.

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.

release

Release ownership of the native socket handle.

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.

shutdown

shutdown overloads

wait

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

Protected Member Functions

Name

Description

local_datagram_socket [constructor]

Constructors

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.

Non-Member Functions

Name

Description

connect_pair

Synchronously connect two AF_UNIX datagram sockets as a connected pair.

Derived Classes

Name

Description

native_local_datagram_socket

An asynchronous Unix datagram socket with devirtualized I/O.

Created with MrDocs