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

Plain TCP stream connection (client-initiated or accepted from a listener). More...

#include <NetworkConnections.h>

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

Public Member Functions

 TCPConnection ()
 ~TCPConnection ()
bool connect (SOCKET s, uint64 localAddr, NetworkDataReceiver *receiver=NULL)
 Adopt an already-connected socket (server side, from a TCPListener).
bool connect (uint64 addr, uint32 timeoutMS, NetworkDataReceiver *receiver=NULL)
 Connect to a remote endpoint given as a packed uint64 (address+port).
bool connect (const char *addr, uint16 port, uint64 &location, uint32 timeoutMS, NetworkDataReceiver *receiver=NULL)
 Connect to a host by name/IP string and port.
bool connect (const uint32 *addresses, uint16 addressCount, uint16 port, uint64 &location, uint32 timeoutMS, NetworkDataReceiver *receiver=NULL)
 Try a list of candidate IPv4 addresses (same port) until one connects.
bool delayedConnect (uint64 addr, uint32 timeoutMS, NetworkDataReceiver *receiver)
 Begin a non-blocking connect; completion is checked with didConnect().
bool delayedConnect (const char *addr, uint16 port, uint64 &location, uint32 timeoutMS, NetworkDataReceiver *receiver)
 Begin a non-blocking connect to a host name/IP string and port.
bool send (const char *data, uint32 size, uint64 receiver=0)
 Send bytes on the stream (see NetworkConnection::send()).
bool reconnect (uint32 timeoutMS)
 Reconnect to the previously resolved remote endpoint.
Public Member Functions inherited from cmlabs::NetworkConnection
 NetworkConnection ()
 ~NetworkConnection ()
virtual bool disconnect (uint16 error=0)
 Close the connection and release the socket.
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 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.

Protected Member Functions

bool findRemoteAddress (uint64 &addr)
 Query the OS for the peer address of the connected socket.
Protected Member Functions inherited from cmlabs::NetworkConnection
void disconnectInternal (uint16 error)
virtual int32 readIntoBuffer ()
virtual bool resizeBuffer (uint32 len)
virtual bool run ()

Friends

THREAD_RET THREAD_FUNCTION_CALL TCPConnectionRun (THREAD_ARG arg)
 Thread entry point for a TCPConnection's push-mode reader loop.

Additional Inherited Members

Public Attributes inherited from cmlabs::NetworkConnection
char * greetingData
 Owned copy of the greeting bytes (NULL if unset).
uint32 greetingSize
 Size of ::greetingData in bytes.
Protected Attributes inherited from cmlabs::NetworkConnection
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

Plain TCP stream connection (client-initiated or accepted from a listener).

Supports immediate connects (with timeout), multi-address connects that try several candidate IPv4 addresses for the same host, and delayed (non-blocking) connects completed later via didConnect() — used by NetworkManager to establish connections without stalling its management thread.

Definition at line 691 of file NetworkConnections.h.

Constructor & Destructor Documentation

◆ TCPConnection()

cmlabs::TCPConnection::TCPConnection ( )

◆ ~TCPConnection()

cmlabs::TCPConnection::~TCPConnection ( )

Definition at line 1349 of file NetworkConnections.cpp.

Member Function Documentation

◆ connect() [1/4]

bool cmlabs::TCPConnection::connect ( const char * addr,
uint16 port,
uint64 & location,
uint32 timeoutMS,
NetworkDataReceiver * receiver = NULL )

Connect to a host by name/IP string and port.

Parameters
addrHost name or dotted IPv4 string (resolved via DNS if needed).
portRemote TCP port.
locationOut: resolved endpoint packed as uint64 (reusable for reconnects).
timeoutMSConnect timeout in ms.
receiverOptional push-mode data receiver.
Returns
true if connected within the timeout. Blocking (includes DNS resolution).

Definition at line 1498 of file NetworkConnections.cpp.

References connect(), GETIPADDRESSPORT, cmlabs::utils::LookupIPAddress(), and cmlabs::NetworkConnection::receiver.

◆ connect() [2/4]

bool cmlabs::TCPConnection::connect ( const uint32 * addresses,
uint16 addressCount,
uint16 port,
uint64 & location,
uint32 timeoutMS,
NetworkDataReceiver * receiver = NULL )

Try a list of candidate IPv4 addresses (same port) until one connects.

Parameters
addressesArray of IPv4 addresses in host order.
addressCountNumber of entries in addresses.
portRemote TCP port.
locationOut: the endpoint that succeeded, packed as uint64.
timeoutMSTotal connect budget in ms.
receiverOptional push-mode data receiver.
Returns
true if any address connected.

Definition at line 1506 of file NetworkConnections.cpp.

References connect(), GETIPADDRESSPORT, and cmlabs::NetworkConnection::receiver.

◆ connect() [3/4]

◆ connect() [4/4]

◆ delayedConnect() [1/2]

bool cmlabs::TCPConnection::delayedConnect ( const char * addr,
uint16 port,
uint64 & location,
uint32 timeoutMS,
NetworkDataReceiver * receiver )

Begin a non-blocking connect to a host name/IP string and port.

Parameters
addrHost name or dotted IPv4 string.
portRemote port.
locationOut: resolved endpoint packed as uint64.
timeoutMSTimeout used by later completion checks.
receiverOptional push-mode data receiver.
Returns
true if the connect was initiated.

Definition at line 1537 of file NetworkConnections.cpp.

References delayedConnect(), GETIPADDRESSPORT, cmlabs::utils::LookupIPAddress(), and cmlabs::NetworkConnection::receiver.

◆ delayedConnect() [2/2]

bool cmlabs::TCPConnection::delayedConnect ( uint64 addr,
uint32 timeoutMS,
NetworkDataReceiver * receiver )

Begin a non-blocking connect; completion is checked with didConnect().

Parameters
addrPacked uint64 endpoint.
timeoutMSTimeout used by later completion checks.
receiverOptional push-mode data receiver.
Returns
true if the connect was initiated.

Definition at line 1516 of file NetworkConnections.cpp.

References cmlabs::NetworkConnection::connectTimeoutMS, cmlabs::ThreadManager::CreateThread(), LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, cmlabs::NetworkConnection::receiver, cmlabs::NetworkConnection::remote, cmlabs::NetworkConnection::remoteAddress, TCPConnectionRun, and cmlabs::NetworkConnection::threadID.

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

◆ findRemoteAddress()

bool cmlabs::TCPConnection::findRemoteAddress ( uint64 & addr)
protected

Query the OS for the peer address of the connected socket.

Parameters
addrOut: packed uint64 endpoint.
Returns
true on success.

Definition at line 1669 of file NetworkConnections.cpp.

References GETIPADDRESSPORT, cmlabs::utils::GetLocalIPAddress(), INVALID_SOCKET, LOCALHOSTIP, and cmlabs::NetworkConnection::socket.

Referenced by connect().

◆ reconnect()

bool cmlabs::TCPConnection::reconnect ( uint32 timeoutMS)
virtual

Reconnect to the previously resolved remote endpoint.

Parameters
timeoutMSConnect timeout in ms.
Returns
true on success.

Implements cmlabs::NetworkConnection.

Definition at line 1663 of file NetworkConnections.cpp.

References connect(), cmlabs::NetworkConnection::disconnect(), cmlabs::NetworkConnection::receiver, and cmlabs::NetworkConnection::remoteAddress.

◆ send()

◆ TCPConnectionRun

THREAD_RET THREAD_FUNCTION_CALL TCPConnectionRun ( THREAD_ARG arg)
friend

Thread entry point for a TCPConnection's push-mode reader loop.

Definition at line 2818 of file NetworkConnections.cpp.

References cmlabs::NetworkConnection::run(), TCPConnection(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.

Referenced by connect(), connect(), and delayedConnect().


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