TDME2 1.9.121
UDPClientMessage.cpp
Go to the documentation of this file.
2
3#include <string.h>
4
5#include <sstream>
6#include <string>
7
8#include <tdme/tdme.h>
12#include <tdme/utilities/Time.h>
13
15
16using std::ios_base;
17using std::string;
18using std::stringstream;
19using std::to_string;
20
25
26UDPClientMessage* UDPClientMessage::parse(const char message[512], const size_t bytes) {
27 // decode header
28 MessageType messageType = MessageType::MESSAGETYPE_NONE;
29 switch(message[0]) {
30 case 'A':
31 messageType = MessageType::MESSAGETYPE_ACKNOWLEDGEMENT;
32 break;
33 case 'C':
34 messageType = MessageType::MESSAGETYPE_CONNECT;
35 break;
36 case 'M':
37 messageType = MessageType::MESSAGETYPE_MESSAGE;
38 break;
39 default:
40 Console::println("UDPClientMessage::parse(): invalid message type: '" + (string() + message[0]) + "' (" + to_string(message[0]) + ")");
41 return nullptr;
42 }
43 uint32_t clientId;
44 uint32_t messageId;
45 uint32_t retries;
46 Integer::decode(string(&message[1], 6), clientId);
47 Integer::decode(string(&message[7], 6), messageId);
48 Integer::decode(string(&message[13], 1), retries);
49 // decode data
50 stringstream* frame = nullptr;
51 if (bytes > 14) {
52 frame = new stringstream();
53 frame->write(&message[14], bytes - 14);
54 }
56}
57
58UDPClientMessage::UDPClientMessage(const MessageType messageType, const uint32_t clientId, const uint32_t messageId, const uint8_t retries, stringstream* frame) :
59 messageType(messageType),
60 clientId(clientId),
61 messageId(messageId),
62 retries(retries),
63 frame(frame)
64{
65 time = Time::getCurrentMillis();
66}
67
69 if (frame != nullptr) delete frame;
70}
71
72const uint64_t UDPClientMessage::getTime() {
73 return time;
74}
75
77 return messageType;
78}
79
81 return clientId;
82}
83
85 return messageId;
86}
87
90}
91
93 return retries;
94}
95
97 retries++;
98}
99
101 return frame;
102}
103
104void UDPClientMessage::generate(char message[512], size_t& bytes) {
105 string datagram;
106 switch(messageType) {
108 datagram+= 'A';
109 break;
111 datagram+= 'C';
112 break;
114 datagram+= 'M';
115 break;
116 default:
117 // FIXME
118 break;
119 }
120 string retriesEncoded;
121 string clientIdEncoded;
122 string messageIdEncoded;
123 Integer::encode(retries, retriesEncoded);
124 Integer::encode(clientId, clientIdEncoded);
125 Integer::encode(messageId, messageIdEncoded);
126 datagram+= clientIdEncoded;
127 datagram+= messageIdEncoded;
128 datagram+= retriesEncoded[retriesEncoded.length()];
129 if (frame != nullptr) {
130 datagram+= frame->str();
131 }
132 bytes = datagram.size();
133 memcpy(message, datagram.c_str(), bytes);
134}
void generate(char message[512], size_t &bytes)
Generate datagram.
void retry()
Mark message to be resend with increased retry count.
UDPClientMessage(const MessageType messageType, const uint32_t clientId, const uint32_t messageId, const uint8_t retries, stringstream *frame)
Public constructor.
static uint64_t getRetryTime(const uint8_t retries)
Get retry time for given retry count.
Definition: UDPClient.cpp:533
Console class.
Definition: Console.h:26
Integer class.
Definition: Integer.h:26
Time utility class.
Definition: Time.h:21