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

Connection-oriented socket for receiving and transmitting ordered, reliable streams of data across the network over ProtocolType::TCP. More...

#include <GREM/networking/Socket.hpp>

Inheritance diagram for grem::networking::TCPSocket:

Public Member Functions

 TCPSocket () noexcept
 Construct a closed TCP socket with an invalid socket handle.
 TCPSocket (SOCKET handle) noexcept
 Construct a TCP socket from a native socket handle.
 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 address and port.
 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 address and port, throwing an exception on failure.
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 socket at the specified address and port.
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 socket at the specified address and port, throwing an exception on failure.
void disconnect (std::error_code &errorCode)
 Shut down the connection on the socket in the outgoing direction, disallowing further transmissions, and initiate a graceful shutdown of the TCP connection.
void disconnect ()
 Shut down the connection on the socket in the outgoing direction, disallowing further transmissions, and initiate a graceful shutdown of the TCP connection, throwing an exception on failure.
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 through a previous call to connect().
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 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)
 Append some data to the socket's outgoing TCP stream, 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)
 Append some data to the socket's outgoing TCP stream, 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)
 Append some data to the socket's outgoing TCP stream, to be sent to a given remote address.
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, throwing an exception on failure.
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, using the default sendto() flags.
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, using the default sendto() flags, 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 get () const noexcept
 Get the native handle of the socket.
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.
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.
SOCKET release () noexcept
 Relinquish ownership of the socket's native handle.
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.

Detailed Description

Connection-oriented socket for receiving and transmitting ordered, reliable streams of data across the network over ProtocolType::TCP.

Constructor & Destructor Documentation

◆ TCPSocket() [1/4]

grem::networking::TCPSocket::TCPSocket ( )
inlinenoexcept

Construct a closed TCP socket with an invalid socket handle.

◆ TCPSocket() [2/4]

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

Construct a TCP socket from a native socket handle.

Parameters
handlenative socket handle for this socket to adopt. Must either be a valid socket handle of type ProtocolType::TCP (SOCK_STREAM) or be equal to INVALID_SOCKET.

◆ TCPSocket() [3/4]

grem::networking::TCPSocket::TCPSocket ( BlockingMode mode,
const Endpoint & endpoint,
std::error_code & errorCode,
const TCPSocketOptions & options = {} )
inline

Construct a new open TCP socket and establish a TCP connection to the remote socket at the specified address and port.

Parameters
modeblocking mode of the new socket.
endpointremote address, port, etc. to connect to. The address family of the endpoint determines the communication domain to open the socket for.
errorCodeerror code that is filled in on failure to create/connect the socket, or cleared on success.
optionssocket options, see TCPSocketOptions.

◆ TCPSocket() [4/4]

grem::networking::TCPSocket::TCPSocket ( BlockingMode mode,
const Endpoint & endpoint,
const TCPSocketOptions & options = {} )
inline

Construct a new open TCP socket and establish a TCP connection to the remote socket at the specified address and port, throwing an exception on failure.

Parameters
modeblocking mode of the new socket.
endpointremote address, port, etc. to connect to. The address family of the endpoint determines the communication domain to open the socket for.
optionssocket options, see TCPSocketOptions.
Exceptions
networking::Erroron failure to create/connect the socket.
std::bad_allocon allocation failure.

Member Function Documentation

◆ connect() [1/2]

void grem::networking::TCPSocket::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 socket at the specified address and port.

Parameters
modeblocking mode of the new socket.
endpointremote address, port, etc. to connect to. The address family of the endpoint determines the communication domain to open the socket for.
errorCodeerror code that is filled in on failure to close the old socket or create/connect the new socket, or cleared on success.
optionssocket options, see TCPSocketOptions.
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.

◆ connect() [2/2]

void grem::networking::TCPSocket::connect ( BlockingMode mode,
const Endpoint & endpoint,
const TCPSocketOptions & options = {} )
inline

Replace this socket with a newly created open socket and establish a TCP connection to the remote socket at the specified address and port, throwing an exception on failure.

Parameters
modeblocking mode of the new socket.
endpointremote address, port, etc. to connect to. The address family of the endpoint determines the communication domain to open the socket for.
optionssocket options, see TCPSocketOptions.
Exceptions
networking::Erroron failure to close the old socket or create/connect 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.

◆ disconnect() [1/2]

void grem::networking::TCPSocket::disconnect ( std::error_code & errorCode)
inline

Shut down the connection on the socket in the outgoing direction, disallowing further transmissions, and initiate a graceful shutdown of the TCP connection.

Parameters
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.

◆ disconnect() [2/2]

void grem::networking::TCPSocket::disconnect ( )
inline

Shut down the connection on the socket in the outgoing direction, disallowing further transmissions, and initiate a graceful shutdown of the TCP connection, throwing an exception on failure.

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.

◆ send() [1/4]

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

Appends some data to the socket's outgoing TCP stream, to be sent to the remote address specified through a previous call to connect().

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.
Unlike the regular Socket::send(), this TCPSocket version ensures that all of the specified data is written before returning, unless there is an error.

◆ send() [2/4]

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

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

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.
Unlike the regular Socket::send(), this TCPSocket version ensures that all of the specified data is written before returning, unless there is an error.

◆ send() [3/4]

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

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

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.
Unlike the regular Socket::send(), this TCPSocket version ensures that all of the specified data is written before returning, unless there is an error.

◆ send() [4/4]

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

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

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.
Unlike the regular Socket::send(), this TCPSocket version ensures that all of the specified data is written before returning, unless there is an error.

◆ sendTo() [1/4]

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

Append some data to the socket's outgoing TCP stream, to be sent to a given remote address.

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
This function requires the socket to be in a connected state.
Unlike the regular Socket::sendTo(), this TCPSocket version ensures that all of the specified data is written before returning, unless there is an error.

◆ sendTo() [2/4]

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

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

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
This function requires the socket to be in a connected state.
Unlike the regular Socket::sendTo(), this TCPSocket version ensures that all of the specified data is written before returning, unless there is an error.

◆ sendTo() [3/4]

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

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

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
This function requires the socket to be in a connected state.
Unlike the regular Socket::sendTo(), this TCPSocket version ensures that all of the specified data is written before returning, unless there is an error.

◆ sendTo() [4/4]

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

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

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
This function requires the socket to be in a connected state.
Unlike the regular Socket::sendTo(), this TCPSocket version ensures that all of the specified data is written before returning, unless there is an error.

◆ 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.

◆ 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.

◆ 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]

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]

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.

◆ 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.

◆ 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()

◆ 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]

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]

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]

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.

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