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

Central owner of all channels, listeners and connections in a process. More...

#include <NetworkManager.h>

Inheritance diagram for cmlabs::NetworkManager:
[legend]
Collaboration diagram for cmlabs::NetworkManager:
[legend]

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)
NetworkChannelcreateListener (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.
NetworkChannelcreateTCPConnection (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.
NetworkChannelcreateTCPConnection (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.
NetworkChannelcreateTCPConnection (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.
NetworkChannelcreateUDPConnection (uint16 port, uint8 protocol, bool isAsync, bool autoreconnect, uint32 channelID, NetworkReceiver *recv, uint64 &conid)
 Bind a UDP port for datagram traffic.
NetworkChanneladdTCPConnection (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.
NetworkChanneladdTCPConnection (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().
NetworkChannelcreateWebsocketConnection (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://).
NetworkChannelcreateWebsocketConnection (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)
NetworkChannelgetConnection (uint64 conid)
NetworkChannelgetTCPConnectionByPort (uint16 port)
NetworkChannelgetUDPConnectionByPort (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.
HTTPReplymakeHTTPRequest (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).
HTTPReplymakeHTTPRequest (HTTPRequest *req, const char *addr, uint16 port, uint8 encryption, uint32 timeout)
 Blocking exchange sending a pre-built request to an explicit endpoint.
HTTPReplymakeHTTPRequest (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.
HTTPReplymakeHTTPRequest (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).

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.
std::string sslCertPath
 PEM certificate path for SSL listeners.
std::string sslKeyPath
 PEM private key path for SSL listeners.
UDPConnectionudpOutputCon
 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

Detailed Description

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.

Ownership
The manager owns its channels and connections. Returned NetworkChannel pointers remain valid until the corresponding stop/end/remove call or manager destruction. HTTPReply objects returned by makeHTTPRequest() are owned by the caller.
Thread-safety
Public methods may be called from any thread; internal maps are guarded. makeHTTPRequest() is blocking (up to its timeout); the create*Connection() factories block up to timeoutMS unless a delayed/async variant is used.

Definition at line 161 of file NetworkManager.h.

Constructor & Destructor Documentation

◆ NetworkManager()

cmlabs::NetworkManager::NetworkManager ( )

◆ ~NetworkManager()

cmlabs::NetworkManager::~NetworkManager ( )
virtual

Definition at line 76 of file NetworkManager.cpp.

References channels, channelsByConnection, listeners, and udpOutputCon.

Member Function Documentation

◆ addConnection()

uint64 cmlabs::NetworkManager::addConnection ( NetworkChannel * channel)

Register an externally created channel with the manager.

Parameters
channelChannel to adopt (ownership transfers).
Returns
Its new connection id.

Definition at line 448 of file NetworkManager.cpp.

References channelsByConnection, lastConnectionID, and NetworkChannel.

◆ addTCPConnection() [1/2]

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.

Parameters
addrHost address.
portTCP port.
locationOut: resolved endpoint (packed uint64).
encryptionNOENC or SSLENC.
protocolSingle PROTOCOL_* flag.
isAsyncPush mode when true.
channelIDChannel to attach to (0 = new).
recvReceiver for async dispatch.
conidOut: new connection id.
timeoutMSConnect timeout in ms.
greetingDataBytes sent immediately after connect (copied).
greetingSizeByte count.

Definition at line 328 of file NetworkManager.cpp.

References cmlabs::NetworkChannel::addTCPConnection(), channels, channelsByConnection, cmlabs::NetworkChannel::cid, getConnection(), lastChannelID, LOG_NETWORK, LogPrint, NetworkChannel, and cmlabs::NetworkChannel::setNewReceiver().

Referenced by UnitTestDelayedConnect().

◆ addTCPConnection() [2/2]

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

Parameters
locationPacked endpoint.
encryptionNOENC or SSLENC.
protocolSingle PROTOCOL_* flag.
isAsyncPush mode when true.
channelIDChannel to attach to (0 = new).
recvReceiver for async dispatch.
conidOut: new connection id.
timeoutMSConnect timeout in ms.
greetingDataBytes sent immediately after connect (copied).
greetingSizeByte count.

Definition at line 362 of file NetworkManager.cpp.

References cmlabs::NetworkChannel::addTCPConnection(), channels, channelsByConnection, cmlabs::NetworkChannel::cid, getConnection(), GETIPADDRESSQUADPORT, lastChannelID, LOG_NETWORK, LogPrint, NetworkChannel, and cmlabs::NetworkChannel::setNewReceiver().

◆ createListener()

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.

Parameters
portTCP port to bind (caller-chosen; no default).
encryptionNOENC or SSLENC (certificate must be set first).
protocolPROTOCOL_* bit flags; several may be OR'ed for auto-detection.
isAsyncPush items to recv when true; queue for waitFor*() when false.
protocolTimeoutMilliseconds allowed to sniff/auto-detect a new connection's protocol.
isDefaultProtocolTreat the first protocol flag as fallback when detection is inconclusive.
channelIDChannel to attach to (0 = allocate a new channel).
recvReceiver for async dispatch (may be NULL for sync mode).
Returns
The channel serving the listener, or NULL if the port could not be bound. Owned by the manager.

Definition at line 103 of file NetworkManager.cpp.

References channels, cmlabs::NetworkChannel::cid, getConnection(), lastChannelID, listeners, NetworkChannel, cmlabs::NetworkChannel::setNewReceiver(), and cmlabs::NetworkChannel::startListener().

Referenced by cmlabs::Test_RequestClient(), UnitTest(), UnitTestDelayedConnect(), UnitTestHTTP(), and WebsocketTest().

◆ createTCPConnection() [1/3]

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.

Parameters
addrHost name or IPv4 string.
portRemote port.
encryptionNOENC or SSLENC.
protocolSingle PROTOCOL_* flag.
isAsyncPush mode when true.
autoreconnectRe-establish automatically on failure.
channelIDChannel to attach to (0 = new).
recvReceiver for async dispatch.
conidOut: new connection id.
locationOut: resolved endpoint (packed uint64).
timeoutMSConnect timeout in ms.
Returns
The channel, or NULL on failure. Blocking up to timeoutMS.

Definition at line 148 of file NetworkManager.cpp.

References channels, channelsByConnection, cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createTCPConnection(), getConnection(), lastChannelID, LOG_NETWORK, LogPrint, NetworkChannel, and cmlabs::NetworkChannel::setNewReceiver().

Referenced by cmlabs::HTTPServerTest(), makeHTTPRequest(), makeHTTPRequest(), makeHTTPRequest(), TestHTTP(), UnitTest(), UnitTestHTTP(), and WebsocketTest().

◆ createTCPConnection() [2/3]

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.

Parameters
addressesCandidate addresses.
addressCountNumber of candidates.
portTCP port.
locationOut: resolved endpoint (packed uint64).
encryptionNOENC or SSLENC.
protocolSingle PROTOCOL_* flag.
isAsyncPush mode when true.
channelIDChannel to attach to (0 = new).
recvReceiver for async dispatch.
conidOut: new connection id.
timeoutMSConnect timeout in ms.
autoreconnectRe-establish automatically on failure.

Definition at line 227 of file NetworkManager.cpp.

References channels, channelsByConnection, cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createTCPConnection(), getConnection(), GETIPADDRESSQUADPORT, lastChannelID, LOG_NETWORK, LogPrint, NetworkChannel, and cmlabs::NetworkChannel::setNewReceiver().

◆ createTCPConnection() [3/3]

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.

Parameters
locationPacked endpoint (address+port).
encryptionNOENC or SSLENC.
protocolSingle PROTOCOL_* flag.
isAsyncPush mode when true.
channelIDChannel to attach to (0 = new).
recvReceiver for async dispatch.
conidOut: new connection id.
timeoutMSConnect timeout in ms.
autoreconnectRe-establish automatically on failure.

Definition at line 182 of file NetworkManager.cpp.

References channels, channelsByConnection, cmlabs::NetworkChannel::cid, cmlabs::TCPConnection::connect(), cmlabs::NetworkChannel::createTCPConnection(), getConnection(), GETIPADDRESSQUADPORT, lastChannelID, LOG_NETWORK, LogPrint, NetworkChannel, cmlabs::NetworkChannel::setNewReceiver(), and cmlabs::NetworkChannel::startConnection().

◆ createUDPConnection()

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.

Parameters
portLocal UDP port.
protocolPROTOCOL_* flag (typically PROTOCOL_MESSAGE).
isAsyncPush mode when true.
autoreconnectRebind on failure.
channelIDChannel to attach to (0 = new).
recvReceiver.
conidOut: new connection id.
Returns
The channel, or NULL on failure.

Definition at line 398 of file NetworkManager.cpp.

References channels, channelsByConnection, cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createUDPConnection(), getConnection(), lastChannelID, NetworkChannel, cmlabs::NetworkChannel::setNewReceiver(), and udpListeners.

Referenced by UnitTest().

◆ createWebsocketConnection() [1/2]

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.

Parameters
uriRequest path.
addrHost.
portPort.
encryptionNOENC or SSLENC.
channelIDChannel to attach to (0 = new).
recvReceiver for frames.
conidOut: new connection id.
protocolNameOptional Sec-WebSocket-Protocol.
originOptional Origin header.
timeoutMSConnect+handshake timeout in ms.

Definition at line 294 of file NetworkManager.cpp.

References channels, channelsByConnection, cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createWebsocketConnection(), getConnection(), lastChannelID, LOG_NETWORK, LogPrint, NetworkChannel, and cmlabs::NetworkChannel::setNewReceiver().

◆ createWebsocketConnection() [2/2]

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

Parameters
urlFull URL including host, port and path.
channelIDChannel to attach to (0 = new).
recvReceiver for frames.
conidOut: new connection id.
protocolNameOptional Sec-WebSocket-Protocol.
originOptional Origin header.
timeoutMSConnect+handshake timeout in ms.
Returns
The channel, or NULL on failure. Blocking up to timeoutMS.

Definition at line 261 of file NetworkManager.cpp.

References channels, channelsByConnection, cmlabs::NetworkChannel::cid, cmlabs::NetworkChannel::createWebsocketConnection(), getConnection(), lastChannelID, LOG_NETWORK, LogPrint, NetworkChannel, and cmlabs::NetworkChannel::setNewReceiver().

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

◆ endConnection()

bool cmlabs::NetworkManager::endConnection ( uint64 conid)

Gracefully close a connection (thread is asked to finish; entry kept for reuse).

Parameters
conidConnection id.
Returns
true if found.

Definition at line 434 of file NetworkManager.cpp.

References channelsByConnection, cmlabs::NetworkChannel::endConnection(), and NetworkChannel.

◆ endUDPConnection()

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

Close the UDP connection bound to port.

Returns
true if found.

Definition at line 441 of file NetworkManager.cpp.

References cmlabs::NetworkChannel::endUDPConnection(), NetworkChannel, and udpListeners.

◆ getConnection()

NetworkChannel * cmlabs::NetworkManager::getConnection ( uint64 conid)
Parameters
conidConnection id.
Returns
The owning channel, or NULL. Owned by the manager.

Definition at line 468 of file NetworkManager.cpp.

References channelsByConnection, and NetworkChannel.

Referenced by addTCPConnection(), addTCPConnection(), createListener(), createTCPConnection(), createTCPConnection(), createTCPConnection(), createUDPConnection(), createWebsocketConnection(), and createWebsocketConnection().

◆ getConnectionType()

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

Definition at line 454 of file NetworkManager.cpp.

References channelsByConnection, cmlabs::NetworkChannel::getConnectionType(), and NetworkChannel.

◆ getRemoteAddress()

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

Definition at line 461 of file NetworkManager.cpp.

References channelsByConnection, cmlabs::NetworkChannel::getRemoteAddress(), and NetworkChannel.

◆ getTCPConnectionByPort()

NetworkChannel * cmlabs::NetworkManager::getTCPConnectionByPort ( uint16 port)
Parameters
portListener port.
Returns
The channel listening on that TCP port, or NULL.

Definition at line 472 of file NetworkManager.cpp.

References listeners, and NetworkChannel.

◆ getUDPConnectionByPort()

NetworkChannel * cmlabs::NetworkManager::getUDPConnectionByPort ( uint16 port)
Parameters
portUDP port.
Returns
The channel bound to that UDP port, or NULL.

Definition at line 476 of file NetworkManager.cpp.

References NetworkChannel, and udpListeners.

◆ makeHTTPRequest() [1/4]

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

Parameters
urlFull URL (http:// or https://, may embed basic-auth credentials).
timeoutTotal timeout in ms.
contentOptional request body (POST).
contentSizeBody size.
Returns
New reply owned by the caller, or NULL on failure/timeout.

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

◆ makeHTTPRequest() [2/4]

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.

Parameters
reqRequest to send (caller keeps ownership).
addrHost.
portPort.
encryptionNOENC or SSLENC.
timeoutTotal timeout in ms.
Returns
New reply owned by the caller, or NULL.

Definition at line 537 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.

◆ makeHTTPRequest() [3/4]

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.

Parameters
opsHTTP_* method id.
addrFull URL or host address.
timeoutms.
headerEntriesExtra header name/value pairs.
contentBody bytes.
contentTypeBody MIME type.
contentSizeBody size.
keepAliveRequest a persistent connection.
ifModifiedSincems epoch or 0.
Returns
New reply owned by the caller, or NULL.

Definition at line 560 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.

◆ makeHTTPRequest() [4/4]

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

Parameters
opsHTTP_* method id.
addrFull URL or host address.
timeoutms.
headerEntriesExtra headers.
bodyEntriesNamed body parts (not owned).
keepAlivePersistent connection flag.
ifModifiedSincems epoch or 0.
Returns
New reply owned by the caller, or NULL.

Definition at line 613 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.

◆ NetworkManagerRun()

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 64 of file NetworkManager.cpp.

References THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.

◆ removeConnection()

bool cmlabs::NetworkManager::removeConnection ( uint64 conid)

Close a connection and remove it from the manager's maps entirely.

Parameters
conidConnection id.
Returns
true if found and removed.

Definition at line 429 of file NetworkManager.cpp.

References channelsByConnection.

◆ run()

bool cmlabs::NetworkManager::run ( )
protected

Supervision loop body (delayed connects, reconnection).

Returns
true to keep running.

◆ sendUDPMessage()

bool cmlabs::NetworkManager::sendUDPMessage ( DataMessage * msg,
uint64 destination )

Send a DataMessage as a UDP datagram via the shared output socket.

Parameters
msgMessage (caller keeps ownership).
destinationPacked uint64 endpoint.
Returns
true if the datagram was sent (delivery not guaranteed).

Definition at line 480 of file NetworkManager.cpp.

References cmlabs::MessageProtocol::SendMessage(), udpOutputCon, and udpOutputConMutex.

Referenced by UnitTest().

◆ setSSLAllowSelfSigned()

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

Definition at line 199 of file NetworkManager.h.

References sslAllowSelfSigned.

◆ setSSLCALocation()

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

Definition at line 203 of file NetworkManager.h.

References sslCAFile, sslCAPath, and sslCASet.

◆ setSSLCertificate()

bool cmlabs::NetworkManager::setSSLCertificate ( const char * sslCertPath,
const char * sslKeyPath )

Set the certificate/key used by SSL listeners created via this manager.

Parameters
sslCertPathPEM certificate path.
sslKeyPathPEM private key path.
Returns
true if stored. Must be set before creating SSLENC listeners.

Definition at line 96 of file NetworkManager.cpp.

References sslCertPath, and sslKeyPath.

◆ stopListener()

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

Stop a listener previously opened with createListener().

Parameters
portListener port.
protocolPROTOCOL_* flags it was created with.
Returns
true if found and stopped.

Definition at line 139 of file NetworkManager.cpp.

References listeners, NetworkChannel, and cmlabs::NetworkChannel::stopListener().

◆ TestHTTP()

bool cmlabs::NetworkManager::TestHTTP ( const char * host,
uint32 port,
std::vector< std::string > & urls )
static

◆ UnitTest()

◆ UnitTestDelayedConnect()

◆ UnitTestHTTP()

◆ WebsocketTest()

◆ NetworkChannel

Member Data Documentation

◆ channels

std::map<uint32, NetworkChannel*> cmlabs::NetworkManager::channels
protected

◆ channelsByConnection

◆ lastChannelID

◆ lastConnectionID

uint64 cmlabs::NetworkManager::lastConnectionID
protected

Last connection id issued.

Definition at line 360 of file NetworkManager.h.

Referenced by addConnection(), and NetworkManager().

◆ listeners

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

TCP listeners by port.

Definition at line 350 of file NetworkManager.h.

Referenced by createListener(), getTCPConnectionByPort(), stopListener(), and ~NetworkManager().

◆ sslAllowSelfSigned

int8 cmlabs::NetworkManager::sslAllowSelfSigned

Definition at line 200 of file NetworkManager.h.

Referenced by NetworkManager(), and setSSLAllowSelfSigned().

◆ sslCAFile

std::string cmlabs::NetworkManager::sslCAFile

Definition at line 204 of file NetworkManager.h.

Referenced by setSSLCALocation().

◆ sslCAPath

std::string cmlabs::NetworkManager::sslCAPath

Definition at line 205 of file NetworkManager.h.

Referenced by setSSLCALocation().

◆ sslCASet

bool cmlabs::NetworkManager::sslCASet

Definition at line 206 of file NetworkManager.h.

Referenced by NetworkManager(), and setSSLCALocation().

◆ sslCertPath

std::string cmlabs::NetworkManager::sslCertPath
protected

PEM certificate path for SSL listeners.

Definition at line 353 of file NetworkManager.h.

Referenced by NetworkChannel, and setSSLCertificate().

◆ sslKeyPath

std::string cmlabs::NetworkManager::sslKeyPath
protected

PEM private key path for SSL listeners.

Definition at line 354 of file NetworkManager.h.

Referenced by NetworkChannel, and setSSLCertificate().

◆ udpListeners

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

UDP listeners by port.

Definition at line 351 of file NetworkManager.h.

Referenced by createUDPConnection(), endUDPConnection(), and getUDPConnectionByPort().

◆ udpOutputCon

UDPConnection* cmlabs::NetworkManager::udpOutputCon
protected

Shared output-only UDP socket for sendUDPMessage().

Definition at line 356 of file NetworkManager.h.

Referenced by NetworkManager(), sendUDPMessage(), and ~NetworkManager().

◆ udpOutputConMutex

utils::Mutex cmlabs::NetworkManager::udpOutputConMutex
protected

Serialises use of ::udpOutputCon.

Definition at line 357 of file NetworkManager.h.

Referenced by sendUDPMessage().


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