33#ifndef _POSIX_C_SOURCE
35#define _POSIX_C_SOURCE 200809L
38#include <sys/socket.h>
287 Socket(
int _fd,
const char* _addr,
int _port)
293 std::strncpy(
addr, _addr, INET6_ADDRSTRLEN);
294 addr[INET6_ADDRSTRLEN-1] =
'\0';
307 Socket(
int _fd,
const char* _addr,
int _port,
char* _buffer,
size_t _buffer_size)
313 std::strncpy(
addr, _addr, INET6_ADDRSTRLEN);
314 addr[INET6_ADDRSTRLEN-1] =
'\0';
331 size_t total_received{0};
338 if (length == 0 && received > 0) {
340 if (sock->
buffer[received-1] ==
'\0') {
348 total_received += received;
349 if (length > 0 && length == total_received) {
353 if (eot || received <
static_cast<ssize_t
>(sock->
buffer_size)) {
360 if (length > 0 && total_received == length) {
364 else if (received == 0) {
365 auto errmsg =
format(
"{}:{} Peer closed the connection.", sock->
addr, sock->
port);
369 else if (received < 0) {
370 auto errmsg =
format(
"{}:{} socket read error {}.", sock->
addr, sock->
port, errno);
374 else if (length > 0 && total_received != length) {
375 auto errmsg =
format(
"{}:{} socket partial read: {} bytes of {}.",
376 sock->
addr, sock->
port, total_received, length);
401 if (data.
size() > 0) {
402 sent = write(sock->
fd, data.
ptr(0), data.
size());
407 auto errmsg =
format(
"{}:{} socket write error {}.", sock->
addr, sock->
port, errno);
411 else if (data.
size() !=
static_cast<size_t>(sent)) {
412 auto errmsg =
format(
"{}:{} socket partial write: {} bytes of {}.",
A byte buffer class with stream-like read/write semantics.
Definition tec_memfile.hpp:50
size_t write(const void *src, size_t len)
Writes data into the buffer at the current position.
Definition tec_memfile.hpp:382
const char * ptr(long pos) const
Returns a const pointer to a specific position in the buffer.
Definition tec_memfile.hpp:273
size_t size() const noexcept
Returns the logical size of the data in the buffer.
Definition tec_memfile.hpp:304
#define TEC_ENTER(name)
Logs an entry message for a named context (e.g., function).
Definition tec_trace.hpp:211
#define TEC_TRACE(...)
Logs a formatted trace message.
Definition tec_trace.hpp:222
Definition tec_compression.hpp:34
@ NetErr
Network-related error.
Input descriptor for character stream mode.
Definition tec_socket.hpp:242
const std::string * str
Pointer to the received null-terminated string.
Definition tec_socket.hpp:244
Output descriptor for character stream mode.
Definition tec_socket.hpp:254
std::string * str
Pointer to the string that will receive the response.
Definition tec_socket.hpp:256
Parameters specific to client-side socket connections.
Definition tec_socket.hpp:145
SocketClientParams()
Default constructor.
Definition tec_socket.hpp:152
Common parameters used for both client and server socket configuration.
Definition tec_socket.hpp:61
size_t compression_min_size
Minimum size of data to apply compression (bytes).
Definition tec_socket.hpp:111
static constexpr char kLocalURI[]
Hostname that resolves to localhost for both IPv4 and IPv6.
Definition tec_socket.hpp:69
static constexpr int kDefaultProtocol
Default protocol: 0 means "any appropriate protocol".
Definition tec_socket.hpp:87
int family
Address family (AF_INET, AF_INET6, AF_UNSPEC, ...).
Definition tec_socket.hpp:105
static constexpr size_t kDefaultBufSize
Default buffer size as defined in stdio.h (8192).
Definition tec_socket.hpp:99
int socktype
Socket type (SOCK_STREAM, SOCK_DGRAM, ...).
Definition tec_socket.hpp:106
SocketParams()
Default constructor.
Definition tec_socket.hpp:119
size_t buffer_size
Buffer size for using in send/recv operations (bytes).
Definition tec_socket.hpp:112
static constexpr int kDefaultSockType
Default socket type: TCP stream socket.
Definition tec_socket.hpp:84
int port
Port number to connect to or bind.
Definition tec_socket.hpp:104
int flags
Flags passed to getaddrinfo().
Definition tec_socket.hpp:108
int protocol
Protocol (usually 0).
Definition tec_socket.hpp:107
std::string addr
Target address or hostname.
Definition tec_socket.hpp:102
static constexpr int kDefaultClientFlags
Default addrinfo flags for client sockets (no special behaviour).
Definition tec_socket.hpp:93
static constexpr int kDefaultFamily
Default address family: AF_UNSPEC allows both IPv4 and IPv6.
Definition tec_socket.hpp:81
static constexpr char kLocalAddr[]
IPv4 loopback address (localhost).
Definition tec_socket.hpp:66
static constexpr char kLocalAddrIP6[]
IPv6 loopback address (localhost).
Definition tec_socket.hpp:75
int compression
Compression algorithm to use (see CompressionParams).
Definition tec_socket.hpp:109
static constexpr char kAnyAddrIP6[]
IPv6 address to bind/accept connections from any interface.
Definition tec_socket.hpp:72
int compression_level
Compression level [0..9] (higher = better compression, slower).
Definition tec_socket.hpp:110
static constexpr char kAnyAddr[]
IPv4 address to bind/accept connections from any interface.
Definition tec_socket.hpp:63
static constexpr char kNullChar
Null character constant (for internal use).
Definition tec_socket.hpp:96
static constexpr int kDefaultServerFlags
Default addrinfo flags for server sockets (bind to local address).
Definition tec_socket.hpp:90
static constexpr int kDefaultPort
Default port number used for testing and examples.
Definition tec_socket.hpp:78
Parameters specific to server-side socket configuration.
Definition tec_socket.hpp:170
static constexpr int kDefaultMode
Default data handling mode.
Definition tec_socket.hpp:185
static constexpr int kModeNetData
Treat incoming data as length-prefixed binary network messages.
Definition tec_socket.hpp:182
size_t thread_pool_size
Number of threads in the thread pool.
Definition tec_socket.hpp:206
static constexpr int kModeCharStream
Treat incoming data as null-terminated character streams.
Definition tec_socket.hpp:179
int mode
Data handling mode (character stream or binary network data).
Definition tec_socket.hpp:201
static constexpr int kOptReuseAddress
Disable SO_REUSEADDR option.
Definition tec_socket.hpp:173
static constexpr bool kUseThreadPool
Whether to use a fixed-size thread pool instead of one-thread-per-connection.
Definition tec_socket.hpp:199
int opt_reuse_addr
Whether to set SO_REUSEADDR (0 = no, 1 = yes).
Definition tec_socket.hpp:203
static constexpr int kDefaultConnQueueSize
Maximum length of the pending connections queue for listen(). Usually 4096.
Definition tec_socket.hpp:196
static constexpr int kDefaultMaxThreads
Default maximum number of threads in a custom thread pool.
Definition tec_socket.hpp:188
bool use_thread_pool
Use the thread pool for handling accepted connections.
Definition tec_socket.hpp:205
SocketServerParams()
Default constructor.
Definition tec_socket.hpp:216
int opt_reuse_port
Whether to set SO_REUSEPORT (if available).
Definition tec_socket.hpp:204
static constexpr int kOptReusePort
Enable SO_REUSEADDR and SO_REUSEPORT (if supported).
Definition tec_socket.hpp:176
int queue_size
Maximum backlog for listen().
Definition tec_socket.hpp:202
Lightweight wrapper around a connected socket file descriptor.
Definition tec_socket.hpp:272
static Status recv(Bytes &data, const Socket *sock, size_t length)
Receive data from a socket into a MemFile (Bytes).
Definition tec_socket.hpp:329
Socket(int _fd, const char *_addr, int _port)
Construct a Socket wrapper from an accepted or connected fd.
Definition tec_socket.hpp:287
static Status send(const Bytes &data, const Socket *sock)
Send the entire contents of a MemFile (Bytes) through a socket.
Definition tec_socket.hpp:395
char * buffer
Buffer used in send/recv operations.
Definition tec_socket.hpp:276
int port
Peer port number.
Definition tec_socket.hpp:275
Socket(int _fd, const char *_addr, int _port, char *_buffer, size_t _buffer_size)
Construct a Socket wrapper from an accepted or connected fd.
Definition tec_socket.hpp:307
size_t buffer_size
Size of the buffer.
Definition tec_socket.hpp:277
int fd
Underlying socket file descriptor.
Definition tec_socket.hpp:273
char addr[INET6_ADDRSTRLEN]
Peer address as a null-terminated string (IPv4 or IPv6).
Definition tec_socket.hpp:274
Common definitions and utilities for the tec namespace.
A byte buffer class with stream-like read/write semantics.
std::string format(const T &arg)
Formats a single argument into a string.
Definition tec_print.hpp:171
Defines error handling types and utilities for the tec namespace.
Provides a thread-safe tracing utility for debugging in the tec namespace.