jsocketpp 1.0
A cross-platform C++20 socket library.
Loading...
Searching...
No Matches
SocketOptions.hpp
Go to the documentation of this file.
1
16
17#pragma once
18
19#include "common.hpp"
20
21namespace jsocketpp
22{
23
51{
52 public:
74 SocketOptions() = delete;
75
98 explicit SocketOptions(const SOCKET sock) noexcept : _sockFd(sock) {}
99
114 virtual ~SocketOptions() = default;
115
137 SocketOptions(const SocketOptions&) = delete;
138
165 SocketOptions(SocketOptions&&) noexcept = default;
166
192 SocketOptions& operator=(const SocketOptions&) = delete;
193
221 SocketOptions& operator=(SocketOptions&&) noexcept = default;
222
275 [[nodiscard]] SOCKET getSocketFd() const noexcept { return _sockFd; }
276
351 void setOption(int level, int optName, int value);
352
423 void setOption(int level, int optName, const void* value, socklen_t len);
424
502 [[nodiscard]] int getOption(int level, int optName) const;
503
581 void getOption(int level, int optName, void* result, socklen_t* len) const;
582
631 void setReuseAddress(bool on);
632
674 [[nodiscard]] bool getReuseAddress() const;
675
718 void setReceiveBufferSize(std::size_t size);
719
759 [[nodiscard]] int getReceiveBufferSize() const;
760
803 void setSendBufferSize(std::size_t size);
804
847 [[nodiscard]] int getSendBufferSize() const;
848
904 void setSoLinger(bool enable, int seconds);
905
948 [[nodiscard]] std::pair<bool, int> getSoLinger() const;
949
995 void setKeepAlive(bool on);
996
1043 [[nodiscard]] bool getKeepAlive() const;
1044
1086 void setSoRecvTimeout(int millis);
1087
1129 void setSoSendTimeout(int millis);
1130
1166 [[nodiscard]] int getSoRecvTimeout() const;
1167
1203 [[nodiscard]] int getSoSendTimeout() const;
1204
1280 void setNonBlocking(bool nonBlocking);
1281
1326 [[nodiscard]] bool getNonBlocking() const;
1327
1388 void setTcpNoDelay(bool on);
1389
1440 [[nodiscard]] bool getTcpNoDelay() const;
1441
1498 void setBroadcast(bool on);
1499
1537 [[nodiscard]] bool getBroadcast() const;
1538
1539#if defined(IPV6_V6ONLY)
1540
1598 void setIPv6Only(bool enable);
1599
1650 [[nodiscard]] bool getIPv6Only() const;
1651
1652#endif
1653
1654#if defined(SO_REUSEPORT)
1655
1707 void setReusePort(const bool enable);
1708
1755 [[nodiscard]] bool getReusePort() const;
1756
1757#endif
1758
1826 void setMulticastTTL(int ttl);
1827
1893 [[nodiscard]] int getMulticastTTL() const;
1894
1969 void setMulticastLoopback(bool enable);
1970
2040 [[nodiscard]] bool getMulticastLoopback() const;
2041
2115 void setMulticastInterfaceIPv4(in_addr addr);
2116
2189 void setMulticastInterfaceIPv6(unsigned int ifindex);
2190
2207 void joinGroupIPv4(in_addr group, in_addr iface);
2208
2218 void leaveGroupIPv4(in_addr group, in_addr iface);
2219
2234 void joinGroupIPv6(in6_addr group, unsigned int ifindex);
2235
2246 void leaveGroupIPv6(in6_addr group, unsigned int ifindex);
2247
2248 protected:
2281 void setSocketFd(const SOCKET sock) noexcept { _sockFd = sock; }
2282
2309 [[nodiscard]] virtual bool isPassiveSocket() const noexcept { return false; }
2310
2370 static bool is_ipv4_multicast(const in_addr v4) { return IN_MULTICAST(ntohl(v4.s_addr)) != 0; }
2371
2430 static bool is_ipv6_multicast(const in6_addr& v6) { return IN6_IS_ADDR_MULTICAST(&v6) != 0; }
2431
2485 static [[nodiscard]] int detectFamily(SOCKET fd);
2486
2487 private:
2489};
2490
2491} // namespace jsocketpp
SOCKET _sockFd
Underlying socket file descriptor.
Definition SocketOptions.hpp:2488
Common platform and utility includes for jsocketpp.
void setMulticastInterfaceIPv4(in_addr addr)
Select the outgoing IPv4 interface for multicast.
Definition SocketOptions.cpp:446
bool getReusePort() const
Checks whether the SO_REUSEPORT option is currently enabled on the socket.
Definition SocketOptions.cpp:333
bool getTcpNoDelay() const
Queries whether Nagle's algorithm (TCP_NODELAY) is currently disabled.
Definition SocketOptions.cpp:269
void setReusePort(const bool enable)
Enables or disables the SO_REUSEPORT socket option.
Definition SocketOptions.cpp:328
static bool is_ipv6_multicast(const in6_addr &v6)
Test whether an IPv6 address is in the multicast range (ff00::/8).
Definition SocketOptions.hpp:2430
static bool is_ipv4_multicast(const in_addr v4)
Test whether an IPv4 address is in the multicast range (224.0.0.0/4).
Definition SocketOptions.hpp:2370
bool getKeepAlive() const
Checks whether TCP-level keepalive (SO_KEEPALIVE) is currently enabled.
Definition SocketOptions.cpp:155
void setMulticastLoopback(bool enable)
Enable or disable multicast loopback for this socket.
Definition SocketOptions.cpp:425
void setMulticastTTL(int ttl)
Set the default multicast hop limit / TTL for this socket.
Definition SocketOptions.cpp:394
std::pair< bool, int > getSoLinger() const
Retrieves the current SO_LINGER configuration of the socket.
Definition SocketOptions.cpp:140
void setOption(int level, int optName, int value)
Sets a low-level socket option on the underlying socket.
Definition SocketOptions.cpp:10
int getSoSendTimeout() const
Retrieves the socket send timeout (SO_SNDTIMEO) in milliseconds.
Definition SocketOptions.cpp:208
bool getIPv6Only() const
Queries whether the IPV6_V6ONLY option is enabled on this socket.
Definition SocketOptions.cpp:305
SocketOptions(SocketOptions &&) noexcept=default
Move constructor for SocketOptions.
void joinGroupIPv6(in6_addr group, unsigned int ifindex)
Join an IPv6 any-source multicast (ASM) group on a specific interface index.
Definition SocketOptions.cpp:500
int getReceiveBufferSize() const
Retrieves the current receive buffer size (SO_RCVBUF) of the socket.
Definition SocketOptions.cpp:112
void leaveGroupIPv4(in_addr group, in_addr iface)
Leave a previously joined IPv4 multicast group on a specific interface.
Definition SocketOptions.cpp:487
int getMulticastTTL() const
Get the socket’s default multicast hop limit / TTL.
Definition SocketOptions.cpp:415
bool getNonBlocking() const
Queries whether the socket is currently in non-blocking mode.
Definition SocketOptions.cpp:246
void setSoRecvTimeout(int millis)
Sets the socket receive timeout (SO_RCVTIMEO) in milliseconds.
Definition SocketOptions.cpp:160
void setNonBlocking(bool nonBlocking)
Enables or disables non-blocking mode on the socket.
Definition SocketOptions.cpp:223
SocketOptions(const SocketOptions &)=delete
Copy constructor (deleted) for SocketOptions.
void joinGroupIPv4(in_addr group, in_addr iface)
Join an IPv4 any-source multicast (ASM) group on a specific interface.
Definition SocketOptions.cpp:470
bool getMulticastLoopback() const
Read the socket’s multicast loopback flag.
Definition SocketOptions.cpp:437
void setReceiveBufferSize(std::size_t size)
Sets the socket's receive buffer size (SO_RCVBUF).
Definition SocketOptions.cpp:107
int getOption(int level, int optName) const
Retrieves the current value of a low-level socket option.
Definition SocketOptions.cpp:43
void setSoLinger(bool enable, int seconds)
Configures the socket's linger behavior (SO_LINGER) during close.
Definition SocketOptions.cpp:128
void setSoSendTimeout(int millis)
Sets the socket send timeout (SO_SNDTIMEO) in milliseconds.
Definition SocketOptions.cpp:178
virtual ~SocketOptions()=default
Virtual destructor for safe polymorphic destruction.
static int detectFamily(SOCKET fd)
Determine the address family (AF_INET or AF_INET6) of a socket.
Definition SocketOptions.cpp:340
void setSendBufferSize(std::size_t size)
Sets the socket's send buffer size (SO_SNDBUF).
Definition SocketOptions.cpp:117
bool getReuseAddress() const
Queries whether the socket is currently configured to allow address reuse.
Definition SocketOptions.cpp:93
void setSocketFd(const SOCKET sock) noexcept
Updates the socket descriptor used by this object.
Definition SocketOptions.hpp:2281
void setTcpNoDelay(bool on)
Enables or disables Nagle’s algorithm (TCP_NODELAY) on TCP sockets.
Definition SocketOptions.cpp:264
void leaveGroupIPv6(in6_addr group, unsigned int ifindex)
Leave a previously joined IPv6 multicast group on a specific interface index.
Definition SocketOptions.cpp:518
SocketOptions()=delete
Default constructor (deleted) for SocketOptions base class.
virtual bool isPassiveSocket() const noexcept
Indicates whether the socket behaves as a passive (listening) socket.
Definition SocketOptions.hpp:2309
int getSoRecvTimeout() const
Retrieves the socket receive timeout (SO_RCVTIMEO) in milliseconds.
Definition SocketOptions.cpp:194
bool getBroadcast() const
Checks whether the socket is currently allowed to send broadcast messages.
Definition SocketOptions.cpp:279
void setReuseAddress(bool on)
Enables or disables the SO_REUSEADDR socket option.
Definition SocketOptions.cpp:74
void setKeepAlive(bool on)
Enables or disables TCP-level keepalive behavior on the socket (SO_KEEPALIVE).
Definition SocketOptions.cpp:150
SocketOptions(const SOCKET sock) noexcept
Initializes the socket option interface with a specific socket descriptor.
Definition SocketOptions.hpp:98
void setIPv6Only(bool enable)
Enables or disables IPV6_V6ONLY mode for IPv6-capable sockets.
Definition SocketOptions.cpp:286
void setBroadcast(bool on)
Enables or disables the ability to send UDP broadcast messages.
Definition SocketOptions.cpp:274
SOCKET getSocketFd() const noexcept
Retrieves the native socket handle (file descriptor or OS-level handle).
Definition SocketOptions.hpp:275
void setMulticastInterfaceIPv6(unsigned int ifindex)
Select the outgoing IPv6 interface for multicast.
Definition SocketOptions.cpp:458
int getSendBufferSize() const
Retrieves the current send buffer size (SO_SNDBUF) of the socket.
Definition SocketOptions.cpp:122
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:16
constexpr SOCKET INVALID_SOCKET
Definition common.hpp:264
int SOCKET
Definition common.hpp:263