|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
Central owner of all channels, listeners and connections in a process. More...
#include <NetworkManager.h>
Public Member Functions | |
| THREAD_RET THREAD_FUNCTION_CALL | NetworkManagerRun (THREAD_ARG arg) |
| Thread entry point of the manager's supervision loop (do not call directly). | |
| NetworkManager () | |
| virtual | ~NetworkManager () |
| bool | setSSLCertificate (const char *sslCertPath, const char *sslKeyPath) |
| Set the certificate/key used by SSL listeners created via this manager. | |
| bool | setSSLAllowSelfSigned (bool allow) |
| bool | setSSLCALocation (const char *caFile, const char *caPath) |
| NetworkChannel * | createListener (uint16 port, uint8 encryption, uint8 protocol, bool isAsync, uint32 protocolTimeout, bool isDefaultProtocol, uint32 channelID, NetworkReceiver *recv) |
| Open a listening port for one or more protocols. | |
| NetworkChannel * | createTCPConnection (const char *addr, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint32 channelID, NetworkReceiver *recv, uint64 &conid, uint64 &location, uint32 timeoutMS=5000) |
| Connect to a remote host by name/IP and start the protocol thread. | |
| NetworkChannel * | createTCPConnection (uint64 location, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint32 channelID, NetworkReceiver *recv, uint64 &conid, uint32 timeoutMS=5000) |
| Connect to a packed uint64 endpoint. | |
| NetworkChannel * | createTCPConnection (const uint32 *addresses, uint16 addressCount, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint32 channelID, NetworkReceiver *recv, uint64 &conid, uint64 &location, uint32 timeoutMS=5000) |
| Connect trying several candidate IPv4 addresses for one host. | |
| NetworkChannel * | createUDPConnection (uint16 port, uint8 protocol, bool isAsync, bool autoreconnect, uint32 channelID, NetworkReceiver *recv, uint64 &conid) |
| Bind a UDP port for datagram traffic. | |
| NetworkChannel * | addTCPConnection (const char *addr, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, uint32 channelID, NetworkReceiver *recv, uint64 &conid, uint64 &location, uint32 timeoutMS=5000, const char *greetingData=NULL, uint32 greetingSize=0) |
| Like createTCPConnection() but sends optional greeting bytes right after connecting (peer identification / protocol selection) and never auto-reconnects. | |
| NetworkChannel * | addTCPConnection (uint64 location, uint8 encryption, uint8 protocol, bool isAsync, uint32 channelID, NetworkReceiver *recv, uint64 &conid, uint32 timeoutMS=5000, const char *greetingData=NULL, uint32 greetingSize=0) |
| Packed-endpoint variant of addTCPConnection(). | |
| NetworkChannel * | createWebsocketConnection (const char *url, uint32 channelID, NetworkReceiver *recv, uint64 &conid, const char *protocolName=NULL, const char *origin=NULL, uint32 timeoutMS=5000) |
| Open a client WebSocket connection from a full URL (ws:// or wss://). | |
| NetworkChannel * | createWebsocketConnection (const char *uri, const char *addr, uint16 port, uint8 encryption, uint32 channelID, NetworkReceiver *recv, uint64 &conid, const char *protocolName=NULL, const char *origin=NULL, uint32 timeoutMS=5000) |
| Open a client WebSocket connection from explicit components. | |
| bool | stopListener (uint16 port, uint8 protocol) |
| Stop a listener previously opened with createListener(). | |
| bool | endConnection (uint64 conid) |
| Gracefully close a connection (thread is asked to finish; entry kept for reuse). | |
| bool | endUDPConnection (uint16 port) |
Close the UDP connection bound to port. | |
| bool | removeConnection (uint64 conid) |
| Close a connection and remove it from the manager's maps entirely. | |
| uint8 | getConnectionType (uint64 conid) |
| uint64 | getRemoteAddress (uint64 conid) |
| NetworkChannel * | getConnection (uint64 conid) |
| NetworkChannel * | findChannelByConnection (uint64 conid) |
| Look up the channel owning a CONNECTION id. | |
| NetworkChannel * | findChannelByID (uint32 channelID) |
| Look up a channel by CHANNEL id. | |
| NetworkChannel * | getTCPConnectionByPort (uint16 port) |
| NetworkChannel * | getUDPConnectionByPort (uint16 port) |
| uint64 | addConnection (NetworkChannel *channel) |
| Register an externally created channel with the manager. | |
| bool | sendUDPMessage (DataMessage *msg, uint64 destination) |
| Send a DataMessage as a UDP datagram via the shared output socket. | |
| HTTPReply * | makeHTTPRequest (const char *url, uint32 timeout, const char *content=NULL, uint32 contentSize=0) |
Blocking HTTP(S) exchange from a URL string (GET, or POST when content given). | |
| HTTPReply * | makeHTTPRequest (HTTPRequest *req, const char *addr, uint16 port, uint8 encryption, uint32 timeout) |
| Blocking exchange sending a pre-built request to an explicit endpoint. | |
| HTTPReply * | makeHTTPRequest (uint8 ops, std::string addr, uint32 timeout, std::map< std::string, std::string > &headerEntries, const char *content, const char *contentType, uint32 contentSize, bool keepAlive=false, uint64 ifModifiedSince=0) |
| Blocking exchange with custom headers and typed raw body. | |
| HTTPReply * | makeHTTPRequest (uint8 ops, std::string addr, uint32 timeout, std::map< std::string, std::string > &headerEntries, std::map< std::string, HTTPPostEntry * > &bodyEntries, bool keepAlive=false, uint64 ifModifiedSince=0) |
| Blocking multipart/form-data exchange (file uploads, form posts). | |
| Public Member Functions inherited from cmlabs::Runnable | |
| Runnable () | |
| Initialise flags: not running, allowed to continue. | |
| virtual | ~Runnable () |
| Destructor requests a stop (with the default timeout) before destruction proceeds. | |
| virtual bool | stop (uint32 timeout=200) |
| Ask the worker loop to finish and wait for it to do so. | |
Static Public Member Functions | |
| static bool | UnitTest () |
| Basic TCP/message-protocol round-trip self-test. | |
| static bool | UnitTestDelayedConnect () |
| Self-test of non-blocking (delayed) connect handling. | |
| static bool | UnitTestHTTP () |
| Self-test of the built-in HTTP server and client. | |
| static bool | TestHTTP (const char *host, uint32 port, std::vector< std::string > &urls) |
| Fetch a list of URLs from a host and report results (manual test helper). | |
| static bool | WebsocketTest () |
| WebSocket upgrade + echo self-test. | |
Public Attributes | |
| int8 | sslAllowSelfSigned |
| std::string | sslCAFile |
| std::string | sslCAPath |
| bool | sslCASet |
Protected Member Functions | |
| bool | run () |
| Supervision loop body (delayed connects, reconnection). | |
Guarded map mutators | |
All four manager maps are shared with connection worker threads, so every insert/erase must hold ::mapMutex. These wrappers exist so the factories do not each hand-roll the locking (and so none can be forgotten). | |
| void | registerChannel (uint32 channelID, NetworkChannel *channel) |
| Register a channel under a channel id, and optionally a TCP listener port. | |
| void | unregisterChannel (uint32 channelID) |
| Remove a channel id (and nothing else) from the registry. | |
| void | registerConnection (uint64 conid, NetworkChannel *channel) |
| Map a connection id to its owning channel. | |
| void | registerTCPListener (uint16 port, NetworkChannel *channel) |
| Bind a TCP listener port to a channel. | |
| void | unregisterTCPListener (uint16 port) |
| Unbind a TCP listener port. | |
| void | registerUDPListener (uint16 port, NetworkChannel *channel) |
| Bind a UDP listener port to a channel. | |
| uint32 | allocateChannelID () |
| Allocate the next unused channel id. | |
Protected Attributes | |
| std::map< uint32, NetworkChannel * > | channels |
| All channels by channel id (owned). | |
| std::map< uint64, NetworkChannel * > | channelsByConnection |
| Channel lookup by connection id. | |
| std::map< uint16, NetworkChannel * > | listeners |
| TCP listeners by port. | |
| std::map< uint16, NetworkChannel * > | udpListeners |
| UDP listeners by port. | |
| utils::Mutex | mapMutex |
| Serialises ALL four maps above. | |
| std::string | sslCertPath |
| PEM certificate path for SSL listeners. | |
| std::string | sslKeyPath |
| PEM private key path for SSL listeners. | |
| UDPConnection * | udpOutputCon |
| Shared output-only UDP socket for sendUDPMessage(). | |
| utils::Mutex | udpOutputConMutex |
| Serialises use of ::udpOutputCon. | |
| uint32 | lastChannelID |
| Last channel id issued. | |
| uint64 | lastConnectionID |
| Last connection id issued. | |
| Protected Attributes inherited from cmlabs::Runnable | |
| uint32 | threadID |
| ThreadManager slot ID of the worker thread (0 until known). | |
| bool | shouldContinue |
| Loop-continuation flag; cleared by stop(). | |
| bool | isRunning |
| Set by the worker while its loop is active. | |
Friends | |
| class | NetworkChannel |
Central owner of all channels, listeners and connections in a process.
Create one NetworkManager, then use the create*() factories to open listeners and outgoing connections. Each factory associates the new listener/connection with a NetworkChannel (identified by channelID; a new channel is created when needed) and an optional NetworkReceiver for push-mode dispatch. The manager's own thread supervises delayed connects and reconnection.
It is also a complete embedded HTTP(S)/WebSocket server — e.g. the PsyProbe web monitoring UI is served by a NetworkManager HTTP listener. Ports are always caller-chosen; there is no built-in default port.
timeoutMS unless a delayed/async variant is used. Definition at line 161 of file NetworkManager.h.
| cmlabs::NetworkManager::NetworkManager | ( | ) |
Definition at line 70 of file NetworkManager.cpp.
References lastChannelID, lastConnectionID, sslAllowSelfSigned, sslCASet, and udpOutputCon.
Referenced by TestHTTP(), UnitTest(), UnitTestDelayedConnect(), UnitTestHTTP(), and WebsocketTest().
|
virtual |
Definition at line 78 of file NetworkManager.cpp.
References channels, channelsByConnection, listeners, mapMutex, udpListeners, and udpOutputCon.
| uint64 cmlabs::NetworkManager::addConnection | ( | NetworkChannel * | channel | ) |
Register an externally created channel with the manager.
| channel | Channel to adopt (ownership transfers). |
Definition at line 455 of file NetworkManager.cpp.
References lastConnectionID, mapMutex, NetworkChannel, and registerConnection().
| NetworkChannel * cmlabs::NetworkManager::addTCPConnection | ( | const char * | addr, |
| uint16 | port, | ||
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| uint32 | channelID, | ||
| NetworkReceiver * | recv, | ||
| uint64 & | conid, | ||
| uint64 & | location, | ||
| uint32 | timeoutMS = 5000, | ||
| const char * | greetingData = NULL, | ||
| uint32 | greetingSize = 0 ) |
Like createTCPConnection() but sends optional greeting bytes right after connecting (peer identification / protocol selection) and never auto-reconnects.
| addr | Host address. |
| port | TCP port. |
| location | Out: resolved endpoint (packed uint64). |
| encryption | NOENC or SSLENC. |
| protocol | Single PROTOCOL_* flag. |
| isAsync | Push mode when true. |
| channelID | Channel to attach to (0 = new). |
| recv | Receiver for async dispatch. |
| conid | Out: new connection id. |
| timeoutMS | Connect timeout in ms. |
| greetingData | Bytes sent immediately after connect (copied). |
| greetingSize | Byte count. |
Definition at line 331 of file NetworkManager.cpp.
References cmlabs::NetworkChannel::addTCPConnection(), allocateChannelID(), cmlabs::NetworkChannel::cid, findChannelByID(), LOG_NETWORK, LogPrint, NetworkChannel, registerChannel(), registerConnection(), cmlabs::NetworkChannel::setNewReceiver(), and unregisterChannel().
Referenced by UnitTestDelayedConnect().
| NetworkChannel * cmlabs::NetworkManager::addTCPConnection | ( | uint64 | location, |
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| uint32 | channelID, | ||
| NetworkReceiver * | recv, | ||
| uint64 & | conid, | ||
| uint32 | timeoutMS = 5000, | ||
| const char * | greetingData = NULL, | ||
| uint32 | greetingSize = 0 ) |
Packed-endpoint variant of addTCPConnection().
| location | Packed endpoint. |
| encryption | NOENC or SSLENC. |
| protocol | Single PROTOCOL_* flag. |
| isAsync | Push mode when true. |
| channelID | Channel to attach to (0 = new). |
| recv | Receiver for async dispatch. |
| conid | Out: new connection id. |
| timeoutMS | Connect timeout in ms. |
| greetingData | Bytes sent immediately after connect (copied). |
| greetingSize | Byte count. |
Definition at line 363 of file NetworkManager.cpp.
References cmlabs::NetworkChannel::addTCPConnection(), allocateChannelID(), cmlabs::NetworkChannel::cid, findChannelByID(), GETIPADDRESSQUADPORT, LOG_NETWORK, LogPrint, NetworkChannel, registerChannel(), registerConnection(), cmlabs::NetworkChannel::setNewReceiver(), and unregisterChannel().
|
protected |
Allocate the next unused channel id.
Definition at line 528 of file NetworkManager.cpp.
References channels, lastChannelID, and mapMutex.
Referenced by addTCPConnection(), addTCPConnection(), createListener(), createTCPConnection(), createTCPConnection(), createTCPConnection(), createUDPConnection(), createWebsocketConnection(), and createWebsocketConnection().
| NetworkChannel * cmlabs::NetworkManager::createListener | ( | uint16 | port, |
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| uint32 | protocolTimeout, | ||
| bool | isDefaultProtocol, | ||
| uint32 | channelID, | ||
| NetworkReceiver * | recv ) |
Open a listening port for one or more protocols.
| port | TCP port to bind (caller-chosen; no default). |
| encryption | NOENC or SSLENC (certificate must be set first). |
| protocol | PROTOCOL_* bit flags; several may be OR'ed for auto-detection. |
| isAsync | Push items to recv when true; queue for waitFor*() when false. |
| protocolTimeout | Milliseconds allowed to sniff/auto-detect a new connection's protocol. |
| isDefaultProtocol | Treat the first protocol flag as fallback when detection is inconclusive. |
| channelID | Channel to attach to (0 = allocate a new channel). |
| recv | Receiver for async dispatch (may be NULL for sync mode). |
Definition at line 116 of file NetworkManager.cpp.
References allocateChannelID(), cmlabs::NetworkChannel::cid, findChannelByID(), getTCPConnectionByPort(), NetworkChannel, registerChannel(), registerTCPListener(), cmlabs::NetworkChannel::setNewReceiver(), cmlabs::NetworkChannel::startListener(), unregisterChannel(), and unregisterTCPListener().
Referenced by cmlabs::Test_RequestClient(), UnitTest(), UnitTestDelayedConnect(), UnitTestHTTP(), and WebsocketTest().
| NetworkChannel * cmlabs::NetworkManager::createTCPConnection | ( | const char * | addr, |
| uint16 | port, | ||
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect, | ||
| uint32 | channelID, | ||
| NetworkReceiver * | recv, | ||
| uint64 & | conid, | ||
| uint64 & | location, | ||
| uint32 | timeoutMS = 5000 ) |
Connect to a remote host by name/IP and start the protocol thread.
| addr | Host name or IPv4 string. |
| port | Remote port. |
| encryption | NOENC or SSLENC. |
| protocol | Single PROTOCOL_* flag. |
| isAsync | Push mode when true. |
| autoreconnect | Re-establish automatically on failure. |
| channelID | Channel to attach to (0 = new). |
| recv | Receiver for async dispatch. |
| conid | Out: new connection id. |
| location | Out: resolved endpoint (packed uint64). |
| timeoutMS | Connect timeout in ms. |
timeoutMS. Definition at line 161 of file NetworkManager.cpp.
References allocateChannelID(), cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createTCPConnection(), findChannelByID(), LOG_NETWORK, LogPrint, NetworkChannel, registerChannel(), registerConnection(), cmlabs::NetworkChannel::setNewReceiver(), and unregisterChannel().
Referenced by cmlabs::HTTPServerTest(), makeHTTPRequest(), makeHTTPRequest(), makeHTTPRequest(), TestHTTP(), UnitTest(), UnitTestHTTP(), and WebsocketTest().
| NetworkChannel * cmlabs::NetworkManager::createTCPConnection | ( | const uint32 * | addresses, |
| uint16 | addressCount, | ||
| uint16 | port, | ||
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect, | ||
| uint32 | channelID, | ||
| NetworkReceiver * | recv, | ||
| uint64 & | conid, | ||
| uint64 & | location, | ||
| uint32 | timeoutMS = 5000 ) |
Connect trying several candidate IPv4 addresses for one host.
| addresses | Candidate addresses. |
| addressCount | Number of candidates. |
| port | TCP port. |
| location | Out: resolved endpoint (packed uint64). |
| encryption | NOENC or SSLENC. |
| protocol | Single PROTOCOL_* flag. |
| isAsync | Push mode when true. |
| channelID | Channel to attach to (0 = new). |
| recv | Receiver for async dispatch. |
| conid | Out: new connection id. |
| timeoutMS | Connect timeout in ms. |
| autoreconnect | Re-establish automatically on failure. |
Definition at line 236 of file NetworkManager.cpp.
References allocateChannelID(), cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createTCPConnection(), findChannelByID(), GETIPADDRESSQUADPORT, LOG_NETWORK, LogPrint, NetworkChannel, registerChannel(), registerConnection(), cmlabs::NetworkChannel::setNewReceiver(), and unregisterChannel().
| NetworkChannel * cmlabs::NetworkManager::createTCPConnection | ( | uint64 | location, |
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect, | ||
| uint32 | channelID, | ||
| NetworkReceiver * | recv, | ||
| uint64 & | conid, | ||
| uint32 | timeoutMS = 5000 ) |
Connect to a packed uint64 endpoint.
See the string-address overload for details.
| location | Packed endpoint (address+port). |
| encryption | NOENC or SSLENC. |
| protocol | Single PROTOCOL_* flag. |
| isAsync | Push mode when true. |
| channelID | Channel to attach to (0 = new). |
| recv | Receiver for async dispatch. |
| conid | Out: new connection id. |
| timeoutMS | Connect timeout in ms. |
| autoreconnect | Re-establish automatically on failure. |
Definition at line 193 of file NetworkManager.cpp.
References allocateChannelID(), cmlabs::NetworkChannel::cid, cmlabs::TCPConnection::connect(), cmlabs::NetworkChannel::createTCPConnection(), findChannelByID(), GETIPADDRESSQUADPORT, LOG_NETWORK, LogPrint, NetworkChannel, registerChannel(), registerConnection(), cmlabs::NetworkChannel::setNewReceiver(), cmlabs::NetworkChannel::startConnection(), and unregisterChannel().
| NetworkChannel * cmlabs::NetworkManager::createUDPConnection | ( | uint16 | port, |
| uint8 | protocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect, | ||
| uint32 | channelID, | ||
| NetworkReceiver * | recv, | ||
| uint64 & | conid ) |
Bind a UDP port for datagram traffic.
| port | Local UDP port. |
| protocol | PROTOCOL_* flag (typically PROTOCOL_MESSAGE). |
| isAsync | Push mode when true. |
| autoreconnect | Rebind on failure. |
| channelID | Channel to attach to (0 = new). |
| recv | Receiver. |
| conid | Out: new connection id. |
Definition at line 397 of file NetworkManager.cpp.
References allocateChannelID(), cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createUDPConnection(), findChannelByID(), NetworkChannel, registerChannel(), registerConnection(), registerUDPListener(), cmlabs::NetworkChannel::setNewReceiver(), and unregisterChannel().
Referenced by UnitTest().
| NetworkChannel * cmlabs::NetworkManager::createWebsocketConnection | ( | const char * | uri, |
| const char * | addr, | ||
| uint16 | port, | ||
| uint8 | encryption, | ||
| uint32 | channelID, | ||
| NetworkReceiver * | recv, | ||
| uint64 & | conid, | ||
| const char * | protocolName = NULL, | ||
| const char * | origin = NULL, | ||
| uint32 | timeoutMS = 5000 ) |
Open a client WebSocket connection from explicit components.
| uri | Request path. |
| addr | Host. |
| port | Port. |
| encryption | NOENC or SSLENC. |
| channelID | Channel to attach to (0 = new). |
| recv | Receiver for frames. |
| conid | Out: new connection id. |
| protocolName | Optional Sec-WebSocket-Protocol. |
| origin | Optional Origin header. |
| timeoutMS | Connect+handshake timeout in ms. |
Definition at line 299 of file NetworkManager.cpp.
References allocateChannelID(), cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createWebsocketConnection(), findChannelByID(), LOG_NETWORK, LogPrint, NetworkChannel, registerChannel(), registerConnection(), cmlabs::NetworkChannel::setNewReceiver(), and unregisterChannel().
| NetworkChannel * cmlabs::NetworkManager::createWebsocketConnection | ( | const char * | url, |
| uint32 | channelID, | ||
| NetworkReceiver * | recv, | ||
| uint64 & | conid, | ||
| const char * | protocolName = NULL, | ||
| const char * | origin = NULL, | ||
| uint32 | timeoutMS = 5000 ) |
Open a client WebSocket connection from a full URL (ws:// or wss://).
| url | Full URL including host, port and path. |
| channelID | Channel to attach to (0 = new). |
| recv | Receiver for frames. |
| conid | Out: new connection id. |
| protocolName | Optional Sec-WebSocket-Protocol. |
| origin | Optional Origin header. |
| timeoutMS | Connect+handshake timeout in ms. |
timeoutMS. Definition at line 268 of file NetworkManager.cpp.
References allocateChannelID(), cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createWebsocketConnection(), findChannelByID(), LOG_NETWORK, LogPrint, NetworkChannel, registerChannel(), registerConnection(), cmlabs::NetworkChannel::setNewReceiver(), and unregisterChannel().
Referenced by cmlabs::TestWebSocketRequestClient::run().
| bool cmlabs::NetworkManager::endConnection | ( | uint64 | conid | ) |
Gracefully close a connection (thread is asked to finish; entry kept for reuse).
| conid | Connection id. |
Definition at line 436 of file NetworkManager.cpp.
References cmlabs::NetworkChannel::endConnection(), findChannelByConnection(), and NetworkChannel.
| bool cmlabs::NetworkManager::endUDPConnection | ( | uint16 | port | ) |
Close the UDP connection bound to port.
Definition at line 447 of file NetworkManager.cpp.
References cmlabs::NetworkChannel::endUDPConnection(), getUDPConnectionByPort(), and NetworkChannel.
| NetworkChannel * cmlabs::NetworkManager::findChannelByConnection | ( | uint64 | conid | ) |
Look up the channel owning a CONNECTION id.
| conid | Connection id. |
Definition at line 542 of file NetworkManager.cpp.
References channelsByConnection, mapMutex, and NetworkChannel.
Referenced by endConnection(), getConnection(), getConnectionType(), and getRemoteAddress().
| NetworkChannel * cmlabs::NetworkManager::findChannelByID | ( | uint32 | channelID | ) |
Look up a channel by CHANNEL id.
| channelID | Channel id. |
Definition at line 553 of file NetworkManager.cpp.
References channels, mapMutex, and NetworkChannel.
Referenced by addTCPConnection(), addTCPConnection(), createListener(), createTCPConnection(), createTCPConnection(), createTCPConnection(), createUDPConnection(), createWebsocketConnection(), and createWebsocketConnection().
| NetworkChannel * cmlabs::NetworkManager::getConnection | ( | uint64 | conid | ) |
| conid | Connection id. |
Definition at line 562 of file NetworkManager.cpp.
References findChannelByConnection(), and NetworkChannel.
| uint8 cmlabs::NetworkManager::getConnectionType | ( | uint64 | conid | ) |
| conid | Connection id. |
Definition at line 464 of file NetworkManager.cpp.
References findChannelByConnection(), cmlabs::NetworkChannel::getConnectionType(), and NetworkChannel.
| uint64 cmlabs::NetworkManager::getRemoteAddress | ( | uint64 | conid | ) |
| conid | Connection id. |
Definition at line 471 of file NetworkManager.cpp.
References findChannelByConnection(), cmlabs::NetworkChannel::getRemoteAddress(), and NetworkChannel.
| NetworkChannel * cmlabs::NetworkManager::getTCPConnectionByPort | ( | uint16 | port | ) |
| port | Listener port. |
Definition at line 566 of file NetworkManager.cpp.
References listeners, mapMutex, and NetworkChannel.
Referenced by createListener(), and stopListener().
| NetworkChannel * cmlabs::NetworkManager::getUDPConnectionByPort | ( | uint16 | port | ) |
| port | UDP port. |
Definition at line 575 of file NetworkManager.cpp.
References mapMutex, NetworkChannel, and udpListeners.
Referenced by endUDPConnection().
| HTTPReply * cmlabs::NetworkManager::makeHTTPRequest | ( | const char * | url, |
| uint32 | timeout, | ||
| const char * | content = NULL, | ||
| uint32 | contentSize = 0 ) |
Blocking HTTP(S) exchange from a URL string (GET, or POST when content given).
| url | Full URL (http:// or https://, may embed basic-auth credentials). |
| timeout | Total timeout in ms. |
| content | Optional request body (POST). |
| contentSize | Body size. |
Definition at line 604 of file NetworkManager.cpp.
References cmlabs::HTTPReply::CreateErrorReply(), cmlabs::HTTPRequest::createRequest(), cmlabs::html::GetHostFromURL(), cmlabs::html::GetPortFromURL(), cmlabs::html::GetProtocolFromURL(), cmlabs::html::GetURIFromURL(), HTTP_GET, HTTP_MALFORMED_URL, HTTP_NOT_IMPLEMENTED, HTTP_POST, makeHTTPRequest(), NOENC, and SSLENC.
Referenced by makeHTTPRequest(), and cmlabs::TestWebRequestClient::run().
| HTTPReply * cmlabs::NetworkManager::makeHTTPRequest | ( | HTTPRequest * | req, |
| const char * | addr, | ||
| uint16 | port, | ||
| uint8 | encryption, | ||
| uint32 | timeout ) |
Blocking exchange sending a pre-built request to an explicit endpoint.
| req | Request to send (caller keeps ownership). |
| addr | Host. |
| port | Port. |
| encryption | NOENC or SSLENC. |
| timeout | Total timeout in ms. |
Definition at line 641 of file NetworkManager.cpp.
References cmlabs::HTTPReply::CreateErrorReply(), createTCPConnection(), cmlabs::NetworkChannel::endConnection(), HTTP_SERVER_UNAVAILABLE, NetworkChannel, NOENC, PROTOCOL_HTTP_CLIENT, cmlabs::NetworkChannel::sendReceiveHTTPRequest(), and SSLENC.
| HTTPReply * cmlabs::NetworkManager::makeHTTPRequest | ( | uint8 | ops, |
| std::string | addr, | ||
| uint32 | timeout, | ||
| std::map< std::string, std::string > & | headerEntries, | ||
| const char * | content, | ||
| const char * | contentType, | ||
| uint32 | contentSize, | ||
| bool | keepAlive = false, | ||
| uint64 | ifModifiedSince = 0 ) |
Blocking exchange with custom headers and typed raw body.
| ops | HTTP_* method id. |
| addr | Full URL or host address. |
| timeout | ms. |
| headerEntries | Extra header name/value pairs. |
| content | Body bytes. |
| contentType | Body MIME type. |
| contentSize | Body size. |
| keepAlive | Request a persistent connection. |
| ifModifiedSince | ms epoch or 0. |
Definition at line 664 of file NetworkManager.cpp.
References cmlabs::HTTPReply::CreateErrorReply(), cmlabs::HTTPRequest::createRequest(), createTCPConnection(), cmlabs::NetworkChannel::endConnection(), cmlabs::html::GetHostFromURL(), cmlabs::html::GetPortFromURL(), cmlabs::html::GetProtocolFromURL(), cmlabs::html::GetURIFromURL(), HTTP_MALFORMED_URL, HTTP_SERVER_UNAVAILABLE, NetworkChannel, NOENC, PROTOCOL_HTTP_CLIENT, cmlabs::NetworkChannel::sendReceiveHTTPRequest(), SSLENC, and stricmp.
| HTTPReply * cmlabs::NetworkManager::makeHTTPRequest | ( | uint8 | ops, |
| std::string | addr, | ||
| uint32 | timeout, | ||
| std::map< std::string, std::string > & | headerEntries, | ||
| std::map< std::string, HTTPPostEntry * > & | bodyEntries, | ||
| bool | keepAlive = false, | ||
| uint64 | ifModifiedSince = 0 ) |
Blocking multipart/form-data exchange (file uploads, form posts).
| ops | HTTP_* method id. |
| addr | Full URL or host address. |
| timeout | ms. |
| headerEntries | Extra headers. |
| bodyEntries | Named body parts (not owned). |
| keepAlive | Persistent connection flag. |
| ifModifiedSince | ms epoch or 0. |
Definition at line 717 of file NetworkManager.cpp.
References cmlabs::HTTPReply::CreateErrorReply(), cmlabs::HTTPRequest::createMultipartRequest(), cmlabs::HTTPRequest::createRequest(), createTCPConnection(), cmlabs::NetworkChannel::endConnection(), cmlabs::html::GetHostFromURL(), cmlabs::html::GetPortFromURL(), cmlabs::html::GetProtocolFromURL(), cmlabs::html::GetURIFromURL(), HTTP_MALFORMED_URL, HTTP_SERVER_UNAVAILABLE, NetworkChannel, NOENC, PROTOCOL_HTTP_CLIENT, cmlabs::NetworkChannel::sendReceiveHTTPRequest(), SSLENC, and stricmp.
| THREAD_RET THREAD_FUNCTION_CALL cmlabs::NetworkManager::NetworkManagerRun | ( | THREAD_ARG | arg | ) |
Thread entry point of the manager's supervision loop (do not call directly).
Definition at line 66 of file NetworkManager.cpp.
References THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
|
protected |
Register a channel under a channel id, and optionally a TCP listener port.
Definition at line 483 of file NetworkManager.cpp.
References channels, mapMutex, and NetworkChannel.
Referenced by addTCPConnection(), addTCPConnection(), createListener(), createTCPConnection(), createTCPConnection(), createTCPConnection(), createUDPConnection(), createWebsocketConnection(), and createWebsocketConnection().
|
protected |
Map a connection id to its owning channel.
Definition at line 497 of file NetworkManager.cpp.
References channelsByConnection, mapMutex, and NetworkChannel.
Referenced by addConnection(), addTCPConnection(), addTCPConnection(), createTCPConnection(), createTCPConnection(), createTCPConnection(), createUDPConnection(), createWebsocketConnection(), and createWebsocketConnection().
|
protected |
Bind a TCP listener port to a channel.
Definition at line 504 of file NetworkManager.cpp.
References listeners, mapMutex, and NetworkChannel.
Referenced by createListener().
|
protected |
Bind a UDP listener port to a channel.
Definition at line 518 of file NetworkManager.cpp.
References mapMutex, NetworkChannel, and udpListeners.
Referenced by createUDPConnection().
| bool cmlabs::NetworkManager::removeConnection | ( | uint64 | conid | ) |
Close a connection and remove it from the manager's maps entirely.
| conid | Connection id. |
Definition at line 426 of file NetworkManager.cpp.
References channelsByConnection, and mapMutex.
|
protected |
Supervision loop body (delayed connects, reconnection).
| bool cmlabs::NetworkManager::sendUDPMessage | ( | DataMessage * | msg, |
| uint64 | destination ) |
Send a DataMessage as a UDP datagram via the shared output socket.
| msg | Message (caller keeps ownership). |
| destination | Packed uint64 endpoint. |
Definition at line 584 of file NetworkManager.cpp.
References cmlabs::MessageProtocol::SendMessage(), udpOutputCon, and udpOutputConMutex.
Referenced by UnitTest().
|
inline |
Definition at line 199 of file NetworkManager.h.
References sslAllowSelfSigned.
|
inline |
Definition at line 203 of file NetworkManager.h.
| bool cmlabs::NetworkManager::setSSLCertificate | ( | const char * | sslCertPath, |
| const char * | sslKeyPath ) |
Set the certificate/key used by SSL listeners created via this manager.
| sslCertPath | PEM certificate path. |
| sslKeyPath | PEM private key path. |
Definition at line 109 of file NetworkManager.cpp.
References sslCertPath, and sslKeyPath.
| bool cmlabs::NetworkManager::stopListener | ( | uint16 | port, |
| uint8 | protocol ) |
Stop a listener previously opened with createListener().
| port | Listener port. |
| protocol | PROTOCOL_* flags it was created with. |
Definition at line 150 of file NetworkManager.cpp.
References getTCPConnectionByPort(), NetworkChannel, cmlabs::NetworkChannel::stopListener(), and unregisterTCPListener().
|
static |
Fetch a list of URLs from a host and report results (manual test helper).
| host | Server host. |
| port | Server port. |
| urls | URI paths to fetch. |
Definition at line 3507 of file NetworkManager.cpp.
References createTCPConnection(), cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkChannel::endConnection(), cmlabs::HTTPServerTestData::host, cmlabs::HTTPServerTest(), cmlabs::ThreadManager::IsThreadRunning(), LOG_SYSTEM, LogPrint, cmlabs::HTTPServerTestData::manager, NetworkChannel, NetworkManager(), NOENC, cmlabs::HTTPServerTestData::port, PROTOCOL_HTTP_CLIENT, cmlabs::utils::Sleep(), cmlabs::HTTPServerTestData::status, and cmlabs::HTTPServerTestData::urls.
|
static |
Basic TCP/message-protocol round-trip self-test.
Definition at line 2387 of file NetworkManager.cpp.
References createListener(), createTCPConnection(), createUDPConnection(), cmlabs::CTRL_TEST, cmlabs::unittest::detail(), cmlabs::NetworkChannel::endConnection(), cmlabs::unittest::fail(), cmlabs::DataMessage::getFrom(), GETIPADDRESSPORT, cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), cmlabs::DataMessage::getType(), LOCALHOSTIP, cmlabs::unittest::metric(), NetworkChannel, NetworkManager(), NOENC, cmlabs::PrintTimeDifString(), cmlabs::unittest::progress(), PROTOCOL_MESSAGE, cmlabs::NetworkChannel::sendMessage(), sendUDPMessage(), cmlabs::DataMessage::setData(), cmlabs::DataMessage::setType(), cmlabs::ThreadManager::Shutdown(), cmlabs::utils::Sleep(), and cmlabs::NetworkChannel::waitForMessage().
Referenced by cmlabs::Register_NetworkManager_Tests().
|
static |
Self-test of non-blocking (delayed) connect handling.
Definition at line 2317 of file NetworkManager.cpp.
References cmlabs::NetworkChannel::addTCPConnection(), addTCPConnection(), createListener(), cmlabs::DataMessage::data, cmlabs::unittest::detail(), cmlabs::unittest::fail(), cmlabs::DataMessage::getSize(), NetworkChannel, NetworkManager(), NOENC, cmlabs::unittest::progress(), PROTOCOL_MESSAGE, cmlabs::DataMessage::setString(), cmlabs::ThreadManager::Shutdown(), and cmlabs::NetworkChannel::waitForMessage().
Referenced by cmlabs::Register_NetworkManager_Tests().
|
static |
Self-test of the built-in HTTP server and client.
Definition at line 2746 of file NetworkManager.cpp.
References createListener(), cmlabs::HTTPRequest::createRequest(), createTCPConnection(), cmlabs::unittest::detail(), cmlabs::NetworkChannel::endConnection(), cmlabs::unittest::fail(), cmlabs::GetTimeNow(), HTTP_GET, cmlabs::unittest::metric(), NetworkChannel, NetworkManager(), NOENC, cmlabs::unittest::progress(), PROTOCOL_HTTP_CLIENT, PROTOCOL_HTTP_SERVER, cmlabs::NetworkChannel::sendReceiveHTTPRequest(), and cmlabs::ThreadManager::Shutdown().
Referenced by cmlabs::Register_NetworkManager_Tests().
|
protected |
Remove a channel id (and nothing else) from the registry.
Definition at line 490 of file NetworkManager.cpp.
References channels, and mapMutex.
Referenced by addTCPConnection(), addTCPConnection(), createListener(), createTCPConnection(), createTCPConnection(), createTCPConnection(), createUDPConnection(), createWebsocketConnection(), and createWebsocketConnection().
|
protected |
Unbind a TCP listener port.
Definition at line 511 of file NetworkManager.cpp.
References listeners, and mapMutex.
Referenced by createListener(), and stopListener().
|
static |
WebSocket upgrade + echo self-test.
Definition at line 3439 of file NetworkManager.cpp.
References createListener(), createTCPConnection(), cmlabs::HTTPRequest::createWebsocketRequest(), cmlabs::unittest::detail(), cmlabs::NetworkChannel::endConnection(), cmlabs::unittest::fail(), cmlabs::HTTPReply::isWebsocketUpgrade(), NetworkChannel, NetworkManager(), NOENC, cmlabs::unittest::progress(), PROTOCOL_HTTP_CLIENT, PROTOCOL_HTTP_SERVER, cmlabs::NetworkChannel::sendReceiveHTTPRequest(), and cmlabs::ThreadManager::Shutdown().
Referenced by cmlabs::Register_NetworkManager_Tests().
|
friend |
Definition at line 162 of file NetworkManager.h.
References NetworkChannel, sslCertPath, sslKeyPath, THREAD_ARG, THREAD_FUNCTION_CALL, and THREAD_RET.
Referenced by addConnection(), addTCPConnection(), addTCPConnection(), createListener(), createTCPConnection(), createTCPConnection(), createTCPConnection(), createUDPConnection(), createWebsocketConnection(), createWebsocketConnection(), endConnection(), endUDPConnection(), findChannelByConnection(), findChannelByID(), getConnection(), getConnectionType(), getRemoteAddress(), getTCPConnectionByPort(), getUDPConnectionByPort(), makeHTTPRequest(), makeHTTPRequest(), makeHTTPRequest(), NetworkChannel, registerChannel(), registerConnection(), registerTCPListener(), registerUDPListener(), stopListener(), TestHTTP(), UnitTest(), UnitTestDelayedConnect(), UnitTestHTTP(), and WebsocketTest().
|
protected |
All channels by channel id (owned).
Definition at line 382 of file NetworkManager.h.
Referenced by allocateChannelID(), findChannelByID(), registerChannel(), unregisterChannel(), and ~NetworkManager().
|
protected |
Channel lookup by connection id.
Definition at line 383 of file NetworkManager.h.
Referenced by findChannelByConnection(), registerConnection(), removeConnection(), and ~NetworkManager().
|
protected |
Last channel id issued.
Definition at line 403 of file NetworkManager.h.
Referenced by allocateChannelID(), and NetworkManager().
|
protected |
Last connection id issued.
Definition at line 404 of file NetworkManager.h.
Referenced by addConnection(), and NetworkManager().
|
protected |
TCP listeners by port.
Definition at line 384 of file NetworkManager.h.
Referenced by getTCPConnectionByPort(), registerTCPListener(), unregisterTCPListener(), and ~NetworkManager().
|
protected |
Serialises ALL four maps above.
They are read and written from BOTH the owning thread and connection worker threads: a worker's teardown path runs removeConnection() (erasing a node) while another thread can be inserting via a create*Connection() factory or looking one up - concurrent mutation of one std::map, which corrupts the tree (measured: 11/30 network_http under ASan, heap-use-after-free on tree nodes). ⚠️ NEVER hold this across a call into NetworkChannel: the channel's teardown joins its worker, and that worker calls removeConnection() which needs this lock - hold-across-join deadlock. Copy the pointer out, release, then call.
Definition at line 395 of file NetworkManager.h.
Referenced by addConnection(), allocateChannelID(), findChannelByConnection(), findChannelByID(), getTCPConnectionByPort(), getUDPConnectionByPort(), registerChannel(), registerConnection(), registerTCPListener(), registerUDPListener(), removeConnection(), unregisterChannel(), unregisterTCPListener(), and ~NetworkManager().
| int8 cmlabs::NetworkManager::sslAllowSelfSigned |
Definition at line 200 of file NetworkManager.h.
Referenced by NetworkManager(), and setSSLAllowSelfSigned().
| std::string cmlabs::NetworkManager::sslCAFile |
Definition at line 204 of file NetworkManager.h.
Referenced by setSSLCALocation().
| std::string cmlabs::NetworkManager::sslCAPath |
Definition at line 205 of file NetworkManager.h.
Referenced by setSSLCALocation().
| bool cmlabs::NetworkManager::sslCASet |
Definition at line 206 of file NetworkManager.h.
Referenced by NetworkManager(), and setSSLCALocation().
|
protected |
PEM certificate path for SSL listeners.
Definition at line 397 of file NetworkManager.h.
Referenced by NetworkChannel, and setSSLCertificate().
|
protected |
PEM private key path for SSL listeners.
Definition at line 398 of file NetworkManager.h.
Referenced by NetworkChannel, and setSSLCertificate().
|
protected |
UDP listeners by port.
Definition at line 385 of file NetworkManager.h.
Referenced by getUDPConnectionByPort(), registerUDPListener(), and ~NetworkManager().
|
protected |
Shared output-only UDP socket for sendUDPMessage().
Definition at line 400 of file NetworkManager.h.
Referenced by NetworkManager(), sendUDPMessage(), and ~NetworkManager().
|
protected |
Serialises use of ::udpOutputCon.
Definition at line 401 of file NetworkManager.h.
Referenced by sendUDPMessage().