556 std::optional<std::size_t> recvBufferSize = std::nullopt,
557 std::optional<std::size_t> sendBufferSize = std::nullopt,
558 std::optional<std::size_t> internalBufferSize = std::nullopt,
bool reuseAddress =
true,
559 int soRecvTimeoutMillis = -1,
int soSendTimeoutMillis = -1,
bool nonBlocking =
false,
560 bool dualStack =
true,
bool autoBind =
true,
bool autoConnect =
false,
561 std::string_view remoteAddress =
"",
Port remotePort = 0,
int connectTimeoutMillis = -1);
733 _haveRemoteAddr.store(rhs._haveRemoteAddr.load(std::memory_order_relaxed));
736 _haveLocalAddr.store(rhs._haveLocalAddr.load(std::memory_order_relaxed));
849 void bind(std::string_view localAddress,
Port localPort);
945 void connect(std::string_view host,
Port port,
int timeoutMillis);
1063 [[nodiscard]] std::string
getLocalIp(
bool convertIPv4Mapped);
1182 [[nodiscard]] std::string
getRemoteIp(
bool convertIPv4Mapped)
const;
1318 void write(std::string_view message)
const;
1383 template <
typename T>
void write(
const T& value)
const
1385 static_assert(std::is_trivially_copyable_v<T>,
"DatagramSocket::write<T>() requires trivially copyable type");
1386 static_assert(std::is_standard_layout_v<T>,
"DatagramSocket::write<T>() requires standard layout type");
1389 throw SocketException(
"DatagramSocket::write<T>(): socket is not open.");
1392 throw SocketException(
"DatagramSocket::write<T>(): socket is not connected. Use writeTo() instead.");
1396 const auto buffer = std::bit_cast<std::array<std::byte,
sizeof(T)>>(value);
1461 void write(std::span<const std::byte> data)
const;
1503 void writeAll(std::string_view message)
const;
1565 template <
typename T>
void writePrefixed(
const std::span<const std::byte> payload)
const
1600 template <
typename T>
1632 template <
typename T>
1633 void writePrefixedTo(
const std::string_view host,
const Port port,
const std::span<const std::byte> payload)
const
1692 void writev(std::span<const std::string_view> buffers)
const;
1750 void writevAll(std::span<const std::string_view> buffers)
const;
1816 void writeFrom(
const void* data, std::size_t len)
const;
2014 void writeTo(std::string_view host,
Port port, std::string_view message);
2089 void writeTo(std::string_view host,
Port port, std::span<const std::byte> data);
2169 template <
typename T>
void writeTo(
const std::string_view host,
const Port port,
const T& value)
const
2171 static_assert(std::is_trivially_copyable_v<T>,
"DatagramSocket::writeTo<T>() requires trivially copyable type");
2172 static_assert(std::is_standard_layout_v<T>,
"DatagramSocket::writeTo<T>() requires standard layout type");
2175 throw SocketException(
"DatagramSocket::writeTo<T>(): socket is not open.");
2177 if constexpr (
sizeof(T) == 0)
2180 const auto buf = std::bit_cast<std::array<std::byte,
sizeof(T)>>(value);
2371 [[nodiscard]] DatagramReadResult
readInto(std::span<char> out,
const DatagramReadOptions& opts = {})
const;
2425 template <
typename T, std::enable_if_t<detail::is_dynamic_buffer_v<T>,
int> = 0>
2429 using Ptr =
decltype(std::declval<T&>().data());
2430 using Elem = std::remove_pointer_t<Ptr>;
2431 static_assert(std::is_same_v<Elem, char> || std::is_same_v<Elem, unsigned char> ||
2432 std::is_same_v<Elem, std::byte>,
2433 "T must be a contiguous byte-like container of char, unsigned char, or std::byte");
2435 if (minCapacity == 0)
2442 std::size_t probed = 0;
2452 capacity = (std::max) (capacity, probed);
2455 catch (
const SocketException&)
2462 out.resize(capacity);
2464 DatagramReadOptions local = opts;
2469 if (
auto res =
readInto(
reinterpret_cast<void*
>(out.data()), out.size(), local); res.
bytes < out.size())
2470 out.resize(res.bytes);
2523 template <
typename T, std::enable_if_t<detail::is_fixed_buffer_v<T>,
int> = 0>
2526 using Ptr =
decltype(std::declval<T&>().data());
2527 using Elem = std::remove_pointer_t<Ptr>;
2528 static_assert(std::is_same_v<Elem, char> || std::is_same_v<Elem, unsigned char> ||
2529 std::is_same_v<Elem, std::byte>,
2530 "T must be a contiguous byte-like container of char, unsigned char, or std::byte");
2531 static_assert(
sizeof(Elem) == 1,
"T element type must be 1 byte wide");
2534 if (out.size() == 0)
2538 (void)
readInto(std::span<char>(
reinterpret_cast<char*
>(out.data()), out.size()), opts);
2614 template <
typename T>
2619 throw SocketException(
"DatagramSocket::readFrom(): socket is not open.");
2626 static_assert(isDynamic || isFixed,
"T must be a supported dynamic or fixed-size contiguous byte container");
2628 auto currSize =
static_cast<std::size_t
>(buffer.size());
2631 if constexpr (isDynamic)
2638 std::size_t probed = 0;
2650 if constexpr (isDynamic)
2652 if (probed > currSize)
2660 throw SocketException(
"DatagramSocket::readFrom(): fixed buffer too small for incoming "
2661 "datagram (preflight).");
2677 if constexpr (isDynamic)
2678 buffer.resize(currSize);
2681 sockaddr_storage src{};
2682 auto srcLen =
static_cast<socklen_t
>(
sizeof(src));
2683 std::size_t datagramSize = 0;
2684 bool truncated =
false;
2688 const std::size_t n =
readIntoBuffer(
reinterpret_cast<char*
>(buffer.data()), currSize, effectiveMode,
2689 opts.
recvFlags, &src, &srcLen, &datagramSize, &truncated);
2692 if (opts.
errorOnTruncate && (truncated || (datagramSize > 0 && datagramSize > currSize)))
2694 throw SocketException(
"DatagramSocket::readFrom(): datagram truncated into destination buffer.");
2698 if constexpr (isDynamic)
2700 if (n < buffer.size())
2706 result.
datagramSize = (datagramSize > 0 ? datagramSize : n);
2716 if ((senderAddr || senderPort) && srcLen > 0)
2718 std::string addrStr;
2722 *senderAddr = std::move(addrStr);
2724 *senderPort = portNum;
2785 [[nodiscard]] DatagramReadResult
readExact(std::span<char> out,
const ReadExactOptions& opts = {})
const;
2860 template <
typename T>
2865 throw SocketException(
"DatagramSocket::readExact(T&,size_t): socket is not open.");
2867 throw SocketException(
"DatagramSocket::readExact(T&,size_t): exactLen must be > 0.");
2870 using Ptr =
decltype(std::declval<T&>().data());
2871 using Elem = std::remove_pointer_t<Ptr>;
2872 static_assert(
sizeof(Elem) == 1,
"T element type must be 1 byte wide");
2873 static_assert(std::is_same_v<Elem, char> || std::is_same_v<Elem, unsigned char> ||
2874 std::is_same_v<Elem, std::byte>,
2875 "T must be a contiguous byte-like container of char, unsigned char, or std::byte");
2882 if (opts.autoResizeDynamic)
2885 throw SocketException(
2886 "DatagramSocket::readExact(T&,size_t): exactLen exceeds MaxDatagramPayloadSafe.");
2887 buffer.resize(exactLen);
2891 if (buffer.size() < exactLen)
2892 throw SocketException(
2893 "DatagramSocket::readExact(T&,size_t): buffer too small and autoResizeDynamic=false.");
2897 res =
readExact(std::span(
reinterpret_cast<char*
>(buffer.data()), exactLen), opts);
2900 if (buffer.size() != exactLen)
2901 buffer.resize(exactLen);
2905 if (buffer.size() < exactLen)
2906 throw SocketException(
"DatagramSocket::readExact(T&,size_t): fixed buffer smaller than exactLen.");
2909 res =
readExact(std::span(
reinterpret_cast<char*
>(buffer.data()), exactLen), opts);
2915 "T must be a supported dynamic or fixed-size contiguous byte container");
2973 std::size_t
readAtMost(std::span<char> out,
const DatagramReadOptions& opts = {})
const;
3025 [[nodiscard]] std::string
readAtMost(std::size_t n)
const;
3136 std::size_t
readAvailable(std::span<char> out,
const DatagramReadOptions& opts = {})
const;
3183 std::size_t
readIntoExact(
void* buffer, std::size_t len)
const;
3273 std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T> && std::is_trivially_copyable_v<T>,
int> = 0>
3275 const std::endian prefixEndian = std::endian::big)
const
3278 throw SocketException(
"DatagramSocket::readPrefixed<T>(): socket is not open.");
3281 if (maxPayloadLen == 0)
3287 if (maxPayloadLen > safeMax)
3288 maxPayloadLen = safeMax;
3291 std::size_t probed = 0;
3295 probed = (std::min) (sz, safeMax);
3303 const std::size_t cap = (probed > 0) ? probed : safeMax;
3304 if (cap <
sizeof(T))
3309 std::string frame(cap,
'\0');
3314 const std::size_t n =
readAvailable(std::span<char>(frame.data(), frame.size()), ro);
3318 if (frame.size() <
sizeof(T))
3319 throw SocketException(
"DatagramSocket::readPrefixed<T>(): datagram smaller than length prefix.");
3322 const auto* p =
reinterpret_cast<const unsigned char*
>(frame.data());
3323 std::uint64_t decl = 0;
3325 if (prefixEndian == std::endian::big)
3327 for (std::size_t i = 0; i <
sizeof(T); ++i)
3328 decl = (decl << 8) |
static_cast<std::uint64_t
>(p[i]);
3332 for (std::size_t i = 0; i <
sizeof(T); ++i)
3333 decl |= (
static_cast<std::uint64_t
>(p[i]) << (8 * i));
3336 const std::size_t declared = decl;
3339 if (
const std::size_t actual = frame.size() -
sizeof(T); declared != actual)
3340 throw SocketException(
"DatagramSocket::readPrefixed<T>(): prefix/payload size mismatch.");
3343 if (declared > maxPayloadLen)
3344 throw SocketException(
"DatagramSocket::readPrefixed<T>(): payload exceeds maxPayloadLen.");
3347 return {frame.data() +
sizeof(T), frame.data() +
sizeof(T) + declared};
3423 void discardExact(std::size_t n,
const DatagramReadOptions& opts = {})
const;
3459 [[nodiscard]] DatagramReadResult
readv(std::span<BufferView> buffers,
const DatagramReadOptions& opts = {})
const;
3510 [[nodiscard]] DatagramReadResult
readvAll(std::span<BufferView> buffers,
3511 const DatagramReadOptions& opts = {})
const;
3563 [[nodiscard]] DatagramReadResult
readvAtMostWithTimeout(std::span<BufferView> buffers,
int timeoutMillis,
3564 const DatagramReadOptions& opts = {})
const;
3692 [[nodiscard]] DatagramReadResult
peek(DatagramPacket& packet,
bool allowResize =
true,
3693 const DatagramReadOptions& opts = {})
const;
3780 [[nodiscard]] std::optional<int>
getMTU()
const;
4048 return std::nullopt;
4111 sockaddr_storage* outSrc, socklen_t* outSrcLen, std::size_t* outDatagramSz,
4112 bool* outTruncated)
const;
4343 static void throwSizeMismatch(
const std::size_t expected,
const std::size_t actual,
const bool isProbedKnown)
4346 constexpr int err = 0;
4347 const std::string msg =
4348 "UDP datagram size mismatch: expected " + std::to_string(expected) +
4349 (isProbedKnown ? (
", probed " + std::to_string(actual)) : (
", received " + std::to_string(actual)));
4410 if (payloadSize == 0)
4414 throw SocketException(0,
"DatagramSocket::enforceSendCapConnected(): socket is not open.");
4417 throw SocketException(0,
"DatagramSocket::enforceSendCapConnected(): socket is not connected.");
4420 int family = AF_UNSPEC;
4423 family =
reinterpret_cast<const sockaddr*
>(&
_remoteAddr)->sa_family;
4427 sockaddr_storage peer{};
4428 auto len =
static_cast<socklen_t
>(
sizeof(peer));
4434 family =
reinterpret_cast<const sockaddr*
>(&peer)->sa_family;
4446 std::string msg =
"UDP datagram payload (" + std::to_string(payloadSize) +
" bytes) ";
4450 msg +=
"exceeds IPv6 theoretical maximum (" + std::to_string(
MaxUdpPayloadIPv6) +
"). ";
4452 else if (family == AF_INET)
4462 msg +=
"Split the payload across multiple datagrams or switch to a stream protocol.";
4544 static std::span<const std::byte>
asBytes(
const std::string_view sv)
noexcept
4546 return {
reinterpret_cast<const std::byte*
>(sv.data()), sv.size()};
4594 static_assert(std::is_integral_v<T> && std::is_unsigned_v<T>,
4595 "T must be an unsigned integral type for the length prefix.");
4596 if (n >
static_cast<std::size_t
>((std::numeric_limits<T>::max)()))
4597 throw SocketException(
"writePrefixed<T>(): payload too large for prefix type T.");
4598 std::array<std::byte,
sizeof(T)> out{};
4599 T v =
static_cast<T
>(n);
4600 for (std::size_t i = 0; i <
sizeof(T); ++i)
4602 out[
sizeof(T) - 1 - i] =
static_cast<std::byte
>(v &
static_cast<T
>(0xFF));
4603 v =
static_cast<T
>(v >> 8);
4671 throw SocketException(
"DatagramSocket::sendPrefixedConnected<T>(): socket is not open.");
4674 "DatagramSocket::sendPrefixedConnected<T>(): socket is not connected. Use write*To().");
4676 const std::size_t n = payload.size();
4678 const std::size_t total =
sizeof(T) + n;
4683 std::vector<std::byte> datagram(total);
4684 std::memcpy(datagram.data(), prefix.data(),
sizeof(T));
4687 std::memcpy(datagram.data() +
sizeof(T), payload.data(), n);
4751 template <
typename T>
4755 throw SocketException(
"DatagramSocket::sendPrefixedUnconnected<T>(): socket is not open.");
4757 const std::size_t n = payload.size();
4759 const std::size_t total =
sizeof(T) + n;
4762 std::vector<std::byte> datagram(total);
4763 std::memcpy(datagram.data(), prefix.data(),
sizeof(T));
4766 std::memcpy(datagram.data() +
sizeof(T), payload.data(), n);
4791 sockaddr_storage ss{};
4792 auto len =
static_cast<socklen_t
>(
sizeof(ss));
4797 if (::getsockname(
getSocketFd(),
reinterpret_cast<sockaddr*
>(&ss), &len) == -1)
4853 mutable sockaddr_storage
Represents a raw writable memory region for scatter/gather I/O.
UDP datagram packet class for jsocketpp.
Defines the SocketOptions base class for cross-platform socket option access.
Type traits and utilities for detecting and validating buffer types.
Represents a UDP datagram packet, encapsulating both payload and addressing information.
Definition DatagramPacket.hpp:48
std::vector< char > _internalBuffer
Internal buffer for read operations.
Definition DatagramSocket.hpp:4862
socklen_t _localAddrLen
Size in bytes of the cached local address stored in _localAddr.
Definition DatagramSocket.hpp:4859
sockaddr_storage _localAddr
Cached local socket address (set by bind()/UDP connect() via getsockname()).
Definition DatagramSocket.hpp:4858
bool _isBound
True if the socket is bound to an address.
Definition DatagramSocket.hpp:4864
void rememberRemote(const sockaddr_storage &src, const socklen_t len) const noexcept
Remember the last remote peer after an unconnected receive.
Definition DatagramSocket.hpp:4294
std::atomic_bool _haveLocalAddr
True if _localAddr/_localAddrLen contain a valid endpoint; reset on close().
Definition DatagramSocket.hpp:4860
Port _port
Port number the socket is bound to (if applicable).
Definition DatagramSocket.hpp:4863
sockaddr_storage _remoteAddr
Storage for the address of the most recent sender (used in unconnected mode).
Definition DatagramSocket.hpp:4854
std::atomic_bool _haveRemoteAddr
Definition DatagramSocket.hpp:4857
bool _isConnected
True if the socket is connected to a remote host.
Definition DatagramSocket.hpp:4865
socklen_t _remoteAddrLen
Length of the valid address data in _remoteAddr (0 if none received yet).
Definition DatagramSocket.hpp:4855
Represents socket-related errors in the jsocketpp library.
Definition SocketException.hpp:63
Common platform and utility includes for jsocketpp.
constexpr std::size_t MaxUdpPayloadIPv6
Theoretical maximum UDP payload size (in bytes) over IPv6.
Definition common.hpp:542
std::uint16_t Port
Type alias representing a TCP or UDP port number (1–65535).
Definition common.hpp:392
constexpr std::size_t MaxDatagramPayloadSafe
Maximum UDP payload size (in bytes) that is safely valid across common stacks.
Definition common.hpp:513
constexpr std::size_t DefaultDatagramReceiveSize
Fallback receive size (in bytes) for UDP datagrams when the exact size is unknown.
Definition common.hpp:485
constexpr std::size_t MaxUdpPayloadIPv4
Maximum UDP payload size (in bytes) over IPv4.
Definition common.hpp:525
void resolveNumericHostPort(const sockaddr *sa, const socklen_t len, std::string &host, Port &port)
Resolve numeric host and port from a socket address.
Definition common.hpp:1418
void sendExact(SOCKET fd, const void *data, std::size_t size)
Sends an entire datagram to a connected peer using send().
Definition common.cpp:477
void setSocketFd(const SOCKET sock) noexcept
Updates the socket descriptor used by this object.
Definition SocketOptions.hpp:2281
SocketOptions()=delete
Default constructor (deleted) for SocketOptions base class.
SOCKET getSocketFd() const noexcept
Retrieves the native socket handle (file descriptor or OS-level handle).
Definition SocketOptions.hpp:275
void disconnect()
Disconnect this UDP socket from its current default peer.
Definition DatagramSocket.cpp:419
std::size_t readvAllWithTotalTimeoutBytes(std::span< BufferView > buffers, int totalTimeoutMillis, const DatagramReadOptions &opts={}) const
Convenience wrapper returning only the number of bytes copied.
Definition DatagramSocket.hpp:3639
std::size_t readvAllBytes(std::span< BufferView > buffers, const DatagramReadOptions &opts={}) const
Back-compat convenience returning only the number of bytes copied.
Definition DatagramSocket.hpp:3522
void cleanupAndThrow(int errorCode)
Releases all socket resources and throws a SocketException with the given error code.
Definition DatagramSocket.cpp:170
void setInternalBufferSize(std::size_t newLen)
Sets the size of the internal buffer used for string-based UDP receive operations.
Definition DatagramSocket.cpp:1786
std::size_t chooseReceiveSize() const
Decide how many bytes to attempt to receive for the next UDP datagram.
Definition DatagramSocket.hpp:4270
static std::span< const std::byte > asBytes(const std::string_view sv) noexcept
View a textual buffer as raw bytes without copying.
Definition DatagramSocket.hpp:4544
DatagramReceiveMode
Receive-time sizing policy for UDP datagrams.
Definition DatagramSocket.hpp:65
bool tryGetRemoteSockaddr(sockaddr_storage &out, socklen_t &outLen) const
Retrieve the remote endpoint sockaddr for this socket, if available.
Definition DatagramSocket.cpp:1713
void discard(const DatagramReadOptions &opts={}) const
Discard the next UDP datagram without copying it out.
Definition DatagramSocket.cpp:1295
T read(const DatagramReadOptions &opts={}) const
Read one UDP datagram into a fixed-size, contiguous byte container (zero allocation).
Definition DatagramSocket.hpp:2524
std::string getLocalSocketAddress(bool convertIPv4Mapped)
Return the local endpoint as a single "ip:port" string.
Definition DatagramSocket.cpp:1708
DatagramReadResult readvAtMostWithTimeout(std::span< BufferView > buffers, int timeoutMillis, const DatagramReadOptions &opts={}) const
Scatter-gather, best-effort read of the next datagram with a per-call timeout.
Definition DatagramSocket.cpp:1582
std::size_t readIntoBuffer(char *buf, std::size_t len, DatagramReceiveMode mode, int recvFlags, sockaddr_storage *outSrc, socklen_t *outSrcLen, std::size_t *outDatagramSz, bool *outTruncated) const
Low-level, single-recv primitive that copies one UDP datagram into a caller buffer.
Definition DatagramSocket.cpp:695
DatagramSocket & operator=(DatagramSocket &&rhs) noexcept
Move assignment operator for DatagramSocket.
Definition DatagramSocket.hpp:713
std::string getRemoteIp(bool convertIPv4Mapped) const
Return the remote peer IP address for this socket.
Definition DatagramSocket.cpp:1751
DatagramSocket(const DatagramSocket &)=delete
Copy constructor (deleted) for DatagramSocket.
void write(const T &value) const
Send one UDP datagram whose payload is the raw object representation of value.
Definition DatagramSocket.hpp:1383
static void throwSizeMismatch(const std::size_t expected, const std::size_t actual, const bool isProbedKnown)
Throw a descriptive exception when a UDP datagram’s size differs from what was expected.
Definition DatagramSocket.hpp:4343
void sendPrefixedConnected(const std::span< const std::byte > payload) const
Build and send a length-prefixed UDP datagram to the connected peer (no pre-wait).
Definition DatagramSocket.hpp:4668
void cacheLocalEndpoint() noexcept
Cache the actual local endpoint assigned by the OS.
Definition DatagramSocket.hpp:4789
void writeWithTimeout(std::string_view data, int timeoutMillis) const
Send one UDP datagram to the connected peer, waiting up to timeoutMillis for writability.
Definition DatagramSocket.cpp:509
DatagramSocket(Port localPort=0, std::string_view localAddress="", std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt, bool reuseAddress=true, int soRecvTimeoutMillis=-1, int soSendTimeoutMillis=-1, bool nonBlocking=false, bool dualStack=true, bool autoBind=true, bool autoConnect=false, std::string_view remoteAddress="", Port remotePort=0, int connectTimeoutMillis=-1)
Creates a UDP socket, optionally binds to a local address, and optionally connects to a remote peer.
Definition DatagramSocket.cpp:12
std::string readAtMostWithTimeout(std::size_t n, int timeoutMillis) const
Read up to n bytes from the next UDP datagram, waiting up to timeoutMillis for data.
Definition DatagramSocket.cpp:1286
void waitReady(Direction dir, int timeoutMillis) const
Block until the socket is ready for I/O or a timeout occurs.
Definition DatagramSocket.cpp:2085
void bind()
Binds the datagram socket to all available interfaces on an ephemeral port.
Definition DatagramSocket.cpp:243
void writePrefixedTo(const std::string_view host, const Port port, const std::span< const std::byte > payload) const
Send a length-prefixed UDP datagram to (host, port) from a byte span (unconnected path).
Definition DatagramSocket.hpp:1633
DatagramReadResult readvAll(std::span< BufferView > buffers, const DatagramReadOptions &opts={}) const
Scatter-gather receive that guarantees the entire next datagram fits the provided buffers.
Definition DatagramSocket.cpp:1562
void sendPrefixedUnconnected(const std::string_view host, const Port port, const std::span< const std::byte > payload)
Build and send a length-prefixed UDP datagram to (host, port) on the unconnected path.
Definition DatagramSocket.hpp:4752
void writeFrom(const void *data, std::size_t len) const
Send one UDP datagram to the connected peer from a raw memory buffer (no pre-wait).
Definition DatagramSocket.cpp:548
void cleanupAndRethrow()
Cleans up the datagram socket and rethrows the currently active exception.
Definition DatagramSocket.cpp:176
Port getRemotePort() const
Return the remote peer UDP port for this socket.
Definition DatagramSocket.cpp:1760
static std::array< std::byte, sizeof(T)> encodeLengthPrefixBE(std::size_t n)
Encode a length value into a fixed-size, big-endian (network-order) byte array.
Definition DatagramSocket.hpp:4592
void writev(std::span< const std::string_view > buffers) const
Send one UDP datagram to the connected peer by concatenating multiple fragments (no pre-wait).
Definition DatagramSocket.cpp:562
void enforceSendCapConnected(const std::size_t payloadSize) const
Enforce UDP payload size limits for connected datagram sends.
Definition DatagramSocket.hpp:4407
DatagramReadResult readFrom(T &buffer, std::string *senderAddr, Port *senderPort, const DatagramReadOptions &opts) const
Read one UDP datagram into a caller-provided byte container and optionally return the sender address/...
Definition DatagramSocket.hpp:2615
Direction
I/O readiness selector used by waitReady().
Definition DatagramSocket.hpp:413
std::string readAvailable() const
Receive the next UDP datagram and return its payload as a string, attempting to avoid truncation.
Definition DatagramSocket.cpp:1177
Port getLocalPort()
Return the local UDP port this socket is bound to.
Definition DatagramSocket.cpp:1681
void connect(std::string_view host, Port port, int timeoutMillis)
Connect this UDP socket to a default peer (set the default destination).
Definition DatagramSocket.cpp:249
DatagramReadResult readvAllWithTotalTimeout(std::span< BufferView > buffers, int totalTimeoutMillis, const DatagramReadOptions &opts={}) const
Scatter-gather, strict no-truncation receive with a per-call total timeout.
Definition DatagramSocket.cpp:1621
void write(std::string_view message) const
Send one UDP datagram to the currently connected peer (no pre-wait).
Definition DatagramSocket.cpp:458
bool hasPendingData(int timeoutMillis) const
Check if the socket is readable within a timeout (no data is consumed).
Definition DatagramSocket.cpp:1925
void close()
Closes the datagram socket and releases its underlying system resources.
Definition DatagramSocket.cpp:196
T read(const DatagramReadOptions &opts={}, std::size_t minCapacity=DefaultDatagramReceiveSize) const
Read one UDP datagram into a dynamically resizable, contiguous byte container (zero-copy into caller ...
Definition DatagramSocket.hpp:2426
void cleanup()
Internal helper that releases socket resources and resets all internal state.
Definition DatagramSocket.cpp:155
std::string getRemoteSocketAddress(bool convertIPv4Mapped) const
Return the remote endpoint as a single "ip:port" string.
Definition DatagramSocket.cpp:1769
DatagramReadResult read(DatagramPacket &packet, const DatagramReadOptions &opts) const
Read one UDP datagram into a DatagramPacket with optional growth/shrink and strict truncation policy.
Definition DatagramSocket.cpp:862
void writePrefixed(const std::string_view payload) const
Send a length-prefixed UDP datagram to the connected peer from text bytes (no pre-wait).
Definition DatagramSocket.hpp:1535
void discardExact(std::size_t n, const DatagramReadOptions &opts={}) const
Discard the next UDP datagram only if its payload size is exactly n bytes.
Definition DatagramSocket.cpp:1315
void sendUnconnectedTo(std::string_view host, Port port, const void *data, std::size_t len)
Resolve and send one unconnected UDP datagram to the first compatible destination.
Definition DatagramSocket.cpp:2168
bool isValid() const noexcept
Checks whether the datagram socket is valid and ready for use.
Definition DatagramSocket.hpp:3955
DatagramReadResult readExact(void *buffer, std::size_t exactLen, const ReadExactOptions &opts={}) const
Receive exactly exactLen bytes from a single UDP datagram into buffer, with strict policy control.
Definition DatagramSocket.cpp:1045
DatagramSocket(DatagramSocket &&rhs) noexcept
Move constructor for DatagramSocket.
Definition DatagramSocket.hpp:665
std::string getLocalIp(bool convertIPv4Mapped)
Return the local interface IP address for this UDP socket.
Definition DatagramSocket.cpp:1651
DatagramSocket & operator=(const DatagramSocket &)=delete
Copy assignment operator (deleted) for DatagramSocket.
std::string readPrefixed(std::size_t maxPayloadLen=MaxDatagramPayloadSafe, const std::endian prefixEndian=std::endian::big) const
Read a length-prefixed UDP datagram and return the payload (prefix type T).
Definition DatagramSocket.hpp:3274
void writeAll(std::string_view message) const
Send one UDP datagram to the connected peer, waiting indefinitely for writability.
Definition DatagramSocket.cpp:493
std::optional< int > getMTU() const
Retrieves the Maximum Transmission Unit (MTU) of the local interface associated with the socket.
Definition DatagramSocket.cpp:1982
std::size_t readvAtMostWithTimeout(std::span< BufferView > buffers, int timeoutMillis) const
Convenience wrapper returning only the number of bytes read.
Definition DatagramSocket.hpp:3571
std::optional< std::pair< sockaddr_storage, socklen_t > > getLastPeerSockAddr() const
Retrieves the raw socket address of the last known remote peer.
Definition DatagramSocket.hpp:4043
bool isConnected() const noexcept
Indicates whether the datagram socket is connected to a specific remote peer.
Definition DatagramSocket.hpp:1016
void writevAll(std::span< const std::string_view > buffers) const
Send one UDP datagram to the connected peer by concatenating multiple fragments, waiting indefinitely...
Definition DatagramSocket.cpp:615
void writeTo(const std::string_view host, const Port port, const T &value) const
Send one unconnected UDP datagram to (host, port) containing the raw bytes of value.
Definition DatagramSocket.hpp:2169
std::size_t readAtMost(std::span< char > out, const DatagramReadOptions &opts={}) const
Read up to out.size() bytes from the next UDP datagram into a caller-provided buffer (no allocation).
Definition DatagramSocket.cpp:1146
DatagramReadResult readInto(void *buffer, std::size_t len, const DatagramReadOptions &opts={}) const
Read one UDP datagram into a caller-provided buffer with explicit truncation policy.
Definition DatagramSocket.cpp:964
bool isBound() const noexcept
Indicates whether the datagram socket has been explicitly bound to a local address or port.
Definition DatagramSocket.hpp:880
DatagramReadResult peek(DatagramPacket &packet, bool allowResize=true, const DatagramReadOptions &opts={}) const
Peek at the next UDP datagram without consuming it (single receive with MSG_PEEK).
Definition DatagramSocket.cpp:1792
DatagramReadResult readv(std::span< BufferView > buffers, const DatagramReadOptions &opts={}) const
Scatter-gather receive: read one UDP datagram into multiple non-contiguous buffers.
Definition DatagramSocket.cpp:1372
std::size_t readIntoExact(void *buffer, std::size_t len) const
Strict exact-length UDP receive into a caller-provided buffer (single datagram).
Definition DatagramSocket.cpp:1268
bool isClosed() const noexcept
Checks whether the datagram socket has been closed or is otherwise invalid.
Definition DatagramSocket.hpp:3988
void writePrefixedTo(const std::string_view host, const Port port, const std::string_view payload) const
Send a length-prefixed UDP datagram to (host, port) from text bytes (unconnected path).
Definition DatagramSocket.hpp:1601
~DatagramSocket() noexcept override
Destructor for DatagramSocket. Ensures socket resources are released.
Definition DatagramSocket.cpp:182
DatagramReadResult readExact(T &buffer, const std::size_t exactLen, const ReadExactOptions &opts={}) const
Receive exactly exactLen bytes from a single UDP datagram into a contiguous byte container.
Definition DatagramSocket.hpp:2861
void writeTo(std::string_view host, Port port, std::string_view message)
Send one unconnected UDP datagram to (host, port) from text bytes (no pre-wait).
Definition DatagramSocket.cpp:671
void writePrefixed(const std::span< const std::byte > payload) const
Send a length-prefixed UDP datagram to the connected peer from a byte span (no pre-wait).
Definition DatagramSocket.hpp:1565
@ PreflightSize
Probe the exact size of the next datagram and size the receive accordingly.
Definition DatagramSocket.hpp:97
@ NoPreflight
Do not probe the datagram size; call recvfrom() directly.
Definition DatagramSocket.hpp:78
@ PreflightMax
Probe the size of the next datagram but cap it at the current buffer length.
Definition DatagramSocket.hpp:113
@ ReadWrite
Wait until the socket is readable or writable (logical OR).
Definition DatagramSocket.hpp:416
constexpr bool is_fixed_buffer_v
Type trait for fixed-size buffer types.
Definition buffer_traits.hpp:147
constexpr bool is_dynamic_buffer_v
Type trait for dynamic buffer types.
Definition buffer_traits.hpp:132
std::size_t nextDatagramSize(SOCKET fd) noexcept
Query the exact size of the next UDP datagram, if the platform can provide it.
Definition common.hpp:1280
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:16
@ Write
Shutdown write operations (SHUT_WR or SD_SEND).
Definition common.hpp:371
@ Read
Shutdown read operations (SHUT_RD or SD_RECEIVE).
Definition common.hpp:370
std::string SocketErrorMessage(int error, bool gaiStrerror=false)
Convert a socket-related error code to a human-readable message.
Definition common.cpp:5
constexpr SOCKET INVALID_SOCKET
Definition common.hpp:264
int GetSocketError()
Definition common.hpp:246
constexpr SOCKET SOCKET_ERROR
Definition common.hpp:265
Options controlling a single UDP receive operation.
Definition DatagramSocket.hpp:136
bool allowShrink
Whether the packet buffer may shrink after receive (applies to DatagramPacket).
Definition DatagramSocket.hpp:161
bool errorOnTruncate
When true, the read fails if the incoming datagram would be truncated.
Definition DatagramSocket.hpp:220
bool resolveNumeric
Whether to resolve numeric host/port into DatagramPacket after receive.
Definition DatagramSocket.hpp:190
bool allowGrow
Whether the packet buffer may grow before receive (applies to DatagramPacket).
Definition DatagramSocket.hpp:152
DatagramReceiveMode mode
Datagram sizing policy to use during receive.
Definition DatagramSocket.hpp:143
bool updateLastRemote
Whether to persist sender into the socket's "last remote" (unconnected sockets).
Definition DatagramSocket.hpp:181
int recvFlags
Extra flags passed to recv/recvfrom (e.g., MSG_PEEK).
Definition DatagramSocket.hpp:172
Telemetry data about a single UDP datagram receive operation.
Definition DatagramSocket.hpp:249
std::size_t bytes
Number of bytes successfully copied into the destination buffer.
Definition DatagramSocket.hpp:259
socklen_t srcLen
Length in bytes of the valid address data in src.
Definition DatagramSocket.hpp:305
std::size_t datagramSize
Full size of the original datagram when it can be determined.
Definition DatagramSocket.hpp:271
sockaddr_storage src
Raw storage for the sender's address information.
Definition DatagramSocket.hpp:294
bool truncated
Indicates whether datagram truncation occurred.
Definition DatagramSocket.hpp:282
Policy for enforcing an exact-byte receive on a single UDP datagram.
Definition DatagramSocket.hpp:327
bool requireExact
Controls whether the datagram size must match exactly.
Definition DatagramSocket.hpp:351
DatagramReadOptions base
Base receive options for controlling preflight behavior, system flags, and side effects.
Definition DatagramSocket.hpp:339
bool padIfSmaller
Controls zero-padding behavior for undersized datagrams.
Definition DatagramSocket.hpp:363
bool errorOnTruncate
Controls error handling for oversized datagrams.
Definition DatagramSocket.hpp:374
bool autoResizeDynamic
Controls automatic resizing of dynamic containers.
Definition DatagramSocket.hpp:387