|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
One logical network interface: a group of listeners/connections with shared dispatch. More...
#include <NetworkManager.h>
Public Member Functions | |
| NetworkChannel (NetworkManager *manager) | |
| virtual | ~NetworkChannel () |
| bool | isConnected (uint64 conid) |
| bool | shutdown () |
| Stop all worker threads and close all listeners/connections of this channel. | |
| bool | startListener (uint64 cid, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, uint32 protocolTimeout=3000, bool isDefaultProtocol=false) |
| Open a listener on this channel (see NetworkManager::createListener() for semantics). | |
| bool | stopListener (uint16 port, uint8 protocol) |
| Stop a listener on this channel. | |
| uint64 | createTCPConnection (const char *addr, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint64 &location, uint32 timeoutMS=5000) |
| Connect to a host by name/IP; see NetworkManager::createTCPConnection(). | |
| uint64 | createTCPConnection (uint64 location, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint32 timeoutMS=5000) |
| Connect to a packed uint64 endpoint. | |
| uint64 | createTCPConnection (const uint32 *addresses, uint16 addressCount, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint64 &location, uint32 timeoutMS=5000) |
| Connect trying several candidate addresses. | |
| uint64 | createUDPConnection (uint16 port, uint8 protocol, bool isAsync, bool autoreconnect) |
| Bind a UDP port on this channel. | |
| uint64 | addTCPConnection (const char *addr, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, uint64 &location, uint32 timeoutMS=5000, const char *greetingData=NULL, uint32 greetingSize=0) |
| Connect with optional greeting bytes (no autoreconnect); see NetworkManager::addTCPConnection(). | |
| uint64 | addTCPConnection (uint64 location, uint8 encryption, uint8 protocol, bool isAsync, uint32 timeoutMS=5000, const char *greetingData=NULL, uint32 greetingSize=0) |
| Packed-endpoint variant of addTCPConnection(). | |
| uint64 | createWebsocketConnection (const char *url, const char *protocolName, const char *origin=NULL, uint32 timeoutMS=5000) |
| Open a client WebSocket connection from a full URL. | |
| uint64 | createWebsocketConnection (const char *uri, const char *addr, uint16 port, uint8 encryption, const char *protocolName, const char *origin=NULL, uint32 timeoutMS=5000) |
| Open a client WebSocket connection from explicit components. | |
| bool | endConnection (uint64 conid) |
| Gracefully close a connection. | |
| bool | endUDPConnection (uint16 port) |
Close the UDP connection bound to port. | |
| uint8 | getConnectionType (uint64 conid) |
| uint64 | getRemoteAddress (uint64 conid) |
| uint64 | autoDetectConnection (NetworkConnection *con, uint16 port, uint32 autoProtocols, uint32 autoProtocolTimeout, uint32 defaultProtocol, bool isAsync, bool autoreconnect) |
Adopt an incoming connection whose protocol is not yet known: sniff its first bytes against autoProtocols and start the matching handler thread. | |
| uint64 | startConnection (NetworkConnection *con, uint8 protocol, bool isAsync, bool autoreconnect, uint32 timeoutMS=5000) |
| Start the protocol worker thread for an already-connected connection. | |
| bool | setSSLAllowSelfSigned (bool allow) |
| bool | setSSLCALocation (const char *caFile, const char *caPath) |
| void | applySSLClientPolicy (SSLConnection *con) |
| bool | setNewReceiver (NetworkReceiver *recv) |
| Replace the channel's receiver for async dispatch. | |
| NetworkEvent * | waitForNetworkEvent (uint32 ms) |
| Wait for the next connection lifecycle event (sync mode). | |
| HTTPRequest * | waitForHTTPRequest (uint64 &conid, uint32 ms) |
| Wait for the next queued HTTP request (sync-mode HTTP server). | |
| TelnetLine * | waitForTelnetLine (uint64 &conid, uint32 ms) |
| Wait for the next queued Telnet line. | |
| DataMessage * | waitForMessage (uint64 &conid, uint32 ms) |
| Wait for the next queued DataMessage. | |
| HTTPReply * | waitForHTTPReply (uint64 &conid, uint32 ms) |
| Wait for the next queued HTTP reply (sync-mode HTTP client). | |
| WebsocketData * | waitForWebsocketData (uint64 &conid, uint32 ms) |
| Wait for the next queued WebSocket message. | |
| bool | sendHTTPReply (HTTPReply *reply, uint64 conid) |
| Send an HTTP reply on a server connection. | |
| bool | sendTelnetLine (TelnetLine *line, uint64 conid) |
| Send a Telnet line. | |
| bool | sendMessage (DataMessage *msg, uint64 conid) |
| Send a DataMessage. | |
| bool | sendHTTPRequest (HTTPRequest *req, uint64 conid) |
| Send an HTTP request without waiting for the reply (reply arrives via receiveHTTPReply()/waitForHTTPReply()). | |
| HTTPReply * | sendReceiveHTTPRequest (HTTPRequest *req, uint64 conid, uint32 timeout) |
| Send an HTTP request and block for its reply on the same connection. | |
| TelnetLine * | sendReceiveTelnetLine (TelnetLine *line, uint64 conid, uint32 timeout, uint32 size=0) |
| Send a Telnet line and block for the response line. | |
| bool | sendWebsocketData (WebsocketData *wsData, uint64 conid) |
| Send a WebSocket frame. | |
| uint32 | getOutputSpeed (uint64 conid) |
| uint32 | getInputSpeed (uint64 conid) |
Public Attributes | |
| uint32 | cid |
| Channel id within the manager. | |
| int8 | sslAllowSelfSigned |
| std::string | sslCAFile |
| std::string | sslCAPath |
| bool | sslCASet |
Protected Member Functions | |
| bool | enterNetworkEvent (uint8 type, uint8 protocol, uint64 conid) |
| Queue or push a lifecycle event. | |
| bool | enterHTTPRequest (HTTPRequest *req, uint64 conid) |
| Queue or push an incoming HTTP request. | |
| bool | enterHTTPReply (HTTPReply *reply, HTTPRequest *req, uint64 conid) |
| Queue or push an incoming HTTP reply. | |
| bool | enterMessage (DataMessage *msg, uint64 conid) |
| Queue or push an incoming DataMessage. | |
| bool | enterTelnetLine (TelnetLine *line, uint64 conid) |
| Queue or push an incoming Telnet line. | |
| bool | enterWebsocketData (WebsocketData *wsData, uint64 conid) |
| Queue or push an incoming WebSocket message. | |
Static Protected Member Functions | |
| static THREAD_RET THREAD_FUNCTION_CALL | NetworkListenerRun (THREAD_ARG arg) |
| Thread entry: accept loop for a TCP listener. | |
| static THREAD_RET THREAD_FUNCTION_CALL | ConnectionAutodetectRun (THREAD_ARG arg) |
| Thread entry: protocol sniffing for a fresh connection (see autoDetectConnection()). | |
| static THREAD_RET THREAD_FUNCTION_CALL | HTTPClientRun (THREAD_ARG arg) |
| Thread entry: HTTP client connection loop (send requests, parse replies). | |
| static THREAD_RET THREAD_FUNCTION_CALL | HTTPServerRun (THREAD_ARG arg) |
| Thread entry: HTTP/WebSocket server connection loop — the built-in web server's per-connection worker (parses requests, dispatches, sends replies, handles the WebSocket upgrade and subsequent frames). | |
| static THREAD_RET THREAD_FUNCTION_CALL | MessageConnectionRun (THREAD_ARG arg) |
| Thread entry: binary DataMessage connection loop. | |
| static THREAD_RET THREAD_FUNCTION_CALL | TelnetServerRun (THREAD_ARG arg) |
| Thread entry: Telnet server connection loop. | |
Friends | |
| class | NetworkManager |
One logical network interface: a group of listeners/connections with shared dispatch.
A channel bundles related listeners and connections (e.g. "the web interface on port 8080" or "the gateway link"), gives each its own worker thread, and funnels all parsed traffic either to the channel's NetworkReceiver (async) or into typed wait queues consumed via the waitFor*() methods (sync). Send methods are addressed by connection id.
Channels are created and owned by NetworkManager; user code normally obtains one from the manager's factory methods rather than constructing it directly.
Definition at line 474 of file NetworkManager.h.
| cmlabs::NetworkChannel::NetworkChannel | ( | NetworkManager * | manager | ) |
| manager | Owning manager (not owned by the channel). |
Definition at line 779 of file NetworkManager.cpp.
References manager, NetworkManager, receiver, sslAllowSelfSigned, and sslCASet.
Referenced by HTTPClientRun(), HTTPServerRun(), MessageConnectionRun(), and TelnetServerRun().
|
virtual |
Definition at line 807 of file NetworkManager.cpp.
References manager, and shutdown().
| uint64 cmlabs::NetworkChannel::addTCPConnection | ( | const char * | addr, |
| uint16 | port, | ||
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| uint64 & | location, | ||
| uint32 | timeoutMS = 5000, | ||
| const char * | greetingData = NULL, | ||
| uint32 | greetingSize = 0 ) |
Connect with optional greeting bytes (no autoreconnect); see NetworkManager::addTCPConnection().
Definition at line 1141 of file NetworkManager.cpp.
References applySSLClientPolicy(), cmlabs::SSLConnection::delayedConnect(), cmlabs::TCPConnection::delayedConnect(), cmlabs::SSLConnection::init(), NOENC, cmlabs::NetworkConnection::setGreetingData(), SSLENC, and startConnection().
Referenced by cmlabs::NetworkManager::addTCPConnection(), cmlabs::NetworkManager::addTCPConnection(), and cmlabs::NetworkManager::UnitTestDelayedConnect().
| uint64 cmlabs::NetworkChannel::addTCPConnection | ( | uint64 | location, |
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| uint32 | timeoutMS = 5000, | ||
| const char * | greetingData = NULL, | ||
| uint32 | greetingSize = 0 ) |
Packed-endpoint variant of addTCPConnection().
Definition at line 1171 of file NetworkManager.cpp.
References applySSLClientPolicy(), cmlabs::SSLConnection::delayedConnect(), cmlabs::TCPConnection::delayedConnect(), cmlabs::SSLConnection::init(), NOENC, cmlabs::NetworkConnection::setGreetingData(), SSLENC, and startConnection().
| void cmlabs::NetworkChannel::applySSLClientPolicy | ( | SSLConnection * | con | ) |
Definition at line 790 of file NetworkManager.cpp.
References manager, cmlabs::SSLConnection::setAllowSelfSigned(), cmlabs::SSLConnection::setCALocation(), sslAllowSelfSigned, sslCAFile, sslCAPath, and sslCASet.
Referenced by addTCPConnection(), addTCPConnection(), createTCPConnection(), createTCPConnection(), createTCPConnection(), and createWebsocketConnection().
| uint64 cmlabs::NetworkChannel::autoDetectConnection | ( | NetworkConnection * | con, |
| uint16 | port, | ||
| uint32 | autoProtocols, | ||
| uint32 | autoProtocolTimeout, | ||
| uint32 | defaultProtocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect ) |
Adopt an incoming connection whose protocol is not yet known: sniff its first bytes against autoProtocols and start the matching handler thread.
| con | The fresh connection (ownership transfers). |
| port | Listener port it arrived on. |
| autoProtocols | PROTOCOL_* bit set to test. |
| autoProtocolTimeout | Max sniffing time in ms. |
| defaultProtocol | Fallback protocol when detection is inconclusive (0 = drop). |
| isAsync | Push vs queue dispatch. |
| autoreconnect | Reconnect on failure. |
Definition at line 1696 of file NetworkManager.cpp.
References cmlabs::NetworkThread::autoProtocols, cmlabs::NetworkThread::autoProtocolTimeout, cmlabs::NetworkThread::autoreconnect, channelMutex, cmlabs::NetworkThread::con, ConnectionAutodetectRun(), connectionThreads, cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkThread::defaultProtocol, cmlabs::ThreadManager::GetThreadHandle(), cmlabs::NetworkThread::hWorker, cmlabs::NetworkThread::isAsync, LOG_NETWORK, LogPrint, manager, cmlabs::NetworkThread::parent, and cmlabs::NetworkThread::threadID.
Referenced by NetworkListenerRun().
|
staticprotected |
Thread entry: protocol sniffing for a fresh connection (see autoDetectConnection()).
Definition at line 1809 of file NetworkManager.cpp.
References cmlabs::NetworkThread::autoProtocols, cmlabs::NetworkThread::autoProtocolTimeout, cmlabs::HTTPProtocol::CheckBufferForCompatibility(), cmlabs::MessageProtocol::CheckBufferForCompatibility(), cmlabs::TelnetProtocol::CheckBufferForCompatibility(), cmlabs::NetworkThread::con, cmlabs::NetworkThread::defaultProtocol, enterNetworkEvent(), cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), HTTPClientRun(), HTTPServerRun(), cmlabs::NetworkThread::id, cmlabs::NetworkThread::isRunning, LOG_NETWORK, LogPrint, MessageConnectionRun(), NETWORKEVENT_CONNECT, cmlabs::NetworkThread::parent, PROTOCOL_HTTP_CLIENT, PROTOCOL_HTTP_SERVER, PROTOCOL_MESSAGE, PROTOCOL_TELNET, cmlabs::NetworkConnection::receiveAvailable(), cmlabs::NetworkThread::shouldContinue, TelnetServerRun(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
Referenced by autoDetectConnection().
| uint64 cmlabs::NetworkChannel::createTCPConnection | ( | const char * | addr, |
| uint16 | port, | ||
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect, | ||
| uint64 & | location, | ||
| uint32 | timeoutMS = 5000 ) |
Connect to a host by name/IP; see NetworkManager::createTCPConnection().
timeoutMS. Definition at line 1071 of file NetworkManager.cpp.
References applySSLClientPolicy(), cmlabs::SSLConnection::connect(), cmlabs::TCPConnection::connect(), cmlabs::SSLConnection::init(), NOENC, SSLENC, and startConnection().
Referenced by cmlabs::NetworkManager::createTCPConnection(), cmlabs::NetworkManager::createTCPConnection(), and cmlabs::NetworkManager::createTCPConnection().
| uint64 cmlabs::NetworkChannel::createTCPConnection | ( | const uint32 * | addresses, |
| uint16 | addressCount, | ||
| uint16 | port, | ||
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect, | ||
| uint64 & | location, | ||
| uint32 | timeoutMS = 5000 ) |
Connect trying several candidate addresses.
Definition at line 1119 of file NetworkManager.cpp.
References applySSLClientPolicy(), cmlabs::SSLConnection::connect(), cmlabs::TCPConnection::connect(), NOENC, SSLENC, and startConnection().
| uint64 cmlabs::NetworkChannel::createTCPConnection | ( | uint64 | location, |
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect, | ||
| uint32 | timeoutMS = 5000 ) |
Connect to a packed uint64 endpoint.
Definition at line 1097 of file NetworkManager.cpp.
References applySSLClientPolicy(), cmlabs::SSLConnection::connect(), cmlabs::TCPConnection::connect(), NOENC, SSLENC, and startConnection().
| uint64 cmlabs::NetworkChannel::createUDPConnection | ( | uint16 | port, |
| uint8 | protocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect ) |
Bind a UDP port on this channel.
Definition at line 1280 of file NetworkManager.cpp.
References channelMutex, cmlabs::UDPConnection::connect(), connectionThreads, startConnection(), and udpListeners.
Referenced by cmlabs::NetworkManager::createUDPConnection().
| uint64 cmlabs::NetworkChannel::createWebsocketConnection | ( | const char * | uri, |
| const char * | addr, | ||
| uint16 | port, | ||
| uint8 | encryption, | ||
| const char * | protocolName, | ||
| const char * | origin = NULL, | ||
| uint32 | timeoutMS = 5000 ) |
Open a client WebSocket connection from explicit components.
Definition at line 1233 of file NetworkManager.cpp.
References applySSLClientPolicy(), cmlabs::SSLConnection::connect(), cmlabs::TCPConnection::connect(), cmlabs::HTTPRequest::CreateWebsocketRequest(), endConnection(), HTTP_SWITCH_PROTOCOL, NOENC, PROTOCOL_HTTP_CLIENT, cmlabs::HTTPProtocol::SendHTTPRequest(), SSLENC, startConnection(), cmlabs::HTTPReply::type, and waitForHTTPReply().
| uint64 cmlabs::NetworkChannel::createWebsocketConnection | ( | const char * | url, |
| const char * | protocolName, | ||
| const char * | origin = NULL, | ||
| uint32 | timeoutMS = 5000 ) |
Open a client WebSocket connection from a full URL.
Definition at line 1202 of file NetworkManager.cpp.
References createWebsocketConnection(), cmlabs::html::GetHostFromURL(), cmlabs::html::GetPortFromURL(), cmlabs::html::GetProtocolFromURL(), cmlabs::html::GetURIFromURL(), NOENC, and SSLENC.
Referenced by createWebsocketConnection(), cmlabs::NetworkManager::createWebsocketConnection(), cmlabs::NetworkManager::createWebsocketConnection(), and cmlabs::TestWebSocketRequestClient::run().
| bool cmlabs::NetworkChannel::endConnection | ( | uint64 | conid | ) |
Gracefully close a connection.
| conid | Connection id. |
Definition at line 1334 of file NetworkManager.cpp.
References cmlabs::NetworkThread::autoreconnect, channelMutex, connectionThreads, cmlabs::NetworkThread::isRunning, cmlabs::ThreadManager::JoinThread(), LOG_NETWORK, LogPrint, manager, cmlabs::NetworkThread::reaping, cmlabs::NetworkThread::shouldContinue, cmlabs::utils::Sleep(), cmlabs::ThreadManager::TerminateThread(), and cmlabs::NetworkThread::threadID.
Referenced by createWebsocketConnection(), cmlabs::NetworkManager::endConnection(), endUDPConnection(), HTTPClientRun(), HTTPServerRun(), cmlabs::NetworkManager::makeHTTPRequest(), cmlabs::NetworkManager::makeHTTPRequest(), cmlabs::NetworkManager::makeHTTPRequest(), MessageConnectionRun(), cmlabs::TestWebSocketRequestClient::run(), TelnetServerRun(), cmlabs::NetworkManager::TestHTTP(), cmlabs::NetworkManager::UnitTest(), cmlabs::NetworkManager::UnitTestHTTP(), and cmlabs::NetworkManager::WebsocketTest().
| bool cmlabs::NetworkChannel::endUDPConnection | ( | uint16 | port | ) |
Close the UDP connection bound to port.
Definition at line 1301 of file NetworkManager.cpp.
References channelMutex, endConnection(), cmlabs::NetworkThread::id, and udpListeners.
Referenced by cmlabs::NetworkManager::endUDPConnection().
|
protected |
Queue or push an incoming HTTP reply.
Definition at line 1647 of file NetworkManager.cpp.
References queueHTTPReplies, queueHTTPRepliesMutex, queueHTTPRepliesSemaphore, and receiver.
Referenced by HTTPClientRun().
|
protected |
Queue or push an incoming HTTP request.
Definition at line 1627 of file NetworkManager.cpp.
References queueHTTPRequests, queueHTTPRequestsMutex, queueHTTPRequestsSemaphore, and receiver.
Referenced by HTTPServerRun().
|
protected |
Queue or push an incoming DataMessage.
Definition at line 1657 of file NetworkManager.cpp.
References queueMessageConIDs, queueMessages, queueMessagesMutex, queueMessagesSemaphore, and receiver.
Referenced by MessageConnectionRun().
|
protected |
Queue or push a lifecycle event.
Definition at line 1680 of file NetworkManager.cpp.
References cid, cmlabs::NetworkEvent::cid, cmlabs::NetworkEvent::conid, eventQueue, eventQueueMutex, eventQueueSemaphore, cmlabs::GetTimeNow(), cmlabs::NetworkEvent::protocol, receiver, cmlabs::NetworkEvent::time, and cmlabs::NetworkEvent::type.
Referenced by ConnectionAutodetectRun(), HTTPClientRun(), HTTPServerRun(), MessageConnectionRun(), and TelnetServerRun().
|
protected |
Queue or push an incoming Telnet line.
Definition at line 1669 of file NetworkManager.cpp.
References queueTelnetLines, queueTelnetLinesMutex, queueTelnetLinesSemaphore, and receiver.
Referenced by TelnetServerRun().
|
protected |
Queue or push an incoming WebSocket message.
Definition at line 1637 of file NetworkManager.cpp.
References queueWebsocketData, queueWebsocketDataMutex, queueWebsocketDataSemaphore, and receiver.
Referenced by HTTPClientRun(), and HTTPServerRun().
| uint8 cmlabs::NetworkChannel::getConnectionType | ( | uint64 | conid | ) |
| conid | Connection id. |
Definition at line 1320 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::getConnectionType().
Referenced by cmlabs::NetworkManager::getConnectionType().
| uint32 cmlabs::NetworkChannel::getInputSpeed | ( | uint64 | conid | ) |
| conid | Connection id. |
Definition at line 1604 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::getInputSpeed().
| uint32 cmlabs::NetworkChannel::getOutputSpeed | ( | uint64 | conid | ) |
| conid | Connection id. |
Definition at line 1597 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::getOutputSpeed().
| uint64 cmlabs::NetworkChannel::getRemoteAddress | ( | uint64 | conid | ) |
| conid | Connection id. |
Definition at line 1327 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::getRemoteAddress().
Referenced by cmlabs::NetworkManager::getRemoteAddress().
|
staticprotected |
Thread entry: HTTP client connection loop (send requests, parse replies).
Definition at line 1926 of file NetworkManager.cpp.
References cmlabs::NetworkThread::autoreconnect, cmlabs::NetworkThread::con, cmlabs::NetworkThread::defaultProtocol, cmlabs::NetworkConnection::disconnect(), endConnection(), enterHTTPReply(), enterNetworkEvent(), enterWebsocketData(), cmlabs::NetworkConnection::greetingData, cmlabs::NetworkConnection::greetingSize, cmlabs::NetworkThread::id, cmlabs::NetworkConnection::isConnected(), cmlabs::NetworkThread::isRunning, cmlabs::WebsocketData::isTerminationRequest(), cmlabs::HTTPReply::isWebsocketUpgrade(), NetworkChannel(), NETWORKERROR_GREETING_ERROR, NETWORKEVENT_DISCONNECT, NETWORKEVENT_DISCONNECT_RETRYING, NETWORKEVENT_RECONNECT, NETWORKEVENT_UNPROCESSED_DATA, cmlabs::NetworkThread::parent, cmlabs::HTTPProtocol::ReceiveHTTPReply(), cmlabs::HTTPProtocol::ReceiveWebsocketData(), cmlabs::NetworkConnection::reconnect(), cmlabs::NetworkConnection::send(), cmlabs::NetworkThread::shouldContinue, cmlabs::utils::Sleep(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
Referenced by ConnectionAutodetectRun(), and startConnection().
|
staticprotected |
Thread entry: HTTP/WebSocket server connection loop — the built-in web server's per-connection worker (parses requests, dispatches, sends replies, handles the WebSocket upgrade and subsequent frames).
Definition at line 2032 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, cmlabs::HTTPRequest::contentLength, cmlabs::HTTPReply::CreateErrorReply(), cmlabs::WebsocketData::CreateTerminationConfirmation(), cmlabs::HTTPReply::CreateWebsocketHTTPReply(), cmlabs::NetworkThread::defaultProtocol, endConnection(), enterHTTPRequest(), enterNetworkEvent(), enterWebsocketData(), cmlabs::HTTPRequest::getHeaderEntry(), cmlabs::HTTPRequest::headerLength, HTTP_ACCESS_DENIED, cmlabs::NetworkThread::id, cmlabs::NetworkConnection::isConnected(), cmlabs::NetworkThread::isRunning, cmlabs::WebsocketData::isTerminationRequest(), cmlabs::HTTPRequest::isWebsocketUpgrade(), LOG_NETWORK, LogPrint, NetworkChannel(), NETWORKEVENT_DISCONNECT, NETWORKEVENT_PROTOCOL_ERROR, NETWORKEVENT_UNPROCESSED_DATA, cmlabs::NetworkThread::parent, cmlabs::HTTPProtocol::ReceiveHTTPRequest(), cmlabs::HTTPProtocol::ReceiveWebsocketData(), sendHTTPReply(), sendWebsocketData(), cmlabs::NetworkThread::shouldContinue, THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
Referenced by ConnectionAutodetectRun(), and startConnection().
| bool cmlabs::NetworkChannel::isConnected | ( | uint64 | conid | ) |
| conid | Connection id. |
Definition at line 812 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::isConnected().
|
staticprotected |
Thread entry: binary DataMessage connection loop.
Definition at line 2150 of file NetworkManager.cpp.
References cmlabs::NetworkThread::autoreconnect, cmlabs::NetworkThread::con, cmlabs::NetworkThread::defaultProtocol, cmlabs::NetworkConnection::disconnect(), endConnection(), enterMessage(), enterNetworkEvent(), cmlabs::NetworkConnection::getRemoteAddress(), cmlabs::GetTimeNow(), cmlabs::NetworkConnection::greetingData, cmlabs::NetworkConnection::greetingSize, cmlabs::NetworkThread::id, cmlabs::NetworkConnection::isConnected(), cmlabs::NetworkThread::isRunning, NetworkChannel(), NETWORKERROR_GREETING_ERROR, NETWORKEVENT_DISCONNECT, NETWORKEVENT_DISCONNECT_RETRYING, NETWORKEVENT_RECONNECT, NETWORKEVENT_UNPROCESSED_DATA, cmlabs::NetworkThread::parent, cmlabs::MessageProtocol::ReceiveMessage(), cmlabs::NetworkConnection::reconnect(), cmlabs::NetworkConnection::send(), cmlabs::NetworkThread::shouldContinue, cmlabs::utils::Sleep(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
Referenced by ConnectionAutodetectRun(), and startConnection().
|
staticprotected |
Thread entry: accept loop for a TCP listener.
Definition at line 1785 of file NetworkManager.cpp.
References cmlabs::TCPListener::acceptConnection(), autoDetectConnection(), cmlabs::NetworkThread::autoProtocols, cmlabs::NetworkThread::autoProtocolTimeout, cmlabs::NetworkThread::defaultProtocol, cmlabs::NetworkThread::isAsync, cmlabs::NetworkThread::isRunning, cmlabs::NetworkThread::listener, cmlabs::NetworkThread::parent, cmlabs::NetworkThread::port, cmlabs::NetworkThread::shouldContinue, THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
Referenced by startListener().
| bool cmlabs::NetworkChannel::sendHTTPReply | ( | HTTPReply * | reply, |
| uint64 | conid ) |
Send an HTTP reply on a server connection.
| reply | Reply (caller keeps ownership). |
| conid | Destination connection. |
Definition at line 1540 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::HTTPProtocol::SendHTTPReply().
Referenced by HTTPServerRun(), and cmlabs::HTTPTestServer::receiveHTTPRequest().
| bool cmlabs::NetworkChannel::sendHTTPRequest | ( | HTTPRequest * | req, |
| uint64 | conid ) |
Send an HTTP request without waiting for the reply (reply arrives via receiveHTTPReply()/waitForHTTPReply()).
| req | Request (caller keeps ownership). |
| conid | Destination. |
Definition at line 1611 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::HTTPProtocol::SendHTTPRequest().
| bool cmlabs::NetworkChannel::sendMessage | ( | DataMessage * | msg, |
| uint64 | conid ) |
Send a DataMessage.
| msg | Message (caller keeps ownership). |
| conid | Destination. |
Definition at line 1575 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::MessageProtocol::SendMessage().
Referenced by cmlabs::Test_RequestClient(), and cmlabs::NetworkManager::UnitTest().
| HTTPReply * cmlabs::NetworkChannel::sendReceiveHTTPRequest | ( | HTTPRequest * | req, |
| uint64 | conid, | ||
| uint32 | timeout ) |
Send an HTTP request and block for its reply on the same connection.
| req | Request (caller keeps ownership). |
| conid | Destination. |
| timeout | Max wait in ms. |
Definition at line 1585 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, cmlabs::HTTPReply::CreateErrorReply(), HTTP_SERVER_UNAVAILABLE, cmlabs::HTTPProtocol::SendHTTPRequest(), and waitForHTTPReply().
Referenced by cmlabs::HTTPServerTest(), cmlabs::NetworkManager::makeHTTPRequest(), cmlabs::NetworkManager::makeHTTPRequest(), cmlabs::NetworkManager::makeHTTPRequest(), cmlabs::NetworkManager::UnitTestHTTP(), and cmlabs::NetworkManager::WebsocketTest().
| TelnetLine * cmlabs::NetworkChannel::sendReceiveTelnetLine | ( | TelnetLine * | line, |
| uint64 | conid, | ||
| uint32 | timeout, | ||
| uint32 | size = 0 ) |
Send a Telnet line and block for the response line.
| line | Line to send (caller keeps ownership). |
| conid | Destination. |
| timeout | Max wait in ms. |
| size | Exact response byte count (0 = up to CR/LF). |
Definition at line 1556 of file NetworkManager.cpp.
References cmlabs::NetworkConnection::clearBuffer(), cmlabs::NetworkThread::con, connectionThreads, cmlabs::NetworkThread::isAsync, cmlabs::TelnetProtocol::ReceiveTelnetLine(), and cmlabs::TelnetProtocol::SendTelnetLine().
| bool cmlabs::NetworkChannel::sendTelnetLine | ( | TelnetLine * | line, |
| uint64 | conid ) |
Send a Telnet line.
| line | Line (caller keeps ownership). |
| conid | Destination. |
Definition at line 1548 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::TelnetProtocol::SendTelnetLine().
| bool cmlabs::NetworkChannel::sendWebsocketData | ( | WebsocketData * | wsData, |
| uint64 | conid ) |
Send a WebSocket frame.
| wsData | Frame (caller keeps ownership). |
| conid | Destination. |
Definition at line 1532 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::HTTPProtocol::SendWebsocketData().
Referenced by HTTPServerRun(), cmlabs::WebsocketTestServer::receiveWebsocketData(), and cmlabs::TestWebSocketRequestClient::run().
| bool cmlabs::NetworkChannel::setNewReceiver | ( | NetworkReceiver * | recv | ) |
Replace the channel's receiver for async dispatch.
| recv | New receiver (not owned; must outlive the channel or be replaced). |
Definition at line 1416 of file NetworkManager.cpp.
References receiver.
Referenced by cmlabs::NetworkManager::addTCPConnection(), cmlabs::NetworkManager::addTCPConnection(), cmlabs::NetworkManager::createListener(), cmlabs::NetworkManager::createTCPConnection(), cmlabs::NetworkManager::createTCPConnection(), cmlabs::NetworkManager::createTCPConnection(), cmlabs::NetworkManager::createUDPConnection(), cmlabs::NetworkManager::createWebsocketConnection(), and cmlabs::NetworkManager::createWebsocketConnection().
|
inline |
Definition at line 548 of file NetworkManager.h.
References sslAllowSelfSigned.
|
inline |
Definition at line 552 of file NetworkManager.h.
| bool cmlabs::NetworkChannel::shutdown | ( | ) |
Stop all worker threads and close all listeners/connections of this channel.
Definition at line 820 of file NetworkManager.cpp.
References cmlabs::NetworkThread::autoreconnect, channelMutex, connectionThreads, eventQueue, eventQueueMutex, cmlabs::NetworkThread::isRunning, cmlabs::ThreadManager::JoinThread(), listeners, LOG_NETWORK, LogPrint, queueHTTPReplies, queueHTTPRepliesMutex, queueHTTPRequests, queueHTTPRequestsMutex, queueMessageConIDs, queueMessages, queueMessagesMutex, queueTelnetLines, queueTelnetLinesMutex, queueWebsocketData, queueWebsocketDataMutex, cmlabs::NetworkThread::reaping, cmlabs::NetworkThread::shouldContinue, cmlabs::utils::Sleep(), cmlabs::ThreadManager::TerminateThread(), and cmlabs::NetworkThread::threadID.
Referenced by ~NetworkChannel().
| uint64 cmlabs::NetworkChannel::startConnection | ( | NetworkConnection * | con, |
| uint8 | protocol, | ||
| bool | isAsync, | ||
| bool | autoreconnect, | ||
| uint32 | timeoutMS = 5000 ) |
Start the protocol worker thread for an already-connected connection.
| con | The connection (ownership transfers). |
| protocol | Single PROTOCOL_* flag. |
| isAsync | Push vs queue dispatch. |
| autoreconnect | Reconnect on failure. |
| timeoutMS | Timeout used during any initial exchange. |
Definition at line 1739 of file NetworkManager.cpp.
References cmlabs::NetworkThread::autoreconnect, channelMutex, cmlabs::NetworkThread::con, connectionThreads, cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkThread::defaultProtocol, cmlabs::ThreadManager::GetThreadHandle(), HTTPClientRun(), HTTPServerRun(), cmlabs::NetworkThread::hWorker, cmlabs::NetworkThread::isAsync, LOG_NETWORK, LogPrint, manager, MessageConnectionRun(), PROTOCOL_HTTP_CLIENT, PROTOCOL_HTTP_SERVER, PROTOCOL_MESSAGE, PROTOCOL_TELNET, cmlabs::NetworkConnection::setConnectTimeout(), TelnetServerRun(), and cmlabs::NetworkThread::threadID.
Referenced by addTCPConnection(), addTCPConnection(), createTCPConnection(), createTCPConnection(), createTCPConnection(), cmlabs::NetworkManager::createTCPConnection(), createUDPConnection(), and createWebsocketConnection().
| bool cmlabs::NetworkChannel::startListener | ( | uint64 | cid, |
| uint16 | port, | ||
| uint8 | encryption, | ||
| uint8 | protocol, | ||
| bool | isAsync, | ||
| uint32 | protocolTimeout = 3000, | ||
| bool | isDefaultProtocol = false ) |
Open a listener on this channel (see NetworkManager::createListener() for semantics).
| cid | Channel id to tag events with. |
| port | TCP port. |
| encryption | NOENC or SSLENC. |
| protocol | PROTOCOL_* flags. |
| isAsync | Push vs queue dispatch. |
| protocolTimeout | Sniffing timeout in ms. |
| isDefaultProtocol | First flag is the fallback. |
Definition at line 970 of file NetworkManager.cpp.
References cmlabs::NetworkThread::autoProtocols, cmlabs::NetworkThread::autoProtocolTimeout, channelMutex, cid, cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkThread::defaultProtocol, cmlabs::ThreadManager::GetThreadHandle(), cmlabs::NetworkThread::hWorker, cmlabs::TCPListener::init(), cmlabs::NetworkThread::isAsync, cmlabs::NetworkThread::listener, listeners, LOG_NETWORK, LogPrint, manager, NetworkListenerRun(), cmlabs::NetworkThread::port, cmlabs::TCPListener::setSSLCertificate(), and cmlabs::NetworkThread::threadID.
Referenced by cmlabs::NetworkManager::createListener().
| bool cmlabs::NetworkChannel::stopListener | ( | uint16 | port, |
| uint8 | protocol ) |
Stop a listener on this channel.
| port | Port. |
| protocol | Its PROTOCOL_* flags. |
Definition at line 1031 of file NetworkManager.cpp.
References cmlabs::NetworkThread::autoProtocols, channelMutex, cmlabs::NetworkThread::defaultProtocol, cmlabs::NetworkThread::isRunning, cmlabs::ThreadManager::JoinThread(), listeners, LOG_NETWORK, LogPrint, cmlabs::NetworkThread::reaping, cmlabs::NetworkThread::shouldContinue, cmlabs::utils::Sleep(), cmlabs::ThreadManager::TerminateThread(), and cmlabs::NetworkThread::threadID.
Referenced by cmlabs::NetworkManager::stopListener().
|
staticprotected |
Thread entry: Telnet server connection loop.
Definition at line 2262 of file NetworkManager.cpp.
References cmlabs::NetworkThread::con, cmlabs::NetworkThread::defaultProtocol, endConnection(), enterNetworkEvent(), enterTelnetLine(), cmlabs::NetworkThread::id, cmlabs::NetworkThread::isAsync, cmlabs::NetworkConnection::isConnected(), cmlabs::NetworkThread::isRunning, NetworkChannel(), NETWORKEVENT_DISCONNECT, NETWORKEVENT_UNPROCESSED_DATA, cmlabs::NetworkThread::parent, cmlabs::TelnetProtocol::ReceiveTelnetLine(), cmlabs::NetworkThread::shouldContinue, cmlabs::utils::Sleep(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
Referenced by ConnectionAutodetectRun(), and startConnection().
| HTTPReply * cmlabs::NetworkChannel::waitForHTTPReply | ( | uint64 & | conid, |
| uint32 | ms ) |
Wait for the next queued HTTP reply (sync-mode HTTP client).
| conid | Out: source connection. |
| ms | Max wait in ms. |
Definition at line 1513 of file NetworkManager.cpp.
References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), queueHTTPReplies, queueHTTPRepliesMutex, and queueHTTPRepliesSemaphore.
Referenced by createWebsocketConnection(), and sendReceiveHTTPRequest().
| HTTPRequest * cmlabs::NetworkChannel::waitForHTTPRequest | ( | uint64 & | conid, |
| uint32 | ms ) |
Wait for the next queued HTTP request (sync-mode HTTP server).
| conid | Out: connection the request arrived on (use with sendHTTPReply()). |
| ms | Max wait in ms. |
Definition at line 1439 of file NetworkManager.cpp.
References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), queueHTTPRequests, queueHTTPRequestsMutex, and queueHTTPRequestsSemaphore.
| DataMessage * cmlabs::NetworkChannel::waitForMessage | ( | uint64 & | conid, |
| uint32 | ms ) |
Wait for the next queued DataMessage.
| conid | Out: source connection. |
| ms | Max wait in ms. |
Definition at line 1490 of file NetworkManager.cpp.
References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), queueMessageConIDs, queueMessages, queueMessagesMutex, and queueMessagesSemaphore.
Referenced by cmlabs::Test_RequestClient(), cmlabs::NetworkManager::UnitTest(), and cmlabs::NetworkManager::UnitTestDelayedConnect().
| NetworkEvent * cmlabs::NetworkChannel::waitForNetworkEvent | ( | uint32 | ms | ) |
Wait for the next connection lifecycle event (sync mode).
| ms | Max wait in milliseconds. |
Definition at line 1422 of file NetworkManager.cpp.
References eventQueue, eventQueueMutex, eventQueueSemaphore, cmlabs::GetTimeAgeMS(), and cmlabs::GetTimeNow().
| TelnetLine * cmlabs::NetworkChannel::waitForTelnetLine | ( | uint64 & | conid, |
| uint32 | ms ) |
Wait for the next queued Telnet line.
| conid | Out: source connection. |
| ms | Max wait in ms. |
Definition at line 1473 of file NetworkManager.cpp.
References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), queueTelnetLines, queueTelnetLinesMutex, and queueTelnetLinesSemaphore.
| WebsocketData * cmlabs::NetworkChannel::waitForWebsocketData | ( | uint64 & | conid, |
| uint32 | ms ) |
Wait for the next queued WebSocket message.
| conid | Out: source connection. |
| ms | Max wait in ms. |
Definition at line 1456 of file NetworkManager.cpp.
References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), queueWebsocketData, queueWebsocketDataMutex, and queueWebsocketDataSemaphore.
Referenced by cmlabs::TestWebSocketRequestClient::run().
|
friend |
Definition at line 475 of file NetworkManager.h.
References manager, and NetworkManager.
Referenced by NetworkChannel(), and NetworkManager.
|
protected |
Definition at line 661 of file NetworkManager.h.
Referenced by autoDetectConnection(), createUDPConnection(), endConnection(), endUDPConnection(), shutdown(), startConnection(), startListener(), and stopListener().
| uint32 cmlabs::NetworkChannel::cid |
Channel id within the manager.
Definition at line 480 of file NetworkManager.h.
Referenced by cmlabs::NetworkManager::addTCPConnection(), cmlabs::NetworkManager::addTCPConnection(), cmlabs::NetworkManager::createListener(), cmlabs::NetworkManager::createTCPConnection(), cmlabs::NetworkManager::createTCPConnection(), cmlabs::NetworkManager::createTCPConnection(), cmlabs::NetworkManager::createUDPConnection(), cmlabs::NetworkManager::createWebsocketConnection(), cmlabs::NetworkManager::createWebsocketConnection(), enterNetworkEvent(), and startListener().
|
protected |
Worker threads by connection id (owned).
Definition at line 622 of file NetworkManager.h.
Referenced by autoDetectConnection(), createUDPConnection(), endConnection(), getConnectionType(), getInputSpeed(), getOutputSpeed(), getRemoteAddress(), isConnected(), sendHTTPReply(), sendHTTPRequest(), sendMessage(), sendReceiveHTTPRequest(), sendReceiveTelnetLine(), sendTelnetLine(), sendWebsocketData(), shutdown(), and startConnection().
|
protected |
Definition at line 626 of file NetworkManager.h.
Referenced by enterNetworkEvent(), shutdown(), and waitForNetworkEvent().
|
protected |
Definition at line 628 of file NetworkManager.h.
Referenced by enterNetworkEvent(), shutdown(), and waitForNetworkEvent().
|
protected |
Definition at line 627 of file NetworkManager.h.
Referenced by enterNetworkEvent(), and waitForNetworkEvent().
|
protected |
TCP listener threads by port (owned).
Definition at line 623 of file NetworkManager.h.
Referenced by shutdown(), startListener(), and stopListener().
|
protected |
Owning manager (not owned).
Definition at line 620 of file NetworkManager.h.
Referenced by applySSLClientPolicy(), autoDetectConnection(), endConnection(), NetworkChannel(), NetworkManager, startConnection(), startListener(), and ~NetworkChannel().
|
protected |
Definition at line 647 of file NetworkManager.h.
Referenced by enterHTTPReply(), shutdown(), and waitForHTTPReply().
|
protected |
Definition at line 649 of file NetworkManager.h.
Referenced by enterHTTPReply(), shutdown(), and waitForHTTPReply().
|
protected |
Definition at line 648 of file NetworkManager.h.
Referenced by enterHTTPReply(), and waitForHTTPReply().
|
protected |
Definition at line 644 of file NetworkManager.h.
Referenced by enterHTTPRequest(), shutdown(), and waitForHTTPRequest().
|
protected |
Definition at line 646 of file NetworkManager.h.
Referenced by enterHTTPRequest(), shutdown(), and waitForHTTPRequest().
|
protected |
Definition at line 645 of file NetworkManager.h.
Referenced by enterHTTPRequest(), and waitForHTTPRequest().
|
protected |
Definition at line 651 of file NetworkManager.h.
Referenced by enterMessage(), shutdown(), and waitForMessage().
|
protected |
Definition at line 650 of file NetworkManager.h.
Referenced by enterMessage(), shutdown(), and waitForMessage().
|
protected |
Definition at line 653 of file NetworkManager.h.
Referenced by enterMessage(), shutdown(), and waitForMessage().
|
protected |
Definition at line 652 of file NetworkManager.h.
Referenced by enterMessage(), and waitForMessage().
|
protected |
Definition at line 654 of file NetworkManager.h.
Referenced by enterTelnetLine(), shutdown(), and waitForTelnetLine().
|
protected |
Definition at line 656 of file NetworkManager.h.
Referenced by enterTelnetLine(), shutdown(), and waitForTelnetLine().
|
protected |
Definition at line 655 of file NetworkManager.h.
Referenced by enterTelnetLine(), and waitForTelnetLine().
|
protected |
Definition at line 657 of file NetworkManager.h.
Referenced by enterWebsocketData(), shutdown(), and waitForWebsocketData().
|
protected |
Definition at line 659 of file NetworkManager.h.
Referenced by enterWebsocketData(), shutdown(), and waitForWebsocketData().
|
protected |
Definition at line 658 of file NetworkManager.h.
Referenced by enterWebsocketData(), and waitForWebsocketData().
|
protected |
Async dispatch target (not owned; may be NULL).
Definition at line 621 of file NetworkManager.h.
Referenced by enterHTTPReply(), enterHTTPRequest(), enterMessage(), enterNetworkEvent(), enterTelnetLine(), enterWebsocketData(), NetworkChannel(), and setNewReceiver().
| int8 cmlabs::NetworkChannel::sslAllowSelfSigned |
Definition at line 549 of file NetworkManager.h.
Referenced by applySSLClientPolicy(), NetworkChannel(), and setSSLAllowSelfSigned().
| std::string cmlabs::NetworkChannel::sslCAFile |
Definition at line 553 of file NetworkManager.h.
Referenced by applySSLClientPolicy(), and setSSLCALocation().
| std::string cmlabs::NetworkChannel::sslCAPath |
Definition at line 554 of file NetworkManager.h.
Referenced by applySSLClientPolicy(), and setSSLCALocation().
| bool cmlabs::NetworkChannel::sslCASet |
Definition at line 555 of file NetworkManager.h.
Referenced by applySSLClientPolicy(), NetworkChannel(), and setSSLCALocation().
|
protected |
UDP listener threads by port (owned).
Definition at line 624 of file NetworkManager.h.
Referenced by createUDPConnection(), and endUDPConnection().