qb
2.0.0.0
C++17 Actor Framework
|
Class implementing UDP socket functionality for datagram-based communication. More...
#include <socket.h>
Public Member Functions | |
socket ()=default | |
Default constructor. | |
socket (socket const &)=delete | |
Deleted copy constructor. | |
socket (socket &&)=default | |
Default move constructor. | |
socket & | operator= (socket &&)=default |
Default move assignment operator. | |
socket (io::socket &&sock) noexcept | |
Constructor from a generic qb::io::socket (move semantics). | |
socket & | operator= (io::socket &&sock) noexcept |
Move assignment operator from a generic qb::io::socket. | |
bool | init (int af=AF_INET) noexcept |
Initialize (open) the UDP socket. | |
int | bind (qb::io::endpoint const &ep) noexcept |
Bind the socket to a specific local endpoint. | |
int | bind (qb::io::uri const &u) noexcept |
Bind the socket to an endpoint specified by a URI. | |
int | bind_v4 (uint16_t port, std::string const &host="0.0.0.0") noexcept |
Bind the socket to a specific IPv4 address and port. | |
int | bind_v6 (uint16_t port, std::string const &host="::") noexcept |
Bind the socket to a specific IPv6 address and port. | |
int | bind_un (std::string const &path) noexcept |
Bind the socket to a Unix domain socket path (for datagrams). | |
int | read (void *dest, std::size_t len, qb::io::endpoint &peer) const noexcept |
Read a datagram from the socket and get the sender's endpoint. | |
int | read_timeout (void *dest, std::size_t len, qb::io::endpoint &peer, const std::chrono::microseconds &timeout) const noexcept |
Read a datagram from the socket with a specified timeout. | |
int | try_read (void *dest, std::size_t len, qb::io::endpoint &peer) const noexcept |
Try to read a datagram from the socket (non-blocking attempt). | |
int | write (const void *data, std::size_t len, qb::io::endpoint const &to) const noexcept |
Write (send) a datagram to a specific remote endpoint. | |
int | set_buffer_size (std::size_t size) noexcept |
Set the socket's send and receive buffer sizes (SO_SNDBUF, SO_RCVBUF). | |
int | set_broadcast (bool enable) noexcept |
Enable or disable the SO_BROADCAST socket option. | |
int | join_multicast_group (const std::string &group, const std::string &iface="") noexcept |
Join an IPv4 or IPv6 multicast group. | |
int | leave_multicast_group (const std::string &group, const std::string &iface="") noexcept |
Leave an IPv4 or IPv6 multicast group. | |
int | set_multicast_ttl (int ttl) noexcept |
Set the multicast Time-To-Live (TTL) for outgoing IPv4 packets or hop limit for IPv6. | |
int | set_multicast_loopback (bool enable) noexcept |
Set whether multicast packets sent from this socket are looped back to the local host. | |
int | address_family () const noexcept |
Get the address family of this UDP socket (e.g., AF_INET, AF_INET6). | |
bool | is_bound () const noexcept |
Check if the UDP socket is currently bound to a local address and port. | |
int | disconnect () const noexcept |
Disconnect a previously connected UDP socket or clear default remote endpoint. |
Static Public Attributes | |
static constexpr const std::size_t | DefaultDatagramSize = 512 |
Default maximum size of a UDP datagram. | |
static constexpr const std::size_t | MaxDatagramSize = 65507 |
Maximum possible size of a UDP datagram. |
Class implementing UDP socket functionality for datagram-based communication.
This class provides UDP socket functionality, inheriting protectedly from qb::io::socket. It supports sending and receiving datagrams over UDP/IPv4, UDP/IPv6, and Unix Domain Sockets (datagram type). It also includes methods for managing multicast group memberships. Used as the underlying I/O primitive for qb::io::transport::udp.
|
default |
Default constructor.
Creates an uninitialized UDP socket. Call init() before use.
|
delete |
Deleted copy constructor.
Sockets are not copyable.
|
noexcept |
Constructor from a generic qb::io::socket (move semantics).
sock | A generic qb::io::socket instance, typically already opened with SOCK_DGRAM. The state of sock is moved into this udp::socket. |
Default move assignment operator.
|
noexcept |
Move assignment operator from a generic qb::io::socket.
sock | A generic qb::io::socket to move from. |
|
noexcept |
Initialize (open) the UDP socket.
af | Address family (e.g., AF_INET, AF_INET6, AF_UNIX). Defaults to AF_INET. |
This method calls the base qb::io::socket::open() with SOCK_DGRAM type.
|
noexcept |
Bind the socket to a specific local endpoint.
ep | The qb::io::endpoint to bind to. For UDP, this sets the local address and port from which packets will be sent and received. |
|
noexcept |
Bind the socket to an endpoint specified by a URI.
u | The qb::io::uri specifying the local address and port to bind to. Scheme should typically be "udp". |
|
noexcept |
Bind the socket to a specific IPv4 address and port.
port | The port number to bind to. |
host | The IPv4 host address string (e.g., "0.0.0.0" for all interfaces). Defaults to "0.0.0.0". |
|
noexcept |
Bind the socket to a specific IPv6 address and port.
port | The port number to bind to. |
host | The IPv6 host address string (e.g., "::" for all interfaces). Defaults to "::". |
|
noexcept |
Bind the socket to a Unix domain socket path (for datagrams).
path | The file system path for the Unix domain socket. |
|
noexcept |
Read a datagram from the socket and get the sender's endpoint.
dest | Pointer to the buffer where the received datagram will be stored. |
len | Maximum number of bytes to read (size of the dest buffer). |
peer | Output parameter: A qb::io::endpoint reference that will be populated with the source address of the received datagram. |
|
noexcept |
Read a datagram from the socket with a specified timeout.
dest | Pointer to the buffer for received data. |
len | Maximum number of bytes to read. |
peer | Output parameter for the sender's endpoint. |
timeout | Maximum time to wait for data. If the timeout expires before data arrives, an error (typically ETIMEDOUT or equivalent) is returned. |
Uses qb::io::socket::recv_n which internally uses select for timeout handling.
|
noexcept |
Try to read a datagram from the socket (non-blocking attempt).
dest | Pointer to the buffer for received data. |
len | Maximum number of bytes to read. |
peer | Output parameter for the sender's endpoint if data is successfully read. |
Sets the socket to non-blocking, attempts a read, then restores its original blocking state.
|
noexcept |
Write (send) a datagram to a specific remote endpoint.
data | Pointer to the data to be sent. |
len | Number of bytes to send from the data buffer. |
to | The destination qb::io::endpoint. |
|
noexcept |
Set the socket's send and receive buffer sizes (SO_SNDBUF, SO_RCVBUF).
size | The desired buffer size in bytes for both send and receive buffers. |
|
noexcept |
Enable or disable the SO_BROADCAST socket option.
enable | true to enable broadcasting, false to disable. |
Enabling this allows the socket to send packets to a broadcast address.
|
noexcept |
Join an IPv4 or IPv6 multicast group.
group | Multicast group address string (e.g., "224.0.0.1" or "ff02::1"). |
iface | Optional local interface address string or interface name to join the group on. If empty or null, the system chooses a default interface. |
Uses IP_ADD_MEMBERSHIP or IPV6_JOIN_GROUP socket options.
|
noexcept |
Leave an IPv4 or IPv6 multicast group.
group | Multicast group address string. |
iface | Optional local interface address string or interface name used when joining. |
Uses IP_DROP_MEMBERSHIP or IPV6_LEAVE_GROUP socket options.
|
noexcept |
Set the multicast Time-To-Live (TTL) for outgoing IPv4 packets or hop limit for IPv6.
ttl | The TTL (for IPv4, typically 1-255) or hop limit (for IPv6) value. A TTL of 0 restricts packets to the local host. A TTL of 1 restricts packets to the local subnet. |
Uses IP_MULTICAST_TTL or IPV6_MULTICAST_HOPS socket options.
|
noexcept |
Set whether multicast packets sent from this socket are looped back to the local host.
enable | true to enable multicast loopback (sender receives its own packets), false to disable (default is usually enabled). |
Uses IP_MULTICAST_LOOP or IPV6_MULTICAST_LOOP socket options.
|
noexcept |
Get the address family of this UDP socket (e.g., AF_INET, AF_INET6).
Retrieves this from the underlying qb::io::socket's endpoint information if bound, or the family it was initialized with.
|
noexcept |
Check if the UDP socket is currently bound to a local address and port.
A UDP socket must be bound to receive unicast messages or to send packets from a specific local port.
|
noexcept |
Disconnect a previously connected UDP socket or clear default remote endpoint.
For UDP, connect can be used to set a default destination for send() and to filter incoming packets to only those from that destination. This disconnect call removes that association. It then calls qb::io::socket::close().
|
staticconstexpr |
Default maximum size of a UDP datagram.
The theoretical maximum size of a UDP datagram is 65507 bytes, but this implementation uses a more conservative default value of 512 bytes. This can be adjusted by using set_buffer_size() method.
|
staticconstexpr |
Maximum possible size of a UDP datagram.
The theoretical maximum size of a UDP datagram is 65507 bytes (IPv4).