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));
Types
Name |
Description |
Represent the awaitable returned by |
|
RAII wrapper for I/O object implementation lifetime. |
|
Define backend hooks for local datagram socket operations. |
|
Service interface for I/O object lifecycle management. |
|
Represent the awaitable returned by |
|
Represent the awaitable returned by |
|
Represent the awaitable returned by |
|
Represent the awaitable returned by |
|
Represent the awaitable returned by |
Member Functions
Name |
Description |
|
Constructors |
|
Destructor. |
|
Move assignment operator. |
Assign an existing file descriptor to this socket. |
|
Query the number of bytes available for reading. |
|
Bind the socket to a local endpoint. |
|
Cancel any pending asynchronous operations. |
|
Close the socket. |
|
Initiate an asynchronous connect to set the default peer. |
|
Return the execution context. |
|
Get a socket option. |
|
Check if the socket is open. |
|
Get the local endpoint of the socket. |
|
Get the native socket handle. |
|
Open the socket. |
|
Receive a datagram from the connected peer. |
|
Receive a datagram and capture the sender's endpoint. |
|
Release ownership of the native socket handle. |
|
Get the remote endpoint of the socket. |
|
Send a datagram to the connected peer. |
|
Send a datagram to the specified destination. |
|
Set a socket option. |
|
|
|
Wait for the socket to become ready in a given direction. |
Protected Member Functions
Name |
Description |
|
Constructors |
|
Move assign from another I/O object. |
Protected Static Member Functions
Name |
Description |
Create a handle bound to a service found in the context. |
Non-Member Functions
Name |
Description |
Synchronously connect two AF_UNIX datagram sockets as a connected pair. |
Derived Classes
Name |
Description |
An asynchronous Unix datagram socket with devirtualized I/O. |
Created with MrDocs