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

Abstract base class for all point-to-point network connections. More...

#include <NetworkConnections.h>

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

Public Member Functions

 NetworkConnection ()
 ~NetworkConnection ()
virtual bool disconnect (uint16 error=0)
 Close the connection and release the socket.
virtual bool reconnect (uint32 timeoutMS)=0
 Re-establish the connection to the previously known remote endpoint.
virtual bool didConnect (int timeout=0)
 Check/complete an in-progress (delayed) connect on the existing socket.
virtual bool didConnect (SOCKET s, int timeout)
 Variant of didConnect() testing an explicit socket handle.
virtual bool isConnected (int timeout=0)
 Test whether the connection is currently alive.
virtual bool isRemote ()
virtual uint64 getRemoteAddress ()
bool setConnectTimeout (uint32 timeoutMS)
 Set the timeout used by subsequent connect()/reconnect() attempts.
virtual bool send (const char *data, uint32 size, uint64 receiver=0)=0
 Send raw bytes on the connection.
virtual bool receive (char *data, uint32 size, uint32 timeout, bool peek=false)
 Receive exactly size bytes into data, waiting up to timeout ms.
virtual bool receiveAvailable (char *data, uint32 &size, uint32 maxSize, uint32 timeout, bool peek=false)
 Receive whatever bytes are available (up to maxSize).
virtual bool discard (uint32 size)
 Drop size bytes from the front of the receive buffer (after a peek).
virtual uint32 clearBuffer ()
 Discard all currently buffered input.
virtual bool waitForDataToRead (uint32 timeout)
 Block until data is readable (buffered or on the socket).
virtual bool waitForDataToBeWritten (uint32 timeout)
 Block until the socket is writable.
virtual uint32 getOutputSpeed ()
virtual uint32 getInputSpeed ()
virtual uint8 getConnectionType ()
bool setGreetingData (const char *data, uint32 size)
 Set greeting bytes sent automatically right after a connection is established (used e.g.
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.

Public Attributes

char * greetingData
 Owned copy of the greeting bytes (NULL if unset).
uint32 greetingSize
 Size of ::greetingData in bytes.

Protected Member Functions

void disconnectInternal (uint16 error)
virtual int32 readIntoBuffer ()
virtual bool resizeBuffer (uint32 len)
virtual bool run ()

Protected Attributes

uint8 type
uint64 remoteAddress
uint64 localAddress
uint64 lastActivity
uint32 threadID
SOCKET socket
bool remote
uint32 bufferLen
uint32 bufferContentLen
uint32 bufferContentPos
NetworkDataReceiverreceiver
utils::Mutex mutex
utils::Mutex sendMutex
char * buffer
uint32 connectTimeoutMS
uint32 inputSpeed
uint32 outputSpeed
uint64 inputBytes
uint64 outputBytes
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.

Detailed Description

Abstract base class for all point-to-point network connections.

Owns the socket, an elastic receive buffer (starting at INITIALBUFFERSIZE and grown on demand), throughput statistics and the connect-timeout setting. Concrete subclasses (TCPConnection, UDPConnection, SSLConnection) implement send(), reconnect() and the socket-specific read path.

Pull vs push
Data can be consumed in pull mode — the owner calls receive() / receiveAvailable() with a timeout — or in push mode by passing a NetworkDataReceiver to connect(), which starts an internal reader thread.
Thread-safety
The receive buffer is protected by mutex and sends are serialised by sendMutex, so one consumer plus concurrent producers are safe. Blocking calls take millisecond timeouts and never block indefinitely.

Definition at line 522 of file NetworkConnections.h.

Constructor & Destructor Documentation

◆ NetworkConnection()

◆ ~NetworkConnection()

cmlabs::NetworkConnection::~NetworkConnection ( )

Member Function Documentation

◆ clearBuffer()

uint32 cmlabs::NetworkConnection::clearBuffer ( )
virtual

Discard all currently buffered input.

Returns
The number of bytes discarded.

Definition at line 689 of file NetworkConnections.cpp.

References bufferContentLen, bufferContentPos, inputBytes, LOG_NETWORK, LogPrint, mutex, and readIntoBuffer().

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

◆ didConnect() [1/2]

bool cmlabs::NetworkConnection::didConnect ( int timeout = 0)
virtual

Check/complete an in-progress (delayed) connect on the existing socket.

Parameters
timeoutMilliseconds to wait for the connect to complete (0 = poll).
Returns
true once the socket is writable, i.e. connected.

Reimplemented in cmlabs::SSLConnection.

Definition at line 949 of file NetworkConnections.cpp.

References cmlabs::utils::GetLastOSErrorNumber(), INVALID_SOCKET, mutex, and socket.

Referenced by cmlabs::TCPConnection::connect(), and cmlabs::SSLConnection::didConnect().

◆ didConnect() [2/2]

bool cmlabs::NetworkConnection::didConnect ( SOCKET s,
int timeout )
virtual

Variant of didConnect() testing an explicit socket handle.

Parameters
sSocket to test.
timeoutMilliseconds to wait.
Returns
true once s is connected.

Definition at line 864 of file NetworkConnections.cpp.

References cmlabs::utils::GetLastOSErrorNumber(), and SOCKET.

◆ discard()

bool cmlabs::NetworkConnection::discard ( uint32 size)
virtual

Drop size bytes from the front of the receive buffer (after a peek).

Parameters
sizeNumber of bytes to discard.
Returns
true on success.

Definition at line 672 of file NetworkConnections.cpp.

References bufferContentLen, bufferContentPos, inputBytes, mutex, and readIntoBuffer().

Referenced by cmlabs::HTTPProtocol::ReceiveHTTPReply(), cmlabs::HTTPProtocol::ReceiveHTTPRequest(), cmlabs::TelnetProtocol::ReceiveTelnetLine(), and cmlabs::HTTPProtocol::ReceiveWebsocketData().

◆ disconnect()

bool cmlabs::NetworkConnection::disconnect ( uint16 error = 0)
virtual

Close the connection and release the socket.

Parameters
errorOptional NETWORKERROR_* reason forwarded to a data receiver.
Returns
true when disconnected. Safe to call repeatedly.

Reimplemented in cmlabs::SSLConnection.

Definition at line 395 of file NetworkConnections.cpp.

References disconnectInternal(), and mutex.

Referenced by cmlabs::NetworkChannel::HTTPClientRun(), cmlabs::LLMConnection::interactStream(), cmlabs::NetworkChannel::MessageConnectionRun(), cmlabs::TCPConnection::reconnect(), cmlabs::UDPConnection::reconnect(), run(), cmlabs::TCPConnection::send(), cmlabs::UDPConnection::send(), and ~NetworkConnection().

◆ disconnectInternal()

◆ getConnectionType()

uint8 cmlabs::NetworkConnection::getConnectionType ( )
virtual
Returns
The transport type: TCPCON, UDPCON, SSLCON or AESCON.

Definition at line 1102 of file NetworkConnections.cpp.

References type.

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

◆ getInputSpeed()

uint32 cmlabs::NetworkConnection::getInputSpeed ( )
virtual
Returns
Recent inbound throughput in bytes/second (moving estimate).

Definition at line 1098 of file NetworkConnections.cpp.

References inputSpeed.

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

◆ getOutputSpeed()

uint32 cmlabs::NetworkConnection::getOutputSpeed ( )
virtual
Returns
Recent outbound throughput in bytes/second (moving estimate).

Definition at line 1094 of file NetworkConnections.cpp.

References outputSpeed.

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

◆ getRemoteAddress()

◆ isConnected()

bool cmlabs::NetworkConnection::isConnected ( int timeout = 0)
virtual

◆ isRemote()

bool cmlabs::NetworkConnection::isRemote ( )
virtual
Returns
true if this connection was accepted from a remote peer (server side), false if it was initiated locally (client side).

Definition at line 374 of file NetworkConnections.cpp.

References remote.

◆ readIntoBuffer()

◆ receive()

bool cmlabs::NetworkConnection::receive ( char * data,
uint32 size,
uint32 timeout,
bool peek = false )
virtual

Receive exactly size bytes into data, waiting up to timeout ms.

Parameters
dataDestination buffer of at least size bytes.
sizeExact number of bytes required.
timeoutMaximum wait in milliseconds.
peekWhen true the bytes remain in the internal buffer (subsequent reads see them again).
Returns
true if size bytes were delivered before the timeout.

Reimplemented in cmlabs::SSLConnection.

Definition at line 606 of file NetworkConnections.cpp.

References buffer, bufferContentLen, bufferContentPos, cmlabs::GetTimeAge(), cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), inputBytes, inputSpeed, mutex, readIntoBuffer(), socket, and cmlabs::utils::WaitForSocketReadability().

Referenced by cmlabs::NetworkTest_SendReceiveData(), cmlabs::NetworkTest_TCPClient(), cmlabs::NetworkTest_TCPServer(), cmlabs::HTTPProtocol::ReceiveHTTPReply(), cmlabs::HTTPProtocol::ReceiveHTTPRequest(), cmlabs::MessageProtocol::ReceiveMessage(), cmlabs::TelnetProtocol::ReceiveTelnetLine(), cmlabs::HTTPProtocol::ReceiveWebsocketData(), and run().

◆ receiveAvailable()

bool cmlabs::NetworkConnection::receiveAvailable ( char * data,
uint32 & size,
uint32 maxSize,
uint32 timeout,
bool peek = false )
virtual

Receive whatever bytes are available (up to maxSize).

Parameters
dataDestination buffer.
sizeOut: number of bytes actually delivered.
maxSizeCapacity of data.
timeoutMaximum wait in milliseconds for at least one byte.
peekWhen true the bytes remain buffered.
Returns
true if any bytes were delivered.

Reimplemented in cmlabs::SSLConnection.

Definition at line 537 of file NetworkConnections.cpp.

References buffer, bufferContentLen, bufferContentPos, bufferLen, disconnectInternal(), cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), INITIALBUFFERSIZE, inputBytes, LOG_NETWORK, LogPrint, mutex, NETWORKERROR_RECEIVE, readIntoBuffer(), resizeBuffer(), socket, and cmlabs::utils::WaitForSocketReadability().

Referenced by cmlabs::NetworkChannel::ConnectionAutodetectRun(), cmlabs::HTTPProtocol::ReceiveHTTPReply(), cmlabs::HTTPProtocol::ReceiveHTTPRequest(), cmlabs::TelnetProtocol::ReceiveTelnetLine(), and cmlabs::HTTPProtocol::ReceiveWebsocketData().

◆ reconnect()

virtual bool cmlabs::NetworkConnection::reconnect ( uint32 timeoutMS)
pure virtual

Re-establish the connection to the previously known remote endpoint.

Parameters
timeoutMSMaximum time to spend connecting, in milliseconds.
Returns
true on success. Pure virtual; UDP treats this as a rebind.

Implemented in cmlabs::SSLConnection, cmlabs::TCPConnection, and cmlabs::UDPConnection.

References SOCKET.

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

◆ resizeBuffer()

bool cmlabs::NetworkConnection::resizeBuffer ( uint32 len)
protectedvirtual

◆ run()

◆ send()

virtual bool cmlabs::NetworkConnection::send ( const char * data,
uint32 size,
uint64 receiver = 0 )
pure virtual

Send raw bytes on the connection.

Parameters
dataBytes to send (caller retains ownership).
sizeNumber of bytes to send.
receiverDestination endpoint (uint64 address+port); used by UDP, ignored by stream transports.
Returns
true if all bytes were written.
Note
Serialised by an internal send mutex; may block until the OS buffer accepts the data or a send timeout occurs.

Implemented in cmlabs::SSLConnection, cmlabs::TCPConnection, and cmlabs::UDPConnection.

References receiver.

Referenced by cmlabs::NetworkChannel::HTTPClientRun(), cmlabs::NetworkChannel::MessageConnectionRun(), cmlabs::NetworkTest_TCPServer(), cmlabs::HTTPProtocol::SendHTTPReply(), cmlabs::HTTPProtocol::SendHTTPRequest(), cmlabs::MessageProtocol::SendMessage(), cmlabs::TelnetProtocol::SendTelnetLine(), and cmlabs::HTTPProtocol::SendWebsocketData().

◆ setConnectTimeout()

bool cmlabs::NetworkConnection::setConnectTimeout ( uint32 timeoutMS)

Set the timeout used by subsequent connect()/reconnect() attempts.

Parameters
timeoutMSTimeout in milliseconds.
Returns
true always.

Definition at line 365 of file NetworkConnections.cpp.

References connectTimeoutMS.

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

◆ setGreetingData()

bool cmlabs::NetworkConnection::setGreetingData ( const char * data,
uint32 size )

Set greeting bytes sent automatically right after a connection is established (used e.g.

to identify the peer or select a protocol).

Parameters
dataGreeting bytes (copied).
sizeByte count.
Returns
true on success.

Definition at line 351 of file NetworkConnections.cpp.

References greetingData, and greetingSize.

Referenced by cmlabs::NetworkChannel::addTCPConnection(), and cmlabs::NetworkChannel::addTCPConnection().

◆ waitForDataToBeWritten()

bool cmlabs::NetworkConnection::waitForDataToBeWritten ( uint32 timeout)
virtual

Block until the socket is writable.

Parameters
timeoutMaximum wait in milliseconds.
Returns
true if writable before the timeout.

Definition at line 713 of file NetworkConnections.cpp.

References socket, and cmlabs::utils::WaitForSocketWriteability().

◆ waitForDataToRead()

bool cmlabs::NetworkConnection::waitForDataToRead ( uint32 timeout)
virtual

Block until data is readable (buffered or on the socket).

Parameters
timeoutMaximum wait in milliseconds.
Returns
true if data is available before the timeout.

Definition at line 705 of file NetworkConnections.cpp.

References bufferContentLen, bufferContentPos, socket, and cmlabs::utils::WaitForSocketReadability().

Referenced by cmlabs::HTTPProtocol::ReceiveHTTPReply(), cmlabs::HTTPProtocol::ReceiveHTTPRequest(), cmlabs::TelnetProtocol::ReceiveTelnetLine(), cmlabs::TelnetProtocol::ReceiveTelnetLine(), and cmlabs::HTTPProtocol::ReceiveWebsocketData().

Member Data Documentation

◆ buffer

◆ bufferContentLen

◆ bufferContentPos

◆ bufferLen

◆ connectTimeoutMS

◆ greetingData

char* cmlabs::NetworkConnection::greetingData

◆ greetingSize

uint32 cmlabs::NetworkConnection::greetingSize

◆ inputBytes

uint64 cmlabs::NetworkConnection::inputBytes
protected

◆ inputSpeed

uint32 cmlabs::NetworkConnection::inputSpeed
protected

◆ lastActivity

uint64 cmlabs::NetworkConnection::lastActivity
protected

Definition at line 619 of file NetworkConnections.h.

Referenced by NetworkConnection(), and ~NetworkConnection().

◆ localAddress

◆ mutex

◆ outputBytes

uint64 cmlabs::NetworkConnection::outputBytes
protected

◆ outputSpeed

uint32 cmlabs::NetworkConnection::outputSpeed
protected

◆ receiver

◆ remote

◆ remoteAddress

◆ sendMutex

utils::Mutex cmlabs::NetworkConnection::sendMutex
protected

◆ socket

◆ threadID

◆ type


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