TDME2  1.9.200
UDPServerClient.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 
6 #include <tdme/tdme.h>
13 
14 using std::string;
15 using std::unordered_map;
16 
21 
22 /**
23  * Base class for network UDP server clients
24  * @author Andreas Drewke
25  */
27  friend class UDPServer;
28  friend class UDPServerIOThread;
29 
30 public:
31  // forbid class copy
33 
34  /**
35  * @brief public constructor should be called in any subclass of UDPNetworkServer
36  * @param clientId client id
37  * @param ip ip
38  * @param port port
39  */
40  UDPServerClient(const uint32_t clientId, const std::string& ip, const uint16_t port);
41 
42  /**
43  * @brief Returns server
44  * @return server
45  */
47 
48  /**
49  * @brief Get client id
50  * @return client id
51  */
52  inline const uint32_t getClientId() {
53  return clientId;
54  }
55 
56  /**
57  * @brief returns client's ip
58  * @return client ip
59  */
60  inline const string& getIp() const {
61  return ip;
62  }
63 
64  /**
65  * @brief returns client port
66  * @return client port
67  */
68  inline const uint16_t getPort() const {
69  return port;
70  }
71 
72  /**
73  * @brief Client identification key
74  * @return client key
75  */
76  inline const string& getKey() const {
77  return key;
78  }
79 
80  /**
81  * @brief sets the clients identification key
82  * @param &key client identification key
83  * @return if setting the key was succesful
84  */
85  const bool setKey(const string &key);
86 
87  /**
88  * @brief Creates a packet to be used with send
89  * @return frame to be send
90  */
91  static UDPPacket* createPacket();
92 
93  /**
94  * @brief Sends a frame to client, takes over ownership of frame
95  * @param packet packet to be send
96  * @param safe safe, requires ack and retransmission
97  * @param deleteFrame delete frame
98  */
99  void send(UDPPacket* packet, bool safe = true, bool deleteFrame = true);
100 
101  /**
102  * @brief Checks if message has already been processed and sends an acknowlegdement to client / safe client messages
103  * @param messageId message id
104  */
105  bool processSafeMessage(const uint32_t messageId);
106 
107  /**
108  * @return time passed until a retry was acknowledged
109  */
110  uint64_t getRetryTime(const uint8_t retries);
111 
112  /**
113  * @brief fires an custom event
114  */
115  void fireEvent(const string &type);
116 
117  /**
118  * @brief Shuts down this network client
119  */
120  void shutdown();
121 
122 protected:
123  /**
124  * @brief public destructor, should only be called implicitly by Reference::releaseReference()
125  */
126  virtual ~UDPServerClient();
127 
128  /**
129  * @brief To be overwritten with a request handler, will be called from worker
130  * @param packet packet
131  * @param messageId message id
132  * @param retries retries
133  */
134  virtual void onRequest(const UDPPacket* packet, const uint32_t messageId, const uint8_t retries) = 0;
135 
136  /*
137  * @brief event method called if client will be closed, will be called from worker
138  */
139  virtual void onClose() = 0;
140 
141  /**
142  * @brief Event, which will be called if packet has been received, defaults to worker thread pool
143  * @param packet packet
144  * @param messageId message id (upd server only)
145  * @param retries retries (udp server only)
146  */
147  virtual void onPacketReceived(const UDPPacket* packet, const uint32_t messageId = 0, const uint8_t retries = 0);
148 
149  /**
150  * @brief Shuts down this network client
151  */
152  void close();
153 
154  /**
155  * @brief initiates this network client
156  */
157  void init();
158 
159  //
162  uint32_t clientId;
163  string ip;
164  uint16_t port;
165 
166 private:
167  static const uint64_t MESSAGESSAFE_KEEPTIME = 5000L;
168  struct Message {
169  uint32_t messageId;
170  uint64_t time;
171  uint8_t receptions;
172  };
173  typedef unordered_map<uint32_t, Message*> MessageMapSafe;
174 
175  /**
176  * @brief Sends an connect message to client
177  */
178  void sendConnected();
179 
180  /**
181  * @brief Clean up safe messages
182  */
183  void cleanUpSafeMessages();
184 
185  //
186  volatile bool shutdownRequested;
189 };
Base class for network server clients.
Definition: ServerClient.h:29
Base class for network UDP server clients.
void cleanUpSafeMessages()
Clean up safe messages.
void init()
initiates this network client
void send(UDPPacket *packet, bool safe=true, bool deleteFrame=true)
Sends a frame to client, takes over ownership of frame.
unordered_map< uint32_t, Message * > MessageMapSafe
const string & getKey() const
Client identification key.
const uint16_t getPort() const
returns client port
void fireEvent(const string &type)
fires an custom event
void close()
Shuts down this network client.
uint64_t getRetryTime(const uint8_t retries)
UDPServer * getServer()
Returns server.
const string & getIp() const
returns client's ip
void shutdown()
Shuts down this network client.
virtual void onPacketReceived(const UDPPacket *packet, const uint32_t messageId=0, const uint8_t retries=0)
Event, which will be called if packet has been received, defaults to worker thread pool.
virtual void onRequest(const UDPPacket *packet, const uint32_t messageId, const uint8_t retries)=0
To be overwritten with a request handler, will be called from worker.
const bool setKey(const string &key)
sets the clients identification key
const uint32_t getClientId()
Get client id.
UDPServerClient(const uint32_t clientId, const std::string &ip, const uint16_t port)
public constructor should be called in any subclass of UDPNetworkServer
static UDPPacket * createPacket()
Creates a packet to be used with send.
virtual ~UDPServerClient()
public destructor, should only be called implicitly by Reference::releaseReference()
void sendConnected()
Sends an connect message to client.
bool processSafeMessage(const uint32_t messageId)
Checks if message has already been processed and sends an acknowlegdement to client / safe client mes...
Base class for network UDP servers.
Definition: UDPServer.h:42
Mutex implementation.
Definition: Mutex.h:19
std::exception Exception
Exception base class.
Definition: Exception.h:18
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6