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

UDP datagram connection (bound port for input, or output-only sender). More...

#include <NetworkConnections.h>

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

Public Member Functions

 UDPConnection ()
 ~UDPConnection ()
bool initForOutputOnly ()
 Create an unbound socket usable only for sending datagrams.
bool connect (uint16 port, NetworkDataReceiver *receiver=NULL)
 Bind a local UDP port for receiving datagrams.
bool send (const char *data, uint32 size, uint64 receiver=0)
 Send one datagram.
bool reconnect (uint32 timeoutMS)
 Rebind the local port (UDP has no session to re-establish).
bool setDefaultReceiver (uint64 receiver)
 Set the default destination used by send() when receiver == 0.
uint64 getLocalAddress ()
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 Attributes

uint64 defaultReceiver
 Default destination endpoint for send() when none is given.
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.

Friends

THREAD_RET THREAD_FUNCTION_CALL UDPConnectionRun (THREAD_ARG arg)
 Thread entry point for a UDPConnection'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 Member Functions inherited from cmlabs::NetworkConnection
void disconnectInternal (uint16 error)
virtual int32 readIntoBuffer ()
virtual bool resizeBuffer (uint32 len)
virtual bool run ()

Detailed Description

UDP datagram connection (bound port for input, or output-only sender).

Used for connectionless messaging (e.g. NetworkManager::sendUDPMessage and discovery/broadcast traffic). Because UDP has no session, reconnect() simply rebinds; datagrams may be lost or reordered by the network.

Definition at line 648 of file NetworkConnections.h.

Constructor & Destructor Documentation

◆ UDPConnection()

cmlabs::UDPConnection::UDPConnection ( )

◆ ~UDPConnection()

cmlabs::UDPConnection::~UDPConnection ( )

Definition at line 1132 of file NetworkConnections.cpp.

Member Function Documentation

◆ connect()

bool cmlabs::UDPConnection::connect ( uint16 port,
NetworkDataReceiver * receiver = NULL )

Bind a local UDP port for receiving datagrams.

Parameters
portLocal port to bind.
receiverOptional push-mode receiver; when set, an internal thread reads datagrams and forwards them.
Returns
true on success, false if the port could not be bound.

Definition at line 1173 of file NetworkConnections.cpp.

References cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkConnection::disconnectInternal(), INVALID_SOCKET, cmlabs::NetworkConnection::localAddress, LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, cmlabs::NetworkConnection::receiver, cmlabs::utils::SetSocketNonBlockingMode(), cmlabs::NetworkConnection::socket, SOCKET_ERROR, cmlabs::NetworkConnection::threadID, and UDPConnectionRun.

Referenced by cmlabs::NetworkChannel::createUDPConnection(), and reconnect().

◆ getLocalAddress()

uint64 cmlabs::UDPConnection::getLocalAddress ( )
Returns
The bound local endpoint packed as uint64, or 0 if unbound.

Definition at line 1340 of file NetworkConnections.cpp.

References cmlabs::NetworkConnection::localAddress.

◆ initForOutputOnly()

bool cmlabs::UDPConnection::initForOutputOnly ( )

Create an unbound socket usable only for sending datagrams.

Returns
true on success.

Definition at line 1135 of file NetworkConnections.cpp.

References cmlabs::NetworkConnection::disconnectInternal(), INVALID_SOCKET, LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, cmlabs::utils::SetSocketNonBlockingMode(), and cmlabs::NetworkConnection::socket.

◆ reconnect()

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

Rebind the local port (UDP has no session to re-establish).

Parameters
timeoutMSUnused for UDP beyond the bind attempt.
Returns
true on success.

Implements cmlabs::NetworkConnection.

Definition at line 1330 of file NetworkConnections.cpp.

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

◆ send()

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

Send one datagram.

Parameters
dataPayload bytes.
sizePayload length (must fit one datagram).
receiverDestination endpoint as packed uint64 (address+port); when 0 the default receiver set via setDefaultReceiver() is used.
Returns
true if the datagram was handed to the OS. Delivery is not guaranteed.

Implements cmlabs::NetworkConnection.

Definition at line 1243 of file NetworkConnections.cpp.

References defaultReceiver, cmlabs::NetworkConnection::disconnect(), GETIPADDRESSQUAD, GETIPPORT, cmlabs::GetTimeAge(), cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), LOG_NETWORK, LogPrint, NETWORKERROR_SEND_ERROR, NETWORKERROR_SEND_TIMEOUT, cmlabs::NetworkConnection::outputBytes, cmlabs::NetworkConnection::outputSpeed, cmlabs::NetworkConnection::receiver, cmlabs::NetworkConnection::sendMutex, cmlabs::NetworkConnection::socket, SOCKET_ERROR, and cmlabs::utils::WaitForSocketWriteability().

◆ setDefaultReceiver()

bool cmlabs::UDPConnection::setDefaultReceiver ( uint64 receiver)

Set the default destination used by send() when receiver == 0.

Parameters
receiverPacked uint64 endpoint.
Returns
true always.

Definition at line 1335 of file NetworkConnections.cpp.

References defaultReceiver, and cmlabs::NetworkConnection::receiver.

◆ UDPConnectionRun

THREAD_RET THREAD_FUNCTION_CALL UDPConnectionRun ( THREAD_ARG arg)
friend

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

Definition at line 2834 of file NetworkConnections.cpp.

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

Referenced by connect().

Member Data Documentation

◆ defaultReceiver

uint64 cmlabs::UDPConnection::defaultReceiver
protected

Default destination endpoint for send() when none is given.

Definition at line 679 of file NetworkConnections.h.

Referenced by send(), setDefaultReceiver(), and UDPConnection().


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