4#ifndef GREM_NETWORKING_SOCKET_HPP
5#define GREM_NETWORKING_SOCKET_HPP
18#include <system_error>
68 :
Socket(INVALID_SOCKET) {}
76 explicit Socket(SOCKET handle) noexcept
90 open(domain, type, errorCode);
114 explicit operator bool() const noexcept {
115 return static_cast<bool>(handle);
153 return handle.release();
190 std::error_code errorCode{};
191 open(domain, type, errorCode);
223 std::error_code errorCode{};
258 std::error_code errorCode{};
293 std::error_code errorCode{};
334 std::error_code errorCode{};
381 std::error_code errorCode{};
382 bind(endpoint, errorCode);
413 std::error_code errorCode{};
445 std::error_code errorCode{};
481 std::error_code errorCode{};
508 GREM_API(
networking)
void listen(
size_t listenQueueBacklogSize, std::error_code& errorCode);
527 void listen(
size_t listenQueueBacklogSize) {
528 std::error_code errorCode{};
529 listen(listenQueueBacklogSize, errorCode);
577 std::error_code errorCode{};
648 std::error_code errorCode{};
788 std::error_code errorCode{};
912 std::error_code errorCode{};
1021 std::error_code errorCode{};
1108 std::error_code errorCode{};
1140 std::error_code errorCode{};
1156 [[nodiscard]] SOCKET
get() const noexcept {
1157 return handle.get();
1161 struct SocketDeleter {
1162 GREM_API(
networking)
void operator()(SOCKET handle)
const noexcept;
1209 bind(mode, endpoint, errorCode, options);
1228 bind(mode, endpoint, options);
1231 using Socket::operator bool;
1303 std::error_code errorCode{};
1304 bind(mode, endpoint, errorCode, options);
1360 connect(mode, endpoint, errorCode, options);
1379 connect(mode, endpoint, options);
1382 using Socket::operator bool;
1434 std::error_code errorCode{};
1435 connect(mode, endpoint, errorCode, options);
1488 size_t bytesSent = 0;
1489 while (bytesSent < bytes.
size()) {
1498 bytesSent += chunkBytesSent.
value_or(0);
1525 std::error_code errorCode{};
1551 return send(bytes, DEFAULT_TCP_SEND_FLAGS, errorCode);
1573 return send(bytes, DEFAULT_TCP_SEND_FLAGS);
1596 size_t bytesSent = 0;
1597 while (bytesSent < bytes.
size()) {
1606 bytesSent += chunkBytesSent.
value_or(0);
1633 std::error_code errorCode{};
1659 return sendTo(endpoint, bytes, DEFAULT_TCP_SEND_FLAGS, errorCode);
1682 return sendTo(endpoint, bytes, DEFAULT_TCP_SEND_FLAGS);
1687 static constexpr MessageFlags DEFAULT_TCP_SEND_FLAGS{};
1689 static constexpr MessageFlags DEFAULT_TCP_SEND_FLAGS{MSG_NOSIGNAL};
1749 listen(mode, endpoint, errorCode, options);
1769 listen(mode, endpoint, options);
1772 using Socket::operator bool;
1841 std::error_code errorCode{};
1842 listen(mode, endpoint, errorCode, options);
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
constexpr T value_or(U &&defaultValue) const &
Get the underlying value, or a fallback value in case the optional is empty.
Definition Optional.hpp:453
constexpr detail::subspan_type_t< T, EXTENT, Offset, N > subspan() const
Definition Span.hpp:284
constexpr size_type size() const noexcept
Definition Span.hpp:207
Generic nullable RAII resource handle with exclusive ownership of a resource that is automatically de...
Definition UniqueHandle.hpp:26
Combined network address and port number (and potentially other info) that identifies a host,...
Definition Endpoint.hpp:1215
AddressFamily getAddressFamily() const noexcept
Get the address family of the endpoint.
Definition Endpoint.hpp:1682
void listen(size_t listenQueueBacklogSize, std::error_code &errorCode)
Start accepting incoming TCP connections on the port that the socket is currently bound to,...
void setSendTimeout(Duration newTimeout, std::error_code &errorCode)
Set the send timeout of the socket.
void setBlockingMode(BlockingMode newMode, std::error_code &errorCode)
Set the blocking mode of the socket.
Optional< Pair< Span< byte >, Endpoint > > receiveFrom(Span< byte > buffer, MessageFlags flags, std::error_code &errorCode)
Get some data and its associated source address from the socket's incoming data queue,...
void awaitReadable(Duration timeout)
Wait for the socket to have incoming data available to be read, throwing an exception on failure or t...
Definition Socket.hpp:1107
Socket(AddressFamily domain, ProtocolType type, std::error_code &errorCode)
Construct a new open socket.
Definition Socket.hpp:88
GREM_ALWAYS_INLINE Optional< Span< byte > > receive(Span< byte > buffer, std::error_code &errorCode)
Get some data from the socket's incoming data queue, received from the remote address(es) specified t...
Definition Socket.hpp:684
void close() noexcept
Close the socket and set its handle to INVALID_SOCKET, ignoring failure.
Definition Socket.hpp:129
void connect(const Endpoint &endpoint)
Initiate a connection on the socket, throwing an exception on failure.
Definition Socket.hpp:333
void awaitWritable(Duration timeout, std::error_code &errorCode)
Wait for the socket to become ready to write outgoing data to.
Optional< Span< byte > > receive(Span< byte > buffer, MessageFlags flags, std::error_code &errorCode)
Get some data from the socket's incoming data queue, received from the remote address(es) specified t...
void bind(const Endpoint &endpoint)
Assign an address to the socket, throwing an exception on failure.
Definition Socket.hpp:380
void setSendTimeout(Duration newTimeout)
Set the send timeout of the socket, throwing an exception on failure.
Definition Socket.hpp:292
GREM_ALWAYS_INLINE Optional< Span< byte > > receive(Span< byte > buffer)
Get some data from the socket's incoming data queue, received from the remote address(es) specified t...
Definition Socket.hpp:717
Socket(AddressFamily domain, ProtocolType type)
Construct a new open socket, throwing an exception on failure.
Definition Socket.hpp:103
Optional< size_t > sendTo(const Endpoint &endpoint, Span< const byte > bytes, MessageFlags flags, std::error_code &errorCode)
Write some data to the socket's outgoing data queue, to be sent to a given remote address.
Optional< Socket > accept()
Accept a single incoming TCP connection from the socket's listen queue that was previously set up by ...
Definition Socket.hpp:576
Optional< Pair< Span< byte >, Endpoint > > receiveFrom(Span< byte > buffer, MessageFlags flags)
Get some data and its associated source address from the socket's incoming data queue,...
Definition Socket.hpp:787
Optional< size_t > send(Span< const byte > bytes, MessageFlags flags)
Write some data to the socket's outgoing data queue, to be sent to the remote address specified throu...
Definition Socket.hpp:911
Optional< Socket > accept(std::error_code &errorCode)
Accept a single incoming TCP connection from the socket's listen queue that was previously set up by ...
void awaitReadable(Duration timeout, std::error_code &errorCode)
Wait for the socket to have incoming data available to be read.
void bind(const Endpoint &endpoint, std::error_code &errorCode)
Assign an address to the socket.
Endpoint getRemoteEndpoint() const
Get the remote endpoint address and port of the socket using getpeername(), throwing an exception on ...
Definition Socket.hpp:480
Optional< Endpoint > getLocalEndpoint(std::error_code &errorCode) const
Get the local endpoint address and port of the socket using getsockname().
Optional< Span< byte > > receive(Span< byte > buffer, MessageFlags flags)
Get some data from the socket's incoming data queue, received from the remote address(es) specified t...
Definition Socket.hpp:647
void listen(size_t listenQueueBacklogSize)
Start accepting incoming TCP connections on the port that the socket is currently bound to,...
Definition Socket.hpp:527
void close(std::error_code &errorCode) noexcept
Close the socket and set its handle to INVALID_SOCKET.
void shutdown(ShutdownType how)
Shut down the connection on the socket in the specified direction(s), disallowing further receptions ...
Definition Socket.hpp:412
GREM_ALWAYS_INLINE Optional< size_t > sendTo(const Endpoint &endpoint, Span< const byte > bytes)
Write some data to the socket's outgoing data queue, to be sent to a given remote address,...
Definition Socket.hpp:1079
GREM_ALWAYS_INLINE Optional< size_t > sendTo(const Endpoint &endpoint, Span< const byte > bytes, std::error_code &errorCode)
Write some data to the socket's outgoing data queue, to be sent to a given remote address,...
Definition Socket.hpp:1051
void setBlockingMode(BlockingMode newMode)
Set the blocking mode of the socket, throwing an exception on failure.
Definition Socket.hpp:222
Socket(SOCKET handle) noexcept
Construct a socket from a native socket handle.
Definition Socket.hpp:76
GREM_ALWAYS_INLINE Optional< size_t > send(Span< const byte > bytes)
Write some data to the socket's outgoing data queue, to be sent to the remote address specified throu...
Definition Socket.hpp:965
Socket() noexcept
Construct a closed socket with an invalid socket handle.
Definition Socket.hpp:67
Optional< size_t > sendTo(const Endpoint &endpoint, Span< const byte > bytes, MessageFlags flags)
Write some data to the socket's outgoing data queue, to be sent to a given remote address,...
Definition Socket.hpp:1020
void setReceiveTimeout(Duration newTimeout)
Set the receive timeout of the socket, throwing an exception on failure.
Definition Socket.hpp:257
static constexpr size_t MAX_LISTEN_QUEUE_BACKLOG_SIZE
Maximum listenQueueBacklogSize value that can be passed to listen().
Definition Socket.hpp:62
Endpoint getLocalEndpoint() const
Get the local endpoint address and port of the socket using getsockname(), throwing an exception on f...
Definition Socket.hpp:444
Optional< Endpoint > getRemoteEndpoint(std::error_code &errorCode) const
Get the remote endpoint address and port of the socket using getpeername().
SOCKET get() const noexcept
Get the native handle of the socket.
Definition Socket.hpp:1156
void setReceiveTimeout(Duration newTimeout, std::error_code &errorCode)
Set the receive timeout of the socket.
void connect(const Endpoint &endpoint, std::error_code &errorCode)
Initiate a connection on the socket.
void open(AddressFamily domain, ProtocolType type, std::error_code &errorCode)
Replace this socket with a newly created open socket.
SOCKET release() noexcept
Relinquish ownership of the socket's native handle.
Definition Socket.hpp:152
Optional< size_t > send(Span< const byte > bytes, MessageFlags flags, std::error_code &errorCode)
Write some data to the socket's outgoing data queue, to be sent to the remote address specified throu...
void awaitWritable(Duration timeout)
Wait for the socket to become ready to write outgoing data to, throwing an exception on failure or ti...
Definition Socket.hpp:1139
GREM_ALWAYS_INLINE Optional< Pair< Span< byte >, Endpoint > > receiveFrom(Span< byte > buffer, std::error_code &errorCode)
Get some data and its associated source address from the socket's incoming data queue,...
Definition Socket.hpp:826
GREM_ALWAYS_INLINE Optional< size_t > send(Span< const byte > bytes, std::error_code &errorCode)
Write some data to the socket's outgoing data queue, to be sent to the remote address specified throu...
Definition Socket.hpp:940
void open(AddressFamily domain, ProtocolType type)
Replace this socket with a newly created open socket, throwing an exception on failure.
Definition Socket.hpp:189
GREM_ALWAYS_INLINE Optional< Pair< Span< byte >, Endpoint > > receiveFrom(Span< byte > buffer)
Get some data and its associated source address from the socket's incoming data queue,...
Definition Socket.hpp:861
void shutdown(ShutdownType how, std::error_code &errorCode)
Shut down the connection on the socket in the specified direction(s), disallowing further receptions ...
Listener socket for accepting incoming connections across the network over ProtocolType::TCP.
Definition Socket.hpp:1714
Optional< TCPSocket > accept()
Accept a single incoming TCP connection from the socket's listen queue that was previously set up by ...
Definition Socket.hpp:1889
TCPListener() noexcept
Construct a closed TCP listener with an invalid socket handle.
Definition Socket.hpp:1721
void listen(BlockingMode mode, const Endpoint &endpoint, std::error_code &errorCode, const TCPListenerOptions &options={})
Replace this socket with a newly created open socket bound to a specific local port,...
Definition Socket.hpp:1797
void listen(BlockingMode mode, const Endpoint &endpoint, const TCPListenerOptions &options={})
Replace this socket with a newly created open socket bound to a specific local port,...
Definition Socket.hpp:1840
TCPListener(BlockingMode mode, const Endpoint &endpoint, const TCPListenerOptions &options={})
Construct a new open TCP listener bound to a specific local port, and start accepting incoming TCP co...
Definition Socket.hpp:1767
TCPListener(SOCKET handle) noexcept
Construct a TCP listener from a native socket handle.
Definition Socket.hpp:1731
TCPListener(BlockingMode mode, const Endpoint &endpoint, std::error_code &errorCode, const TCPListenerOptions &options={})
Construct a new open TCP listener bound to a specific local port, and start accepting incoming TCP co...
Definition Socket.hpp:1747
Optional< TCPSocket > accept(std::error_code &errorCode)
Accept a single incoming TCP connection from the socket's listen queue that was previously set up by ...
Definition Socket.hpp:1866
Connection-oriented socket for receiving and transmitting ordered, reliable streams of data across th...
Definition Socket.hpp:1328
GREM_ALWAYS_INLINE Optional< size_t > sendTo(const Endpoint &endpoint, Span< const byte > bytes)
Append some data to the socket's outgoing TCP stream, to be sent to a given remote address,...
Definition Socket.hpp:1681
Optional< size_t > send(Span< const byte > bytes, MessageFlags flags, std::error_code &errorCode)
Appends some data to the socket's outgoing TCP stream, to be sent to the remote address specified thr...
Definition Socket.hpp:1487
TCPSocket() noexcept
Construct a closed TCP socket with an invalid socket handle.
Definition Socket.hpp:1333
TCPSocket(SOCKET handle) noexcept
Construct a TCP socket from a native socket handle.
Definition Socket.hpp:1343
TCPSocket(BlockingMode mode, const Endpoint &endpoint, const TCPSocketOptions &options={})
Construct a new open TCP socket and establish a TCP connection to the remote socket at the specified ...
Definition Socket.hpp:1377
Optional< size_t > sendTo(const Endpoint &endpoint, Span< const byte > bytes, MessageFlags flags)
Append some data to the socket's outgoing TCP stream, to be sent to a given remote address,...
Definition Socket.hpp:1632
GREM_ALWAYS_INLINE Optional< size_t > send(Span< const byte > bytes)
Append some data to the socket's outgoing TCP stream, to be sent to the remote address specified thro...
Definition Socket.hpp:1572
GREM_ALWAYS_INLINE Optional< size_t > sendTo(const Endpoint &endpoint, Span< const byte > bytes, std::error_code &errorCode)
Append some data to the socket's outgoing TCP stream, to be sent to a given remote address,...
Definition Socket.hpp:1658
void connect(BlockingMode mode, const Endpoint &endpoint, const TCPSocketOptions &options={})
Replace this socket with a newly created open socket and establish a TCP connection to the remote soc...
Definition Socket.hpp:1433
void disconnect()
Shut down the connection on the socket in the outgoing direction, disallowing further transmissions,...
Definition Socket.hpp:1465
void disconnect(std::error_code &errorCode)
Shut down the connection on the socket in the outgoing direction, disallowing further transmissions,...
Definition Socket.hpp:1451
TCPSocket(BlockingMode mode, const Endpoint &endpoint, std::error_code &errorCode, const TCPSocketOptions &options={})
Construct a new open TCP socket and establish a TCP connection to the remote socket at the specified ...
Definition Socket.hpp:1358
void connect(BlockingMode mode, const Endpoint &endpoint, std::error_code &errorCode, const TCPSocketOptions &options={})
Replace this socket with a newly created open socket and establish a TCP connection to the remote soc...
GREM_ALWAYS_INLINE Optional< size_t > send(Span< const byte > bytes, std::error_code &errorCode)
Append some data to the socket's outgoing TCP stream, to be sent to the remote address specified thro...
Definition Socket.hpp:1550
Optional< size_t > send(Span< const byte > bytes, MessageFlags flags)
Append some data to the socket's outgoing TCP stream, to be sent to the remote address specified thro...
Definition Socket.hpp:1524
Optional< size_t > sendTo(const Endpoint &endpoint, Span< const byte > bytes, MessageFlags flags, std::error_code &errorCode)
Append some data to the socket's outgoing TCP stream, to be sent to a given remote address.
Definition Socket.hpp:1595
Connectionless socket for receiving and transmitting unordered, unreliable datagrams across the netwo...
Definition Socket.hpp:1177
UDPSocket() noexcept
Construct a closed UDP socket with an invalid socket handle.
Definition Socket.hpp:1182
void bind(BlockingMode mode, const Endpoint &endpoint, std::error_code &errorCode, const UDPSocketOptions &options={})
Replace this socket with a newly created open socket bound to a specific local port,...
Definition Socket.hpp:1260
UDPSocket(BlockingMode mode, const Endpoint &endpoint, const UDPSocketOptions &options={})
Construct a new open UDP socket bound to a specific local port, and set the remote address(es) to rec...
Definition Socket.hpp:1226
UDPSocket(BlockingMode mode, const Endpoint &endpoint, std::error_code &errorCode, const UDPSocketOptions &options={})
Construct a new open UDP socket bound to a specific local port, and set the remote address(es) to rec...
Definition Socket.hpp:1207
void bind(BlockingMode mode, const Endpoint &endpoint, const UDPSocketOptions &options={})
Replace this socket with a newly created open socket bound to a specific local port,...
Definition Socket.hpp:1302
UDPSocket(SOCKET handle) noexcept
Construct a UDP socket from a native socket handle.
Definition Socket.hpp:1192
Definition Endpoint.hpp:28
BlockingMode
Blocking mode of a Socket.
Definition Socket.hpp:25
@ UNSPECIFIED
Preserve current blocking mode.
Definition Socket.hpp:26
@ NON_BLOCKING
Disable blocking.
Definition Socket.hpp:28
@ BLOCKING
Enable blocking.
Definition Socket.hpp:27
ProtocolType
Communication semantics of a Socket.
Definition Socket.hpp:34
@ TCP
Transmission Control Protocol (connection-based, sequenced, reliable two-way data stream).
Definition Socket.hpp:36
@ UDP
User Datagram Protocol (connectionless, unreliable messages of a fixed maximum length).
Definition Socket.hpp:35
std::error_code make_error_code(EndpointError error)
Create a std::error_code from an EndpointError value.
Definition Error.hpp:153
@ FAILED
The operation failed.
Definition Error.hpp:165
@ WAIT
Try again later.
Definition Error.hpp:162
@ PARTIAL
The data was partially transmitted.
Definition Error.hpp:163
AddressFamily
Address family of an IP endpoint.
Definition Endpoint.hpp:52
ShutdownType
Direction(s) in which to shut down the connection of a Socket.
Definition Socket.hpp:42
@ RECEIVE
Shut down further receptions.
Definition Socket.hpp:43
@ SEND
Shut down further transmissions.
Definition Socket.hpp:44
@ BOTH
Shut down both further receptions and transmissions.
Definition Socket.hpp:45
MessageFlags
Strong type for a bitmask of native MSG_... flags.
Definition Socket.hpp:51
Clock::duration Duration
Definition Clock.hpp:25
Exception type for domain-specific errors originating from the grem::networking module.
Definition Error.hpp:22
Configuration options for a TCPListener.
Definition Socket.hpp:1696
size_t listenQueueBacklogSize
Maximum number of incoming connections to allow to be queued up between calls to accept().
Definition Socket.hpp:1707
Configuration options for a TCPSocket.
Definition Socket.hpp:1314
Duration connectionTimeout
How long to wait for the connection to be established when the socket is created.
Definition Socket.hpp:1321
Configuration options for a UDPSocket.
Definition Socket.hpp:1171