|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
UDP datagram connection (bound port for input, or output-only sender). More...
#include <NetworkConnections.h>
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 |
| 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. | |
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 () |
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.
| cmlabs::UDPConnection::UDPConnection | ( | ) |
Definition at line 1127 of file NetworkConnections.cpp.
References defaultReceiver, cmlabs::NetworkConnection::NetworkConnection(), cmlabs::NetworkConnection::type, and UDPCON.
Referenced by UDPConnectionRun.
| cmlabs::UDPConnection::~UDPConnection | ( | ) |
Definition at line 1132 of file NetworkConnections.cpp.
| bool cmlabs::UDPConnection::connect | ( | uint16 | port, |
| NetworkDataReceiver * | receiver = NULL ) |
Bind a local UDP port for receiving datagrams.
| port | Local port to bind. |
| receiver | Optional push-mode receiver; when set, an internal thread reads datagrams and forwards them. |
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().
| uint64 cmlabs::UDPConnection::getLocalAddress | ( | ) |
Definition at line 1340 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::localAddress.
| bool cmlabs::UDPConnection::initForOutputOnly | ( | ) |
Create an unbound socket usable only for sending datagrams.
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.
|
virtual |
Rebind the local port (UDP has no session to re-establish).
| timeoutMS | Unused for UDP beyond the bind attempt. |
Implements cmlabs::NetworkConnection.
Definition at line 1330 of file NetworkConnections.cpp.
References connect(), cmlabs::NetworkConnection::disconnect(), GETIPPORT, cmlabs::NetworkConnection::localAddress, and cmlabs::NetworkConnection::receiver.
|
virtual |
Send one datagram.
| data | Payload bytes. |
| size | Payload length (must fit one datagram). |
| receiver | Destination endpoint as packed uint64 (address+port); when 0 the default receiver set via setDefaultReceiver() is used. |
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().
| bool cmlabs::UDPConnection::setDefaultReceiver | ( | uint64 | receiver | ) |
Set the default destination used by send() when receiver == 0.
| receiver | Packed uint64 endpoint. |
Definition at line 1335 of file NetworkConnections.cpp.
References defaultReceiver, and cmlabs::NetworkConnection::receiver.
|
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().
|
protected |
Default destination endpoint for send() when none is given.
Definition at line 679 of file NetworkConnections.h.
Referenced by send(), setDefaultReceiver(), and UDPConnection().