TDME2 1.9.121
UDPServer.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#include <map>
6#include <set>
7#include <sstream>
8#include <string>
9
11
12#include <tdme/tdme.h>
22
23using std::stringstream;
24
34
35/**
36 * Base class for network UDP servers
37 * @author Andreas Drewke
38 */
39class tdme::network::udpserver::UDPServer: public Thread, public Server<UDPServerClient, UDPServerGroup> {
40 friend class UDPServerClient;
41 friend class UDPServerIOThread;
43
44public:
46 int64_t time { -1LL };
47 uint32_t received { 0 };
48 uint32_t sent { 0 };
49 uint32_t clients { 0 };
50 uint32_t accepts { 0 };
51 uint32_t errors { 0 };
52 };
53
54 /**
55 * @brief Public constructor
56 * @param name server name
57 * @param host host where to bind the server socket
58 * @param port port to listen on
59 * @param maxCCU max ccu
60 */
61 UDPServer(const std::string& name, const std::string& host, const unsigned int port, const unsigned int maxCCU);
62
63 /**
64 * @brief destructor
65 */
66 virtual ~UDPServer();
67
68 /**
69 * main event loop
70 */
71 virtual void run();
72
73 /**
74 * @returns UDP client statistics
75 */
76 const UDPServer_Statistics getStatistics();
77
78protected:
80
81 /**
82 * @brief method to implement for accepting clients
83 * @param clientId client id
84 * @param ip ip
85 * @param port port
86 * @return server client class
87 */
88 virtual UDPServerClient* accept(const uint32_t clientId, const std::string& ip, const unsigned int port);
89
90 /**
91 * Identifies a client message
92 * @param frame frame
93 * @param messageType message type (see UDPServer::MessageType)
94 * @param connectionId connection id
95 * @param messageId message id
96 * @param retries retries
97 * @throws tdme::network::udpserver::NetworkServerException
98 * @return client or NULL
99 */
100 virtual void identify(stringstream* frame, MessageType& messageType, uint32_t& connectionId, uint32_t& messageId, uint8_t& retries);
101
102 /**
103 * Validates a client message
104 * @param frame frame
105 * @throws tdme::network::udpserver::NetworkServerException
106 */
107 virtual void validate(stringstream* frame);
108
109 /**
110 * Writes a empty header to message
111 * @param frame frame
112 * @throws tdme::network::udpserver::NetworkServerException
113 */
114 static void initializeHeader(stringstream* frame);
115
116 /**
117 * Writes a message header to message
118 * @param frame frame
119 * @param messageType message type
120 * @param clientId client id
121 * @param messageId message id
122 * @param retries retries
123 * @throws tdme::network::udpserver::NetworkServerException
124 */
125 virtual void writeHeader(stringstream* frame, MessageType messageType, const uint32_t clientId, const uint32_t messageId, const uint8_t retries);
126private:
127 static const uint64_t CLIENT_CLEANUP_IDLETIME = 120000L;
128 struct ClientId {
129 uint32_t clientId;
131 uint64_t time;
132 };
133 typedef std::map<uint32_t, ClientId*> ClientIdMap;
134 typedef std::map<string, UDPServerClient*> ClientIpMap;
135 typedef std::set<UDPServerClient*> ClientSet;
136 static const uint32_t MESSAGE_ID_NONE = 0;
137
138 /**
139 * @brief maps a new client to a given client id
140 * @param client client
141 * @throws tdme::network::udpserver::NetworkServerException if id is already in use
142 */
143 void addClient(UDPServerClient* client);
144
145 /**
146 * @brief removes a client
147 * @param client client
148 * @throws tdme::network::udpserver::NetworkServerException if id is not in use
149 */
150 void removeClient(UDPServerClient* client);
151
152 /**
153 * @brief Look ups a client by client id
154 * @param clientId client id
155 * @throws tdme::network::udpserver::NetworkServerException if client does not exist
156 * @return client
157 */
158 UDPServerClient* lookupClient(const uint32_t clientId);
159
160 /**
161 * @brief Returns client by host name and port
162 * @param ip ip
163 * @param port port
164 * @return client
165 */
166 UDPServerClient* getClientByIp(const string& ip, const unsigned int port);
167
168 /**
169 * @brief Clean up clients that have been idle for some time or are flagged to be shut down
170 */
171 void cleanUpClients();
172
173 /**
174 * @brief pushes a message to be send, takes over ownership of frame
175 * @param client client
176 * @param frame frame to be send
177 * @param safe safe, requires ack and retransmission
178 * @param deleteFrame delete frame
179 * @param messageType message type
180 * @param messageId message id (only for MESSAGETYPE_MESSAGE)
181 * @throws tdme::network::udpserver::NetworkServerException
182 */
183 void sendMessage(const UDPServerClient* client, stringstream* frame, const bool safe, const bool deleteFrame, const MessageType messageType, const uint32_t messageId = MESSAGE_ID_NONE);
184
185 /**
186 * @brief Processes an acknowlegdement reception
187 * @param client client
188 * @param messageId message id
189 * @throws tdme::network::udpserver::NetworkServerException
190 */
191 void processAckReceived(UDPServerClient* client, const uint32_t messageId);
192
193 /**
194 * @brief Allocates a client id for a new client
195 * @return client id
196 */
197 const uint32_t allocateClientId();
198
199 //
202
205
206 unsigned int ioThreadCurrent;
209
210 uint32_t clientCount;
211 uint32_t messageCount;
212
214};
215
Base exception class for network server exceptions.
Base class for network server groups.
Definition: ServerGroup.h:34
Simple server worker thread pool class.
Base class for network servers.
Definition: Server.h:26
Base class for network UDP server clients.
Base class for network UDP servers.
Definition: UDPServer.h:39
static void initializeHeader(stringstream *frame)
Writes a empty header to message.
Definition: UDPServer.cpp:229
UDPServer_Statistics statistics
Definition: UDPServer.h:213
virtual void run()
main event loop
Definition: UDPServer.cpp:55
static const uint32_t MESSAGE_ID_NONE
Definition: UDPServer.h:136
std::map< string, UDPServerClient * > ClientIpMap
Definition: UDPServer.h:134
void processAckReceived(UDPServerClient *client, const uint32_t messageId)
Processes an acknowlegdement reception.
Definition: UDPServer.cpp:490
std::map< uint32_t, ClientId * > ClientIdMap
Definition: UDPServer.h:133
virtual ~UDPServer()
destructor
Definition: UDPServer.cpp:52
const UDPServer_Statistics getStatistics()
Definition: UDPServer.cpp:499
void addClient(UDPServerClient *client)
maps a new client to a given client id
Definition: UDPServer.cpp:280
UDPServerClient * lookupClient(const uint32_t clientId)
Look ups a client by client id.
Definition: UDPServer.cpp:389
UDPServer(const std::string &name, const std::string &host, const unsigned int port, const unsigned int maxCCU)
Public constructor.
Definition: UDPServer.cpp:39
virtual void identify(stringstream *frame, MessageType &messageType, uint32_t &connectionId, uint32_t &messageId, uint8_t &retries)
Identifies a client message.
Definition: UDPServer.cpp:167
std::set< UDPServerClient * > ClientSet
Definition: UDPServer.h:135
UDPServerClient * getClientByIp(const string &ip, const unsigned int port)
Returns client by host name and port.
Definition: UDPServer.cpp:423
void cleanUpClients()
Clean up clients that have been idle for some time or are flagged to be shut down.
Definition: UDPServer.cpp:436
static const uint64_t CLIENT_CLEANUP_IDLETIME
Definition: UDPServer.h:127
UDPServerIOThread ** ioThreads
Definition: UDPServer.h:207
virtual void validate(stringstream *frame)
Validates a client message.
Definition: UDPServer.cpp:226
const uint32_t allocateClientId()
Allocates a client id for a new client.
Definition: UDPServer.cpp:495
virtual UDPServerClient * accept(const uint32_t clientId, const std::string &ip, const unsigned int port)
method to implement for accepting clients
Definition: UDPServer.cpp:163
ServerWorkerThreadPool * workerThreadPool
Definition: UDPServer.h:208
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
Definition: UDPServer.cpp:469
void removeClient(UDPServerClient *client)
removes a client
Definition: UDPServer.cpp:341
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.
Definition: UDPServer.cpp:241
Barrier implementation.
Definition: Barrier.h:21
Implementation for read/write lock.
Definition: ReadWriteLock.h:21
Base class for threads.
Definition: Thread.h:26