Skip to content

UDP (pace::udp)

简体中文 · Networking

Overview

pace::udp provides nonblocking UDP sockets bound to an io_context, owning send and receive helpers, and one-call request/reply helpers.

Header Package config
<pace/net/udp.hpp> (included by <pace/net.hpp>) always included

Endpoints are numeric IPv4 or IPv6 addresses; resolve host names with the DNS module. UDP keeps message boundaries but does not guarantee delivery, order or uniqueness. With modules = true the names are exported by import pace.net;. The checked-in example is examples/tutorial_udp.cpp (xmake run tutorial_udp).

Adoption, packet information and batches

udp::socket::adopt(io, fd) accepts an idle native IPv4 or IPv6 datagram socket and preserves its binding and dual-stack mode. Ownership transfers on success.

Pass udp::bind_options{.dont_fragment = true} to enable DF before binding, or call socket.set_dont_fragment(true) on an existing socket. Pass false to disable it. Omitting the bind option preserves the kernel setting. Explicit settings follow automatic IPv4/IPv6 selection. On a dual-stack socket both IPv4 and IPv6 policies must be available. Use separate sockets if the OS rejects the IPv4 option on an IPv6 descriptor. The native stack reports unsupported options and datagrams too large for the path.

Wildcard listeners can set bind_options{.packet_info = true} and use receive_from(buffer, info). Each successful receive replaces udp::packet_info with the local destination address and the interface index and ECN available from the kernel. An unavailable interface index is zero. FreeBSD IPv4 uses IP_RECVDSTADDR to receive the address and IP_SENDSRCADDR to select it for the reply. Use send_to(bytes, peer, info) to select a reply source and ECN. On Linux, .transparent = true also delivers the original address and port in original_destination.

send_batch(span<outgoing_datagram>) and receive_batch(span<incoming_datagram>) process up to 32 slots and return the completed prefix length. Use one destination family per send batch. Keep slots and buffers alive and advance the span by the returned count. Linux uses sendmmsg/recvmmsg. To request GSO/USO set packet_info::segment_size. With .coalesce = true (GRO/URO), use the receive overload taking packet_info& and split the payload by the reported segment size. The last segment may be shorter.

Synopsis

#include <pace/net.hpp>
#include <pace/runtime.hpp>

#include <iostream>

pace::task<void> hello()
{
    auto receiver = pace::udp::bind({"127.0.0.1", 0});
    auto sender = pace::udp::open();
    co_await sender.send("Hello, UDP!", receiver.local_endpoint());

    auto packet = co_await receiver.receive();
    std::cout << packet.data << '\n';
    co_await receiver.send("reply", packet.sender);
}

int main()
{
    pace::run(hello);
}

Types

endpoint

struct endpoint
{
    std::string address = "127.0.0.1";
    std::uint16_t port = 0;
    address_family family = address_family::unspecified;
    std::uint32_t scope_id = 0;
};

The same fields and rules as tcp::endpoint: a numeric, unbracketed IPv4 or IPv6 address, the port, an optional family that must match the address, and the IPv6 interface index in scope_id (nonzero only for IPv6). Host names, brackets, NUL bytes, malformed addresses and %zone suffixes throw std::invalid_argument; write {"fe80::1", port, pace::address_family::ipv6, 3} instead of fe80::1%3. ::1 is IPv6 loopback and :: the IPv6 wildcard. Sender and local endpoints report the concrete family and keep the scope separately from the address. PACE does not resolve interface names.

bind_options

struct bind_options
{
    bool ipv6_only = true;
    bool packet_info = false;
    bool transparent = false;
    bool coalesce = false;
    std::optional<bool> dont_fragment{};
};

On an IPv6 endpoint, sets IPV6_V6ONLY before binding, whatever the OS default. false makes one socket bound to :: receive both families, for example pace::udp::socket receiver(io, {"::", 0}, {.ipv6_only = false});. It must stay true on an IPv4 endpoint. On such a dual-stack socket, IPv4 senders appear as IPv4-mapped IPv6 endpoints (::ffff:127.0.0.1) that can be passed straight back to send_to, and send_to maps a plain IPv4 destination to that form. An IPv6-only socket rejects IPv4 destinations.

datagram

struct datagram
{
    std::size_t size = 0;
    endpoint sender;
    bool truncated = false;
};

The result of receive_from. size is the number of bytes copied into the buffer; 0 is a valid empty datagram, not EOF. sender is the source address and port. truncated is true when the datagram was larger than the buffer: its remainder has been discarded, and the next receive starts with the next datagram.

packet

struct packet
{
    std::string data;
    endpoint sender;
    bool truncated = false;
};

The result of receive and exchange, owning its bytes. An empty data is a valid empty datagram. truncated has the same meaning as in datagram.

socket

class socket
{
public:
    explicit socket(io_context& context);
    socket(io_context& context, address_family family);
    socket(io_context& context, endpoint local, bind_options options = {});
    ~socket();
    socket(socket&& other) noexcept;
    socket& operator=(socket&& other) noexcept;
    socket(const socket&) = delete;
    socket& operator=(const socket&) = delete;

    void bind(endpoint local, bind_options options = {});
    endpoint local_endpoint() const;
    void set_broadcast(bool enabled = true);
    void set_dont_fragment(bool enabled = true);
    task<std::size_t> send_to(std::span<const std::byte> bytes, endpoint destination);
    task<datagram> receive_from(std::span<std::byte> buffer);
    task<std::size_t> send(std::string bytes, endpoint destination);
    task<packet> receive(std::size_t capacity = 65536);
    void close() noexcept;

    bool is_open() const noexcept;
    native_socket_type native_handle() const noexcept;
    io_context& context() const noexcept;
};

A move-only UDP socket owned by one io_context.

Construction and family

Constructor Effect
socket(io) Unbound socket. Starts with an IPv4 handle; the first bind or send_to with an IPv6 address replaces it with an IPv6 one
socket(io, family) Unbound socket of an explicit family that keeps its handle and rejects addresses of the other family
socket(io, local, options) Creates the socket and binds it to local

The family of socket(io) is fixed by the first bind, send_to, receive_from or set_broadcast (the last two fix IPv4); a later address of the other family throws std::system_error with address_family_not_supported. Use the explicit-family constructor when you set native options on native_handle() before the first operation. Construction can run before the context runs; OS failures throw std::system_error.

bind

void bind(endpoint local, bind_options options = {});

Binds an idle socket to a local address. Port 0 picks a free port; read it back with local_endpoint(). Binding while a send or receive is outstanding throws std::logic_error. Synchronous; may run before the context runs.

Sending

task<std::size_t> send_to(std::span<const std::byte> bytes, endpoint destination);
task<std::size_t> send(std::string bytes, endpoint destination);

Sends one datagram and returns its size. send_to borrows the bytes until it completes; pass text as send_to(pace::as_bytes(text), destination). send owns its string. An empty payload sends an empty datagram. A payload larger than 65,507 bytes, or one the OS accepts only in part, throws std::system_error with std::errc::message_size; the path MTU may make the useful limit much smaller. Completion means the local socket accepted the datagram, not that it was delivered.

Receiving

task<datagram> receive_from(std::span<std::byte> buffer);
task<packet> receive(std::size_t capacity = 65536);

Waits for one datagram. receive_from copies it into the borrowed buffer, which must not be empty (std::invalid_argument). receive allocates capacity bytes, returns a packet holding only the received bytes, and throws std::invalid_argument for capacity == 0 and std::length_error for capacity > 65536, before any I/O. A small capacity can truncate a datagram; check truncated. Use receive_from to reuse a buffer across receives; a character buffer goes through pace::as_writable_bytes:

std::array<char, 1500> buffer{};
for (;;)
{
    const auto received = co_await socket.receive_from(pace::as_writable_bytes(buffer));
    const std::string_view payload(buffer.data(), received.size);
    co_await socket.send_to(pace::as_bytes(payload), received.sender);
}

set_broadcast

void set_broadcast(bool enabled = true);

Sets or clears SO_BROADCAST. IPv4 only: on an IPv6 socket it throws std::system_error with operation_not_supported, since IPv6 has no broadcast. There is no multicast membership API.

close and accessors

close() cancels pending waits and closes the handle; it is idempotent and also run by the destructor. Suspended operations then finish with std::system_error, so keep the object alive until they have unwound. local_endpoint() returns the bound address and port and throws std::system_error on a closed socket. is_open() reports whether the socket owns a handle, native_handle() returns the borrowed native socket (do not close it or change its blocking mode) and context() the owning context.

Functions

open and bind

socket open(const client_options& options = {});
socket bind(io_context& io, endpoint local, bind_options options = {});
socket bind(endpoint local, bind_options options = {});

Both functions create a socket synchronously. Before running the context, use open({.context = &io}) for an unbound socket or bind(io, local) to bind an address. Omitting the context selects the one running at the call.

exchange and async_exchange

task<packet> async_exchange(endpoint peer, std::string request, client_options options = {});
packet exchange(endpoint peer, std::string request, client_options options = {});

Sends request as one datagram from a fresh socket of the peer's family, then returns the first reply that comes from the same IP address, port, family and IPv6 scope (compared after canonicalizing the address). Datagrams from other senders are dropped and do not extend any deadline. This filtering is not authentication, not correlation by request ID, and not retransmission: a lost request or reply is only noticed through a deadline. Services that reply from a different address or port, and broadcast or multicast, need a socket.

  • options.max_reply_size must be between 1 and 65536 bytes (std::invalid_argument for zero, std::length_error above), and peer.port must be nonzero (std::invalid_argument); both are checked before sending.
  • A matching truncated reply is returned with truncated = true, not retried. An empty reply is valid.
  • async_exchange inherits the caller’s deadline and cancellation by default. All filtered datagrams share the same budget.
  • exchange runs on a fresh context with a 30 s default total timeout. Use options.timeout or options.deadline to bound the operation. Expiry reports timed_out.

send

std::size_t send(endpoint peer, std::string message, client_options options = {});

Blocking: sends one datagram from a fresh socket on a fresh context and returns the byte count accepted by the local socket. It does not wait for or prove delivery. client_options behaves as for exchange. Inside a task, use co_await socket.send(message, peer) on an owned socket instead.

Errors

Condition Exception
Malformed endpoint, host name, %zone, family or scope mismatch std::invalid_argument
ipv6_only = false on an IPv4 endpoint std::invalid_argument
Address of the other family after the family is fixed std::system_error (address_family_not_supported)
Empty receive_from buffer, receive/exchange capacity 0, exchange port 0 std::invalid_argument
receive/exchange capacity above 65536 std::length_error
Payload above 65,507 bytes or sent only in part std::system_error (message_size)
set_broadcast on IPv6 std::system_error (operation_not_supported)
A second send or a second receive while one is outstanding; bind while busy std::logic_error
Asynchronous operation run outside its owning context std::logic_error
Operation on a closed socket std::system_error (bad_file_descriptor)
Context stopped, socket closed during the wait, or task canceled std::system_error (operation_canceled)
Deadline of the awaiting task, or exchange/send budget, expired std::system_error (timed_out)
Other OS failure std::system_error with the native code

As with TCP, compare error.code() with std::errc conditions rather than numeric errno values.

Threads and concurrency

  • Asynchronous operations must run on the owning context; bind, set_broadcast, local_endpoint and close may also run before the context runs or after it has stopped. To close a socket from another thread, post the call to its context.
  • One send_to/send and one receive_from/receive may be outstanding at the same time. A second operation in the same direction throws std::logic_error immediately.

Lifetime and ownership

  • The io_context must outlive every socket created on it.
  • Keep the socket alive and at the same address while any operation is suspended, and keep send_to and receive_from buffers (including the storage behind a pace::as_bytes or pace::as_writable_bytes view) valid until their operation completes. send, receive and the free functions own their data.
  • Move a socket only while it is idle. Move assignment closes the destination's previous socket first.

Limits and defaults

Item Default or bound
endpoint defaults "127.0.0.1", port 0, unspecified, scope 0
bind_options::ipv6_only true
Largest payload send_to accepts 65,507 bytes
receive capacity / exchange max_reply_size default 65536, allowed 1–65536
exchange / send timeout (client_options::timeout) 30 s
async_exchange timeout inherited by default

See also