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

Raw socket transport layer: TCP/UDP/SSL connections and TCP listeners with buffered, thread-safe I/O. More...

#include "ThreadManager.h"
#include "HTML.h"
Include dependency graph for NetworkConnections.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  cmlabs::NetworkConnectionReceiver
 Callback interface for asynchronous delivery of newly accepted connections. More...
class  cmlabs::NetworkDataReceiver
 Callback interface for asynchronous (push) delivery of received bytes. More...
class  cmlabs::TCPListener
 TCP server socket: binds a port and accepts inbound connections (plain or SSL). More...
class  cmlabs::NetworkConnection
 Abstract base class for all point-to-point network connections. More...
class  cmlabs::UDPConnection
 UDP datagram connection (bound port for input, or output-only sender). More...
class  cmlabs::TCPConnection
 Plain TCP stream connection (client-initiated or accepted from a listener). More...
class  cmlabs::SSLConnection
 SSL/TLS-encrypted TCP connection (OpenSSL) with configurable peer verification. More...

Namespaces

namespace  cmlabs

Macros

#define INITIALBUFFERSIZE   4096
 Initial size in bytes of a connection's internal receive buffer; it grows on demand via NetworkConnection::resizeBuffer().
Encryption modes

Values accepted by listener/connection factory functions to select the transport security applied to a TCP stream.

#define NOENC   1
 Plain, unencrypted transport.
#define SSLENC   2
 SSL/TLS encryption (requires build with _USE_SSL_).
#define AESENC   3
 AES encryption (reserved; not currently implemented).
Connection types

Values returned by NetworkConnection::getConnectionType().

#define TCPCON   1
 Plain TCP stream connection.
#define UDPCON   2
 UDP datagram connection.
#define SSLCON   3
 SSL/TLS-encrypted TCP connection.
#define AESCON   4
 AES-encrypted connection (reserved).
Network error codes

Error identifiers passed to NetworkConnectionReceiver::registerError() and NetworkDataReceiver::registerError(), and to disconnect().

#define NETWORKERROR_ACCEPT   1
 accept() failed on a listener socket.
#define NETWORKERROR_RECEIVE   2
 A read from the socket failed (peer closed or socket error).
#define NETWORKERROR_SEND_ERROR   3
 A write to the socket failed.
#define NETWORKERROR_SEND_TIMEOUT   4
 A write could not complete within the timeout.
#define NETWORKERROR_MEMORYFULL   5
 The receive buffer could not grow to hold incoming data.
#define NETWORKERROR_GREETING_ERROR   6
 The initial greeting/handshake data exchange failed.

Functions

THREAD_RET THREAD_FUNCTION_CALL cmlabs::SSLConnectionRun (THREAD_ARG arg)
THREAD_RET THREAD_FUNCTION_CALL cmlabs::TCPListenerRun (THREAD_ARG arg)
 Thread entry point for a TCPListener's internal accept loop.
THREAD_RET THREAD_FUNCTION_CALL cmlabs::TCPConnectionRun (THREAD_ARG arg)
 Thread entry point for a TCPConnection's push-mode reader loop.
THREAD_RET THREAD_FUNCTION_CALL cmlabs::UDPConnectionRun (THREAD_ARG arg)
 Thread entry point for a UDPConnection's push-mode reader loop.
bool cmlabs::NetworkTest_TCPServer (uint16 port, uint32 count=0)
 Loopback test server: listens on port and echoes test payloads.
bool cmlabs::NetworkTest_TCPClient (const char *address, uint16 port, uint32 count=0)
 Loopback test client matching NetworkTest_TCPServer().
bool cmlabs::NetworkTest_SendReceiveData (TCPConnection *con, char *data, uint32 dataLen, uint32 c, bool receiveFirst=false)
 Send-and-verify helper used by the TCP tests.

Detailed Description

Raw socket transport layer: TCP/UDP/SSL connections and TCP listeners with buffered, thread-safe I/O.

This header defines the lowest layer of the CMSDK networking stack: the classes that own actual OS sockets. Above it sit NetworkProtocols.h (wire formats: HTTP, WebSocket, Telnet, binary messages) and NetworkManager.h (channel/connection lifecycle management and dispatch).

Class roles:

  • TCPListener: binds and listens on a TCP port, accepting inbound connections (plain or SSL, chosen by the encryption argument).
  • NetworkConnection: abstract base for all point-to-point connections; provides the shared receive buffer, byte counters, speed statistics, timeout-aware receive helpers and the reconnect contract.
  • TCPConnection / UDPConnection / SSLConnection: concrete transports. SSLConnection adds certificate handling and peer/hostname verification policy (compiled in only when _USE_SSL_ is defined).
Platform notes
Sockets differ between Windows and POSIX: on Windows the Winsock2 API is used (requiring one-time WSAStartup initialisation, performed via the CHECKNETWORKINIT macro in constructors) and sockets are of type SOCKET; on Linux/macOS BSD sockets (plain int file descriptors) are used and arpa/inet.h supplies htonl()/ntohl() byte-order conversion. All multi-byte values sent on the wire are converted to network byte order (big-endian) with htonl()/htons() so that mixed-endian hosts interoperate. The implementation (NetworkConnections.cpp) isolates these differences behind #ifdef WINDOWS / WIN32 / _WIN64 branches.
Addressing
Throughout the stack, a remote endpoint is packed into a single uint64 "location": the IPv4 address in the upper bits and the port in the lower bits. This lets a full endpoint be passed, stored and compared as one integer (see getRemoteAddress(), getLocalAddress()).
Thread-safety
Each connection guards its receive buffer with an internal mutex and serialises writers with a separate send mutex, so one reader thread and multiple writer threads may safely share a connection. Blocking calls (receive(), waitForDataToRead(), acceptConnection()) take explicit timeouts in milliseconds.

Definition in file NetworkConnections.h.

Macro Definition Documentation

◆ AESCON

#define AESCON   4

AES-encrypted connection (reserved).

Definition at line 362 of file NetworkConnections.h.

◆ AESENC

#define AESENC   3

AES encryption (reserved; not currently implemented).

Definition at line 354 of file NetworkConnections.h.

Referenced by cmlabs::TCPListener::acceptConnection().

◆ INITIALBUFFERSIZE

#define INITIALBUFFERSIZE   4096

Initial size in bytes of a connection's internal receive buffer; it grows on demand via NetworkConnection::resizeBuffer().

Definition at line 367 of file NetworkConnections.h.

Referenced by cmlabs::NetworkConnection::NetworkConnection(), cmlabs::SSLConnection::readIntoBuffer(), cmlabs::NetworkConnection::receiveAvailable(), and cmlabs::NetworkConnection::run().

◆ NETWORKERROR_ACCEPT

#define NETWORKERROR_ACCEPT   1

accept() failed on a listener socket.

Definition at line 372 of file NetworkConnections.h.

Referenced by cmlabs::TCPListener::acceptConnection().

◆ NETWORKERROR_GREETING_ERROR

#define NETWORKERROR_GREETING_ERROR   6

The initial greeting/handshake data exchange failed.

Definition at line 377 of file NetworkConnections.h.

Referenced by cmlabs::NetworkChannel::HTTPClientRun(), and cmlabs::NetworkChannel::MessageConnectionRun().

◆ NETWORKERROR_MEMORYFULL

#define NETWORKERROR_MEMORYFULL   5

The receive buffer could not grow to hold incoming data.

Definition at line 376 of file NetworkConnections.h.

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

◆ NETWORKERROR_RECEIVE

◆ NETWORKERROR_SEND_ERROR

#define NETWORKERROR_SEND_ERROR   3

A write to the socket failed.

Definition at line 374 of file NetworkConnections.h.

Referenced by cmlabs::TCPConnection::send(), and cmlabs::UDPConnection::send().

◆ NETWORKERROR_SEND_TIMEOUT

#define NETWORKERROR_SEND_TIMEOUT   4

A write could not complete within the timeout.

Definition at line 375 of file NetworkConnections.h.

Referenced by cmlabs::SSLConnection::send(), cmlabs::TCPConnection::send(), and cmlabs::UDPConnection::send().

◆ NOENC

◆ SSLCON

#define SSLCON   3

SSL/TLS-encrypted TCP connection.

Definition at line 361 of file NetworkConnections.h.

Referenced by cmlabs::SSLConnection::SSLConnection().

◆ SSLENC

◆ TCPCON

#define TCPCON   1

Plain TCP stream connection.

Definition at line 359 of file NetworkConnections.h.

Referenced by cmlabs::NetworkConnection::readIntoBuffer(), and cmlabs::TCPConnection::TCPConnection().

◆ UDPCON