TDME2 1.9.121
UDPClient.h
Go to the documentation of this file.
1#pragma once
2
3#if defined(_WIN32) && defined(_MSC_VER)
4 #define ssize_t int
5#endif
6
7#include <stdint.h>
8
9#include <map>
10#include <queue>
11
12#include <tdme/tdme.h>
16
21
22using std::map;
23using std::queue;
24using std::string;
25
30
33
34/**
35 * UDP client
36 * @author Andreas Drewke
37 */
39public:
41 int64_t time { -1LL };
42 uint32_t received { 0 };
43 uint32_t sent { 0 };
44 uint32_t errors { 0 };
45 };
46
47 /**
48 * Get retry time for given retry count
49 * @param retries retry count
50 * @return approximatly retry time in ms
51 */
52 static uint64_t getRetryTime(const uint8_t retries);
53
54 /**
55 * Public constructor
56 * @param ip server ip
57 * @param port server port
58 */
59 UDPClient(const string& ip, const unsigned int port);
60
61 /**
62 * @return initialized
63 */
64 bool isInitialized();
65
66 /**
67 * @return initialized
68 */
69 bool isConnected();
70
71 /**
72 * @return server ip
73 */
74 const string& getIp();
75
76 /**
77 * @return server port
78 */
79 const unsigned int getPort();
80
81 /**
82 * @return client key
83 */
84 const string& getClientKey();
85
86 /**
87 * Set client key
88 * @param clientKey client key
89 */
90 void setClientKey(const string& clientKey);
91
92 /**
93 * Run thread program
94 */
95 virtual void run();
96
97 /**
98 * Returns if a message should be processed or already have been processed
99 * @param clientMessage client message
100 * @return if message should be processed or not
101 * @throws tdme::network::udpclient::NetworkClientException
102 */
103 bool processSafeMessage(UDPClientMessage* clientMessage);
104
105 /**
106 * Receive message
107 * @return message or nullptr, needs to be deleted after processing
108 */
110
111 /**
112 * Pushes a message to be send, takes over ownership of message
113 * @param clientMessage client message object
114 * @param safe safe
115 * @throws tdme::network::udpclient::NetworkClientException
116 */
117 void sendMessage(UDPClientMessage* clientMessage, bool safe);
118
119 /**
120 * Create message
121 * @param frame frame content
122 */
123 UDPClientMessage* createMessage(stringstream* frame);
124
125 /**
126 * @returns UDP client statistics
127 */
128 const UDPClient_Statistics getStatistics();
129
130private:
131 /**
132 * Processes ack reveived
133 * @param messageId message id
134 * @throws tdme::network::udpclient::NetworkClientException
135 */
136 void processAckReceived(const uint32_t messageId);
137
138 /**
139 * Process ack messages
140 */
141 void processAckMessages();
142
143 /**
144 * Clean up safe messages
145 */
146 void cleanUpSafeMessages();
147
148 //
151 string ip;
152 uint32_t port;
153 uint32_t clientId;
154 uint32_t messageCount;
155 string clientKey;
156
157 const static int MESSAGEACK_RESENDTIMES_TRIES = 7;
159 const static int MESSAGEQUEUE_SEND_BATCH_SIZE = 100;
160 const static uint64_t MESSAGEACK_KEEPTIME = 5000L;
161 struct Message {
162 uint64_t time;
163 uint8_t messageType;
164 uint32_t messageId;
165 uint8_t retries;
166 char message[512];
167 size_t bytes;
168 };
169 typedef queue<Message*> MessageQueue;
170 typedef map<uint32_t, Message*> MessageMapAck;
171 typedef queue<UDPClientMessage*> RecvMessageQueue;
172
173 static const uint64_t MESSAGESSAFE_KEEPTIME = 5000L;
174 struct SafeMessage {
175 uint32_t messageId;
176 uint64_t time;
177 uint8_t receptions;
178 };
179 typedef map<uint32_t, SafeMessage*> MessageMapSafe;
180
181
183
186
189
192
195
197
199};
200
Base exception class for network client exceptions.
void cleanUpSafeMessages()
Clean up safe messages.
Definition: UDPClient.cpp:512
static const int MESSAGEACK_RESENDTIMES_TRIES
Definition: UDPClient.h:157
virtual void run()
Run thread program.
Definition: UDPClient.cpp:87
void processAckReceived(const uint32_t messageId)
Processes ack reveived.
Definition: UDPClient.cpp:375
queue< UDPClientMessage * > RecvMessageQueue
Definition: UDPClient.h:171
map< uint32_t, Message * > MessageMapAck
Definition: UDPClient.h:170
static const uint64_t MESSAGEACK_KEEPTIME
Definition: UDPClient.h:160
UDPClient(const string &ip, const unsigned int port)
Public constructor.
Definition: UDPClient.cpp:48
void processAckMessages()
Process ack messages.
Definition: UDPClient.cpp:400
queue< Message * > MessageQueue
Definition: UDPClient.h:169
void setClientKey(const string &clientKey)
Set client key.
Definition: UDPClient.cpp:83
static const uint64_t MESSAGESSAFE_KEEPTIME
Definition: UDPClient.h:173
const UDPClient_Statistics getStatistics()
Definition: UDPClient.cpp:560
static uint64_t getRetryTime(const uint8_t retries)
Get retry time for given retry count.
Definition: UDPClient.cpp:533
UDPClientMessage * createMessage(stringstream *frame)
Create message.
Definition: UDPClient.cpp:550
map< uint32_t, SafeMessage * > MessageMapSafe
Definition: UDPClient.h:179
static const int MESSAGEQUEUE_SEND_BATCH_SIZE
Definition: UDPClient.h:159
void sendMessage(UDPClientMessage *clientMessage, bool safe)
Pushes a message to be send, takes over ownership of message.
Definition: UDPClient.cpp:314
const unsigned int getPort()
Definition: UDPClient.cpp:75
static const uint64_t MESSAGEACK_RESENDTIMES[MESSAGEACK_RESENDTIMES_TRIES]
Definition: UDPClient.h:158
bool processSafeMessage(UDPClientMessage *clientMessage)
Returns if a message should be processed or already have been processed.
Definition: UDPClient.cpp:467
UDPClientMessage * receiveMessage()
Receive message.
Definition: UDPClient.cpp:539
UDPClient_Statistics statistics
Definition: UDPClient.h:198
Interface to kernel event mechanismns.
Class representing a UDP socket.
Definition: UDPSocket.h:27
Mutex implementation.
Definition: Mutex.h:27
Base class for threads.
Definition: Thread.h:26