oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
Public Member Functions | List of all members
ove::UdpSocket Class Reference

RAII wrapper around an oveRTOS UDP (datagram) socket. More...

#include <net.hpp>

Public Member Functions

 UdpSocket ()
 Opens a new UDP socket.
 
 ~UdpSocket () noexcept
 Closes the socket if it is still open.
 
 UdpSocket (const UdpSocket &)=delete
 
UdpSocketoperator= (const UdpSocket &)=delete
 
 UdpSocket (UdpSocket &&other) noexcept
 Move constructor — transfers ownership of the socket.
 
UdpSocketoperator= (UdpSocket &&other) noexcept
 Move-assignment operator — transfers ownership of the socket.
 
Result< void > bind (const Address &addr) noexcept
 Binds the socket to a local address.
 
Result< size_t > send_to (const void *data, size_t len, const Address &dest) noexcept
 Sends a datagram to a specific destination.
 
Result< size_t > recv_from (void *buf, size_t len, Address *src=nullptr, std::chrono::nanoseconds timeout=wait_forever) noexcept
 Receives a datagram and the sender's address.
 
void close ()
 Closes the UDP socket.
 
bool is_open () const
 Returns true if the socket is open.
 
ove_socket_t handle () const
 Returns the raw oveRTOS socket handle.
 

Detailed Description

RAII wrapper around an oveRTOS UDP (datagram) socket.

A new socket is opened on construction and closed on destruction.

Note
Non-copyable. Move-only when heap allocation is enabled.

Constructor & Destructor Documentation

◆ UdpSocket() [1/2]

ove::UdpSocket::UdpSocket ( )
inline

Opens a new UDP socket.

Asserts at startup if socket creation fails.

◆ UdpSocket() [2/2]

ove::UdpSocket::UdpSocket ( UdpSocket &&  other)
inlinenoexcept

Move constructor — transfers ownership of the socket.

Parameters
otherThe source; left in a closed, null state after the move.

Member Function Documentation

◆ operator=()

UdpSocket & ove::UdpSocket::operator= ( UdpSocket &&  other)
inlinenoexcept

Move-assignment operator — transfers ownership of the socket.

Parameters
otherThe source; left in a closed, null state after the move.
Returns
Reference to this object.

◆ bind()

Result< void > ove::UdpSocket::bind ( const Address addr)
inlinenoexcept

Binds the socket to a local address.

Parameters
[in]addrLocal address to bind.
Returns
Empty Result<void> on success; unexpected with the appropriate Error on failure (Error::NetAddrInUse, …).

◆ send_to()

Result< size_t > ove::UdpSocket::send_to ( const void *  data,
size_t  len,
const Address dest 
)
inlinenoexcept

Sends a datagram to a specific destination.

Parameters
[in]dataPointer to data to send.
[in]lenNumber of bytes to send.
[in]destDestination address.
Returns
On success, the number of bytes actually sent. On failure, an unexpected Error.

◆ recv_from()

Result< size_t > ove::UdpSocket::recv_from ( void *  buf,
size_t  len,
Address src = nullptr,
std::chrono::nanoseconds  timeout = wait_forever 
)
inlinenoexcept

Receives a datagram and the sender's address.

Parameters
[out]bufBuffer to receive into.
[in]lenBuffer size in bytes.
[out]srcFilled with the sender's address on success (may be nullptr to ignore). Kept as an out-param rather than folded into the Result value side so the byte-count is the natural success payload.
[in]timeoutReceive timeout (any std::chrono::duration unit; defaults to wait_forever).
Returns
On success, the number of bytes received. On failure, an unexpected Error.

◆ close()

void ove::UdpSocket::close ( )
inline

Closes the UDP socket.

Idempotent. Safe to call on an already-closed socket — the call is silently a no-op in that case. The destructor calls this as part of cleanup, so explicit close() is only needed when you want to release the underlying file descriptor before the UdpSocket goes out of scope.

Effect on pending I/O. Another thread blocked on recv_from (or any send_to in progress) against the same handle will unblock with a backend-specific error code — typically OVE_ERR_NET_CLOSED. Further send_to / recv_from calls on this wrapper fail fast.

Thread-safety. Not safe to call concurrently with another close() on the same handle, nor concurrently with send_to or recv_from on a thread that does not expect the handle to disappear. Coordinate teardown via the usual signal-then-close pattern.

◆ is_open()

bool ove::UdpSocket::is_open ( ) const
inline

Returns true if the socket is open.

Returns
Socket open state.

◆ handle()

ove_socket_t ove::UdpSocket::handle ( ) const
inline

Returns the raw oveRTOS socket handle.

Returns
The opaque ove_socket_t handle.

The documentation for this class was generated from the following file: