GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Socket.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_NETWORKING_SOCKET_HPP
5#define GREM_NETWORKING_SOCKET_HPP
6
8
17
18#include <system_error> // std::error_code, std::errc, std::make_error_code(std::errc)
19
20namespace grem::networking {
21
30
34enum class ProtocolType : int { // NOLINT(performance-enum-size)
35 UDP = SOCK_DGRAM,
36 TCP = SOCK_STREAM,
37};
38
42enum class ShutdownType : int { // NOLINT(performance-enum-size)
43 RECEIVE = SD_RECEIVE,
44 SEND = SD_SEND,
45 BOTH = SD_BOTH,
46};
47
51enum class MessageFlags : int {};
52
57class Socket {
58public:
62 static constexpr size_t MAX_LISTEN_QUEUE_BACKLOG_SIZE = SOMAXCONN;
63
67 Socket() noexcept
68 : Socket(INVALID_SOCKET) {}
69
76 explicit Socket(SOCKET handle) noexcept
77 : handle(handle) {}
78
88 Socket(AddressFamily domain, ProtocolType type, std::error_code& errorCode)
89 : Socket() {
90 open(domain, type, errorCode);
91 }
92
104 : Socket() {
105 open(domain, type);
106 }
107
114 explicit operator bool() const noexcept {
115 return static_cast<bool>(handle);
116 }
117
124 GREM_API(networking) void close(std::error_code& errorCode) noexcept;
125
129 void close() noexcept {
130 handle.reset();
131 }
132
152 SOCKET release() noexcept {
153 return handle.release();
154 }
155
170 GREM_API(networking) void open(AddressFamily domain, ProtocolType type, std::error_code& errorCode);
171
189 void open(AddressFamily domain, ProtocolType type) {
190 std::error_code errorCode{};
191 open(domain, type, errorCode);
192 if (errorCode) {
193 throw networking::Error{errorCode};
194 }
195 }
196
208 GREM_API(networking) void setBlockingMode(BlockingMode newMode, std::error_code& errorCode);
209
223 std::error_code errorCode{};
224 setBlockingMode(newMode, errorCode);
225 if (errorCode) {
226 throw networking::Error{errorCode};
227 }
228 }
229
242 GREM_API(networking) void setReceiveTimeout(Duration newTimeout, std::error_code& errorCode);
243
257 void setReceiveTimeout(Duration newTimeout) {
258 std::error_code errorCode{};
259 setReceiveTimeout(newTimeout, errorCode);
260 if (errorCode) {
261 throw networking::Error{errorCode};
262 }
263 }
264
277 GREM_API(networking) void setSendTimeout(Duration newTimeout, std::error_code& errorCode);
278
292 void setSendTimeout(Duration newTimeout) {
293 std::error_code errorCode{};
294 setSendTimeout(newTimeout, errorCode);
295 if (errorCode) {
296 throw networking::Error{errorCode};
297 }
298 }
299
315 GREM_API(networking) void connect(const Endpoint& endpoint, std::error_code& errorCode);
316
333 void connect(const Endpoint& endpoint) {
334 std::error_code errorCode{};
335 connect(endpoint, errorCode);
336 if (errorCode && errorCode != SocketError::WAIT) {
337 throw networking::Error{errorCode};
338 }
339 }
340
359 GREM_API(networking) void bind(const Endpoint& endpoint, std::error_code& errorCode);
360
380 void bind(const Endpoint& endpoint) {
381 std::error_code errorCode{};
382 bind(endpoint, errorCode);
383 if (errorCode && errorCode != SocketError::WAIT) {
384 throw networking::Error{errorCode};
385 }
386 }
387
398 GREM_API(networking) void shutdown(ShutdownType how, std::error_code& errorCode);
399
413 std::error_code errorCode{};
414 shutdown(how, errorCode);
415 if (errorCode && errorCode != SocketError::WAIT) {
416 throw networking::Error{errorCode};
417 }
418 }
419
431 [[nodiscard]] GREM_API(networking) Optional<Endpoint> getLocalEndpoint(std::error_code& errorCode) const;
432
444 [[nodiscard]] Endpoint getLocalEndpoint() const {
445 std::error_code errorCode{};
446 Optional<Endpoint> result = getLocalEndpoint(errorCode);
447 if (errorCode) {
448 throw networking::Error{errorCode};
449 }
450 if (!result) {
452 }
453 return *result;
454 }
455
467 [[nodiscard]] GREM_API(networking) Optional<Endpoint> getRemoteEndpoint(std::error_code& errorCode) const;
468
480 [[nodiscard]] Endpoint getRemoteEndpoint() const {
481 std::error_code errorCode{};
482 Optional<Endpoint> result = getRemoteEndpoint(errorCode);
483 if (errorCode) {
484 throw networking::Error{errorCode};
485 }
486 if (!result) {
488 }
489 return *result;
490 }
491
508 GREM_API(networking) void listen(size_t listenQueueBacklogSize, std::error_code& errorCode);
509
527 void listen(size_t listenQueueBacklogSize) {
528 std::error_code errorCode{};
529 listen(listenQueueBacklogSize, errorCode);
530 if (errorCode && errorCode != SocketError::WAIT) {
531 throw networking::Error{errorCode};
532 }
533 }
534
554 [[nodiscard]] GREM_API(networking) Optional<Socket> accept(std::error_code& errorCode);
555
576 [[nodiscard]] Optional<Socket> accept() {
577 std::error_code errorCode{};
578 Optional<Socket> result = accept(errorCode);
579 if (errorCode && errorCode != SocketError::WAIT) {
580 throw networking::Error{errorCode};
581 }
582 return result;
583 }
584
615 [[nodiscard]] GREM_API(networking) Optional<Span<byte>> receive(Span<byte> buffer, MessageFlags flags, std::error_code& errorCode);
616
647 [[nodiscard]] Optional<Span<byte>> receive(Span<byte> buffer, MessageFlags flags) {
648 std::error_code errorCode{};
649 Optional<Span<byte>> result = receive(buffer, flags, errorCode);
650 if (errorCode && errorCode != SocketError::WAIT) {
651 throw networking::Error{errorCode};
652 }
653 return result;
654 }
655
684 [[nodiscard]] GREM_ALWAYS_INLINE Optional<Span<byte>> receive(Span<byte> buffer, std::error_code& errorCode) {
685 return receive(buffer, MessageFlags{}, errorCode);
686 }
687
718 return receive(buffer, MessageFlags{});
719 }
720
752 [[nodiscard]] GREM_API(networking) Optional<Pair<Span<byte>, Endpoint>> receiveFrom(Span<byte> buffer, MessageFlags flags, std::error_code& errorCode);
753
788 std::error_code errorCode{};
789 Optional<Pair<Span<byte>, Endpoint>> result = receiveFrom(buffer, flags, errorCode);
790 if (errorCode && errorCode != SocketError::WAIT) {
791 throw networking::Error{errorCode};
792 }
793 return result;
794 }
795
826 [[nodiscard]] GREM_ALWAYS_INLINE Optional<Pair<Span<byte>, Endpoint>> receiveFrom(Span<byte> buffer, std::error_code& errorCode) {
827 return receiveFrom(buffer, MessageFlags{}, errorCode);
828 }
829
864
886 GREM_API(networking) Optional<size_t> send(Span<const byte> bytes, MessageFlags flags, std::error_code& errorCode);
887
912 std::error_code errorCode{};
913 Optional<size_t> result = send(bytes, flags, errorCode);
914 if (errorCode && errorCode != SocketError::WAIT) {
915 throw networking::Error{errorCode};
916 }
917 return result;
918 }
919
940 GREM_ALWAYS_INLINE Optional<size_t> send(Span<const byte> bytes, std::error_code& errorCode) {
941 return send(bytes, MessageFlags{}, errorCode);
942 }
943
966 return send(bytes, MessageFlags{});
967 }
968
993 GREM_API(networking) Optional<size_t> sendTo(const Endpoint& endpoint, Span<const byte> bytes, MessageFlags flags, std::error_code& errorCode);
994
1020 [[nodiscard]] Optional<size_t> sendTo(const Endpoint& endpoint, Span<const byte> bytes, MessageFlags flags) {
1021 std::error_code errorCode{};
1022 Optional<size_t> result = sendTo(endpoint, bytes, flags, errorCode);
1023 if (errorCode && errorCode != SocketError::WAIT) {
1024 throw networking::Error{errorCode};
1025 }
1026 return result;
1027 }
1028
1051 GREM_ALWAYS_INLINE Optional<size_t> sendTo(const Endpoint& endpoint, Span<const byte> bytes, std::error_code& errorCode) {
1052 return sendTo(endpoint, bytes, MessageFlags{}, errorCode);
1053 }
1054
1080 return sendTo(endpoint, bytes, MessageFlags{});
1081 }
1082
1093 GREM_API(networking) void awaitReadable(Duration timeout, std::error_code& errorCode);
1094
1107 void awaitReadable(Duration timeout) {
1108 std::error_code errorCode{};
1109 awaitReadable(timeout, errorCode);
1110 if (errorCode) {
1111 throw networking::Error{errorCode};
1112 }
1113 }
1114
1125 GREM_API(networking) void awaitWritable(Duration timeout, std::error_code& errorCode);
1126
1139 void awaitWritable(Duration timeout) {
1140 std::error_code errorCode{};
1141 awaitWritable(timeout, errorCode);
1142 if (errorCode) {
1143 throw networking::Error{errorCode};
1144 }
1145 }
1146
1156 [[nodiscard]] SOCKET get() const noexcept {
1157 return handle.get();
1158 }
1159
1160private:
1161 struct SocketDeleter {
1162 GREM_API(networking) void operator()(SOCKET handle) const noexcept;
1163 };
1164
1166};
1167
1172
1177class UDPSocket : private Socket {
1178public:
1182 UDPSocket() noexcept
1183 : UDPSocket(INVALID_SOCKET) {}
1184
1192 explicit UDPSocket(SOCKET handle) noexcept
1193 : Socket(handle) {}
1194
1207 UDPSocket(BlockingMode mode, const Endpoint& endpoint, std::error_code& errorCode, const UDPSocketOptions& options = {})
1208 : UDPSocket() {
1209 bind(mode, endpoint, errorCode, options);
1210 }
1211
1226 UDPSocket(BlockingMode mode, const Endpoint& endpoint, const UDPSocketOptions& options = {})
1227 : UDPSocket() {
1228 bind(mode, endpoint, options);
1229 }
1230
1231 using Socket::operator bool;
1232 using Socket::close;
1233 using Socket::get;
1235 using Socket::receiveFrom;
1236 using Socket::release;
1237 using Socket::sendTo;
1241
1260 void bind(BlockingMode mode, const Endpoint& endpoint, std::error_code& errorCode, const UDPSocketOptions& options = {}) {
1261 (void)options;
1262 if (*this) {
1263 Socket::close(errorCode);
1264 if (errorCode) {
1265 return;
1266 }
1267 }
1268 Socket::open(endpoint.getAddressFamily(), ProtocolType::UDP, errorCode);
1269 if (errorCode) {
1270 return;
1271 }
1273 if (errorCode) {
1274 return;
1275 }
1276 Socket::bind(endpoint, errorCode);
1277 if (mode == BlockingMode::BLOCKING) {
1279 }
1280 }
1281
1302 void bind(BlockingMode mode, const Endpoint& endpoint, const UDPSocketOptions& options = {}) {
1303 std::error_code errorCode{};
1304 bind(mode, endpoint, errorCode, options);
1305 if (errorCode) {
1306 throw networking::Error{errorCode};
1307 }
1308 }
1309};
1310
1323
1328class TCPSocket : private Socket {
1329public:
1333 TCPSocket() noexcept
1334 : TCPSocket(INVALID_SOCKET) {}
1335
1343 explicit TCPSocket(SOCKET handle) noexcept
1344 : Socket(handle) {}
1345
1358 TCPSocket(BlockingMode mode, const Endpoint& endpoint, std::error_code& errorCode, const TCPSocketOptions& options = {})
1359 : TCPSocket() {
1360 connect(mode, endpoint, errorCode, options);
1361 }
1362
1377 TCPSocket(BlockingMode mode, const Endpoint& endpoint, const TCPSocketOptions& options = {})
1378 : TCPSocket() {
1379 connect(mode, endpoint, options);
1380 }
1381
1382 using Socket::operator bool;
1383 using Socket::close;
1384 using Socket::get;
1387 using Socket::receive;
1388 using Socket::receiveFrom;
1389 using Socket::release;
1393
1411 GREM_API(networking) void connect(BlockingMode mode, const Endpoint& endpoint, std::error_code& errorCode, const TCPSocketOptions& options = {});
1412
1433 void connect(BlockingMode mode, const Endpoint& endpoint, const TCPSocketOptions& options = {}) {
1434 std::error_code errorCode{};
1435 connect(mode, endpoint, errorCode, options);
1436 if (errorCode && errorCode != SocketError::WAIT) {
1437 throw networking::Error{errorCode};
1438 }
1439 }
1440
1451 void disconnect(std::error_code& errorCode) {
1452 shutdown(ShutdownType::SEND, errorCode);
1453 }
1454
1468
1487 Optional<size_t> send(Span<const byte> bytes, MessageFlags flags, std::error_code& errorCode) {
1488 size_t bytesSent = 0;
1489 while (bytesSent < bytes.size()) {
1490 const Optional<size_t> chunkBytesSent = Socket::send(bytes.subspan(bytesSent), flags, errorCode);
1491 if (errorCode) {
1492 if (errorCode == SocketError::WAIT && bytesSent > 0) {
1494 return bytesSent;
1495 }
1496 return {};
1497 }
1498 bytesSent += chunkBytesSent.value_or(0);
1499 }
1500 errorCode.clear();
1501 return bytesSent;
1502 }
1503
1525 std::error_code errorCode{};
1526 Optional<size_t> result = send(bytes, flags, errorCode);
1527 if (errorCode && errorCode != SocketError::WAIT && errorCode != SocketError::PARTIAL) {
1528 throw networking::Error{errorCode};
1529 }
1530 return result;
1531 }
1532
1550 GREM_ALWAYS_INLINE Optional<size_t> send(Span<const byte> bytes, std::error_code& errorCode) {
1551 return send(bytes, DEFAULT_TCP_SEND_FLAGS, errorCode);
1552 }
1553
1573 return send(bytes, DEFAULT_TCP_SEND_FLAGS);
1574 }
1575
1595 Optional<size_t> sendTo(const Endpoint& endpoint, Span<const byte> bytes, MessageFlags flags, std::error_code& errorCode) {
1596 size_t bytesSent = 0;
1597 while (bytesSent < bytes.size()) {
1598 const Optional<size_t> chunkBytesSent = Socket::sendTo(endpoint, bytes.subspan(bytesSent), flags, errorCode);
1599 if (errorCode) {
1600 if (errorCode == SocketError::WAIT && bytesSent > 0) {
1602 return bytesSent;
1603 }
1604 return {};
1605 }
1606 bytesSent += chunkBytesSent.value_or(0);
1607 }
1608 errorCode.clear();
1609 return bytesSent;
1610 }
1611
1632 [[nodiscard]] Optional<size_t> sendTo(const Endpoint& endpoint, Span<const byte> bytes, MessageFlags flags) {
1633 std::error_code errorCode{};
1634 Optional<size_t> result = sendTo(endpoint, bytes, flags, errorCode);
1635 if (errorCode && errorCode != SocketError::WAIT && errorCode != SocketError::PARTIAL) {
1636 throw networking::Error{errorCode};
1637 }
1638 return result;
1639 }
1640
1658 GREM_ALWAYS_INLINE Optional<size_t> sendTo(const Endpoint& endpoint, Span<const byte> bytes, std::error_code& errorCode) {
1659 return sendTo(endpoint, bytes, DEFAULT_TCP_SEND_FLAGS, errorCode);
1660 }
1661
1682 return sendTo(endpoint, bytes, DEFAULT_TCP_SEND_FLAGS);
1683 }
1684
1685private:
1686#ifdef _WIN32
1687 static constexpr MessageFlags DEFAULT_TCP_SEND_FLAGS{};
1688#else
1689 static constexpr MessageFlags DEFAULT_TCP_SEND_FLAGS{MSG_NOSIGNAL};
1690#endif
1691};
1692
1709
1714class TCPListener : private Socket {
1715public:
1717
1721 TCPListener() noexcept
1722 : TCPListener(INVALID_SOCKET) {}
1723
1731 explicit TCPListener(SOCKET handle) noexcept
1732 : Socket(handle) {}
1733
1747 TCPListener(BlockingMode mode, const Endpoint& endpoint, std::error_code& errorCode, const TCPListenerOptions& options = {})
1748 : TCPListener() {
1749 listen(mode, endpoint, errorCode, options);
1750 }
1751
1767 TCPListener(BlockingMode mode, const Endpoint& endpoint, const TCPListenerOptions& options = {})
1768 : TCPListener() {
1769 listen(mode, endpoint, options);
1770 }
1771
1772 using Socket::operator bool;
1773 using Socket::close;
1774 using Socket::get;
1776 using Socket::release;
1778
1797 void listen(BlockingMode mode, const Endpoint& endpoint, std::error_code& errorCode, const TCPListenerOptions& options = {}) {
1798 if (*this) {
1799 Socket::close(errorCode);
1800 if (errorCode) {
1801 return;
1802 }
1803 }
1804 Socket::open(endpoint.getAddressFamily(), ProtocolType::TCP, errorCode);
1805 if (errorCode) {
1806 return;
1807 }
1808 Socket::setBlockingMode(mode, errorCode);
1809 if (errorCode) {
1810 return;
1811 }
1812 Socket::bind(endpoint, errorCode);
1813 if (errorCode) {
1814 return;
1815 }
1816 Socket::listen(options.listenQueueBacklogSize, errorCode);
1817 }
1818
1840 void listen(BlockingMode mode, const Endpoint& endpoint, const TCPListenerOptions& options = {}) {
1841 std::error_code errorCode{};
1842 listen(mode, endpoint, errorCode, options);
1843 if (errorCode && errorCode != SocketError::WAIT) {
1844 throw networking::Error{errorCode};
1845 }
1846 }
1847
1866 [[nodiscard]] Optional<TCPSocket> accept(std::error_code& errorCode) {
1867 return Socket::accept(errorCode).transform([](Socket socket) -> TCPSocket { return TCPSocket{socket.release()}; });
1868 }
1869
1889 [[nodiscard]] Optional<TCPSocket> accept() {
1890 return Socket::accept().transform([](Socket socket) -> TCPSocket { return TCPSocket{socket.release()}; });
1891 }
1892};
1893
1894} // namespace grem::networking
1895
1896#endif
#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
Definition Span.hpp:146
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