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

Connectionless socket for receiving and transmitting unordered, unreliable datagrams across the network over ProtocolType::UDP. More...

#include <GREM/networking/Socket.hpp>

Inheritance diagram for grem::networking::UDPSocket:

Public Member Functions

 UDPSocket () noexcept
 Construct a closed UDP socket with an invalid socket handle.
 UDPSocket (SOCKET handle) noexcept
 Construct a UDP socket from a native socket handle.
 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 receive packets from on that port.
 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 receive packets from on that port, throwing an exception on failure.
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, and set the remote address(es) to receive packets from on that port.
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, and set the remote address(es) to receive packets from on that port, 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< 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.
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 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

Connectionless socket for receiving and transmitting unordered, unreliable datagrams across the network over ProtocolType::UDP.

Constructor & Destructor Documentation

◆ UDPSocket() [1/4]

grem::networking::UDPSocket::UDPSocket ( )
inlinenoexcept

Construct a closed UDP socket with an invalid socket handle.

◆ UDPSocket() [2/4]

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

Construct a UDP 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::UDP (SOCK_DGRAM) or equal to INVALID_SOCKET.

◆ UDPSocket() [3/4]

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

Construct a new open UDP socket bound to a specific local port, and set the remote address(es) to receive packets from on that port.

Parameters
modeblocking mode of the new socket.
endpointremote address(es) to receive from and local port to bind the socket 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/bind the socket, or cleared on success.
optionssocket options, see UDPSocketOptions.

◆ UDPSocket() [4/4]

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

Construct a new open UDP socket bound to a specific local port, and set the remote address(es) to receive packets from on that port, throwing an exception on failure.

Parameters
modeblocking mode of the new socket.
endpointremote address(es) to receive from and local port to bind the socket to. The address family of the endpoint determines the communication domain to open the socket for.
optionssocket options, see UDPSocketOptions.
Exceptions
networking::Erroron failure to create/bind the socket.
std::bad_allocon allocation failure.

Member Function Documentation

◆ bind() [1/2]

void grem::networking::UDPSocket::bind ( BlockingMode mode,
const Endpoint & endpoint,
std::error_code & errorCode,
const UDPSocketOptions & options = {} )
inline

Replace this socket with a newly created open socket bound to a specific local port, and set the remote address(es) to receive packets from on that port.

Parameters
modeblocking mode of the new socket.
endpointremote address(es) to receive from and local port to bind the socket 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/bind the new socket, or cleared on success.
optionssocket options, see UDPSocketOptions.
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.

◆ bind() [2/2]

void grem::networking::UDPSocket::bind ( BlockingMode mode,
const Endpoint & endpoint,
const UDPSocketOptions & options = {} )
inline

Replace this socket with a newly created open socket bound to a specific local port, and set the remote address(es) to receive packets from on that port, throwing an exception on failure.

Parameters
modeblocking mode of the new socket.
endpointremote address(es) to receive from and local port to bind the socket to. The address family of the endpoint determines the communication domain to open the socket for.
optionssocket options, see UDPSocketOptions.
Exceptions
networking::Erroron failure to close the old socket or create/bind 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.

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

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

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

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