xentara-utils v2.0.4
The Xentara Utility Library
|
A socket. More...
#include <xentara/utils/network/Socket.hpp>
Public Types | |
enum class | Direction { Read , Write , ReadWrite } |
Directions for sutdown(). More... | |
Public Member Functions | |
Socket () noexcept=default | |
Default constructor. | |
template<tools::OneOf< NonBlockingTag, Protocol >... Options> | |
Socket (int addressFamily, int socketType, Options... options) | |
Constructor that creates a socket. | |
Socket (posix::File socketFileDescriptor) noexcept | |
Conversion from a Posix file descriptor. | |
Socket (windows::Socket socket) noexcept | |
Conversion from a native socket. | |
operator bool () const noexcept | |
Checks whether the object contains a valid socket. | |
auto | addressFamily () const noexcept -> int |
Gets the address family of the socket, also called the socket domain. | |
auto | socketType () const noexcept -> int |
Gets the socket type. | |
auto | protocol () const noexcept -> int |
Gets the protocol of the socket. | |
template<typename Value > | |
auto | option (int level, int optionName) const -> Value |
Gets a socket option. | |
template<typename Value > | |
auto | setOption (int level, int optionName, const Value &value) -> void |
Sets a socket option. | |
template<typename Value > | |
auto | getOption (int level, int optionName, std::nothrow_t) const noexcept -> xentara::utils::eh::expected< Value, std::error_code > |
Gets a socket option. | |
template<typename Value > | |
auto | setOption (int level, int optionName, const Value &value, std::nothrow_t) noexcept -> std::error_code |
Sets a socket option without throwing exceptions. | |
auto | makeNonBlocking (bool nonBlocking=true) -> void |
Sets the non-blocking option of the socket. | |
auto | connect (const Address &address) -> bool |
Connects to an address. | |
auto | bind (PortNumber portNumber) -> void |
Binds the socket to a port number on all local interfaces. | |
auto | bind (const Address &address) -> void |
Binds the socket to a specific address. | |
template<tools::OneOf< Backlog >... Options> | |
auto | listen (Options... options) -> void |
Puts the socket into listening mode. | |
template<tools::OneOf< NonBlockingTag >... Options> | |
auto | accept (Options... options) const -> Socket |
Accepts a connection from a socket in listening mode. | |
auto | localAddress () const -> Address |
Gets the local address of a bound or connected socket. | |
auto | remoteAddress () const -> Address |
Gets the address that the socket is connected to. | |
auto | poll (short events, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) const -> short |
Waits for events on the socket. | |
auto | poll (short events, const io::StopSource &stopSource, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) const -> short |
Waits for events on the socket using a stop source. | |
auto | waitForEvent (short events, const io::StopSource &stopSource, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) const -> short |
Waits for an event on the socket, using a timeout. | |
auto | performOverlappedIo (functional::function_ref< auto(SOCKET, OVERLAPPED *) → BOOL > operation, HANDLE event, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) -> std::optional< DWORD > |
Performs an overlapped I/O operation. | |
auto | performOverlappedIo (functional::function_ref< auto(SOCKET, OVERLAPPED *) → BOOL > operation, std::span< HANDLE > waitObjects, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) -> std::optional< windows::OverlappedResult > |
Performs an overlapped I/O operation. | |
auto | send (const void *data, std::size_t size, int flags=0) const -> std::size_t |
Sends data to the socket. | |
auto | receive (void *buffer, std::size_t size, int flags=0) const -> std::optional< std::size_t > |
Receive data from the socket. | |
auto | sendTo (const void *data, std::size_t size, const Address &destination, int flags=0) const -> std::size_t |
Sends data to a specific destination over the socket. | |
auto | receiveFrom (void *buffer, std::size_t size, int flags=0) const -> std::pair< std::optional< std::size_t >, Address > |
Receive data from the socket and returns the sender address. | |
auto | error () const -> std::error_code |
Returns the error status, and clears it. | |
template<std::derived_from< std::system_error > Exception, typename... ExceptionArguments> | |
auto | throwError (ExceptionArguments &&...exceptionArguments) -> void |
Throws an exception if the socket contains an error. | |
auto | shutdown (Direction directions=Direction::ReadWrite) -> void |
Shuts down the socket. | |
auto | close () noexcept -> void |
Closes the socket, if one is open. | |
auto | native () const noexcept -> int |
Gets the underlying Posix file descriptor for the socket. | |
auto | native () const noexcept -> SOCKET |
Gets the underlying Windows Sockets 2 SOCKET. | |
auto | release () noexcept -> int |
Removes the the socket and passes ownership to the caller. | |
auto | release () noexcept -> SOCKET |
Removes the socket and passes ownership to the caller. | |
auto | description () const noexcept -> std::string_view |
Gets a description for the socket. | |
Static Public Member Functions | |
static auto | poll (std::span< pollfd > sockets, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) -> bool |
Waits for events on one or more sockets. | |
static auto | poll (std::span< pollfd > sockets, const io::StopSource &stopSource, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) -> bool |
Waits for events on one or more sockets using a stop source. | |
template<tools::OneOf< NonBlockingTag, Protocol >... Options> | |
static auto | compatibleSocket (int socketType, const Address &address, Options... options) -> Socket |
Creates a socket that is compatible with an address. | |
template<tools::OneOf< NonBlockingTag, Protocol, Timeout, WithStopSource >... Options> | |
static auto | connectedSocket (int socketType, const Address &address, Options... options) -> Socket |
Creates a socket that is connected to an address. | |
template<tools::OneOf< NonBlockingTag, Protocol, Timeout, WithStopSource >... Options> | |
static auto | connectedSocket (int socketType, const std::vector< Address > &addresses, Options... options) -> Socket |
Creates a socket that is connected to the first available address in a list. | |
template<tools::OneOf< NonBlockingTag, Protocol, DefaultPort, WithPort, AddressFamily, Timeout, WithStopSource >... Options> | |
static auto | connectedSocket (int socketType, std::string_view hostName, Options... options) -> Socket |
Creates a socket that is connected to a named host. | |
template<tools::OneOf< NonBlockingTag, Protocol, AddressFamily, Backlog >... Options> | |
static auto | listeningSocket (int socketType, PortNumber portNumber, Options... options) -> Socket |
Creates socket that listens on all local interfaces. | |
template<tools::OneOf< NonBlockingTag, Protocol, Backlog >... Options> | |
static auto | listeningSocket (int socketType, const Address &address, Options... options) -> Socket |
Creates socket that listens on a specific address. | |
A socket.
|
strong |
|
defaultnoexcept |
Default constructor.
Creates an invalid socket object.
xentara::utils::network::Socket::Socket | ( | int | addressFamily, |
int | socketType, | ||
Options... | options | ||
) |
Constructor that creates a socket.
addressFamily | The address family, also called the socket domain. Can be any of the AF_XXX address family defines, like AF_INET or AF_INET6. | ||||
socketType | The socket type of socket to create. Can be any of the SOCK_XXX socket type defines, like SOCK_STREAM or SOCK_DGRAM. | ||||
options | The options for the socket. See Option Parameters below.
|
std::runtime_error | An error occurred getting the option |
std::invalid_argument | An address family of AF_UNSPEC was specified |
std::invalid_argument | A socket type of 0 was specified |
|
noexcept |
Conversion from a Posix file descriptor.
This constructor takes ownership of the socket.
socketFileDescriptor | The file descriptor of the new socket, or -1 to create a object that does not contain a socket. |
|
noexcept |
Conversion from a native socket.
This constructor takes ownership of the socket.
socket | The new socket, or INVALID_SOCKET to create a object that does not contain a socket. |
auto xentara::utils::network::Socket::accept | ( | Options... | options | ) | const -> Socket |
Accepts a connection from a socket in listening mode.
This function may only be called on sockets that have previously been put into listening mode using listen()
options | The Options to use for the socket. See Option Parameters below.
|
std::runtime_error | An error occurred accepting the connection, or the socket is not in listening mode |
|
noexcept |
Gets the address family of the socket, also called the socket domain.
auto xentara::utils::network::Socket::bind | ( | const Address & | address | ) | -> void |
Binds the socket to a specific address.
address | The address to bind to. If you specify an address returned by Address::any(), then the socket will be bound to the relevant port on all local interfaces. |
std::runtime_error | An error occurred binding the socket |
std::invalid_argument | The address is default constructed, or has an address family of AF_UNSPEC. |
auto xentara::utils::network::Socket::bind | ( | PortNumber | portNumber | ) | -> void |
Binds the socket to a port number on all local interfaces.
portNumber | The port number to bind to, or 0 to allow the network stack to assign a port number. For protocols that do not use port numbers, the port should always be 0. |
std::runtime_error | An error occurred binding the socket |
|
noexcept |
Closes the socket, if one is open.
|
static |
Creates a socket that is compatible with an address.
This function creates a socket that can be used to listen to or communicate with a certain address. The socket will not actually be bound or connected to the address, use bind() or connect() if you need to bind to or connect to the address.
socketType | The type of socket to create. Can be any of the SOCK_XXX socket type defines, like SOCK_STREAM or SOCK_SEQPACKET. | ||||
address | The addresses that the socket will be used for. | ||||
options | The Options to use for the socket. See Option Parameters below.
|
std::runtime_error | An error occurred creating or connecting the socket for all of the given addresses |
std::invalid_argument | A socket type of 0 was specified |
std::invalid_argument | The address is default constructed, or has an address family of AF_UNSPEC. |
auto xentara::utils::network::Socket::connect | ( | const Address & | address | ) | -> bool |
Connects to an address.
address | The address to connect to |
std::runtime_error | An error occurred connecting the socket |
std::invalid_argument | The address is default constructed, or has an address family of AF_UNSPEC. |
|
static |
Creates a socket that is connected to an address.
socketType | The type of socket to create. Can be any of the SOCK_XXX socket type defines, like SOCK_STREAM or SOCK_SEQPACKET. | ||||||||
address | The addresses to connect to. | ||||||||
options | The Options to use for the connection. See Option Parameters below.
|
io::StopRequested | A stop source was specified, and the stop source was triggered. |
std::runtime_error | An error occurred creating or connecting the socket for all of the given addresses |
std::invalid_argument | A socket type of 0 was specified |
std::invalid_argument | The address is default constructed, or has an address family of AF_UNSPEC. |
|
static |
Creates a socket that is connected to the first available address in a list.
socketType | The type of socket to create. Can be any of the SOCK_XXX socket type defines, like SOCK_STREAM or SOCK_SEQPACKET. | ||||||||
addresses | The addresses to try to connect to, in the order that they should be tried. Addresses that do not support the given socket type are ignored, as are default constructed addresses and addresses with address family AF_UNSPEC. | ||||||||
options | The Options to use for the connection. See Option Parameters below.
|
HostNotFoundError | The address list did not contain any suitable addresses. |
io::StopRequested | A stop source was specified, and the stop source was triggered. |
std::runtime_error | An error occurred creating or connecting the socket for all of the given addresses |
std::invalid_argument | A socket type of 0 was specified |
|
static |
Creates a socket that is connected to a named host.
Normally, hostName must contain a port number after the host name or address string. You can pass the options xentara::utils::network::DefaultPort or xentara::utils::network::WithPort in the options parameter pack to make the port number optional, or to specify a fixed port number. See Option Parameters below for a description of these options.
socketType | The type of socket to create. Can be any of the SOCK_XXX socket type defines, like SOCK_STREAM or SOCK_SEQPACKET. | ||||||||||||||
hostName | The host name to connect to | ||||||||||||||
options | The Options to use for the connection. See Option Parameters below.
|
io::StopRequested | A stop source was specified, and the stop source was triggered. |
std::runtime_error | An error occurred creating or connecting the socket |
std::invalid_argument | A socket type of 0 was specified |
|
noexcept |
Gets a description for the socket.
This function can be used in error messages to describe the socket. It will return a string like “TCP/IP socket”.
auto xentara::utils::network::Socket::error | ( | ) | const -> std::error_code |
Returns the error status, and clears it.
This function can be used to retreive the error if poll() returned POLLERR.
Fetching the error status clears it. This is why this function is not const.
std::runtime_error | An error occurred getting the error status. |
|
noexcept |
Gets a socket option.
This function behaves the same as option(), but returns an error code on error instead of throwing an exception. Use this function if you want to do your own error handling, like throwing an exception with a more descriptive what() string.
Value | The type of the socket option's value. Each option requires a specific type of value. Refer to the documentation for the appropriate option for the supported types. |
level | The level at which the option resides. Must be SOL_SOCKET to get a socket level option, or one of the XXXPROTO_YYY protocol defines, like IPPROTO_TCP or IPPROTO_SCTP, to get a protocol level options. Other socket levels may also be defined by the operating system. |
optionName | The name of the option. This is an option level specific integer that describes which option to set. |
auto xentara::utils::network::Socket::listen | ( | Options... | options | ) | -> void |
Puts the socket into listening mode.
This function puts the socket into listening mode, so that is can accept incoming connections. For most protocols, a socket must be bound to a local port using bind() before it can be put into the listening state.
options | The Options to use for listening. See Option Parameters below.
|
std::runtime_error | An error occurred putting the socket into listening mode |
std::invalid_argument | A backlog of less than 1 was specified. |
|
static |
Creates socket that listens on a specific address.
This function creates a socket, binds it to the given address, and puts it into the listening state so that it can accept connections.
socketType | The type of socket to create. Must be a SOCK_XXX socket type define for a socket type that supports listening, like SOCK_STREAM or SOCK_SEQPACKET. | ||||||
address | The address to listen on. If you specify an address returned by Address::any(), then the socket will listen to the relevant port on all local interfaces. | ||||||
options | The Options to use. See Option Parameters below.
|
std::runtime_error | An error occurred creating the socket, or putting it into the listening state. |
std::invalid_argument | A socket type of 0 was specified |
std::invalid_argument | The address is default constructed, or has an address family of AF_UNSPEC. |
std::invalid_argument | A backlog of less than 1 was specified. |
|
static |
Creates socket that listens on all local interfaces.
This function creates a socket, binds it to the given port on all local interfaces, and puts it into the listening state, so that it can accept connections.
socketType | The type of socket to create. Must be a SOCK_XXX socket type define for a socket type that supports listening, like SOCK_STREAM or SOCK_SEQPACKET. | ||||||||
portNumber | The port number to bind to, or 0 to allow the network stack to assign a port number. For protocols that do not use port numbers, the port should always be 0. | ||||||||
options | The Options to use. See Option Parameters below.
|
std::runtime_error | An error occurred creating the socket, or putting it into the listening state. |
std::invalid_argument | A socket type of 0 was specified |
std::invalid_argument | A backlog of less than 1 was specified. |
auto xentara::utils::network::Socket::localAddress | ( | ) | const -> Address |
Gets the local address of a bound or connected socket.
std::runtime_error | An error occurred getting the socket address, or the socket is not bound and not connected. |
auto xentara::utils::network::Socket::makeNonBlocking | ( | bool | nonBlocking = true | ) | -> void |
Sets the non-blocking option of the socket.
This function allows you to make a socket blocking or non-blocking after it has been created. To create a socket that is non-blocking from the start, simply pass the kNonBlocking option to the constructor.
nonBlocking | true to make the socket non-blocking, or false to make is blocking. |
std::runtime_error | An error occurred setting the option |
|
noexcept |
Gets the underlying Posix file descriptor for the socket.
|
noexcept |
Gets the underlying Windows Sockets 2 SOCKET.
|
explicitnoexcept |
Checks whether the object contains a valid socket.
auto xentara::utils::network::Socket::option | ( | int | level, |
int | optionName | ||
) | const -> Value |
Gets a socket option.
Value | The type of the socket option's value. Each option requires a specific type of value. Refer to the documentation for the appropriate option for the supported types. |
level | The level at which the option resides. Must be SOL_SOCKET to get a socket level option, or one of the XXXPROTO_YYY protocol defines, like IPPROTO_TCP or IPPROTO_SCTP, to get a protocol level options. Other socket levels may also be defined by the operating system. |
optionName | The name of the option. This is an option level specific integer that describes which option to set. |
std::runtime_error | An error occurred getting the option |
auto xentara::utils::network::Socket::performOverlappedIo | ( | functional::function_ref< auto(SOCKET, OVERLAPPED *) → BOOL > | operation, |
HANDLE | event, | ||
std::optional< std::chrono::nanoseconds > | timeout = std::nullopt |
||
) | -> std::optional<DWORD> |
Performs an overlapped I/O operation.
This function creates an OVERLAPPED structure and calls a function object with it. The function object must call a Windows overlapped socket function like WSARecv() or WSASend(), passing the overlapped structure to it, and return the result. The function must also preserve the error code stored in GetLastError(), specifically the error code ERROR_IO_PENDING, which the function uses to determine if waiting is necessary.
operation | The operation to perform. operation will be called with the socket and an overlapped structure as parameters. It must return TRUE on success or FALSE on error. If FALSE is returned, the last error code must be set to an appropriate error. If an overlapped I/O operation is pending, the function must return FALSE and set the last error to ERROR_IO_PENDING in the manner of overlapped I/O functions like WSARecv() and WSASend(). |
event | The event to use in the OVERLAPPED structure. |
timeout | The timeout, or std::nullopt for infinite. Negative timeouts are treated the same as a timeout of 0. |
Returns the number of bytes transferred on success, or std::nullopt on error. To get extended error information, call GetLastError(). On timeout, the last error is set to ERROR_OPERATION_ABORTED.
The meaning of “number of bytes transferred” depends on the overlapped operation. See the documentation of the lpNumberOfBytesTransferred parameter of GetOverlappedResult().
auto xentara::utils::network::Socket::performOverlappedIo | ( | functional::function_ref< auto(SOCKET, OVERLAPPED *) → BOOL > | operation, |
std::span< HANDLE > | waitObjects, | ||
std::optional< std::chrono::nanoseconds > | timeout = std::nullopt |
||
) | -> std::optional<windows::OverlappedResult> |
Performs an overlapped I/O operation.
This function creates an OVERLAPPED structure and calls a function object with it. The function object must call a Windows overlapped socket function like WSARecv() or WSASend(), passing the overlapped structure to it, and return the result. The function must also preserve the error code stored in GetLastError(), specifically the error code ERROR_IO_PENDING, which the function uses to determine if waiting is necessary.
operation | The operation to perform. operation will be called with the socket and an overlapped structure as parameters. It must return TRUE on success or FALSE on error. If FALSE is returned, the last error code must be set to an appropriate error. If an overlapped I/O operation is pending, the function must return FALSE and set the last error to ERROR_IO_PENDING in the manner of overlapped I/O functions like WSARecv() and WSASend(). |
waitObjects | The objects to wait for. The first position in the span must contain the event to use in the OVERLAPPED structure. |
timeout | The timeout, or std::nullopt for infinite. Negative timeouts are treated the same as a timeout of 0. |
std::invalid_argument | waitObjects is empty, or contains more objects than Windows can handle. |
auto xentara::utils::network::Socket::poll | ( | short | events, |
const io::StopSource & | stopSource, | ||
std::optional< std::chrono::nanoseconds > | timeout = std::nullopt |
||
) | const -> short |
Waits for events on the socket using a stop source.
This function provides an interruptible way to wait for events on a socket. The stop source can be used to interrupt the wait, usually to shutdown and close the socket. If the stop source is triggered, the function will throw an exception of type io::StopRequested.
events | The events to wait for. Must be a bit combination of the POLXXX poll event defines, like POLLIN or POLLOUT. |
stopSource | The stop source to watch for stop requests. |
timeout | The timeout, or std::nullopt to wait without timeout. |
io::StopRequested | The stop source was triggered. |
std::runtime_error | An error occurred waiting for the events. |
auto xentara::utils::network::Socket::poll | ( | short | events, |
std::optional< std::chrono::nanoseconds > | timeout = std::nullopt |
||
) | const -> short |
Waits for events on the socket.
events | The events to wait for. Must be a bit combination of the POLXXX poll event defines, like POLLIN or POLLOUT. |
timeout | The timeout, or std::nullopt to wait without timeout. |
std::runtime_error | An error occurred waiting for the events. |
|
static |
Waits for events on one or more sockets using a stop source.
This function provides an interruptible way to wait for events on one or more sockets. The stop source can be used to interrupt the wait, usually to shutdown and close the sockets. If the stop source is triggered, the function will throw an exception of type io::StopRequested.
sockets | The sockets and event to wait for. The revents member of the sockets is set to the events that were actually received. Under Posix, the fd membner of the pollfd structures can hold any type of file descriptor, so this function can be used to wait on pipes and other types of file descriptors. Under Windows, only actual sockets can be used. |
stopSource | The stop source to watch for stop requests. |
timeout | The timeout, or std::nullopt to wait without timeout. |
io::StopRequested | The stop source was triggered. |
std::runtime_error | An error occurred waiting for the events. |
|
static |
Waits for events on one or more sockets.
sockets | The sockets and event to wait for. The revents member of the sockets is set to the events that were actually received. Under Posix, the fd membner of the pollfd structures can hold any type of file descriptor, so this function can be used to wait on pipes and other types of file descriptors. Under Windows, only actual sockets can be used. |
timeout | The timeout, or std::nullopt to wait without timeout. |
std::runtime_error | An error occurred waiting for the events. |
|
noexcept |
Gets the protocol of the socket.
auto xentara::utils::network::Socket::receive | ( | void * | buffer, |
std::size_t | size, | ||
int | flags = 0 |
||
) | const -> std::optional< std::size_t > |
Receive data from the socket.
buffer | A pointer to the buffer that will receive the data |
size | The size of the buffer. |
flags | The flags. This can be a combination of operationg system receive flags, like MSG_PEEK or MSG_OOB. |
std::runtime_error | An error occurred reading from the socket. |
auto xentara::utils::network::Socket::receiveFrom | ( | void * | buffer, |
std::size_t | size, | ||
int | flags = 0 |
||
) | const -> std::pair< std::optional< std::size_t >, Address > |
Receive data from the socket and returns the sender address.
buffer | A pointer to the buffer that will receive the data |
size | The size of the buffer. |
flags | The flags. This can be a combination of operationg system receive flags, like MSG_PEEK or MSG_OOB. |
The number of bytes actually read, together with the address of the sender. If no data is available without blocking, std::nullopt is returned, together with a default-consructed address object.
For stream oriented sockets, a size of 0 is returned if the socket was gracefully closed from the remote side. For datagram sockets, a size of 0 is returned in first if a zero-length datagram was received.
0 is also returned for the size if size is 0.
std::runtime_error | An error occurred reading from the socket. |
|
noexcept |
Removes the the socket and passes ownership to the caller.
After this call, this object will no longer contain a socket.
|
noexcept |
Removes the socket and passes ownership to the caller.
After this call, this object will no longer contain a socket.
auto xentara::utils::network::Socket::remoteAddress | ( | ) | const -> Address |
Gets the address that the socket is connected to.
std::runtime_error | An error occurred getting the socket address, or the socket is not connected. |
auto xentara::utils::network::Socket::send | ( | const void * | data, |
std::size_t | size, | ||
int | flags = 0 |
||
) | const -> std::size_t |
Sends data to the socket.
data | A pointer to the data to send |
size | The size of the data. |
flags | The flags. This can be a combination of operationg system send flags, like MSG_DONTROUTE or MSG_OOB. Under Posix, MSG_NOSIGNAL is automatically added. |
std::runtime_error | An error occurred writing to the socket. |
ConnectionClosed | Errors thrown because the connection was closed are derived from ConnectionClosed in addition to std::runtime_error. |
auto xentara::utils::network::Socket::sendTo | ( | const void * | data, |
std::size_t | size, | ||
const Address & | destination, | ||
int | flags = 0 |
||
) | const -> std::size_t |
Sends data to a specific destination over the socket.
data | A pointer to the data to send |
size | The size of the data. |
destination | The destination address. Must be compatible with the socket. |
flags | The flags. This can be a combination of operationg system send flags, like MSG_DONTROUTE or MSG_OOB. Under Posix, MSG_NOSIGNAL is automatically added. |
std::runtime_error | An error occurred writing to the socket. |
ConnectionClosed | Errors thrown because the connection was closed are derived from ConnectionClosed in addition to std::runtime_error. |
auto xentara::utils::network::Socket::setOption | ( | int | level, |
int | optionName, | ||
const Value & | value | ||
) | -> void |
Sets a socket option.
Value | The type of the socket option's value. Each option requires a specific type of value. Refer to the documentation for the appropriate option for the supported types. |
level | The level at which the option resides. Must be SOL_SOCKET to set a socket level option, or one of the XXXPROTO_YYY protocol defines, like IPPROTO_TCP or IPPROTO_SCTP, to set a protocol level option. Other socket levels may also be defined by the operating system. |
optionName | The name of the option. This is an option level specific integer that describes which option to set. |
value | The value to set |
std::runtime_error | An error occurred setting the option |
|
noexcept |
Sets a socket option without throwing exceptions.
This function behaves the same as setOption(), but returns an error code on error instead of throwing an exception. Use this function if you want to do your own error handling, like throwing an exception with a more descriptive what() string.
level | The level at which the option resides. Must be SOL_SOCKET to get a socket level option, or one of the XXXPROTO_YYY protocol defines, like IPPROTO_TCP or IPPROTO_SCTP, to get a protocol level options. Other socket levels may also be defined by the operating system. |
optionName | The name of the option. This is an option level specific integer that describes which option to set. |
value | The value to set |
auto xentara::utils::network::Socket::shutdown | ( | Direction | directions = Direction::ReadWrite | ) | -> void |
Shuts down the socket.
directions | Which directions to shut down. |
std::runtime_error | An error occurred. |
|
noexcept |
Gets the socket type.
auto xentara::utils::network::Socket::throwError | ( | ExceptionArguments &&... | exceptionArguments | ) | -> void |
Throws an exception if the socket contains an error.
This function can be used to throw an exception if poll() returned POLLERR.
Fetching the error status clears it. This is why this function is not const.
std::runtime_error | An error occurred getting the error status. |
auto xentara::utils::network::Socket::waitForEvent | ( | short | events, |
const io::StopSource & | stopSource, | ||
std::optional< std::chrono::nanoseconds > | timeout = std::nullopt |
||
) | const -> short |
Waits for an event on the socket, using a timeout.
This function provides an interruptible way to wait for events on a socket. The stop source can be used to interrupt the wait, usually to shutdown and close the socket. If the stop source is triggered, the function will throw an exception of type io::StopRequested.
events | The events to wait for. Must be a bit combination of the POLXXX poll event defines, like POLLIN or POLLOUT. |
stopSource | The stop source to watch for stop requests. |
timeout | The timeout, or std::nullopt to wait without timeout. |
io::StopRequested | The stop source was triggered. |
std::runtime_error | An error occurred waiting for the events. |