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

TCP server socket: binds a port and accepts inbound connections (plain or SSL). More...

#include <NetworkConnections.h>

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

Public Member Functions

 TCPListener ()
virtual ~TCPListener ()
bool setSSLCertificate (const char *sslCertPath, const char *sslKeyPath)
 Set the SSL certificate and private key used for inbound SSL connections.
bool init (uint16 port, uint8 encryption, NetworkConnectionReceiver *receiver=NULL, NetworkDataReceiver *dataReceiver=NULL)
 Bind and start listening on a port.
bool disconnect (uint16 error=0)
 Stop listening and close the socket.
bool isConnected ()
uint64 getLocalAddress ()
NetworkConnectionacceptConnection (uint32 timeout)
 Synchronously wait for and accept one inbound connection.
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.

Friends

THREAD_RET THREAD_FUNCTION_CALL TCPListenerRun (THREAD_ARG arg)
 Thread entry point for a TCPListener's internal accept loop.

Additional Inherited Members

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

TCP server socket: binds a port and accepts inbound connections (plain or SSL).

A TCPListener owns one listening socket. After init(), connections can be accepted either synchronously via acceptConnection() or asynchronously by supplying a NetworkConnectionReceiver to init(), in which case an internal thread runs the accept loop. When encryption is SSLENC the accepted connections are SSLConnection objects using the certificate set with setSSLCertificate(); otherwise they are plain TCPConnection objects.

Note
The socket is bound with SO_REUSEADDR semantics where available; init() fails (returns false) if the port is already in use.

Definition at line 451 of file NetworkConnections.h.

Constructor & Destructor Documentation

◆ TCPListener()

cmlabs::TCPListener::TCPListener ( )

Definition at line 67 of file NetworkConnections.cpp.

References CHECKNETWORKINIT, INVALID_SOCKET, NOENC, and cmlabs::Runnable::threadID.

Referenced by TCPListenerRun.

◆ ~TCPListener()

cmlabs::TCPListener::~TCPListener ( )
virtual

Definition at line 79 of file NetworkConnections.cpp.

References disconnect(), INVALID_SOCKET, and cmlabs::Runnable::threadID.

Member Function Documentation

◆ acceptConnection()

NetworkConnection * cmlabs::TCPListener::acceptConnection ( uint32 timeout)

Synchronously wait for and accept one inbound connection.

Parameters
timeoutMaximum wait in milliseconds.
Returns
A new NetworkConnection (TCPConnection or SSLConnection depending on the encryption mode) owned by the caller, or NULL on timeout/error.
Note
Blocking; only meaningful when no NetworkConnectionReceiver was given to init() (otherwise the internal thread consumes connections).

Definition at line 197 of file NetworkConnections.cpp.

References AESENC, closesocket, cmlabs::SSLConnection::connect(), cmlabs::TCPConnection::connect(), disconnect(), cmlabs::utils::GetLastOSErrorNumber(), cmlabs::GetTimeNow(), cmlabs::SSLConnection::init(), LOG_NETWORK, LogPrint, NETWORKERROR_ACCEPT, SD_BOTH, SOCKET, SOCKETTRYAGAIN, SOCKETWOULDBLOCK, SSLENC, and cmlabs::utils::WaitForSocketReadability().

Referenced by cmlabs::NetworkChannel::NetworkListenerRun(), and cmlabs::NetworkTest_TCPServer().

◆ disconnect()

bool cmlabs::TCPListener::disconnect ( uint16 error = 0)

Stop listening and close the socket.

Parameters
errorOptional NETWORKERROR_* reason propagated to the receiver.
Returns
true when shut down. Safe to call more than once.

Definition at line 101 of file NetworkConnections.cpp.

Referenced by acceptConnection(), and ~TCPListener().

◆ getLocalAddress()

uint64 cmlabs::TCPListener::getLocalAddress ( )
Returns
The local endpoint packed as a uint64 (IPv4 address + port), or 0 if not bound.

Definition at line 187 of file NetworkConnections.cpp.

◆ init()

bool cmlabs::TCPListener::init ( uint16 port,
uint8 encryption,
NetworkConnectionReceiver * receiver = NULL,
NetworkDataReceiver * dataReceiver = NULL )

Bind and start listening on a port.

Parameters
portTCP port to bind on all local interfaces.
encryptionNOENC or SSLENC (requires a certificate to be set).
receiverOptional; when non-NULL an internal thread accepts connections and pushes them to this receiver (async mode).
dataReceiverOptional data receiver installed on each accepted connection.
Returns
true on success; false if binding/listening failed (e.g. port in use).

Definition at line 112 of file NetworkConnections.cpp.

References cmlabs::ThreadManager::CreateThread(), cmlabs::utils::GetLastOSErrorNumber(), INVALID_SOCKET, LOG_NETWORK, LogPrint, cmlabs::utils::SetSocketNonBlockingMode(), SOCKET_ERROR, TCPListenerRun, and cmlabs::Runnable::threadID.

Referenced by cmlabs::NetworkTest_TCPServer(), and cmlabs::NetworkChannel::startListener().

◆ isConnected()

bool cmlabs::TCPListener::isConnected ( )
Returns
true while the listening socket is open and accepting.

Definition at line 108 of file NetworkConnections.cpp.

References INVALID_SOCKET.

◆ setSSLCertificate()

bool cmlabs::TCPListener::setSSLCertificate ( const char * sslCertPath,
const char * sslKeyPath )

Set the SSL certificate and private key used for inbound SSL connections.

Parameters
sslCertPathPath to the PEM certificate file.
sslKeyPathPath to the PEM private key file.
Returns
true if the paths were stored (validated later at handshake time).
Note
Must be called before init() when using SSLENC.

Definition at line 191 of file NetworkConnections.cpp.

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

◆ TCPListenerRun

THREAD_RET THREAD_FUNCTION_CALL TCPListenerRun ( THREAD_ARG arg)
friend

Thread entry point for a TCPListener's internal accept loop.

Definition at line 2824 of file NetworkConnections.cpp.

References TCPListener(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.

Referenced by init().


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