TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::SocketNd Struct Reference

Specialized socket wrapper optimized for sending/receiving NetData protocol messages. More...

#include <tec_socket_nd.hpp>

Inheritance diagram for tec::SocketNd:
tec::Socket

Public Member Functions

 SocketNd (const Socket &sock)
 Constructs SocketNd by copying from an existing Socket object.
 
 SocketNd (int _fd, const char *_addr, int _port, char *_buffer, size_t _buffer_size)
 Constructs SocketNd with explicit parameters.
 
- Public Member Functions inherited from tec::Socket
 Socket (int _fd, const char *_addr, int _port)
 Construct a Socket wrapper from an accepted or connected fd.
 
 Socket (int _fd, const char *_addr, int _port, char *_buffer, size_t _buffer_size)
 Construct a Socket wrapper from an accepted or connected fd.
 

Static Public Member Functions

static Status send_nd (const NetData *nd, const SocketNd *sock)
 Sends a complete NetData message (header + payload) over the socket.
 
static Status recv_nd (NetData *nd, const SocketNd *sock)
 Receives one complete NetData message (header + payload)
 
- Static Public Member Functions inherited from tec::Socket
static Status recv (Bytes &data, const Socket *sock, size_t length)
 Receive data from a socket into a MemFile (Bytes).
 
static Status send (const Bytes &data, const Socket *sock)
 Send the entire contents of a MemFile (Bytes) through a socket.
 

Additional Inherited Members

- Public Attributes inherited from tec::Socket
int fd
 Underlying socket file descriptor.
 
char addr [INET6_ADDRSTRLEN]
 Peer address as a null-terminated string (IPv4 or IPv6).
 
int port
 Peer port number.
 
charbuffer
 Buffer used in send/recv operations.
 
size_t buffer_size
 Size of the buffer.
 

Detailed Description

Specialized socket wrapper optimized for sending/receiving NetData protocol messages.

SocketNd extends the basic Socket class with protocol-aware send/receive operations specifically tailored for the NetData message format (header + payload).

Main differences from base Socket class:

  • Understands NetData::Header structure
  • Performs header validation
  • Uses MSG_PEEK to safely inspect message size before committing to read
  • Combines header + payload operations into atomic-like protocol messages

Constructor & Destructor Documentation

◆ SocketNd() [1/2]

tec::SocketNd::SocketNd ( const Socket sock)
inlineexplicit

Constructs SocketNd by copying from an existing Socket object.

Parameters
sockSource Socket object to copy file descriptor and address information from

◆ SocketNd() [2/2]

tec::SocketNd::SocketNd ( int  _fd,
const char _addr,
int  _port,
char _buffer,
size_t  _buffer_size 
)
inline

Constructs SocketNd with explicit parameters.

Parameters
_fdFile descriptor of the socket
_addrRemote peer address (usually string representation of IP)
_portRemote peer port number
_bufferPointer to external buffer used for receive operations
_buffer_sizeSize of the provided external buffer (in bytes)

Member Function Documentation

◆ recv_nd()

static Status tec::SocketNd::recv_nd ( NetData nd,
const SocketNd sock 
)
inlinestatic

Receives one complete NetData message (header + payload)

Protocol flow:

  1. Peeks at the header using MSG_PEEK (non-destructive read)
  2. Validates header sanity (magic, version, size field, etc.)
  3. Removes header from receive queue with normal read()
  4. Reads payload according to size specified in header
  5. Rewinds the NetData internal cursor

Important: Uses MSG_PEEK to avoid reading partial/invalid messages. This helps implement reliable framed message reading over TCP.

Parameters
[out]ndPointer to NetData object that will receive the message Must be properly constructed (buffer must be valid)
sockSocketNd instance representing the connection
Returns
Status::success() when complete message was received, various error codes when:
  • connection closed (EIO)
  • read error (errno value)
  • malformed header (EBADMSG)
  • invalid protocol fields (EBADMSG)
  • payload receive failure (from Socket::recv)

◆ send_nd()

static Status tec::SocketNd::send_nd ( const NetData nd,
const SocketNd sock 
)
inlinestatic

Sends a complete NetData message (header + payload) over the socket.

The function performs two write operations:

  1. Writes the fixed-size NetData header
  2. Writes the variable-length payload (if present)
Parameters
ndPointer to the NetData message to send
sockSocketNd instance representing the connection
Returns
Status success if entire message was sent, appropriate error status otherwise (EIO, connection closed, partial write, etc.)

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