GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::networking::Socket Class Reference

Generic socket handle/file descriptor for receiving and transmitting data across the network. More...

#include <GREM/networking/Socket.hpp>

Inheritance diagram for grem::networking::Socket:

Public Member Functions

 Socket () noexcept
 Construct a closed socket with an invalid socket handle.
 Socket (SOCKET handle) noexcept
 Construct a socket from a native socket handle.
 Socket (AddressFamily domain, ProtocolType type, std::error_code &errorCode)
 Construct a new open socket.
 Socket (AddressFamily domain, ProtocolType type)
 Construct a new open socket, throwing an exception on failure.
 operator bool () const noexcept
 Check if this is a valid open (though not necessarily bound or connected) socket, i.e. if its handle is not equal to INVALID_SOCKET.
void close (std::error_code &errorCode) noexcept
 Close the socket and set its handle to INVALID_SOCKET.
void close () noexcept
 Close the socket and set its handle to INVALID_SOCKET, ignoring failure.
SOCKET release () noexcept
 Relinquish ownership of the socket's native handle.
void open (AddressFamily domain, ProtocolType type, std::error_code &errorCode)
 Replace this socket with a newly created open socket.
void open (AddressFamily domain, ProtocolType type)
 Replace this socket with a newly created open socket, throwing an exception on failure.
void setBlockingMode (BlockingMode newMode, std::error_code &errorCode)
 Set the blocking mode of the socket.
void setBlockingMode (BlockingMode newMode)
 Set the blocking mode of the socket, throwing an exception on failure.
void setReceiveTimeout (Duration newTimeout, std::error_code &errorCode)
 Set the receive timeout of the socket.
void setReceiveTimeout (Duration newTimeout)
 Set the receive timeout of the socket, throwing an exception on failure.
void setSendTimeout (Duration newTimeout, std::error_code &errorCode)
 Set the send timeout of the socket.
void setSendTimeout (Duration newTimeout)
 Set the send timeout of the socket, throwing an exception on failure.
void connect (const Endpoint &endpoint, std::error_code &errorCode)
 Initiate a connection on the socket.
void connect (const Endpoint &endpoint)
 Initiate a connection on the socket, throwing an exception on failure.
void bind (const Endpoint &endpoint, std::error_code &errorCode)
 Assign an address to the socket.
void bind (const Endpoint &endpoint)
 Assign an address to the socket, throwing an exception on failure.
void shutdown (ShutdownType how, std::error_code &errorCode)
 Shut down the connection on the socket in the specified direction(s), disallowing further receptions and/or transmissions.
void shutdown (ShutdownType how)
 Shut down the connection on the socket in the specified direction(s), disallowing further receptions and/or transmissions, throwing an exception on failure.
Optional< Endpoint > getLocalEndpoint (std::error_code &errorCode) const
 Get the local endpoint address and port of the socket using getsockname().
Endpoint getLocalEndpoint () const
 Get the local endpoint address and port of the socket using getsockname(), throwing an exception on failure.
Optional< Endpoint > getRemoteEndpoint (std::error_code &errorCode) const
 Get the remote endpoint address and port of the socket using getpeername().
Endpoint getRemoteEndpoint () const
 Get the remote endpoint address and port of the socket using getpeername(), throwing an exception on failure.
void listen (size_t listenQueueBacklogSize, std::error_code &errorCode)
 Start accepting incoming TCP connections on the port that the socket is currently bound to, from the remote address(es) that were specified through a previous call to bind().
void listen (size_t listenQueueBacklogSize)
 Start accepting incoming TCP connections on the port that the socket is currently bound to, from the remote address(es) that were specified through a previous call to bind(), throwing an exception on failure.
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 a call to listen().
Optional< Socket > accept ()
 Accept a single incoming TCP connection from the socket's listen queue that was previously set up by a call to listen(), throwing an exception on failure.
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 through a previous call to bind() or connect().
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 through a previous call to bind() or connect(), throwing an exception on failure.
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 through a previous call to bind() or connect(), using the default recv() flags.
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 through a previous call to bind() or connect(), using the default recv() flags, throwing an exception on failure.
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, received from the remote address(es) specified through a previous call to bind() or connect().
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, received from the remote address(es) specified through a previous call to bind() or connect(), throwing an exception on failure.
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, received from the remote address(es) specified through a previous call to bind() or connect(), using the default recvfrom() flags.
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, received from the remote address(es) specified through a previous call to bind() or connect(), using the default recvfrom() flags, throwing an exception on failure.
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 through a previous call to connect().
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 through a previous call to connect(), throwing an exception on failure.
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 through a previous call to connect(), using the default send() flags.
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 through a previous call to connect(), using the default send() flags, throwing an exception on failure.
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< 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, throwing an exception on failure.
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, using the default sendto() flags.
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, using the default sendto() flags, throwing an exception on failure.
void awaitReadable (Duration timeout, std::error_code &errorCode)
 Wait for the socket to have incoming data available to be read.
void awaitReadable (Duration timeout)
 Wait for the socket to have incoming data available to be read, throwing an exception on failure or timeout.
void awaitWritable (Duration timeout, std::error_code &errorCode)
 Wait for the socket to become ready to write outgoing data to.
void awaitWritable (Duration timeout)
 Wait for the socket to become ready to write outgoing data to, throwing an exception on failure or timeout.
SOCKET get () const noexcept
 Get the native handle of the socket.

Static Public Attributes

static constexpr size_t MAX_LISTEN_QUEUE_BACKLOG_SIZE = SOMAXCONN
 Maximum listenQueueBacklogSize value that can be passed to listen().

Detailed Description

Generic socket handle/file descriptor for receiving and transmitting data across the network.

Constructor & Destructor Documentation

◆ Socket() [1/4]

grem::networking::Socket::Socket ( )
inlinenoexcept

Construct a closed socket with an invalid socket handle.

◆ Socket() [2/4]

grem::networking::Socket::Socket ( SOCKET handle)
inlineexplicitnoexcept

Construct a socket from a native socket handle.

Parameters
handlenative socket handle for this socket to adopt. Must either be a valid socket handle or be equal to INVALID_SOCKET.

◆ Socket() [3/4]

grem::networking::Socket::Socket ( AddressFamily domain,
ProtocolType type,
std::error_code & errorCode )
inline

Construct a new open socket.

Parameters
domainaddress family of the communication domain to open the socket for.
typecommunication semantics of the socket.
errorCodeerror code that is filled in on failure to create the socket, or cleared on success.

◆ Socket() [4/4]

grem::networking::Socket::Socket ( AddressFamily domain,
ProtocolType type )
inline

Construct a new open socket, throwing an exception on failure.

Parameters
domainaddress family of the communication domain to open the socket for.
typecommunication semantics of the socket.
Exceptions
networking::Erroron failure to create the socket.
std::bad_allocon allocation failure.

Member Function Documentation

◆ operator bool()

grem::networking::Socket::operator bool ( ) const
inlineexplicitnoexcept

Check if this is a valid open (though not necessarily bound or connected) socket, i.e. if its handle is not equal to INVALID_SOCKET.

Returns
get() != INVALID_SOCKET.

◆ close() [1/2]

void grem::networking::Socket::close ( std::error_code & errorCode)
noexcept

Close the socket and set its handle to INVALID_SOCKET.

Parameters
errorCodeerror code that is filled in on failure to close the socket, or cleared on success.

◆ close() [2/2]

void grem::networking::Socket::close ( )
inlinenoexcept

Close the socket and set its handle to INVALID_SOCKET, ignoring failure.

◆ release()

SOCKET grem::networking::Socket::release ( )
inlinenoexcept

Relinquish ownership of the socket's native handle.

This socket will have its handle reset to INVALID_SOCKET without destroying/closing the socket associated with the returned handle.

Returns
the socket handle that was released, or INVALID_SOCKET if the socket was already closed.
Warning
After calling this function, the associated socket will no longer be closed automatically along with this object. It instead becomes the responsibility of the caller to ensure that the socket is properly cleaned up. If the intent is to reset the handle to INVALID_SOCKET while closing the associated socket in the process, use close() instead.
See also
close()
get()

◆ open() [1/2]

void grem::networking::Socket::open ( AddressFamily domain,
ProtocolType type,
std::error_code & errorCode )

Replace this socket with a newly created open socket.

Parameters
domainaddress family of the communication domain to open the socket for.
typecommunication semantics of the socket.
errorCodeerror code that is filled in on failure to close the old socket or create the new socket, or cleared on success.
Note
If the socket is already open, it will be closed before creating the new socket. To avoid losing the old socket on failure, create a new Socket object and move-assign it to this one instead of using this function.

◆ open() [2/2]

void grem::networking::Socket::open ( AddressFamily domain,
ProtocolType type )
inline

Replace this socket with a newly created open socket, throwing an exception on failure.

Parameters
domainaddress family of the communication domain to open the socket for.
typecommunication semantics of the socket.
Exceptions
networking::Erroron failure to close the old socket or create the new socket.
std::bad_allocon allocation failure.
Note
If the socket is already open, it will be closed before creating the new socket. To avoid losing the old socket on failure, create a new Socket object and move-assign it to this one instead of using this function.

◆ setBlockingMode() [1/2]

void grem::networking::Socket::setBlockingMode ( BlockingMode newMode,
std::error_code & errorCode )

Set the blocking mode of the socket.

Parameters
newModenew blocking mode to set, or BlockingMode::UNSPECIFIED to leave the blocking mode unchanged.
errorCodeerror code that is filled in on failure to set the blocking mode, or cleared on success.
Note
This function requires the socket to be in an open state (though not necessarily bound or connected).

◆ setBlockingMode() [2/2]

void grem::networking::Socket::setBlockingMode ( BlockingMode newMode)
inline

Set the blocking mode of the socket, throwing an exception on failure.

Parameters
newModenew blocking mode to set, or BlockingMode::UNSPECIFIED to leave the blocking mode unchanged.
Exceptions
networking::Erroron failure to set the blocking mode.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in an open state (though not necessarily bound or connected).

◆ setReceiveTimeout() [1/2]

void grem::networking::Socket::setReceiveTimeout ( Duration newTimeout,
std::error_code & errorCode )

Set the receive timeout of the socket.

Parameters
newTimeoutnew receive timeout to set.
errorCodeerror code that is filled in on failure to set the receive timeout, or cleared on success.
Note
This function requires the socket to be in an open state (though not necessarily bound or connected).
If the socket's blocking mode is BlockingMode::NON_BLOCKING, the receive timeout is not used.

◆ setReceiveTimeout() [2/2]

void grem::networking::Socket::setReceiveTimeout ( Duration newTimeout)
inline

Set the receive timeout of the socket, throwing an exception on failure.

Parameters
newTimeoutnew receive timeout to set.
Exceptions
networking::Erroron failure to set the receive timeout.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in an open state (though not necessarily bound or connected).
If the socket's blocking mode is BlockingMode::NON_BLOCKING, the receive timeout is not used.

◆ setSendTimeout() [1/2]

void grem::networking::Socket::setSendTimeout ( Duration newTimeout,
std::error_code & errorCode )

Set the send timeout of the socket.

Parameters
newTimeoutnew send timeout to set.
errorCodeerror code that is filled in on failure to set the send timeout, or cleared on success.
Note
This function requires the socket to be in an open state (though not necessarily bound or connected).
If the socket's blocking mode is BlockingMode::NON_BLOCKING, the send timeout is not used.

◆ setSendTimeout() [2/2]

void grem::networking::Socket::setSendTimeout ( Duration newTimeout)
inline

Set the send timeout of the socket, throwing an exception on failure.

Parameters
newTimeoutnew send timeout to set.
Exceptions
networking::Erroron failure to set the send timeout.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in an open state (though not necessarily bound or connected).
If the socket's blocking mode is BlockingMode::NON_BLOCKING, the send timeout is not used.

◆ connect() [1/2]

void grem::networking::Socket::connect ( const Endpoint & endpoint,
std::error_code & errorCode )

Initiate a connection on the socket.

If the socket is of type ProtocolType::UDP, this sets the address to which packets are sent by default.

If the socket is of type ProtocolType::TCP, it attempts to establish a TCP connection to the remote socket at the specified address.

Parameters
endpointremote address, port, etc. to connect to.
errorCodeerror code that is filled in on failure to connect the socket, or cleared on success.
Note
This function requires the socket to be in an open state.

◆ connect() [2/2]

void grem::networking::Socket::connect ( const Endpoint & endpoint)
inline

Initiate a connection on the socket, throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this sets the address and port to which packets are sent by default.

If the socket is of type ProtocolType::TCP, it attempts to establish a TCP connection to the remote socket at the specified address and port.

Parameters
endpointremote address, port, etc. to connect to.
Exceptions
networking::Erroron failure to connect the socket.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in an open state.

◆ bind() [1/2]

void grem::networking::Socket::bind ( const Endpoint & endpoint,
std::error_code & errorCode )

Assign an address to the socket.

If the socket is of type ProtocolType::UDP, this binds the socket to the specified local port and sets the remote address(es) to receive packets from on that port.

If the socket is of type ProtocolType::TCP, this binds the socket to the specified local port and makes it possible to listen() for incoming connections from the specified remote address(es) on that port.

Parameters
endpointremote address(es) to receive from and local port to bind the socket to.
errorCodeerror code that is filled in on failure to bind the socket, or cleared on success.
Note
This function requires the socket to be in an open state.

◆ bind() [2/2]

void grem::networking::Socket::bind ( const Endpoint & endpoint)
inline

Assign an address to the socket, throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this binds the socket to the specified local port and sets the remote address(es) to receive packets from on that port.

If the socket is of type ProtocolType::TCP, this binds the socket to the specified local port and makes it possible to listen() for incoming connections from the specified remote address(es) on that port.

Parameters
endpointremote address(es) to receive from and local port to bind the socket to.
Exceptions
networking::Erroron failure to bind the socket.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in an open state.

◆ shutdown() [1/2]

void grem::networking::Socket::shutdown ( ShutdownType how,
std::error_code & errorCode )

Shut down the connection on the socket in the specified direction(s), disallowing further receptions and/or transmissions.

Parameters
howdirection(s) in which to shut down the connection.
errorCodeerror code that is filled in on failure to shut down the connection, or cleared on success.
Note
This function requires the socket to be in a connected state.

◆ shutdown() [2/2]

void grem::networking::Socket::shutdown ( ShutdownType how)
inline

Shut down the connection on the socket in the specified direction(s), disallowing further receptions and/or transmissions, throwing an exception on failure.

Parameters
howdirection(s) in which to shut down the connection.
Exceptions
networking::Erroron failure to shut down the connection.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in a connected state.

◆ getLocalEndpoint() [1/2]

Optional< Endpoint > grem::networking::Socket::getLocalEndpoint ( std::error_code & errorCode) const
nodiscard

Get the local endpoint address and port of the socket using getsockname().

Parameters
errorCodeerror code that is filled in on failure to get the local endpoint, or cleared on success.
Returns
the local endpoint, or an empty optional on failure.
Note
This function requires the socket to be in a connected state.

◆ getLocalEndpoint() [2/2]

Endpoint grem::networking::Socket::getLocalEndpoint ( ) const
inlinenodiscard

Get the local endpoint address and port of the socket using getsockname(), throwing an exception on failure.

Returns
the local endpoint.
Exceptions
networking::Erroron failure to get the local endpoint.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in a connected state.

◆ getRemoteEndpoint() [1/2]

Optional< Endpoint > grem::networking::Socket::getRemoteEndpoint ( std::error_code & errorCode) const
nodiscard

Get the remote endpoint address and port of the socket using getpeername().

Parameters
errorCodeerror code that is filled in on failure to get the remote endpoint, or cleared on success.
Returns
the remote endpoint, or an empty optional on failure.
Note
This function requires the socket to be in a connected state.

◆ getRemoteEndpoint() [2/2]

Endpoint grem::networking::Socket::getRemoteEndpoint ( ) const
inlinenodiscard

Get the remote endpoint address and port of the socket using getpeername(), throwing an exception on failure.

Returns
the remote endpoint.
Exceptions
networking::Erroron failure to get the remote endpoint.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in a connected state.

◆ listen() [1/2]

void grem::networking::Socket::listen ( size_t listenQueueBacklogSize,
std::error_code & errorCode )

Start accepting incoming TCP connections on the port that the socket is currently bound to, from the remote address(es) that were specified through a previous call to bind().

Parameters
listenQueueBacklogSizemaximum number of incoming connections to allow to be queued up between calls to accept(). The value will be clamped between 0 and Socket::MAX_LISTEN_QUEUE_BACKLOG_SIZE (inclusive). A value of 0 is interpreted as a shorthand for Socket::MAX_LISTEN_QUEUE_BACKLOG_SIZE.
errorCodeerror code that is filled in on failure to start listening, or cleared on success.
Note
This function requires the socket to be of type ProtocolType::TCP and be in a bound state.

◆ listen() [2/2]

void grem::networking::Socket::listen ( size_t listenQueueBacklogSize)
inline

Start accepting incoming TCP connections on the port that the socket is currently bound to, from the remote address(es) that were specified through a previous call to bind(), throwing an exception on failure.

Parameters
listenQueueBacklogSizemaximum number of incoming connections to allow to be queued up between calls to accept(). The value will be clamped between 0 and Socket::MAX_LISTEN_QUEUE_BACKLOG_SIZE (inclusive). A value of 0 is interpreted as a shorthand for Socket::MAX_LISTEN_QUEUE_BACKLOG_SIZE.
Exceptions
networking::Erroron failure to start listening.
std::bad_allocon allocation failure.
Note
This function requires the socket to be of type ProtocolType::TCP and be in a bound state.

◆ accept() [1/2]

Optional< Socket > grem::networking::Socket::accept ( std::error_code & errorCode)
nodiscard

Accept a single incoming TCP connection from the socket's listen queue that was previously set up by a call to listen().

Parameters
errorCodeerror code that is filled in on failure to accept a connection, or cleared on success.
Returns
the newly created TCP socket in a connected state, or an empty optional on failure or timeout.
Note
This function requires the socket to be of type ProtocolType::TCP and be in a listening state.
The send/receive timeout, blocking mode, and other parameters of the new socket are unspecified. They may for example be inherited from the accepting socket, or have default values, depending on the underlying sockets backend. To ensure consistent behavior, the relevant parameters should be explicitly set on the returned socket.

◆ accept() [2/2]

Optional< Socket > grem::networking::Socket::accept ( )
inlinenodiscard

Accept a single incoming TCP connection from the socket's listen queue that was previously set up by a call to listen(), throwing an exception on failure.

Returns
the newly created TCP socket in a connected state, or an empty optional on timeout.
Exceptions
networking::Erroron failure to accept a connection.
std::bad_allocon allocation failure.
Note
This function requires the socket to be of type ProtocolType::TCP and be in a listening state.
The send/receive timeout, blocking mode, and other parameters of the new socket are unspecified. They may for example be inherited from the accepting socket, or have default values, depending on the underlying sockets backend. To ensure consistent behavior, the relevant parameters should be explicitly set on the returned socket.

◆ receive() [1/4]

Optional< Span< byte > > grem::networking::Socket::receive ( Span< byte > buffer,
MessageFlags flags,
std::error_code & errorCode )
nodiscard

Get some data from the socket's incoming data queue, received from the remote address(es) specified through a previous call to bind() or connect().

If the socket is of type ProtocolType::UDP, this will consume one datagram from the incoming packet queue. If the full packet does not fit in the given buffer, the remaining data in the consumed packet is discarded.

If the socket is of type ProtocolType::TCP, this will consume available data from the incoming TCP stream, up to the requested number of bytes in the given buffer.

Parameters
bufferwritable span to read received data into. The size of the span determines the maximum number of bytes that are attempted to be read.
flagsbitmask of MSG_... flags to pass to recv(), or 0 for no flags.
errorCodeerror code that is filled in on failure to receive data, or cleared on success.
Returns
a subspan of the start of the given buffer containing the data that was successfully read, or an empty optional on failure or timeout.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ receive() [2/4]

Optional< Span< byte > > grem::networking::Socket::receive ( Span< byte > buffer,
MessageFlags flags )
inlinenodiscard

Get some data from the socket's incoming data queue, received from the remote address(es) specified through a previous call to bind() or connect(), throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this will consume one datagram from the incoming packet queue. If the full packet does not fit in the given buffer, the remaining data in the consumed packet is discarded.

If the socket is of type ProtocolType::TCP, this will consume available data from the incoming TCP stream, up to the requested number of bytes in the given buffer.

Parameters
bufferwritable span to read received data into. The size of the span determines the maximum number of bytes that are attempted to be read.
flagsbitmask of MSG_... flags to pass to recv(), or 0 for no flags.
Returns
a subspan of the start of the given buffer containing the data that was successfully read, or an empty optional on timeout.
Exceptions
networking::Erroron failure to receive data.
std::bad_allocon allocation failure.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ receive() [3/4]

GREM_ALWAYS_INLINE Optional< Span< byte > > grem::networking::Socket::receive ( Span< byte > buffer,
std::error_code & errorCode )
inlinenodiscard

Get some data from the socket's incoming data queue, received from the remote address(es) specified through a previous call to bind() or connect(), using the default recv() flags.

If the socket is of type ProtocolType::UDP, this will consume one datagram from the incoming packet queue. If the full packet does not fit in the given buffer, the remaining data in the consumed packet is discarded.

If the socket is of type ProtocolType::TCP, this will consume available data from the incoming TCP stream, up to the requested number of bytes in the given buffer.

Parameters
bufferwritable span to read received data into. The size of the span determines the maximum number of bytes that are attempted to be read.
errorCodeerror code that is filled in on failure to receive data, or cleared on success.
Returns
a subspan of the start of the given buffer containing the data that was successfully read, or an empty optional on failure or timeout.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ receive() [4/4]

GREM_ALWAYS_INLINE Optional< Span< byte > > grem::networking::Socket::receive ( Span< byte > buffer)
inlinenodiscard

Get some data from the socket's incoming data queue, received from the remote address(es) specified through a previous call to bind() or connect(), using the default recv() flags, throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this will consume one datagram from the incoming packet queue. If the full packet does not fit in the given buffer, the remaining data in the consumed packet is discarded.

If the socket is of type ProtocolType::TCP, this will consume available data from the incoming TCP stream, up to the requested number of bytes in the given buffer.

Parameters
bufferwritable span to read received data into. The size of the span determines the maximum number of bytes that are attempted to be read.
Returns
a subspan of the start of the given buffer containing the data that was successfully read, or an empty optional on timeout.
Exceptions
networking::Erroron failure to receive data.
std::bad_allocon allocation failure.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ receiveFrom() [1/4]

Optional< Pair< Span< byte >, Endpoint > > grem::networking::Socket::receiveFrom ( Span< byte > buffer,
MessageFlags flags,
std::error_code & errorCode )
nodiscard

Get some data and its associated source address from the socket's incoming data queue, received from the remote address(es) specified through a previous call to bind() or connect().

If the socket is of type ProtocolType::UDP, this will consume one datagram from the incoming packet queue. If the full packet does not fit in the given buffer, the remaining data in the consumed packet is discarded.

If the socket is of type ProtocolType::TCP, this will consume available data from the incoming TCP stream, up to the requested number of bytes in the given buffer.

Parameters
bufferwritable span to read received data into. The size of the span determines the maximum number of bytes that are attempted to be read.
flagsbitmask of MSG_... flags to pass to recvfrom(), or 0 for no flags.
errorCodeerror code that is filled in on failure to receive data, or cleared on success.
Returns
an empty optional on failure or timeout, or a pair of:
  • a subspan of the start of the given buffer containing the data that was successfully read.
  • the source address and port that the data was received from.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ receiveFrom() [2/4]

Optional< Pair< Span< byte >, Endpoint > > grem::networking::Socket::receiveFrom ( Span< byte > buffer,
MessageFlags flags )
inlinenodiscard

Get some data and its associated source address from the socket's incoming data queue, received from the remote address(es) specified through a previous call to bind() or connect(), throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this will consume one datagram from the incoming packet queue. If the full packet does not fit in the given buffer, the remaining data in the consumed packet is discarded.

If the socket is of type ProtocolType::TCP, this will consume available data from the incoming TCP stream, up to the requested number of bytes in the given buffer.

Parameters
bufferwritable span to read received data into. The size of the span determines the maximum number of bytes that are attempted to be read.
flagsbitmask of MSG_... flags to pass to recvfrom(), or 0 for no flags.
Returns
an empty optional on timeout, or a pair of:
  • a subspan of the start of the given buffer containing the data that was successfully read.
  • the source address and port that the data was received from.
Exceptions
networking::Erroron failure to receive data.
std::bad_allocon allocation failure.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ receiveFrom() [3/4]

GREM_ALWAYS_INLINE Optional< Pair< Span< byte >, Endpoint > > grem::networking::Socket::receiveFrom ( Span< byte > buffer,
std::error_code & errorCode )
inlinenodiscard

Get some data and its associated source address from the socket's incoming data queue, received from the remote address(es) specified through a previous call to bind() or connect(), using the default recvfrom() flags.

If the socket is of type ProtocolType::UDP, this will consume one datagram from the incoming packet queue. If the full packet does not fit in the given buffer, the remaining data in the consumed packet is discarded.

If the socket is of type ProtocolType::TCP, this will consume available data from the incoming TCP stream, up to the requested number of bytes in the given buffer.

Parameters
bufferwritable span to read received data into. The size of the span determines the maximum number of bytes that are attempted to be read.
errorCodeerror code that is filled in on failure to receive data, or cleared on success.
Returns
an empty optional on failure or timeout, or a pair of:
  • a subspan of the start of the given buffer containing the data that was successfully read.
  • the source address and port that the data was received from.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ receiveFrom() [4/4]

GREM_ALWAYS_INLINE Optional< Pair< Span< byte >, Endpoint > > grem::networking::Socket::receiveFrom ( Span< byte > buffer)
inlinenodiscard

Get some data and its associated source address from the socket's incoming data queue, received from the remote address(es) specified through a previous call to bind() or connect(), using the default recvfrom() flags, throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this will consume one datagram from the incoming packet queue. If the full packet does not fit in the given buffer, the remaining data in the consumed packet is discarded.

If the socket is of type ProtocolType::TCP, this will consume available data from the incoming TCP stream, up to the requested number of bytes in the given buffer.

Parameters
bufferwritable span to read received data into. The size of the span determines the maximum number of bytes that are attempted to be read.
Returns
an empty optional on timeout, or a pair of:
  • a subspan of the start of the given buffer containing the data that was successfully read.
  • the source address and port that the data was received from.
Exceptions
networking::Erroron failure to receive data.
std::bad_allocon allocation failure.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ send() [1/4]

Optional< size_t > grem::networking::Socket::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 through a previous call to connect().

If the socket is of type ProtocolType::UDP, this will produce one datagram for the outgoing packet queue.

If the socket is of type ProtocolType::TCP, this will append data to the outgoing TCP stream.

Parameters
bytesdata to write.
flagsbitmask of MSG_... flags to pass to send(), or 0 for no flags.
errorCodeerror code that is filled in on failure to send data, or cleared on success.
Returns
the number of bytes that were successfully written, or an empty optional on failure or timeout.
Note
This function requires the socket to be in a connected state.

◆ send() [2/4]

Optional< size_t > grem::networking::Socket::send ( Span< const byte > bytes,
MessageFlags flags )
inlinenodiscard

Write some data to the socket's outgoing data queue, to be sent to the remote address specified through a previous call to connect(), throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this will produce one datagram for the outgoing packet queue.

If the socket is of type ProtocolType::TCP, this will append data to the outgoing TCP stream.

Parameters
bytesdata to write.
flagsbitmask of MSG_... flags to pass to send(), or 0 for no flags.
Returns
the number of bytes that were successfully written, or an empty optional on timeout.
Exceptions
networking::Erroron failure to send data.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in a connected state.

◆ send() [3/4]

GREM_ALWAYS_INLINE Optional< size_t > grem::networking::Socket::send ( Span< const byte > bytes,
std::error_code & errorCode )
inline

Write some data to the socket's outgoing data queue, to be sent to the remote address specified through a previous call to connect(), using the default send() flags.

If the socket is of type ProtocolType::UDP, this will produce one datagram for the outgoing packet queue.

If the socket is of type ProtocolType::TCP, this will append data to the outgoing TCP stream.

Parameters
bytesdata to write.
errorCodeerror code that is filled in on failure to send data, or cleared on success.
Returns
the number of bytes that were successfully written, or an empty optional on failure or timeout.
Note
This function requires the socket to be in a connected state.

◆ send() [4/4]

GREM_ALWAYS_INLINE Optional< size_t > grem::networking::Socket::send ( Span< const byte > bytes)
inlinenodiscard

Write some data to the socket's outgoing data queue, to be sent to the remote address specified through a previous call to connect(), using the default send() flags, throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this will produce one datagram for the outgoing packet queue.

If the socket is of type ProtocolType::TCP, this will append data to the outgoing TCP stream.

Parameters
bytesdata to write.
Returns
the number of bytes that were successfully written, or an empty optional on timeout.
Exceptions
networking::Erroron failure to send data.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in a connected state.

◆ sendTo() [1/4]

Optional< size_t > grem::networking::Socket::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.

If the socket is of type ProtocolType::UDP, this will produce one datagram for the outgoing packet queue.

If the socket is of type ProtocolType::TCP, this will append data to the outgoing TCP stream.

Parameters
endpointdestination address to send the data to.
bytesdata to write.
flagsbitmask of MSG_... flags to pass to sendto(), or 0 for no flags.
errorCodeerror code that is filled in on failure to send data, or cleared on success.
Returns
the number of bytes that were successfully written, or an empty optional on failure or timeout.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ sendTo() [2/4]

Optional< size_t > grem::networking::Socket::sendTo ( const Endpoint & endpoint,
Span< const byte > bytes,
MessageFlags flags )
inlinenodiscard

Write some data to the socket's outgoing data queue, to be sent to a given remote address, throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this will produce one datagram for the outgoing packet queue.

If the socket is of type ProtocolType::TCP, this will append data to the outgoing TCP stream.

Parameters
endpointdestination address to send the data to.
bytesdata to write.
flagsbitmask of MSG_... flags to pass to sendto(), or 0 for no flags.
Returns
the number of bytes that were successfully written, or an empty optional on timeout.
Exceptions
networking::Erroron failure to send data.
std::bad_allocon allocation failure.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ sendTo() [3/4]

GREM_ALWAYS_INLINE Optional< size_t > grem::networking::Socket::sendTo ( const Endpoint & endpoint,
Span< const byte > bytes,
std::error_code & errorCode )
inline

Write some data to the socket's outgoing data queue, to be sent to a given remote address, using the default sendto() flags.

If the socket is of type ProtocolType::UDP, this will produce one datagram for the outgoing packet queue.

If the socket is of type ProtocolType::TCP, this will append data to the outgoing TCP stream.

Parameters
endpointdestination address to send the data to.
bytesdata to write.
errorCodeerror code that is filled in on failure to send data, or cleared on success.
Returns
the number of bytes that were successfully written, or an empty optional on failure or timeout.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ sendTo() [4/4]

GREM_ALWAYS_INLINE Optional< size_t > grem::networking::Socket::sendTo ( const Endpoint & endpoint,
Span< const byte > bytes )
inlinenodiscard

Write some data to the socket's outgoing data queue, to be sent to a given remote address, using the default sendto() flags, throwing an exception on failure.

If the socket is of type ProtocolType::UDP, this will produce one datagram for the outgoing packet queue.

If the socket is of type ProtocolType::TCP, this will append data to the outgoing TCP stream.

Parameters
endpointdestination address to send the data to.
bytesdata to write.
Returns
the number of bytes that were successfully written, or an empty optional on timeout.
Exceptions
networking::Erroron failure to send data.
std::bad_allocon allocation failure.
Note
If the socket is of type ProtocolType::UDP, this function requires the socket to be in a bound state. If the socket is of type ProtocolType::TCP, it is required to be in a connected state.

◆ awaitReadable() [1/2]

void grem::networking::Socket::awaitReadable ( Duration timeout,
std::error_code & errorCode )

Wait for the socket to have incoming data available to be read.

Parameters
timeoutmaximum duration of time to wait for. Must be non-negative.
errorCodeerror code that is filled in on failure, or cleared on success.
Note
This function requires the socket to be in a bound state.

◆ awaitReadable() [2/2]

void grem::networking::Socket::awaitReadable ( Duration timeout)
inline

Wait for the socket to have incoming data available to be read, throwing an exception on failure or timeout.

Parameters
timeoutmaximum duration of time to wait for. Must be non-negative.
Exceptions
networking::Erroron failure to await, or timeout.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in a bound state.

◆ awaitWritable() [1/2]

void grem::networking::Socket::awaitWritable ( Duration timeout,
std::error_code & errorCode )

Wait for the socket to become ready to write outgoing data to.

Parameters
timeoutmaximum duration of time to wait for. Must be non-negative.
errorCodeerror code that is filled in on failure, or cleared on success.
Note
This function requires the socket to be in a bound state.

◆ awaitWritable() [2/2]

void grem::networking::Socket::awaitWritable ( Duration timeout)
inline

Wait for the socket to become ready to write outgoing data to, throwing an exception on failure or timeout.

Parameters
timeoutmaximum duration of time to wait for. Must be non-negative.
Exceptions
networking::Erroron failure to await, or timeout.
std::bad_allocon allocation failure.
Note
This function requires the socket to be in a bound state.

◆ get()

SOCKET grem::networking::Socket::get ( ) const
inlinenodiscardnoexcept

Get the native handle of the socket.

Returns
the native socket handle, or INVALID_SOCKET if the socket is in a closed state.
Note
This function does NOT relinquish ownership of the socket handle. To acquire ownership of the handle, use release() instead.

Member Data Documentation

◆ MAX_LISTEN_QUEUE_BACKLOG_SIZE

size_t grem::networking::Socket::MAX_LISTEN_QUEUE_BACKLOG_SIZE = SOMAXCONN
staticconstexpr

Maximum listenQueueBacklogSize value that can be passed to listen().


The documentation for this class was generated from the following file: