|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
Plain TCP stream connection (client-initiated or accepted from a listener). More...
#include <NetworkConnections.h>
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 |
| NetworkDataReceiver * | receiver |
| 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. | |
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.
| cmlabs::TCPConnection::TCPConnection | ( | ) |
Definition at line 1345 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::NetworkConnection(), TCPCON, and cmlabs::NetworkConnection::type.
Referenced by TCPConnectionRun.
| cmlabs::TCPConnection::~TCPConnection | ( | ) |
Definition at line 1349 of file NetworkConnections.cpp.
| 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.
| addr | Host name or dotted IPv4 string (resolved via DNS if needed). |
| port | Remote TCP port. |
| location | Out: resolved endpoint packed as uint64 (reusable for reconnects). |
| timeoutMS | Connect timeout in ms. |
| receiver | Optional push-mode data receiver. |
Definition at line 1498 of file NetworkConnections.cpp.
References connect(), GETIPADDRESSPORT, cmlabs::utils::LookupIPAddress(), and cmlabs::NetworkConnection::receiver.
| 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.
| addresses | Array of IPv4 addresses in host order. |
| addressCount | Number of entries in addresses. |
| port | Remote TCP port. |
| location | Out: the endpoint that succeeded, packed as uint64. |
| timeoutMS | Total connect budget in ms. |
| receiver | Optional push-mode data receiver. |
Definition at line 1506 of file NetworkConnections.cpp.
References connect(), GETIPADDRESSPORT, and cmlabs::NetworkConnection::receiver.
| bool cmlabs::TCPConnection::connect | ( | SOCKET | s, |
| uint64 | localAddr, | ||
| NetworkDataReceiver * | receiver = NULL ) |
Adopt an already-connected socket (server side, from a TCPListener).
| s | Connected socket handle (ownership transfers to this object). |
| localAddr | Local endpoint packed as uint64. |
| receiver | Optional push-mode data receiver. |
Definition at line 1352 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::bufferContentLen, cmlabs::NetworkConnection::bufferContentPos, cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkConnection::disconnectInternal(), findRemoteAddress(), GETIPADDRESSQUAD, GETIPPORT, cmlabs::NetworkConnection::localAddress, LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, cmlabs::NetworkConnection::receiver, cmlabs::NetworkConnection::remote, cmlabs::NetworkConnection::remoteAddress, cmlabs::utils::SetSocketNonBlockingMode(), SOCKET, cmlabs::NetworkConnection::socket, TCPConnectionRun, and cmlabs::NetworkConnection::threadID.
Referenced by cmlabs::TCPListener::acceptConnection(), connect(), connect(), connect(), cmlabs::NetworkChannel::createTCPConnection(), cmlabs::NetworkChannel::createTCPConnection(), cmlabs::NetworkChannel::createTCPConnection(), cmlabs::NetworkManager::createTCPConnection(), cmlabs::NetworkChannel::createWebsocketConnection(), cmlabs::LLMConnection::interactStream(), cmlabs::NetworkTest_TCPClient(), and reconnect().
| bool cmlabs::TCPConnection::connect | ( | uint64 | addr, |
| uint32 | timeoutMS, | ||
| NetworkDataReceiver * | receiver = NULL ) |
Connect to a remote endpoint given as a packed uint64 (address+port).
| addr | Packed endpoint. |
| timeoutMS | Connect timeout in ms. |
| receiver | Optional push-mode data receiver. |
Definition at line 1392 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::bufferContentLen, cmlabs::NetworkConnection::bufferContentPos, closesocket, connect(), cmlabs::NetworkConnection::connectTimeoutMS, cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkConnection::didConnect(), cmlabs::NetworkConnection::disconnectInternal(), GETIPPORT, cmlabs::utils::GetLastOSErrorNumber(), INVALID_SOCKET, LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, cmlabs::NetworkConnection::receiver, cmlabs::NetworkConnection::remote, cmlabs::NetworkConnection::remoteAddress, cmlabs::utils::SetSocketNonBlockingMode(), cmlabs::utils::Sleep(), SOCKET, cmlabs::NetworkConnection::socket, SOCKETTRYAGAIN, SOCKETWOULDBLOCK, TCPConnectionRun, and cmlabs::NetworkConnection::threadID.
| 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.
| addr | Host name or dotted IPv4 string. |
| port | Remote port. |
| location | Out: resolved endpoint packed as uint64. |
| timeoutMS | Timeout used by later completion checks. |
| receiver | Optional push-mode data receiver. |
Definition at line 1537 of file NetworkConnections.cpp.
References delayedConnect(), GETIPADDRESSPORT, cmlabs::utils::LookupIPAddress(), and cmlabs::NetworkConnection::receiver.
| bool cmlabs::TCPConnection::delayedConnect | ( | uint64 | addr, |
| uint32 | timeoutMS, | ||
| NetworkDataReceiver * | receiver ) |
Begin a non-blocking connect; completion is checked with didConnect().
| addr | Packed uint64 endpoint. |
| timeoutMS | Timeout used by later completion checks. |
| receiver | Optional push-mode data receiver. |
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().
|
protected |
Query the OS for the peer address of the connected socket.
| addr | Out: packed uint64 endpoint. |
Definition at line 1669 of file NetworkConnections.cpp.
References GETIPADDRESSPORT, cmlabs::utils::GetLocalIPAddress(), INVALID_SOCKET, LOCALHOSTIP, and cmlabs::NetworkConnection::socket.
Referenced by connect().
|
virtual |
Reconnect to the previously resolved remote endpoint.
| timeoutMS | Connect timeout in ms. |
Implements cmlabs::NetworkConnection.
Definition at line 1663 of file NetworkConnections.cpp.
References connect(), cmlabs::NetworkConnection::disconnect(), cmlabs::NetworkConnection::receiver, and cmlabs::NetworkConnection::remoteAddress.
|
virtual |
Send bytes on the stream (see NetworkConnection::send()).
| data | Bytes to send. |
| size | Byte count. |
| receiver | Ignored for TCP. |
Implements cmlabs::NetworkConnection.
Definition at line 1548 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::disconnect(), cmlabs::utils::GetLastOSErrorNumber(), cmlabs::GetTimeAge(), cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), LOG_NETWORK, LogPrint, NETWORKERROR_SEND_ERROR, NETWORKERROR_SEND_TIMEOUT, cmlabs::NetworkConnection::outputBytes, cmlabs::NetworkConnection::outputSpeed, cmlabs::NetworkConnection::receiver, send(), cmlabs::NetworkConnection::sendMutex, cmlabs::NetworkConnection::socket, SOCKET_ERROR, SOCKETTRYAGAIN, SOCKETWOULDBLOCK, and cmlabs::utils::WaitForSocketWriteability().
Referenced by cmlabs::NetworkTest_SendReceiveData(), cmlabs::NetworkTest_TCPClient(), and send().
|
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().