jsocketpp 1.0
A cross-platform C++20 socket library.
Loading...
Searching...
No Matches
ServerSocket.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "Socket.hpp"
12#include "common.hpp"
13
14#include <exception>
15#include <functional>
16#include <future>
17#include <optional>
18
20
21namespace jsocketpp
22{
23
100{
101
102 public:
174 explicit ServerSocket(Port port, std::string_view localAddress = {}, bool autoBindListen = true,
175 bool reuseAddress = true, int soTimeoutMillis = -1, bool dualStack = true);
176
196 [[nodiscard]] std::string getInetAddress() const;
197
219 [[nodiscard]] Port getLocalPort() const;
220
239 [[nodiscard]] std::string getLocalSocketAddress() const;
240
259 ServerSocket(const ServerSocket& rhs) = delete; //-Weffc++
260
279 ServerSocket& operator=(const ServerSocket& rhs) = delete; //-Weffc++
280
311 : _serverSocket(rhs._serverSocket), _srvAddrInfo(rhs._srvAddrInfo), _selectedAddrInfo(rhs._selectedAddrInfo),
312 _port(rhs._port), _isBound(rhs._isBound), _isListening(rhs._isListening),
313 _soTimeoutMillis(rhs._soTimeoutMillis), _defaultReceiveBufferSize(rhs._defaultReceiveBufferSize)
314 {
315 rhs._serverSocket = INVALID_SOCKET;
316 rhs._srvAddrInfo = nullptr;
317 rhs._selectedAddrInfo = nullptr;
318 rhs._isBound = false;
319 rhs._isListening = false;
320 }
321
350 {
351 if (this != &rhs)
352 {
353 try
354 {
355 close(); // Clean up existing resources
356 }
357 catch (...)
358 {
359 }
360 _serverSocket = rhs._serverSocket;
361 _srvAddrInfo = rhs._srvAddrInfo;
362 _selectedAddrInfo = rhs._selectedAddrInfo;
363 _port = rhs._port;
364 _isBound = rhs._isBound;
365 _isListening = rhs._isListening;
366 _soTimeoutMillis = rhs._soTimeoutMillis;
367 _defaultReceiveBufferSize = rhs._defaultReceiveBufferSize;
368
369 rhs._serverSocket = INVALID_SOCKET;
370 rhs._srvAddrInfo = nullptr;
371 rhs._selectedAddrInfo = nullptr;
372 rhs._isBound = false;
373 rhs._isListening = false;
374 }
375 return *this;
376 }
377
410 ~ServerSocket() noexcept;
411
449 void bind();
450
463 [[nodiscard]] bool isBound() const noexcept { return _isBound; }
464
507 void listen(int backlog = 128);
508
521 [[nodiscard]] bool isListening() const noexcept { return _isListening; }
522
570 [[nodiscard]] Socket accept(std::optional<std::size_t> recvBufferSize = std::nullopt,
571 std::optional<std::size_t> sendBufferSize = std::nullopt,
572 std::optional<std::size_t> internalBufferSize = std::nullopt) const;
573
635 [[nodiscard]] Socket accept(int timeoutMillis, std::optional<std::size_t> recvBufferSize = std::nullopt,
636 std::optional<std::size_t> sendBufferSize = std::nullopt,
637 std::optional<std::size_t> internalBufferSize = std::nullopt) const;
638
704 [[nodiscard]] std::optional<Socket> tryAccept(std::optional<std::size_t> recvBufferSize = std::nullopt,
705 std::optional<std::size_t> sendBufferSize = std::nullopt,
706 std::optional<std::size_t> internalBufferSize = std::nullopt) const;
707
768 [[nodiscard]] std::optional<Socket> tryAccept(int timeoutMillis,
769 std::optional<std::size_t> recvBufferSize = std::nullopt,
770 std::optional<std::size_t> sendBufferSize = std::nullopt,
771 std::optional<std::size_t> internalBufferSize = std::nullopt) const;
772
840 [[nodiscard]] Socket acceptBlocking(std::optional<std::size_t> recvBufferSize = std::nullopt,
841 std::optional<std::size_t> sendBufferSize = std::nullopt,
842 std::optional<std::size_t> internalBufferSize = std::nullopt) const;
843
899 [[nodiscard]] std::optional<Socket>
900 acceptNonBlocking(std::optional<std::size_t> recvBufferSize = std::nullopt,
901 std::optional<std::size_t> sendBufferSize = std::nullopt,
902 std::optional<std::size_t> internalBufferSize = std::nullopt) const;
903
956 [[nodiscard]] std::future<Socket> acceptAsync(std::optional<std::size_t> recvBufferSize = std::nullopt,
957 std::optional<std::size_t> sendBufferSize = std::nullopt,
958 std::optional<std::size_t> internalBufferSize = std::nullopt) const;
959
1020 void acceptAsync(std::function<void(std::optional<Socket>, std::exception_ptr)> callback,
1021 std::optional<std::size_t> recvBufferSize = std::nullopt,
1022 std::optional<std::size_t> sendBufferSize = std::nullopt,
1023 std::optional<std::size_t> internalBufferSize = std::nullopt) const;
1024
1059 void close();
1060
1077 [[nodiscard]] bool isValid() const noexcept { return this->_serverSocket != INVALID_SOCKET; }
1078
1091 [[nodiscard]] bool isClosed() const noexcept { return this->_serverSocket == INVALID_SOCKET; }
1092
1124 void setOption(int level, int optName, int value);
1125
1152 [[nodiscard]] int getOption(int level, int optName) const;
1153
1173 [[nodiscard]] static int getSocketReuseOption();
1174
1199 void setReuseAddress(bool enable);
1200
1222 [[nodiscard]] bool getReuseAddress() const;
1223
1242 void setNonBlocking(bool nonBlocking);
1243
1256 [[nodiscard]] bool getNonBlocking() const;
1257
1288 [[nodiscard]] bool waitReady(std::optional<int> timeoutMillis = std::nullopt) const;
1289
1314 void setSoTimeout(const int timeoutMillis) { _soTimeoutMillis = timeoutMillis; }
1315
1332 [[nodiscard]] int getSoTimeout() const noexcept { return _soTimeoutMillis; }
1333
1334#if defined(IPV6_V6ONLY)
1348 void setIPv6Only(bool enable);
1349
1359 [[nodiscard]] bool getIPv6Only() const;
1360#endif
1361
1378 void setDefaultReceiveBufferSize(const std::size_t size) { _defaultReceiveBufferSize = size; }
1379
1393 [[nodiscard]] std::size_t getDefaultReceiveBufferSize() const noexcept { return _defaultReceiveBufferSize; }
1394
1411 void setDefaultSendBufferSize(const std::size_t size) { _defaultSendBufferSize = size; }
1412
1426 [[nodiscard]] std::size_t getDefaultSendBufferSize() const noexcept { return _defaultSendBufferSize; }
1427
1445 void setDefaultInternalBufferSize(const std::size_t size) { _defaultInternalBufferSize = size; }
1446
1464 [[nodiscard]] std::size_t getDefaultInternalBufferSize() const noexcept { return _defaultInternalBufferSize; }
1465
1466#if defined(SO_REUSEPORT)
1492 void setReusePort(bool enable);
1493
1508 [[nodiscard]] bool getReusePort() const;
1509#endif
1510
1542 [[nodiscard]] SOCKET getHandle() const { return _serverSocket; }
1543
1544 protected:
1558 void cleanupAndThrow(int errorCode);
1559
1560 private:
1577 [[nodiscard]] std::size_t getEffectiveReceiveBufferSize(const std::optional<std::size_t> recvBufferSize) const
1578 {
1579 return recvBufferSize.value_or(_defaultReceiveBufferSize);
1580 }
1581
1598 [[nodiscard]] std::size_t getEffectiveSendBufferSize(std::optional<std::size_t> sendBufferSize) const
1599 {
1600 return sendBufferSize.value_or(_defaultSendBufferSize);
1601 }
1602
1624 [[nodiscard]] std::size_t getEffectiveInternalBufferSize(std::optional<std::size_t> internalBufferSize) const
1625 {
1626 return internalBufferSize.value_or(_defaultInternalBufferSize);
1627 }
1628
1645 [[nodiscard]] std::tuple<std::size_t, std::size_t, std::size_t>
1646 resolveBuffers(const std::optional<std::size_t> recv, const std::optional<std::size_t> send,
1647 const std::optional<std::size_t> internal) const
1648 {
1651 }
1652
1654 addrinfo* _srvAddrInfo = nullptr;
1655 addrinfo* _selectedAddrInfo = nullptr;
1657 bool _isBound = false;
1658 bool _isListening = false;
1665};
1666
1667} // namespace jsocketpp
TCP client socket abstraction for jsocketpp.
bool _isListening
True if the server socket is listening.
Definition ServerSocket.hpp:1658
Port _port
Port number the server will listen on.
Definition ServerSocket.hpp:1656
int _soTimeoutMillis
Timeout for accept(); -1 = no timeout, 0 = poll, >0 = timeout in milliseconds.
Definition ServerSocket.hpp:1659
std::size_t _defaultReceiveBufferSize
Default buffer size used for accepted client sockets when no specific size is provided.
Definition ServerSocket.hpp:1660
addrinfo * _selectedAddrInfo
Selected address info for binding.
Definition ServerSocket.hpp:1655
std::size_t _defaultSendBufferSize
Default send buffer size for accepted client sockets.
Definition ServerSocket.hpp:1662
SOCKET _serverSocket
Underlying socket file descriptor.
Definition ServerSocket.hpp:1653
bool _isBound
True if the server socket is bound.
Definition ServerSocket.hpp:1657
addrinfo * _srvAddrInfo
Address info for binding (from getaddrinfo)
Definition ServerSocket.hpp:1654
std::size_t _defaultInternalBufferSize
Default internal buffer size for accepted client sockets, used by some read() methods.
Definition ServerSocket.hpp:1663
TCP client connection abstraction (Java-like interface).
Definition Socket.hpp:91
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 close()
Closes the server socket and releases its associated system resources.
Definition ServerSocket.cpp:260
std::tuple< std::size_t, std::size_t, std::size_t > resolveBuffers(const std::optional< std::size_t > recv, const std::optional< std::size_t > send, const std::optional< std::size_t > internal) const
Resolves effective receive and send buffer sizes from optional user inputs.
Definition ServerSocket.hpp:1646
void setOption(int level, int optName, int value)
Set a socket option for the listening server socket.
Definition ServerSocket.cpp:453
bool isBound() const noexcept
Check if the server socket is bound to a local address.
Definition ServerSocket.hpp:463
std::size_t getEffectiveSendBufferSize(std::optional< std::size_t > sendBufferSize) const
Get the effective send buffer size to use for socket write operations.
Definition ServerSocket.hpp:1598
bool isListening() const noexcept
Check if the server socket is currently listening for incoming connections.
Definition ServerSocket.hpp:521
void bind()
Binds the server socket to the configured port and network interface.
Definition ServerSocket.cpp:273
void setSoTimeout(const int timeoutMillis)
Set the logical timeout (in milliseconds) for accepting client connections.
Definition ServerSocket.hpp:1314
std::size_t getDefaultReceiveBufferSize() const noexcept
Get the current default receive buffer size for accepted client sockets.
Definition ServerSocket.hpp:1393
void setReuseAddress(bool enable)
Enable or disable address reuse for this server socket.
Definition ServerSocket.cpp:223
bool getNonBlocking() const
Check if the server socket is in non-blocking mode.
Definition ServerSocket.cpp:504
SOCKET getHandle() const
Get the underlying native socket handle/descriptor.
Definition ServerSocket.hpp:1542
void setIPv6Only(bool enable)
Enable or disable IPv6-only mode for this server socket.
Definition ServerSocket.cpp:608
void listen(int backlog=128)
Marks the socket as a passive (listening) socket, ready to accept incoming TCP connection requests.
Definition ServerSocket.cpp:297
bool isValid() const noexcept
Check whether the server socket is currently open and valid.
Definition ServerSocket.hpp:1077
std::string getLocalSocketAddress() const
Get the local socket address (IP and port) to which the server socket is bound.
Definition ServerSocket.cpp:194
bool waitReady(std::optional< int > timeoutMillis=std::nullopt) const
Wait for the server socket to become ready to accept an incoming connection.
Definition ServerSocket.cpp:523
void setReusePort(bool enable)
Enable or disable the SO_REUSEPORT socket option.
Definition ServerSocket.cpp:593
std::future< Socket > acceptAsync(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Asynchronously accept an incoming client connection, returning a future.
Definition ServerSocket.cpp:419
bool getReuseAddress() const
Query whether the address reuse option is enabled on this server socket.
Definition ServerSocket.cpp:229
Socket acceptBlocking(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Accept an incoming client connection, always blocking until a client connects (unless the socket is s...
Definition ServerSocket.cpp:369
void setNonBlocking(bool nonBlocking)
Set the server socket to non-blocking or blocking mode.
Definition ServerSocket.cpp:485
int getSoTimeout() const noexcept
Get the logical timeout (in milliseconds) for accept operations.
Definition ServerSocket.hpp:1332
bool getReusePort() const
Query whether SO_REUSEPORT is enabled for this socket.
Definition ServerSocket.cpp:599
std::size_t getDefaultSendBufferSize() const noexcept
Get the current default send buffer size for accepted client sockets.
Definition ServerSocket.hpp:1426
ServerSocket & operator=(const ServerSocket &rhs)=delete
Copy assignment operator (deleted).
Socket accept(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Accept an incoming client connection, respecting the configured socket timeout.
Definition ServerSocket.cpp:305
std::optional< Socket > tryAccept(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Attempt to accept an incoming client connection, returning immediately or after the configured timeou...
Definition ServerSocket.cpp:337
ServerSocket & operator=(ServerSocket &&rhs) noexcept
Move assignment operator for ServerSocket.
Definition ServerSocket.hpp:349
~ServerSocket() noexcept
Destructor that automatically closes the server socket and releases all associated resources.
Definition ServerSocket.cpp:244
std::string getInetAddress() const
Get the local IP address to which the server socket is bound.
Definition ServerSocket.cpp:156
void setDefaultInternalBufferSize(const std::size_t size)
Set the per-instance default internal buffer size used for buffered read operations.
Definition ServerSocket.hpp:1445
void cleanupAndThrow(int errorCode)
Cleans up server socket resources and throws a SocketException.
Definition ServerSocket.cpp:140
std::size_t getDefaultInternalBufferSize() const noexcept
Get the per-instance default internal buffer size used for buffered read operations.
Definition ServerSocket.hpp:1464
ServerSocket(const ServerSocket &rhs)=delete
Copy constructor (deleted).
ServerSocket(ServerSocket &&rhs) noexcept
Move constructor that transfers ownership of server socket resources.
Definition ServerSocket.hpp:310
ServerSocket(Port port, std::string_view localAddress={}, bool autoBindListen=true, bool reuseAddress=true, int soTimeoutMillis=-1, bool dualStack=true)
Constructs a ServerSocket for listening to incoming TCP connections with full configuration control.
Definition ServerSocket.cpp:8
Port getLocalPort() const
Retrieve the local port number to which the server socket is bound.
Definition ServerSocket.cpp:173
void setDefaultSendBufferSize(const std::size_t size)
Set the default send buffer size for accepted client sockets.
Definition ServerSocket.hpp:1411
std::optional< Socket > acceptNonBlocking(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Attempt to accept a client connection in non-blocking fashion.
Definition ServerSocket.cpp:388
std::size_t getEffectiveReceiveBufferSize(const std::optional< std::size_t > recvBufferSize) const
Get the effective receive buffer size to use for socket read operations.
Definition ServerSocket.hpp:1577
bool isClosed() const noexcept
Check if the server socket has been closed.
Definition ServerSocket.hpp:1091
std::size_t getEffectiveInternalBufferSize(std::optional< std::size_t > internalBufferSize) const
Get the effective internal buffer size to use for buffered socket read operations.
Definition ServerSocket.hpp:1624
int getOption(int level, int optName) const
Retrieve the current value of a socket option for the listening server socket.
Definition ServerSocket.cpp:465
static int getSocketReuseOption()
Returns the correct socket option constant for address reuse, depending on the platform.
Definition ServerSocket.cpp:213
void setDefaultReceiveBufferSize(const std::size_t size)
Set the default receive buffer size for accepted client sockets.
Definition ServerSocket.hpp:1378
bool getIPv6Only() const
Query whether IPv6-only mode is enabled.
Definition ServerSocket.cpp:626
Implementation-only utilities and platform abstractions for jsocketpp.
Definition BufferView.hpp:49
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:13
constexpr SOCKET INVALID_SOCKET
Definition common.hpp:220
int SOCKET
Definition common.hpp:219