ConnectionManager

class ConnectionManager

The primary interface to the low-level networking layer in this package. A ConnectionManager is used to establish and destroy TCP and UDP connections. Communication on these connections, once established, is handled via ConnectionReader, ConnectionWriter, and ConnectionListener.

You may use this class directly if you don’t care about tracking which connections have been unexpectedly closed; otherwise, you should use QueuedConnectionManager to get reports about these events (or derive your own class to handle these events properly).

Inheritance diagram

Inheritance diagram of ConnectionManager

class Interface
Interface(ConnectionManager::Interface const&) = default
NetAddress const &get_broadcast(void) const
NetAddress const &get_ip(void) const
std::string const &get_mac_address(void) const
std::string const &get_name(void) const
NetAddress const &get_netmask(void) const
NetAddress const &get_p2p(void) const
bool has_broadcast(void) const
bool has_ip(void) const
bool has_netmask(void) const
bool has_p2p(void) const
void output(std::ostream &out) const
ConnectionManager(void)
bool close_connection(PointerTo<Connection> const &connection)

Terminates a UDP or TCP socket previously opened. This also removes it from any associated ConnectionReader or ConnectionListeners.

The socket itself may not be immediately closed–it will not be closed until all outstanding pointers to it are cleared, including any pointers remaining in NetDatagrams recently received from the socket.

The return value is true if the connection was marked to be closed, or false if close_connection() had already been called (or the connection did not belong to this ConnectionManager). In neither case can you infer anything about whether the connection has actually been closed yet based on the return value.

static std::string get_host_name(void)

Returns the name of this particular machine on the network, if available, or the empty string if the hostname cannot be determined.

Interface const &get_interface(std::size_t n)

Returns the nth usable network interface detected on this machine. See scan_interfaces() to repopulate this list.

std::size_t get_num_interfaces(void)

This returns the number of usable network interfaces detected on this machine. See scan_interfaces() to repopulate this list.

PointerTo<Connection> open_TCP_client_connection(NetAddress const &address, int timeout_ms)
PointerTo<Connection> open_TCP_client_connection(std::string const &hostname, uint16_t port, int timeout_ms)

Attempts to establish a TCP client connection to a server at the indicated address. If the connection is not established within timeout_ms milliseconds, a null connection is returned.

This is a shorthand version of the function to directly establish communications to a named host and port.

PointerTo<Connection> open_TCP_server_rendezvous(uint16_t port, int backlog)
PointerTo<Connection> open_TCP_server_rendezvous(std::string const &hostname, uint16_t port, int backlog)
PointerTo<Connection> open_TCP_server_rendezvous(NetAddress const &address, int backlog)

Creates a socket to be used as a rendezvous socket for a server to listen for TCP connections. The socket returned by this call should only be added to a ConnectionListener (not to a generic ConnectionReader).

This variant of this method accepts a single port, and will listen to that port on all available interfaces, both IPv4 and IPv6.

backlog is the maximum length of the queue of pending connections.

Creates a socket to be used as a rendezvous socket for a server to listen for TCP connections. The socket returned by this call should only be added to a ConnectionListener (not to a generic ConnectionReader).

This variant of this method accepts a “hostname”, which is usually just an IP address in dotted notation, and a port number. It will listen on the interface indicated by the IP address. If the IP address is empty string, it will listen on all interfaces.

backlog is the maximum length of the queue of pending connections.

Creates a socket to be used as a rendezvous socket for a server to listen for TCP connections. The socket returned by this call should only be added to a ConnectionListener (not to a generic ConnectionReader).

This variant of this method accepts a NetAddress, which allows you to specify a specific interface to listen to.

backlog is the maximum length of the queue of pending connections.

PointerTo<Connection> open_UDP_connection(uint16_t port = 0)
PointerTo<Connection> open_UDP_connection(std::string const &hostname, uint16_t port, bool for_broadcast = false)

Opens a socket for sending and/or receiving UDP packets. If the port number is greater than zero, the UDP connection will be opened for listening on the indicated port; otherwise, it will be useful only for sending.

Use a ConnectionReader and ConnectionWriter to handle the actual communication.

Opens a socket for sending and/or receiving UDP packets. If the port number is greater than zero, the UDP connection will be opened for listening on the indicated port; otherwise, it will be useful only for sending.

This variant accepts both a hostname and port to listen on a particular interface; if the hostname is empty, all interfaces will be available, both IPv4 and IPv6.

If for_broadcast is true, this UDP connection will be configured to send and/or receive messages on the broadcast address (255.255.255.255); otherwise, these messages may be automatically filtered by the OS.

Use a ConnectionReader and ConnectionWriter to handle the actual communication.

void scan_interfaces(void)

Repopulates the list reported by get_num_interface()/get_interface(). It is not necessary to call this explicitly, unless you want to re-determine the connected interfaces (for instance, if you suspect the hardware has recently changed).

bool wait_for_readers(double timeout)

Blocks the process for timeout number of seconds, or until any data is available on any of the non-threaded ConnectionReaders or ConnectionListeners, whichever comes first. The return value is true if there is data available (but you have to iterate through all readers to find it), or false if the timeout occurred without any data.

If the timeout value is negative, this will block forever or until data is available.

This only works if all ConnectionReaders and ConnectionListeners are non- threaded. If any threaded ConnectionReaders are part of the ConnectionManager, the timeout value is implicitly treated as 0.