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

RAII wrapper around an oveRTOS TCP (stream) socket. More...

#include <net.hpp>

Public Member Functions

 TcpSocket ()
 Opens a new TCP socket.
 
 ~TcpSocket () noexcept
 Closes the socket if it is still open.
 
 TcpSocket (const TcpSocket &)=delete
 
TcpSocketoperator= (const TcpSocket &)=delete
 
 TcpSocket (TcpSocket &&other) noexcept
 Move constructor — transfers ownership of the socket.
 
TcpSocketoperator= (TcpSocket &&other) noexcept
 Move-assignment operator — transfers ownership of the socket.
 
Result< void > connect (const Address &addr, std::chrono::nanoseconds timeout=wait_forever) noexcept
 Connects to a remote address.
 
Result< size_t > send (const void *data, size_t len) noexcept
 Sends data on the connected socket.
 
Result< size_t > recv (void *buf, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
 Receives data from the connected socket.
 
void close ()
 Closes the TCP socket.
 
bool is_open () const
 Returns true if the socket is open.
 
ove_socket_t handle () const
 Returns the raw oveRTOS socket handle.
 

Friends

class TcpListener
 

Detailed Description

RAII wrapper around an oveRTOS TCP (stream) socket.

A new socket is opened on construction and closed on destruction. TcpListener::accept() can also produce a TcpSocket by adopting an already-accepted connection via the private constructor.

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

Constructor & Destructor Documentation

◆ TcpSocket() [1/2]

ove::TcpSocket::TcpSocket ( )
inline

Opens a new TCP socket.

Asserts at startup if socket creation fails.

◆ TcpSocket() [2/2]

ove::TcpSocket::TcpSocket ( TcpSocket &&  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=()

TcpSocket & ove::TcpSocket::operator= ( TcpSocket &&  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.

◆ connect()

Result< void > ove::TcpSocket::connect ( const Address addr,
std::chrono::nanoseconds  timeout = wait_forever 
)
inlinenoexcept

Connects to a remote address.

Parameters
[in]addrRemote address.
[in]timeoutConnect timeout (any std::chrono::duration unit; defaults to wait_forever).
Returns
Empty Result<void> on success; unexpected with the appropriate Error on failure (Error::Timeout, Error::NetRefused, Error::NetUnreachable, …).

◆ send()

Result< size_t > ove::TcpSocket::send ( const void *  data,
size_t  len 
)
inlinenoexcept

Sends data on the connected socket.

Parameters
[in]dataPointer to data to send.
[in]lenNumber of bytes to attempt to send.
Returns
On success, the number of bytes actually written (may be less than len if the substrate decides to commit a partial buffer). On failure, an unexpected Error.

◆ recv()

Result< size_t > ove::TcpSocket::recv ( void *  buf,
size_t  len,
std::chrono::nanoseconds  timeout = wait_forever 
)
inlinenoexcept

Receives data from the connected socket.

Parameters
[out]bufBuffer to receive into.
[in]lenBuffer size in bytes.
[in]timeoutReceive timeout (any std::chrono::duration unit; defaults to wait_forever).
Returns
On success, the number of bytes actually read (may be less than len; never 0 — a clean peer close is reported as unexpected(Error::NetClosed)).

◆ close()

void ove::TcpSocket::close ( )
inline

Closes the TCP 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 TcpSocket goes out of scope.

Effect on pending I/O. Another thread blocked on send or recv against the same handle will unblock with a backend-specific error code — typically OVE_ERR_NET_CLOSED. The kernel-side socket is torn down by ove_socket_close; any further send / recv calls on this wrapper fail fast with the closed-socket error.

Thread-safety. Not safe to call concurrently with another close() on the same handle, nor concurrently with send or recv on a thread that does not expect the handle to disappear. The shutdown sequence is the caller's responsibility — typically: signal the worker thread, join it, then close.

◆ is_open()

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

Returns true if the socket is open.

Returns
Socket open state.

◆ handle()

ove_socket_t ove::TcpSocket::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: