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

Connection/channel management layer: multi-protocol listeners, typed dispatch, HTTP client — and the CMSDK's built-in HTTP/WebSocket server. More...

Include dependency graph for NetworkManager.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cmlabs::NetworkEvent
 Notification of a connection lifecycle change (connect, disconnect, buffer state...). More...
class  cmlabs::NetworkReceiver
 Callback interface for asynchronous delivery of parsed network traffic. More...
class  cmlabs::HTTPTestServer
 Minimal HTTP server used by the unit tests: replies with a canned page. More...
class  cmlabs::WebsocketTestServer
 Minimal WebSocket echo server used by the unit tests (handles upgrade + echo). More...
class  cmlabs::NetworkManager
 Central owner of all channels, listeners and connections in a process. More...
class  cmlabs::NetworkThread
 Bookkeeping for one worker thread of a NetworkChannel (per listener or connection). More...
class  cmlabs::NetworkChannel
 One logical network interface: a group of listeners/connections with shared dispatch. More...

Namespaces

namespace  cmlabs

Macros

Network event types

Values of NetworkEvent::type describing connection lifecycle changes.

#define NETWORKEVENT_CONNECT   1
 Connection established.
#define NETWORKEVENT_RECONNECT   2
 Connection re-established after a failure (autoreconnect).
#define NETWORKEVENT_DISCONNECT   3
 Connection closed for good.
#define NETWORKEVENT_DISCONNECT_RETRYING   4
 Connection lost; reconnection attempts in progress.
#define NETWORKEVENT_BUFFER_LOW   5
 Receive buffer back below the pressure threshold.
#define NETWORKEVENT_BUFFER_FULL   6
 Receive buffer full; incoming data may back up.
#define NETWORKEVENT_UNPROCESSED_DATA   7
 Bytes arrived that no protocol handler consumed.
#define NETWORKEVENT_PROTOCOL_ERROR   8
 Protocol parsing/framing error on the connection.

Detailed Description

Connection/channel management layer: multi-protocol listeners, typed dispatch, HTTP client — and the CMSDK's built-in HTTP/WebSocket server.

NetworkManager is the top of the CMSDK networking stack. It owns NetworkChannel objects, each of which groups listeners and connections for one logical interface and dispatches parsed protocol objects (HTTPRequest, DataMessage, TelnetLine, WebsocketData) either by pushing them to a NetworkReceiver callback (async mode) or by queuing them for waitFor*() calls (sync mode).

Key capabilities:

  • Full HTTP(S)/WebSocket server: createListener() with PROTOCOL_HTTP_SERVER turns any port into a web server; the PsyProbe diagnostics/monitoring web UI is served through exactly this mechanism. There is deliberately no hard-coded default port — every listener port is chosen by the caller (system spec); the RequestGateway layer adds ports via addPort() the same way.
  • HTTP(S) client: the makeHTTPRequest() overloads perform blocking request/response exchanges, including multipart POST bodies.
  • Protocol auto-detection: a listener can accept several protocols on one port; the first bytes of each incoming connection are sniffed (NetworkChannel::autoDetectConnection()) to pick the handler thread.
  • Reconnection: connections created with autoreconnect=true are transparently re-established after failures; observers learn about lifecycle changes through NETWORKEVENT_* events.
Threads
The manager runs one management thread (NetworkManagerRun); each listener and each connection gets its own protocol-specific thread (HTTPServerRun, MessageConnectionRun, ...). Callback receivers are therefore invoked concurrently and must be thread-safe.
Platform notes
Built on NetworkConnections.h; all Winsock-vs-BSD-socket differences are encapsulated below this layer (see NetworkConnections.h).

A synchronous binary-message peer (listen, receive, reply) in a few lines:

NetworkManager net;
NetworkChannel* ch = net.createListener(9000, NOENC, PROTOCOL_MESSAGE,
false, 3000, true, 1, NULL); // sync mode
uint64 conid;
DataMessage* msg = ch->waitForMessage(conid, 1000); // caller owns msg
if (msg) {
DataMessage* reply = new DataMessage(msg);
reply->setString("Result", "ok");
ch->sendMessage(reply, conid); // sendMessage does NOT take ownership
delete reply;
delete msg;
}
#define NOENC
Plain, unencrypted transport.
#define PROTOCOL_MESSAGE
CMSDK binary DataMessage protocol (size-prefixed frames).

Definition in file NetworkManager.h.

Macro Definition Documentation

◆ NETWORKEVENT_BUFFER_FULL

#define NETWORKEVENT_BUFFER_FULL   6

Receive buffer full; incoming data may back up.

Definition at line 376 of file NetworkManager.h.

◆ NETWORKEVENT_BUFFER_LOW

#define NETWORKEVENT_BUFFER_LOW   5

Receive buffer back below the pressure threshold.

Definition at line 375 of file NetworkManager.h.

◆ NETWORKEVENT_CONNECT

#define NETWORKEVENT_CONNECT   1

◆ NETWORKEVENT_DISCONNECT

◆ NETWORKEVENT_DISCONNECT_RETRYING

#define NETWORKEVENT_DISCONNECT_RETRYING   4

◆ NETWORKEVENT_PROTOCOL_ERROR

#define NETWORKEVENT_PROTOCOL_ERROR   8

Protocol parsing/framing error on the connection.

Definition at line 378 of file NetworkManager.h.

Referenced by cmlabs::NetworkChannel::HTTPServerRun().

◆ NETWORKEVENT_RECONNECT

◆ NETWORKEVENT_UNPROCESSED_DATA

#define NETWORKEVENT_UNPROCESSED_DATA   7