xentara-utils v2.0.4
The Xentara Utility Library
|
A class to create a socket server. More...
#include <xentara/utils/network/SocketServer.hpp>
Public Member Functions | |
SocketServer ()=default | |
Default constructor. | |
SocketServer (std::reference_wrapper< const io::StopSource > stopSource) noexcept | |
Constructor that associates a stop source with the socket server. | |
template<tools::OneOf< Protocol, AddressFamily, Backlog, WithStopSource >... Options> | |
SocketServer (PortNumber portNumber, Options... options) | |
Constructor for a server that listens on all local interfaces. | |
template<tools::OneOf< Protocol, Backlog, WithStopSource >... Options> | |
SocketServer (const Address &address, Options... options) | |
Constructor for a server that listens on a specific address. | |
auto | setStopSource (const io::StopSource &stopSource) noexcept -> void |
Associates a stop source with the server. | |
auto | removeStopSource () noexcept -> void |
Removes any associated stop source from the server. | |
template<tools::OneOf< Protocol, AddressFamily, Backlog >... Options> | |
auto | listen (PortNumber portNumber, Options... options) -> void |
Starts listening on all local interfaces. | |
template<tools::OneOf< Protocol, Backlog >... Options> | |
auto | listen (const Address &address, Options... options) -> void |
Starts listening on a specific address. | |
auto | address () const -> Address |
Gets the address the server is listening on. | |
auto | accept () -> SocketDevice |
Accepts a client connection. | |
auto | accept (std::optional< std::chrono::nanoseconds > timeout) -> SocketDevice |
Accepts a client connection. | |
auto | close () -> void |
Stops listening. | |
A class to create a socket server.
|
default |
Default constructor.
|
explicitnoexcept |
Constructor that associates a stop source with the socket server.
stopSource | The stop source to watch for stop requests. The stop source can be used to interrupt the accept() function, usually to stop the server. If the stop source is triggered, accept() will throw an exception of type io::StopRequested. Accepted sockets returned by accept() will also use the specified stop source. |
xentara::utils::network::SocketServer::SocketServer | ( | PortNumber | portNumber, |
Options... | options | ||
) |
Constructor for a server that listens 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. | ||||||||
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 backlog of less than 1 was specified. |
xentara::utils::network::SocketServer::SocketServer | ( | const Address & | address, |
Options... | options | ||
) |
Constructor for a server that listens on a specific address.
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 | The address is default constructed, or has an address family of AF_UNSPEC. |
std::invalid_argument | A backlog of less than 1 was specified. |
auto xentara::utils::network::SocketServer::accept | ( | ) | -> SocketDevice |
Accepts a client connection.
io::StopRequested | The stop source associated with the socket server was triggered. |
std::runtime_error | An error occurred accepting the connection |
auto xentara::utils::network::SocketServer::accept | ( | std::optional< std::chrono::nanoseconds > | timeout | ) | -> SocketDevice |
Accepts a client connection.
timeout | The timeout, or std::nullopt to use an infinite timeout. |
The accepted socket, or a default constructed socket on timeout. You can use the [operator bool()]SocketDevice::operator bool()) to check whether the returned socket is valid.
If the socket server has a stop source associated with it, the returned object will use the same stop source.
Since SocketStream has a conversion constructor from SocketDevice, you can assign the returned socket directly to a SocketStream, if desired.
io::StopRequested | The stop source associated with the socket server was triggered. |
std::runtime_error | An error occurred accepting the connection |
auto xentara::utils::network::SocketServer::address | ( | ) | const -> Address |
Gets the address the server is listening on.
std::runtime_error | An error occurred getting the address |
auto xentara::utils::network::SocketServer::close | ( | ) | -> void |
Stops listening.
auto xentara::utils::network::SocketServer::listen | ( | const Address & | address, |
Options... | options | ||
) | -> void |
Starts listening on a specific address.
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 | The address is default constructed, or has an address family of AF_UNSPEC. |
std::invalid_argument | A backlog of less than 1 was specified. |
auto xentara::utils::network::SocketServer::listen | ( | PortNumber | portNumber, |
Options... | options | ||
) | -> void |
Starts listening 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. | ||||||
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 backlog of less than 1 was specified. |
|
noexcept |
Removes any associated stop source from the server.
|
noexcept |
Associates a stop source with the server.
The stop source can be used to interrupt the accept() function, usually to stop the server. If the stop source is triggered, accept() will throw an exception of type io::StopRequested. Accepted sockets returned by accept() will also use the specified stop source.
stopSource | The stop source to watch for stop requests |