136 Socket(
SOCKET client,
const sockaddr_storage& addr, socklen_t len, std::size_t recvBufferSize,
137 std::size_t sendBufferSize, std::size_t internalBufferSize);
198 Socket(std::string_view host,
Port port, std::optional<std::size_t> recvBufferSize = std::nullopt,
199 std::optional<std::size_t> sendBufferSize = std::nullopt,
200 std::optional<std::size_t> internalBufferSize = std::nullopt);
263 rhs._cliAddrInfo =
nullptr;
264 rhs._selectedAddrInfo =
nullptr;
350 rhs._cliAddrInfo =
nullptr;
351 rhs._selectedAddrInfo =
nullptr;
478 void connect(
int timeoutMillis = -1) const;
555 static_assert(std::is_trivially_copyable_v<T>,
"Socket::read<T>() only supports trivially copyable types");
557 std::array<char,
sizeof(T)> buffer;
558 size_t totalRead = 0;
559 size_t remaining =
sizeof(T);
561 while (remaining > 0)
563 const auto len = recv(this->
_sockFd, buffer.data() + totalRead,
565 static_cast<int>(remaining),
578 std::memcpy(&r, buffer.data(),
sizeof(T));
631 std::string
readExact(std::size_t n)
const;
682 std::string
readUntil(
char delimiter, std::size_t maxLen = 8192,
bool includeDelimiter =
true)
const;
713 std::string
readLine(
const std::size_t maxLen = 8192,
const bool includeDelimiter =
true)
const
715 return readUntil(
'\n', maxLen, includeDelimiter);
1003 static_assert(std::is_integral_v<T> && std::is_trivially_copyable_v<T>,
1004 "Prefix type must be a trivially copyable integral type");
1008 return readExact(
static_cast<std::size_t
>(length));
1074 template <
typename T> std::string
readPrefixed(
const std::size_t maxPayloadLen)
1076 static_assert(std::is_integral_v<T> && std::is_trivially_copyable_v<T>,
1077 "Prefix type must be a trivially copyable integral type");
1082 const auto payloadLen =
static_cast<std::size_t
>(length);
1083 if (payloadLen > maxPayloadLen)
1085 throw SocketException(0,
"readPrefixed: Prefix length " + std::to_string(payloadLen) +
1086 " exceeds maximum allowed payload length of " + std::to_string(maxPayloadLen));
1219 std::size_t
readv(std::span<BufferView> buffers)
const;
1252 std::size_t
readvAll(std::span<BufferView> buffers)
const;
1365 std::string
peek(std::size_t n)
const;
1389 void discard(std::size_t n, std::size_t chunkSize = 1024)
const;
1547 [[nodiscard]]
size_t write(std::string_view message)
const;
1597 size_t writeAll(std::string_view message)
const;
1656 static_assert(std::is_integral_v<T> && std::is_trivially_copyable_v<T>,
1657 "Prefix type must be a trivially copyable integral type");
1659 const std::size_t payloadSize = payload.size();
1666 if (payloadSize >
static_cast<std::size_t
>((std::numeric_limits<T>::max)()))
1668 throw SocketException(0,
"writePrefixed: Payload size " + std::to_string(payloadSize) +
1669 " exceeds maximum encodable size for prefix type");
1675 const std::size_t prefixSent =
writeAll(std::string_view(
reinterpret_cast<const char*
>(&len),
sizeof(T)));
1678 const std::size_t dataSent =
writeAll(payload);
1680 return prefixSent + dataSent;
1734 template <
typename T> std::size_t
writePrefixed(
const void* data, std::size_t len)
const
1736 static_assert(std::is_integral_v<T> && std::is_trivially_copyable_v<T>,
1737 "Prefix type must be a trivially copyable integral type");
1739 if (!data && len > 0)
1740 throw SocketException(0,
"writePrefixed: Null data pointer with non-zero length");
1742 if (len >
static_cast<std::size_t
>((std::numeric_limits<T>::max)()))
1744 throw SocketException(0,
"writePrefixed: Payload length exceeds capacity of prefix type");
1750 writeAll(std::string_view(
reinterpret_cast<const char*
>(&prefix),
sizeof(T)));
1755 writeAll(std::string_view(
static_cast<const char*
>(data), len));
1758 return sizeof(T) + len;
1798 std::size_t
writev(std::span<const std::string_view> buffers)
const;
1836 std::size_t
writevAll(std::span<const std::string_view> buffers)
const;
1905 std::size_t
writeFrom(
const void* data, std::size_t len)
const;
1937 std::size_t
writeFromAll(
const void* data, std::size_t len)
const;
2047 std::size_t
writevFrom(std::span<const BufferView> buffers)
const;
2083 std::size_t
writevFromAll(std::span<BufferView> buffers)
const;
2555 void setSoTimeout(
int millis,
bool forRead =
true,
bool forWrite =
true);
2565 bool waitReady(
bool forWrite,
int timeoutMillis)
const;
2691 static void stringToAddress(
const std::string& str, sockaddr_storage& addr);
2710 void setOption(
int level,
int optName,
int value);
2728 [[nodiscard]]
int getOption(
int level,
int optName)
const;
2795 std::size_t
readIntoInternal(
void* buffer, std::size_t len,
bool exact =
false)
const;
Represents a raw writable memory region for scatter/gather I/O.
Exception class for socket-related errors in jsocketpp.
Represents socket-related errors in the jsocketpp library.
Definition SocketException.hpp:58
std::vector< char > _internalBuffer
Internal buffer for read operations, not thread-safe.
Definition Socket.hpp:2828
friend class ServerSocket
Grants ServerSocket access to private members.
Definition Socket.hpp:104
void enableNoDelay(bool enable)
Enable or disable TCP_NODELAY (Nagle's algorithm) on the socket.
Definition Socket.cpp:494
static void stringToAddress(const std::string &str, sockaddr_storage &addr)
Convert a string (ip:port) to sockaddr_storage.
Definition Socket.cpp:553
bool waitReady(bool forWrite, int timeoutMillis) const
Wait for the socket to be ready for reading or writing.
Definition Socket.cpp:413
bool isConnected() const
Check if the socket is still connected (TCP only).
Definition Socket.cpp:448
SOCKET _sockFd
Underlying socket file descriptor.
Definition Socket.hpp:2822
socklen_t _remoteAddrLen
Length of remote address (for recvfrom/recvmsg)
Definition Socket.hpp:2825
void enableKeepAlive(bool enable)
Enable or disable SO_KEEPALIVE on the socket.
Definition Socket.cpp:503
int getOption(int level, int optName) const
Get the current value of a socket option at the specified level.
Definition Socket.cpp:597
addrinfo * _selectedAddrInfo
Selected address info for connection.
Definition Socket.hpp:2827
addrinfo * _cliAddrInfo
Address info for connection (from getaddrinfo)
Definition Socket.hpp:2826
static std::string addressToString(const sockaddr_storage &addr)
Convert an address and port to a string using getnameinfo.
Definition Socket.cpp:511
sockaddr_storage _remoteAddr
(portability)
Definition Socket.hpp:2823
void setOption(int level, int optName, int value)
Set a socket option at the specified level.
Definition Socket.cpp:585
Common platform and utility includes for jsocketpp.
std::uint16_t Port
Type alias representing a TCP or UDP port number (1–65535).
Definition common.hpp:315
constexpr std::size_t DefaultBufferSize
Default internal buffer size (in bytes) for TCP socket read operations.
Definition common.hpp:352
void setReceiveBufferSize(std::size_t size)
Sets the socket's receive buffer size (SO_RCVBUF).
Definition Socket.cpp:315
Socket & operator=(Socket &&rhs) noexcept
Move assignment operator that transfers socket ownership safely.
Definition Socket.hpp:328
std::size_t writeAtMostWithTimeout(std::string_view data, int timeoutMillis) const
Performs a best-effort write with a total timeout.
Definition Socket.cpp:1005
void setSendBufferSize(std::size_t size)
Sets the socket's send buffer size (SO_SNDBUF).
Definition Socket.cpp:321
~Socket() noexcept
Destructs the Socket object, closing connections and freeing resources.
Definition Socket.cpp:164
std::size_t writePrefixed(const std::string &payload)
Writes a length-prefixed payload using a fixed-size prefix type.
Definition Socket.hpp:1654
std::string readLine(const std::size_t maxLen=8192, const bool includeDelimiter=true) const
Reads a line terminated by ' ' from the socket.
Definition Socket.hpp:713
std::string readAtMostWithTimeout(std::size_t n, int timeoutMillis) const
Performs a best-effort read up to n bytes, with a maximum timeout.
Definition Socket.cpp:746
std::string readUntil(char delimiter, std::size_t maxLen=8192, bool includeDelimiter=true) const
Reads data from the socket until a specified delimiter character.
Definition Socket.cpp:649
std::string readExact(std::size_t n) const
Read exactly n bytes from the socket into a string.
Definition Socket.cpp:616
std::size_t writevFromWithTotalTimeout(std::span< BufferView > buffers, int timeoutMillis) const
Writes all raw memory buffers fully within a timeout using scatter I/O.
Definition Socket.cpp:1423
void setInternalBufferSize(std::size_t newLen)
Sets the size of the internal read buffer used for string operations.
Definition Socket.cpp:336
Socket & operator=(const Socket &rhs)=delete
Copy assignment operator (deleted) for Socket class.
int getReceiveBufferSize() const
Get the socket's receive buffer size (SO_RCVBUF).
Definition Socket.cpp:326
std::string peek(std::size_t n) const
Peeks at incoming data without consuming it.
Definition Socket.cpp:854
void connect(int timeoutMillis=-1) const
Establishes a TCP connection to the remote host with optional timeout control.
Definition Socket.cpp:82
std::size_t writevFrom(std::span< const BufferView > buffers) const
Writes multiple raw memory regions using vectorized I/O.
Definition Socket.cpp:1350
std::size_t writeFromAll(const void *data, std::size_t len) const
Writes all bytes from a raw memory buffer, retrying until complete.
Definition Socket.cpp:1060
std::size_t readvAtMostWithTimeout(std::span< BufferView > buffers, int timeoutMillis) const
Attempts a single vectorized read into multiple buffers with a timeout.
Definition Socket.cpp:1336
void setNonBlocking(bool nonBlocking) const
Set the socket to non-blocking or blocking mode.
Definition Socket.cpp:342
bool getNonBlocking() const
Check if the socket is currently in non-blocking mode.
Definition Socket.cpp:361
void discard(std::size_t n, std::size_t chunkSize=1024) const
Discards exactly n bytes from the socket by reading and discarding them.
Definition Socket.cpp:883
std::size_t readIntoInternal(void *buffer, std::size_t len, bool exact=false) const
Reads data from the socket into a user-supplied buffer.
Definition Socket.cpp:712
void close()
Closes the socket connection and releases associated resources.
Definition Socket.cpp:179
std::string readAvailable() const
Reads all bytes currently available on the socket without blocking.
Definition Socket.cpp:775
std::size_t writevFromAll(std::span< BufferView > buffers) const
Writes all raw memory regions fully using scatter/gather I/O.
Definition Socket.cpp:1380
std::string readAtMost(std::size_t n) const
Reads up to n bytes from the socket into a string.
Definition Socket.cpp:686
std::size_t writevAll(std::span< const std::string_view > buffers) const
Writes all buffers fully using vectorized I/O with automatic retry on partial sends.
Definition Socket.cpp:963
size_t write(std::string_view message) const
Writes data to the socket, handling partial writes.
Definition Socket.cpp:279
std::size_t writevWithTotalTimeout(std::span< const std::string_view > buffers, int timeoutMillis) const
Writes all buffers fully within a total timeout using vectorized I/O.
Definition Socket.cpp:1142
std::size_t readv(std::span< BufferView > buffers) const
Performs a vectorized read into multiple buffers using a single system call.
Definition Socket.cpp:1198
std::string getRemoteSocketAddress(bool convertIPv4Mapped=true) const
Get the remote peer's address and port as a formatted string.
Definition Socket.cpp:243
Socket(SOCKET client, const sockaddr_storage &addr, socklen_t len, std::size_t recvBufferSize, std::size_t sendBufferSize, std::size_t internalBufferSize)
Protected constructor used internally to create Socket objects for accepted client connections.
Definition Socket.cpp:12
std::size_t writeWithTotalTimeout(std::string_view data, int timeoutMillis) const
Writes the full payload with a total timeout across all retries.
Definition Socket.cpp:1095
T read()
Reads a fixed-size trivially copyable value of type T from the socket.
Definition Socket.hpp:553
void shutdown(ShutdownMode how) const
Shutdown specific communication aspects of the socket.
Definition Socket.cpp:196
std::size_t writePrefixed(const void *data, std::size_t len) const
Writes a binary payload prefixed with its length using a fixed-size integer type.
Definition Socket.hpp:1734
void setSoTimeout(int millis, bool forRead=true, bool forWrite=true)
Set timeout for socket send and/or receive operations.
Definition Socket.cpp:381
std::string readPrefixed()
Reads a length-prefixed payload using a fixed-size prefix type.
Definition Socket.hpp:1001
std::size_t writeFrom(const void *data, std::size_t len) const
Writes up to len bytes from a raw memory buffer in a single send call.
Definition Socket.cpp:1033
std::size_t readIntoExact(void *buffer, const std::size_t len) const
Reads exactly len bytes into the given buffer (looped recv).
Definition Socket.hpp:872
Socket()=delete
Default constructor (deleted) for Socket class.
std::size_t readIntoAvailable(void *buffer, std::size_t bufferSize) const
Reads all currently available bytes into the provided buffer without blocking.
Definition Socket.cpp:814
std::size_t readInto(void *buffer, const std::size_t len) const
Reads available data from the socket into the provided buffer.
Definition Socket.hpp:819
std::string readPrefixed(const std::size_t maxPayloadLen)
Reads a length-prefixed message with an upper bound check.
Definition Socket.hpp:1074
int getSendBufferSize() const
Get the socket's send buffer size (SO_SNDBUF).
Definition Socket.cpp:331
Socket(Socket &&rhs) noexcept
Move constructor that transfers ownership of socket resources.
Definition Socket.hpp:257
size_t writeAll(std::string_view message) const
Writes the entire contents of a message to the socket, handling partial writes.
Definition Socket.cpp:301
std::size_t readvAll(std::span< BufferView > buffers) const
Reads exactly the full contents of all provided buffers.
Definition Socket.cpp:1234
std::size_t writev(std::span< const std::string_view > buffers) const
Writes multiple buffers in a single system call using scatter/gather I/O.
Definition Socket.cpp:916
bool isValid() const noexcept
Check if the socket is valid and open for communication.
Definition Socket.hpp:2387
std::size_t readvAllWithTotalTimeout(std::span< BufferView > buffers, int timeoutMillis) const
Reads exactly the full contents of all buffers within a timeout.
Definition Socket.cpp:1279
Socket(const Socket &rhs)=delete
Copy constructor (deleted) for Socket class.
void cleanupAndThrow(int errorCode)
Cleans up client socket resources and throws a SocketException.
Definition Socket.cpp:66
uint16_t toNetwork(const uint16_t val)
Converts a 16-bit unsigned integer from host to network byte order.
Definition common.hpp:444
uint16_t fromNetwork(const uint16_t val)
Converts a 16-bit unsigned integer from network to host byte order.
Definition common.hpp:460
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:13
ShutdownMode
Enum for socket shutdown modes.
Definition common.hpp:292
int CloseSocket(SOCKET fd)
Definition common.hpp:208
std::string SocketErrorMessage(int error, bool gaiStrerror=false)
Returns a human-readable error message for a socket error code.
Definition common.cpp:5
constexpr SOCKET INVALID_SOCKET
Definition common.hpp:220
int GetSocketError()
Definition common.hpp:202
int SOCKET
Definition common.hpp:219
constexpr SOCKET SOCKET_ERROR
Definition common.hpp:221