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

Lightweight wrapper around a connected socket file descriptor. More...

#include <tec_socket.hpp>

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

Public Member Functions

 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 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.
 

Public Attributes

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

Lightweight wrapper around a connected socket file descriptor.

Stores the file descriptor along with peer address and port for logging and diagnostic purposes.

Constructor & Destructor Documentation

◆ Socket() [1/2]

tec::Socket::Socket ( int  _fd,
const char _addr,
int  _port 
)
inline

Construct a Socket wrapper from an accepted or connected fd.

Parameters
_fdSocket file descriptor.
_addrPeer address string (must fit in INET6_ADDRSTRLEN).
_portPeer port number.
Note
All other members are set to corresponding default values.

◆ Socket() [2/2]

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

Construct a Socket wrapper from an accepted or connected fd.

Parameters
_fdSocket file descriptor.
_addrPeer address string (must fit in INET6_ADDRSTRLEN).
_portPeer port number.
_bufferPointer to an internal buffer used in send/recv operations.
_buffer_sizeSize of the internal buffer.

Member Function Documentation

◆ recv()

static Status tec::Socket::recv ( Bytes data,
const Socket sock,
size_t  length 
)
inlinestatic

Receive data from a socket into a MemFile (Bytes).

Reads until the requested length is received, or until a null terminator is detected when length == 0 (character stream mode).

Parameters
dataMemFile (Bytes) to append received data to.
sockPointer to the Socket instance.
lengthExpected number of bytes (0 = read until null terminator).
Returns
Status::Kind::Ok on success, or an error status with details.

◆ send()

static Status tec::Socket::send ( const Bytes data,
const Socket sock 
)
inlinestatic

Send the entire contents of a MemFile (Bytes) through a socket.

Blocks until all data is sent or an error occurs.

Parameters
dataMemFile (Bytes) containing data to send.
sockPointer to the Socket instance.
Returns
Status::Kind::Ok on success, or an error status with details.

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