TDME2 1.9.121
Classes | Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
UDPServer Class Reference

Base class for network UDP servers. More...

#include <tdme/network/udpserver/UDPServer.h>

Inheritance diagram for UDPServer:
Inheritance graph
Collaboration diagram for UDPServer:
Collaboration graph

Classes

struct  ClientId
 
struct  UDPServer_Statistics
 

Public Member Functions

 UDPServer (const std::string &name, const std::string &host, const unsigned int port, const unsigned int maxCCU)
 Public constructor. More...
 
virtual ~UDPServer ()
 destructor More...
 
virtual void run ()
 main event loop More...
 
const UDPServer_Statistics getStatistics ()
 
- Public Member Functions inherited from Thread
 Thread (const string &name, size_t stackSize=2 *1024 *1024)
 Public constructor. More...
 
virtual ~Thread ()
 Public destructor. More...
 
void join ()
 Blocks caller thread until this thread has been terminated. More...
 
void start ()
 Starts this objects thread. More...
 
virtual void run ()=0
 Abstract run() method, should be implemented by subclassed class, will be called after spawn by start() More...
 
void stop ()
 Requests that this thread should be stopped. More...
 
bool isStopRequested ()
 Returns if stop has been requested. More...
 
- Public Member Functions inherited from Server< UDPServerClient, UDPServerGroup >
 Server (const std::string &name, const std::string &host, const unsigned int port, const unsigned int maxCCU)
 
void setIOThreadCount (const unsigned int ioThreadCount)
 Sets up the numbers of threads to handle IO and framing. More...
 
void setWorkerThreadCount (const unsigned int workerThreadCount)
 Sets up the number of workers that handle requests in thread pool. More...
 
void setThreadPoolMaxElements (const unsigned int maxElements)
 Sets up max number of elements in worker thread pool queue. More...
 
virtual ~Server ()
 destructor More...
 
ClientKeySet getClientKeySet ()
 get a copy of current client keys More...
 
UDPServerClientgetClientByKey (const std::string &clientKey)
 retrieve a client by key, the client reference is acquired, must be released after usage More...
 
ClientKeySet getGroupKeySet ()
 get a copy of current group keys More...
 
UDPServerGroupgetGroupByKey (const std::string &groupKey)
 retrieve a group by key, the group reference is acquired, must be released after usage More...
 

Protected Types

enum  MessageType { MESSAGETYPE_CONNECT = 0 , MESSAGETYPE_MESSAGE = 1 , MESSAGETYPE_ACKNOWLEDGEMENT = 2 }
 
- Protected Types inherited from Server< UDPServerClient, UDPServerGroup >
typedef std::map< const std::string, UDPServerClient * > ClientKeyMap
 
typedef std::map< const std::string, UDPServerClient * > GroupKeyMap
 
typedef std::set< UDPServerClient * > ClientSet
 

Protected Member Functions

virtual UDPServerClientaccept (const uint32_t clientId, const std::string &ip, const unsigned int port)
 method to implement for accepting clients More...
 
virtual void identify (stringstream *frame, MessageType &messageType, uint32_t &connectionId, uint32_t &messageId, uint8_t &retries)
 Identifies a client message. More...
 
virtual void validate (stringstream *frame)
 Validates a client message. More...
 
virtual void writeHeader (stringstream *frame, MessageType messageType, const uint32_t clientId, const uint32_t messageId, const uint8_t retries)
 Writes a message header to message. More...
 
- Protected Member Functions inherited from Server< UDPServerClient, UDPServerGroup >
bool setClientKey (UDPServerClient *client, const std::string &clientKey)
 sets a client identification key More...
 
void closeClient (UDPServerClient *client)
 closes a client connection More...
 
bool setGroupKey (UDPServerGroup *group, const std::string &groupKey)
 sets a group identification key More...
 
void closeGroup (UDPServerGroup *group)
 closes a group connection More...
 

Static Protected Member Functions

static void initializeHeader (stringstream *frame)
 Writes a empty header to message. More...
 

Private Types

typedef std::map< uint32_t, ClientId * > ClientIdMap
 
typedef std::map< string, UDPServerClient * > ClientIpMap
 
typedef std::set< UDPServerClient * > ClientSet
 

Private Member Functions

void addClient (UDPServerClient *client)
 maps a new client to a given client id More...
 
void removeClient (UDPServerClient *client)
 removes a client More...
 
UDPServerClientlookupClient (const uint32_t clientId)
 Look ups a client by client id. More...
 
UDPServerClientgetClientByIp (const string &ip, const unsigned int port)
 Returns client by host name and port. More...
 
void cleanUpClients ()
 Clean up clients that have been idle for some time or are flagged to be shut down. More...
 
void sendMessage (const UDPServerClient *client, stringstream *frame, const bool safe, const bool deleteFrame, const MessageType messageType, const uint32_t messageId=MESSAGE_ID_NONE)
 pushes a message to be send, takes over ownership of frame More...
 
void processAckReceived (UDPServerClient *client, const uint32_t messageId)
 Processes an acknowlegdement reception. More...
 
const uint32_t allocateClientId ()
 Allocates a client id for a new client. More...
 

Private Attributes

ClientIdMap clientIdMap
 
ReadWriteLock clientIdMapReadWriteLock
 
ClientIpMap clientIpMap
 
ReadWriteLock clientIpMapReadWriteLock
 
unsigned int ioThreadCurrent
 
UDPServerIOThread ** ioThreads
 
ServerWorkerThreadPoolworkerThreadPool
 
uint32_t clientCount
 
uint32_t messageCount
 
UDPServer_Statistics statistics
 

Static Private Attributes

static const uint64_t CLIENT_CLEANUP_IDLETIME = 120000L
 
static const uint32_t MESSAGE_ID_NONE = 0
 

Friends

class UDPServerClient
 
class UDPServerIOThread
 
class ServerGroup< UDPServer, UDPServerClient, UDPServerGroup >
 

Additional Inherited Members

- Public Types inherited from Server< UDPServerClient, UDPServerGroup >
typedef std::set< std::string > ClientKeySet
 
typedef std::set< std::string > GroupKeySet
 
- Static Public Member Functions inherited from Thread
static int getHardwareThreadCount ()
 
static void sleep (const uint64_t milliseconds)
 sleeps current thread for given time in milliseconds More...
 
- Protected Attributes inherited from Server< UDPServerClient, UDPServerGroup >
std::string name
 
std::string host
 
unsigned int port
 
unsigned int maxCCU
 
BarrierstartUpBarrier
 
ClientKeyMap clientKeyMap
 
ClientKeySet clientKeySet
 
ReadWriteLock clientKeyListsReadWriteLock
 
GroupKeyMap groupKeyMap
 
GroupKeySet groupKeySet
 
ReadWriteLock groupKeyListsReadWriteLock
 
unsigned int ioThreadCount
 
unsigned int workerThreadPoolCount
 
unsigned int workerThreadPoolMaxElements
 

Detailed Description

Base class for network UDP servers.

Author
Andreas Drewke

Definition at line 39 of file UDPServer.h.

Member Typedef Documentation

◆ ClientIdMap

typedef std::map<uint32_t, ClientId*> ClientIdMap
private

Definition at line 133 of file UDPServer.h.

◆ ClientIpMap

typedef std::map<string, UDPServerClient*> ClientIpMap
private

Definition at line 134 of file UDPServer.h.

◆ ClientSet

typedef std::set<UDPServerClient*> ClientSet
private

Definition at line 135 of file UDPServer.h.

Member Enumeration Documentation

◆ MessageType

enum MessageType
protected
Enumerator
MESSAGETYPE_CONNECT 
MESSAGETYPE_MESSAGE 
MESSAGETYPE_ACKNOWLEDGEMENT 

Definition at line 79 of file UDPServer.h.

Constructor & Destructor Documentation

◆ UDPServer()

UDPServer ( const std::string &  name,
const std::string &  host,
const unsigned int  port,
const unsigned int  maxCCU 
)

Public constructor.

Parameters
nameserver name
hosthost where to bind the server socket
portport to listen on
maxCCUmax ccu

Definition at line 39 of file UDPServer.cpp.

◆ ~UDPServer()

~UDPServer ( )
virtual

destructor

Definition at line 52 of file UDPServer.cpp.

Member Function Documentation

◆ accept()

UDPServerClient * accept ( const uint32_t  clientId,
const std::string &  ip,
const unsigned int  port 
)
protectedvirtual

method to implement for accepting clients

Parameters
clientIdclient id
ipip
portport
Returns
server client class

Definition at line 163 of file UDPServer.cpp.

◆ addClient()

void addClient ( UDPServerClient client)
private

maps a new client to a given client id

Parameters
clientclient
Exceptions
tdme::network::udpserver::NetworkServerExceptionif id is already in use

Definition at line 280 of file UDPServer.cpp.

◆ allocateClientId()

const uint32_t allocateClientId ( )
private

Allocates a client id for a new client.

Returns
client id

Definition at line 495 of file UDPServer.cpp.

◆ cleanUpClients()

void cleanUpClients ( )
private

Clean up clients that have been idle for some time or are flagged to be shut down.

Definition at line 436 of file UDPServer.cpp.

◆ getClientByIp()

UDPServerClient * getClientByIp ( const string &  ip,
const unsigned int  port 
)
private

Returns client by host name and port.

Parameters
ipip
portport
Returns
client

Definition at line 423 of file UDPServer.cpp.

◆ getStatistics()

const UDPServer::UDPServer_Statistics getStatistics ( )
Returns
UDP client statistics

Definition at line 499 of file UDPServer.cpp.

◆ identify()

void identify ( stringstream *  frame,
MessageType messageType,
uint32_t &  connectionId,
uint32_t &  messageId,
uint8_t &  retries 
)
protectedvirtual

Identifies a client message.

Parameters
frameframe
messageTypemessage type (see UDPServer::MessageType)
connectionIdconnection id
messageIdmessage id
retriesretries
Exceptions
tdme::network::udpserver::NetworkServerException
Returns
client or NULL

Definition at line 167 of file UDPServer.cpp.

◆ initializeHeader()

void initializeHeader ( stringstream *  frame)
staticprotected

Writes a empty header to message.

Parameters
frameframe
Exceptions
tdme::network::udpserver::NetworkServerException

Definition at line 229 of file UDPServer.cpp.

◆ lookupClient()

UDPServerClient * lookupClient ( const uint32_t  clientId)
private

Look ups a client by client id.

Parameters
clientIdclient id
Exceptions
tdme::network::udpserver::NetworkServerExceptionif client does not exist
Returns
client

Definition at line 389 of file UDPServer.cpp.

◆ processAckReceived()

void processAckReceived ( UDPServerClient client,
const uint32_t  messageId 
)
private

Processes an acknowlegdement reception.

Parameters
clientclient
messageIdmessage id
Exceptions
tdme::network::udpserver::NetworkServerException

Definition at line 490 of file UDPServer.cpp.

◆ removeClient()

void removeClient ( UDPServerClient client)
private

removes a client

Parameters
clientclient
Exceptions
tdme::network::udpserver::NetworkServerExceptionif id is not in use

Definition at line 341 of file UDPServer.cpp.

◆ run()

void run ( )
virtual

main event loop

Implements Thread.

Definition at line 55 of file UDPServer.cpp.

◆ sendMessage()

void sendMessage ( const UDPServerClient client,
stringstream *  frame,
const bool  safe,
const bool  deleteFrame,
const MessageType  messageType,
const uint32_t  messageId = MESSAGE_ID_NONE 
)
private

pushes a message to be send, takes over ownership of frame

Parameters
clientclient
frameframe to be send
safesafe, requires ack and retransmission
deleteFramedelete frame
messageTypemessage type
messageIdmessage id (only for MESSAGETYPE_MESSAGE)
Exceptions
tdme::network::udpserver::NetworkServerException

Definition at line 469 of file UDPServer.cpp.

◆ validate()

void validate ( stringstream *  frame)
protectedvirtual

Validates a client message.

Parameters
frameframe
Exceptions
tdme::network::udpserver::NetworkServerException

Definition at line 226 of file UDPServer.cpp.

◆ writeHeader()

void writeHeader ( stringstream *  frame,
MessageType  messageType,
const uint32_t  clientId,
const uint32_t  messageId,
const uint8_t  retries 
)
protectedvirtual

Writes a message header to message.

Parameters
frameframe
messageTypemessage type
clientIdclient id
messageIdmessage id
retriesretries
Exceptions
tdme::network::udpserver::NetworkServerException

Definition at line 241 of file UDPServer.cpp.

Friends And Related Function Documentation

◆ ServerGroup< UDPServer, UDPServerClient, UDPServerGroup >

Definition at line 41 of file UDPServer.h.

◆ UDPServerClient

friend class UDPServerClient
friend

Definition at line 40 of file UDPServer.h.

◆ UDPServerIOThread

friend class UDPServerIOThread
friend

Definition at line 41 of file UDPServer.h.

Member Data Documentation

◆ CLIENT_CLEANUP_IDLETIME

const uint64_t CLIENT_CLEANUP_IDLETIME = 120000L
staticprivate

Definition at line 127 of file UDPServer.h.

◆ clientCount

uint32_t clientCount
private

Definition at line 210 of file UDPServer.h.

◆ clientIdMap

ClientIdMap clientIdMap
private

Definition at line 200 of file UDPServer.h.

◆ clientIdMapReadWriteLock

ReadWriteLock clientIdMapReadWriteLock
private

Definition at line 201 of file UDPServer.h.

◆ clientIpMap

ClientIpMap clientIpMap
private

Definition at line 203 of file UDPServer.h.

◆ clientIpMapReadWriteLock

ReadWriteLock clientIpMapReadWriteLock
private

Definition at line 204 of file UDPServer.h.

◆ ioThreadCurrent

unsigned int ioThreadCurrent
private

Definition at line 206 of file UDPServer.h.

◆ ioThreads

UDPServerIOThread** ioThreads
private

Definition at line 207 of file UDPServer.h.

◆ MESSAGE_ID_NONE

const uint32_t MESSAGE_ID_NONE = 0
staticprivate

Definition at line 136 of file UDPServer.h.

◆ messageCount

uint32_t messageCount
private

Definition at line 211 of file UDPServer.h.

◆ statistics

UDPServer_Statistics statistics
private

Definition at line 213 of file UDPServer.h.

◆ workerThreadPool

ServerWorkerThreadPool* workerThreadPool
private

Definition at line 208 of file UDPServer.h.


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