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