CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::NetworkChannel Class Reference

One logical network interface: a group of listeners/connections with shared dispatch. More...

#include <NetworkManager.h>

Collaboration diagram for cmlabs::NetworkChannel:
[legend]

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.
NetworkEventwaitForNetworkEvent (uint32 ms)
 Wait for the next connection lifecycle event (sync mode).
HTTPRequestwaitForHTTPRequest (uint64 &conid, uint32 ms)
 Wait for the next queued HTTP request (sync-mode HTTP server).
TelnetLinewaitForTelnetLine (uint64 &conid, uint32 ms)
 Wait for the next queued Telnet line.
DataMessagewaitForMessage (uint64 &conid, uint32 ms)
 Wait for the next queued DataMessage.
HTTPReplywaitForHTTPReply (uint64 &conid, uint32 ms)
 Wait for the next queued HTTP reply (sync-mode HTTP client).
WebsocketDatawaitForWebsocketData (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()).
HTTPReplysendReceiveHTTPRequest (HTTPRequest *req, uint64 conid, uint32 timeout)
 Send an HTTP request and block for its reply on the same connection.
TelnetLinesendReceiveTelnetLine (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.

Protected Attributes

NetworkManagermanager
 Owning manager (not owned).
NetworkReceiverreceiver
 Async dispatch target (not owned; may be NULL).
std::map< uint64, NetworkThread * > connectionThreads
 Worker threads by connection id (owned).
std::map< uint16, NetworkThread * > listeners
 TCP listener threads by port (owned).
std::map< uint16, NetworkThread * > udpListeners
 UDP listener threads by port (owned).
std::queue< NetworkEvent * > eventQueue
utils::Semaphore eventQueueSemaphore
utils::Mutex eventQueueMutex
std::queue< HTTPRequest * > queueHTTPRequests
utils::Semaphore queueHTTPRequestsSemaphore
utils::Mutex queueHTTPRequestsMutex
std::queue< HTTPReply * > queueHTTPReplies
utils::Semaphore queueHTTPRepliesSemaphore
utils::Mutex queueHTTPRepliesMutex
std::queue< DataMessage * > queueMessages
std::queue< uint64 > queueMessageConIDs
utils::Semaphore queueMessagesSemaphore
utils::Mutex queueMessagesMutex
std::queue< TelnetLine * > queueTelnetLines
utils::Semaphore queueTelnetLinesSemaphore
utils::Mutex queueTelnetLinesMutex
std::queue< WebsocketData * > queueWebsocketData
utils::Semaphore queueWebsocketDataSemaphore
utils::Mutex queueWebsocketDataMutex
utils::Mutex channelMutex

Friends

class NetworkManager

Detailed Description

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.

Thread-safety
All public methods are safe to call from any thread; the waitFor*() methods block up to their timeout and return ownership of the object.

Definition at line 424 of file NetworkManager.h.

Constructor & Destructor Documentation

◆ NetworkChannel()

cmlabs::NetworkChannel::NetworkChannel ( NetworkManager * manager)
Parameters
managerOwning manager (not owned by the channel).

Definition at line 675 of file NetworkManager.cpp.

References manager, NetworkManager, receiver, sslAllowSelfSigned, and sslCASet.

Referenced by HTTPClientRun(), HTTPServerRun(), MessageConnectionRun(), and TelnetServerRun().

◆ ~NetworkChannel()

cmlabs::NetworkChannel::~NetworkChannel ( )
virtual

Definition at line 703 of file NetworkManager.cpp.

References manager, and shutdown().

Member Function Documentation

◆ addTCPConnection() [1/2]

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 )

◆ addTCPConnection() [2/2]

uint64 cmlabs::NetworkChannel::addTCPConnection ( uint64 location,
uint8 encryption,
uint8 protocol,
bool isAsync,
uint32 timeoutMS = 5000,
const char * greetingData = NULL,
uint32 greetingSize = 0 )

◆ applySSLClientPolicy()

◆ autoDetectConnection()

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.

Parameters
conThe fresh connection (ownership transfers).
portListener port it arrived on.
autoProtocolsPROTOCOL_* bit set to test.
autoProtocolTimeoutMax sniffing time in ms.
defaultProtocolFallback protocol when detection is inconclusive (0 = drop).
isAsyncPush vs queue dispatch.
autoreconnectReconnect on failure.
Returns
The new connection id, or 0 on failure.

Definition at line 1516 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::NetworkThread::isAsync, manager, cmlabs::NetworkThread::parent, and cmlabs::NetworkThread::threadID.

Referenced by NetworkListenerRun().

◆ ConnectionAutodetectRun()

◆ createTCPConnection() [1/3]

uint64 cmlabs::NetworkChannel::createTCPConnection ( const char * addr,
uint16 port,
uint8 encryption,
uint8 protocol,
bool isAsync,
bool autoreconnect,
uint64 & location,
uint32 timeoutMS = 5000 )

◆ createTCPConnection() [2/3]

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.

Returns
The new connection id, or 0.

Definition at line 970 of file NetworkManager.cpp.

References applySSLClientPolicy(), cmlabs::SSLConnection::connect(), cmlabs::TCPConnection::connect(), NOENC, SSLENC, and startConnection().

◆ createTCPConnection() [3/3]

uint64 cmlabs::NetworkChannel::createTCPConnection ( uint64 location,
uint8 encryption,
uint8 protocol,
bool isAsync,
bool autoreconnect,
uint32 timeoutMS = 5000 )

Connect to a packed uint64 endpoint.

Returns
The new connection id, or 0.

Definition at line 948 of file NetworkManager.cpp.

References applySSLClientPolicy(), cmlabs::SSLConnection::connect(), cmlabs::TCPConnection::connect(), NOENC, SSLENC, and startConnection().

◆ createUDPConnection()

uint64 cmlabs::NetworkChannel::createUDPConnection ( uint16 port,
uint8 protocol,
bool isAsync,
bool autoreconnect )

Bind a UDP port on this channel.

Returns
The new connection id, or 0.

Definition at line 1131 of file NetworkManager.cpp.

References channelMutex, cmlabs::UDPConnection::connect(), connectionThreads, startConnection(), and udpListeners.

Referenced by cmlabs::NetworkManager::createUDPConnection().

◆ createWebsocketConnection() [1/2]

uint64 cmlabs::NetworkChannel::createWebsocketConnection ( const char * uri,
const char * addr,
uint16 port,
uint8 encryption,
const char * protocolName,
const char * origin = NULL,
uint32 timeoutMS = 5000 )

◆ createWebsocketConnection() [2/2]

uint64 cmlabs::NetworkChannel::createWebsocketConnection ( const char * url,
const char * protocolName,
const char * origin = NULL,
uint32 timeoutMS = 5000 )

◆ endConnection()

◆ endUDPConnection()

bool cmlabs::NetworkChannel::endUDPConnection ( uint16 port)

Close the UDP connection bound to port.

Returns
true if found.

Definition at line 1152 of file NetworkManager.cpp.

References channelMutex, endConnection(), cmlabs::NetworkThread::id, and udpListeners.

Referenced by cmlabs::NetworkManager::endUDPConnection().

◆ enterHTTPReply()

bool cmlabs::NetworkChannel::enterHTTPReply ( HTTPReply * reply,
HTTPRequest * req,
uint64 conid )
protected

Queue or push an incoming HTTP reply.

Returns
true if delivered/queued.

Definition at line 1467 of file NetworkManager.cpp.

References queueHTTPReplies, queueHTTPRepliesMutex, queueHTTPRepliesSemaphore, and receiver.

Referenced by HTTPClientRun().

◆ enterHTTPRequest()

bool cmlabs::NetworkChannel::enterHTTPRequest ( HTTPRequest * req,
uint64 conid )
protected

Queue or push an incoming HTTP request.

Returns
true if delivered/queued.

Definition at line 1447 of file NetworkManager.cpp.

References queueHTTPRequests, queueHTTPRequestsMutex, queueHTTPRequestsSemaphore, and receiver.

Referenced by HTTPServerRun().

◆ enterMessage()

bool cmlabs::NetworkChannel::enterMessage ( DataMessage * msg,
uint64 conid )
protected

Queue or push an incoming DataMessage.

Returns
true if delivered/queued.

Definition at line 1477 of file NetworkManager.cpp.

References queueMessageConIDs, queueMessages, queueMessagesMutex, queueMessagesSemaphore, and receiver.

Referenced by MessageConnectionRun().

◆ enterNetworkEvent()

bool cmlabs::NetworkChannel::enterNetworkEvent ( uint8 type,
uint8 protocol,
uint64 conid )
protected

◆ enterTelnetLine()

bool cmlabs::NetworkChannel::enterTelnetLine ( TelnetLine * line,
uint64 conid )
protected

Queue or push an incoming Telnet line.

Returns
true if delivered/queued.

Definition at line 1489 of file NetworkManager.cpp.

References queueTelnetLines, queueTelnetLinesMutex, queueTelnetLinesSemaphore, and receiver.

Referenced by TelnetServerRun().

◆ enterWebsocketData()

bool cmlabs::NetworkChannel::enterWebsocketData ( WebsocketData * wsData,
uint64 conid )
protected

Queue or push an incoming WebSocket message.

Returns
true if delivered/queued.

Definition at line 1457 of file NetworkManager.cpp.

References queueWebsocketData, queueWebsocketDataMutex, queueWebsocketDataSemaphore, and receiver.

Referenced by HTTPClientRun(), and HTTPServerRun().

◆ getConnectionType()

uint8 cmlabs::NetworkChannel::getConnectionType ( uint64 conid)
Parameters
conidConnection id.
Returns
Transport type (TCPCON etc.), or 0.

Definition at line 1168 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::getConnectionType().

Referenced by cmlabs::NetworkManager::getConnectionType().

◆ getInputSpeed()

uint32 cmlabs::NetworkChannel::getInputSpeed ( uint64 conid)
Parameters
conidConnection id.
Returns
Recent inbound throughput in bytes/second.

Definition at line 1424 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::getInputSpeed().

◆ getOutputSpeed()

uint32 cmlabs::NetworkChannel::getOutputSpeed ( uint64 conid)
Parameters
conidConnection id.
Returns
Recent outbound throughput in bytes/second.

Definition at line 1417 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::getOutputSpeed().

◆ getRemoteAddress()

uint64 cmlabs::NetworkChannel::getRemoteAddress ( uint64 conid)
Parameters
conidConnection id.
Returns
Remote endpoint packed as uint64, or 0.

Definition at line 1175 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::getRemoteAddress().

Referenced by cmlabs::NetworkManager::getRemoteAddress().

◆ HTTPClientRun()

◆ HTTPServerRun()

◆ isConnected()

bool cmlabs::NetworkChannel::isConnected ( uint64 conid)
Parameters
conidConnection id.
Returns
true if that connection is currently up.

Definition at line 708 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::NetworkConnection::isConnected().

◆ MessageConnectionRun()

◆ NetworkListenerRun()

◆ sendHTTPReply()

bool cmlabs::NetworkChannel::sendHTTPReply ( HTTPReply * reply,
uint64 conid )

Send an HTTP reply on a server connection.

Parameters
replyReply (caller keeps ownership).
conidDestination connection.
Returns
true if fully sent.

Definition at line 1360 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::HTTPProtocol::SendHTTPReply().

Referenced by HTTPServerRun(), and cmlabs::HTTPTestServer::receiveHTTPRequest().

◆ sendHTTPRequest()

bool cmlabs::NetworkChannel::sendHTTPRequest ( HTTPRequest * req,
uint64 conid )

Send an HTTP request without waiting for the reply (reply arrives via receiveHTTPReply()/waitForHTTPReply()).

Parameters
reqRequest (caller keeps ownership).
conidDestination.
Returns
true if sent.

Definition at line 1431 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::HTTPProtocol::SendHTTPRequest().

◆ sendMessage()

bool cmlabs::NetworkChannel::sendMessage ( DataMessage * msg,
uint64 conid )

Send a DataMessage.

Parameters
msgMessage (caller keeps ownership).
conidDestination.
Returns
true if sent.

Definition at line 1395 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::MessageProtocol::SendMessage().

Referenced by cmlabs::Test_RequestClient(), and cmlabs::NetworkManager::UnitTest().

◆ sendReceiveHTTPRequest()

HTTPReply * cmlabs::NetworkChannel::sendReceiveHTTPRequest ( HTTPRequest * req,
uint64 conid,
uint32 timeout )

Send an HTTP request and block for its reply on the same connection.

Parameters
reqRequest (caller keeps ownership).
conidDestination.
timeoutMax wait in ms.
Returns
New reply owned by the caller, or NULL. Blocking.

Definition at line 1405 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().

◆ sendReceiveTelnetLine()

TelnetLine * cmlabs::NetworkChannel::sendReceiveTelnetLine ( TelnetLine * line,
uint64 conid,
uint32 timeout,
uint32 size = 0 )

Send a Telnet line and block for the response line.

Parameters
lineLine to send (caller keeps ownership).
conidDestination.
timeoutMax wait in ms.
sizeExact response byte count (0 = up to CR/LF).
Returns
New line owned by the caller, or NULL. Blocking.

Definition at line 1376 of file NetworkManager.cpp.

References cmlabs::NetworkConnection::clearBuffer(), cmlabs::NetworkThread::con, connectionThreads, cmlabs::NetworkThread::isAsync, cmlabs::TelnetProtocol::ReceiveTelnetLine(), and cmlabs::TelnetProtocol::SendTelnetLine().

◆ sendTelnetLine()

bool cmlabs::NetworkChannel::sendTelnetLine ( TelnetLine * line,
uint64 conid )

Send a Telnet line.

Parameters
lineLine (caller keeps ownership).
conidDestination.
Returns
true if sent.

Definition at line 1368 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::TelnetProtocol::SendTelnetLine().

◆ sendWebsocketData()

bool cmlabs::NetworkChannel::sendWebsocketData ( WebsocketData * wsData,
uint64 conid )

Send a WebSocket frame.

Parameters
wsDataFrame (caller keeps ownership).
conidDestination.
Returns
true if sent.

Definition at line 1352 of file NetworkManager.cpp.

References cmlabs::NetworkThread::con, connectionThreads, and cmlabs::HTTPProtocol::SendWebsocketData().

Referenced by HTTPServerRun(), cmlabs::WebsocketTestServer::receiveWebsocketData(), and cmlabs::TestWebSocketRequestClient::run().

◆ setNewReceiver()

◆ setSSLAllowSelfSigned()

bool cmlabs::NetworkChannel::setSSLAllowSelfSigned ( bool allow)
inline

Definition at line 498 of file NetworkManager.h.

References sslAllowSelfSigned.

◆ setSSLCALocation()

bool cmlabs::NetworkChannel::setSSLCALocation ( const char * caFile,
const char * caPath )
inline

Definition at line 502 of file NetworkManager.h.

References sslCAFile, sslCAPath, and sslCASet.

◆ shutdown()

◆ startConnection()

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.

Parameters
conThe connection (ownership transfers).
protocolSingle PROTOCOL_* flag.
isAsyncPush vs queue dispatch.
autoreconnectReconnect on failure.
timeoutMSTimeout used during any initial exchange.
Returns
The new connection id, or 0 on failure.

Definition at line 1557 of file NetworkManager.cpp.

References cmlabs::NetworkThread::autoreconnect, channelMutex, cmlabs::NetworkThread::con, connectionThreads, cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkThread::defaultProtocol, HTTPClientRun(), HTTPServerRun(), cmlabs::NetworkThread::isAsync, 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().

◆ startListener()

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).

Parameters
cidChannel id to tag events with.
portTCP port.
encryptionNOENC or SSLENC.
protocolPROTOCOL_* flags.
isAsyncPush vs queue dispatch.
protocolTimeoutSniffing timeout in ms.
isDefaultProtocolFirst flag is the fallback.
Returns
true if the port was bound.

Definition at line 839 of file NetworkManager.cpp.

References cmlabs::NetworkThread::autoProtocols, cmlabs::NetworkThread::autoProtocolTimeout, channelMutex, cid, cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkThread::defaultProtocol, cmlabs::TCPListener::init(), cmlabs::NetworkThread::isAsync, cmlabs::NetworkThread::listener, listeners, manager, NetworkListenerRun(), cmlabs::NetworkThread::port, cmlabs::TCPListener::setSSLCertificate(), and cmlabs::NetworkThread::threadID.

Referenced by cmlabs::NetworkManager::createListener().

◆ stopListener()

bool cmlabs::NetworkChannel::stopListener ( uint16 port,
uint8 protocol )

◆ TelnetServerRun()

◆ waitForHTTPReply()

HTTPReply * cmlabs::NetworkChannel::waitForHTTPReply ( uint64 & conid,
uint32 ms )

Wait for the next queued HTTP reply (sync-mode HTTP client).

Parameters
conidOut: source connection.
msMax wait in ms.
Returns
New reply owned by the caller, or NULL. Blocking.

Definition at line 1333 of file NetworkManager.cpp.

References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), queueHTTPReplies, queueHTTPRepliesMutex, and queueHTTPRepliesSemaphore.

Referenced by createWebsocketConnection(), and sendReceiveHTTPRequest().

◆ waitForHTTPRequest()

HTTPRequest * cmlabs::NetworkChannel::waitForHTTPRequest ( uint64 & conid,
uint32 ms )

Wait for the next queued HTTP request (sync-mode HTTP server).

Parameters
conidOut: connection the request arrived on (use with sendHTTPReply()).
msMax wait in ms.
Returns
New request owned by the caller, or NULL. Blocking.

Definition at line 1259 of file NetworkManager.cpp.

References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), queueHTTPRequests, queueHTTPRequestsMutex, and queueHTTPRequestsSemaphore.

◆ waitForMessage()

DataMessage * cmlabs::NetworkChannel::waitForMessage ( uint64 & conid,
uint32 ms )

Wait for the next queued DataMessage.

Parameters
conidOut: source connection.
msMax wait in ms.
Returns
New message owned by the caller, or NULL. Blocking.

Definition at line 1310 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().

◆ waitForNetworkEvent()

NetworkEvent * cmlabs::NetworkChannel::waitForNetworkEvent ( uint32 ms)

Wait for the next connection lifecycle event (sync mode).

Parameters
msMax wait in milliseconds.
Returns
New event owned by the caller, or NULL on timeout. Blocking.

Definition at line 1242 of file NetworkManager.cpp.

References eventQueue, eventQueueMutex, eventQueueSemaphore, cmlabs::GetTimeAgeMS(), and cmlabs::GetTimeNow().

◆ waitForTelnetLine()

TelnetLine * cmlabs::NetworkChannel::waitForTelnetLine ( uint64 & conid,
uint32 ms )

Wait for the next queued Telnet line.

Parameters
conidOut: source connection.
msMax wait in ms.
Returns
New line owned by the caller, or NULL. Blocking.

Definition at line 1293 of file NetworkManager.cpp.

References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), queueTelnetLines, queueTelnetLinesMutex, and queueTelnetLinesSemaphore.

◆ waitForWebsocketData()

WebsocketData * cmlabs::NetworkChannel::waitForWebsocketData ( uint64 & conid,
uint32 ms )

Wait for the next queued WebSocket message.

Parameters
conidOut: source connection.
msMax wait in ms.
Returns
New frame owned by the caller, or NULL. Blocking.

Definition at line 1276 of file NetworkManager.cpp.

References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), queueWebsocketData, queueWebsocketDataMutex, and queueWebsocketDataSemaphore.

Referenced by cmlabs::TestWebSocketRequestClient::run().

◆ NetworkManager

friend class NetworkManager
friend

Definition at line 425 of file NetworkManager.h.

References manager, and NetworkManager.

Referenced by NetworkChannel(), and NetworkManager.

Member Data Documentation

◆ channelMutex

utils::Mutex cmlabs::NetworkChannel::channelMutex
protected

◆ cid

◆ connectionThreads

◆ eventQueue

std::queue<NetworkEvent*> cmlabs::NetworkChannel::eventQueue
protected

Definition at line 576 of file NetworkManager.h.

Referenced by enterNetworkEvent(), shutdown(), and waitForNetworkEvent().

◆ eventQueueMutex

utils::Mutex cmlabs::NetworkChannel::eventQueueMutex
protected

Definition at line 578 of file NetworkManager.h.

Referenced by enterNetworkEvent(), shutdown(), and waitForNetworkEvent().

◆ eventQueueSemaphore

utils::Semaphore cmlabs::NetworkChannel::eventQueueSemaphore
protected

Definition at line 577 of file NetworkManager.h.

Referenced by enterNetworkEvent(), and waitForNetworkEvent().

◆ listeners

std::map<uint16, NetworkThread*> cmlabs::NetworkChannel::listeners
protected

TCP listener threads by port (owned).

Definition at line 573 of file NetworkManager.h.

Referenced by shutdown(), startListener(), and stopListener().

◆ manager

NetworkManager* cmlabs::NetworkChannel::manager
protected

◆ queueHTTPReplies

std::queue<HTTPReply*> cmlabs::NetworkChannel::queueHTTPReplies
protected

Definition at line 597 of file NetworkManager.h.

Referenced by enterHTTPReply(), shutdown(), and waitForHTTPReply().

◆ queueHTTPRepliesMutex

utils::Mutex cmlabs::NetworkChannel::queueHTTPRepliesMutex
protected

Definition at line 599 of file NetworkManager.h.

Referenced by enterHTTPReply(), shutdown(), and waitForHTTPReply().

◆ queueHTTPRepliesSemaphore

utils::Semaphore cmlabs::NetworkChannel::queueHTTPRepliesSemaphore
protected

Definition at line 598 of file NetworkManager.h.

Referenced by enterHTTPReply(), and waitForHTTPReply().

◆ queueHTTPRequests

std::queue<HTTPRequest*> cmlabs::NetworkChannel::queueHTTPRequests
protected

Definition at line 594 of file NetworkManager.h.

Referenced by enterHTTPRequest(), shutdown(), and waitForHTTPRequest().

◆ queueHTTPRequestsMutex

utils::Mutex cmlabs::NetworkChannel::queueHTTPRequestsMutex
protected

Definition at line 596 of file NetworkManager.h.

Referenced by enterHTTPRequest(), shutdown(), and waitForHTTPRequest().

◆ queueHTTPRequestsSemaphore

utils::Semaphore cmlabs::NetworkChannel::queueHTTPRequestsSemaphore
protected

Definition at line 595 of file NetworkManager.h.

Referenced by enterHTTPRequest(), and waitForHTTPRequest().

◆ queueMessageConIDs

std::queue<uint64> cmlabs::NetworkChannel::queueMessageConIDs
protected

Definition at line 601 of file NetworkManager.h.

Referenced by enterMessage(), shutdown(), and waitForMessage().

◆ queueMessages

std::queue<DataMessage*> cmlabs::NetworkChannel::queueMessages
protected

Definition at line 600 of file NetworkManager.h.

Referenced by enterMessage(), shutdown(), and waitForMessage().

◆ queueMessagesMutex

utils::Mutex cmlabs::NetworkChannel::queueMessagesMutex
protected

Definition at line 603 of file NetworkManager.h.

Referenced by enterMessage(), shutdown(), and waitForMessage().

◆ queueMessagesSemaphore

utils::Semaphore cmlabs::NetworkChannel::queueMessagesSemaphore
protected

Definition at line 602 of file NetworkManager.h.

Referenced by enterMessage(), and waitForMessage().

◆ queueTelnetLines

std::queue<TelnetLine*> cmlabs::NetworkChannel::queueTelnetLines
protected

Definition at line 604 of file NetworkManager.h.

Referenced by enterTelnetLine(), shutdown(), and waitForTelnetLine().

◆ queueTelnetLinesMutex

utils::Mutex cmlabs::NetworkChannel::queueTelnetLinesMutex
protected

Definition at line 606 of file NetworkManager.h.

Referenced by enterTelnetLine(), shutdown(), and waitForTelnetLine().

◆ queueTelnetLinesSemaphore

utils::Semaphore cmlabs::NetworkChannel::queueTelnetLinesSemaphore
protected

Definition at line 605 of file NetworkManager.h.

Referenced by enterTelnetLine(), and waitForTelnetLine().

◆ queueWebsocketData

std::queue<WebsocketData*> cmlabs::NetworkChannel::queueWebsocketData
protected

Definition at line 607 of file NetworkManager.h.

Referenced by enterWebsocketData(), shutdown(), and waitForWebsocketData().

◆ queueWebsocketDataMutex

utils::Mutex cmlabs::NetworkChannel::queueWebsocketDataMutex
protected

Definition at line 609 of file NetworkManager.h.

Referenced by enterWebsocketData(), shutdown(), and waitForWebsocketData().

◆ queueWebsocketDataSemaphore

utils::Semaphore cmlabs::NetworkChannel::queueWebsocketDataSemaphore
protected

Definition at line 608 of file NetworkManager.h.

Referenced by enterWebsocketData(), and waitForWebsocketData().

◆ receiver

NetworkReceiver* cmlabs::NetworkChannel::receiver
protected

Async dispatch target (not owned; may be NULL).

Definition at line 571 of file NetworkManager.h.

Referenced by enterHTTPReply(), enterHTTPRequest(), enterMessage(), enterNetworkEvent(), enterTelnetLine(), enterWebsocketData(), NetworkChannel(), and setNewReceiver().

◆ sslAllowSelfSigned

int8 cmlabs::NetworkChannel::sslAllowSelfSigned

Definition at line 499 of file NetworkManager.h.

Referenced by applySSLClientPolicy(), NetworkChannel(), and setSSLAllowSelfSigned().

◆ sslCAFile

std::string cmlabs::NetworkChannel::sslCAFile

Definition at line 503 of file NetworkManager.h.

Referenced by applySSLClientPolicy(), and setSSLCALocation().

◆ sslCAPath

std::string cmlabs::NetworkChannel::sslCAPath

Definition at line 504 of file NetworkManager.h.

Referenced by applySSLClientPolicy(), and setSSLCALocation().

◆ sslCASet

bool cmlabs::NetworkChannel::sslCASet

Definition at line 505 of file NetworkManager.h.

Referenced by applySSLClientPolicy(), NetworkChannel(), and setSSLCALocation().

◆ udpListeners

std::map<uint16, NetworkThread*> cmlabs::NetworkChannel::udpListeners
protected

UDP listener threads by port (owned).

Definition at line 574 of file NetworkManager.h.

Referenced by createUDPConnection(), and endUDPConnection().


The documentation for this class was generated from the following files: