forked from hbdgr/simple_gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataeater.cpp
More file actions
134 lines (109 loc) · 3.32 KB
/
dataeater.cpp
File metadata and controls
134 lines (109 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "dataeater.hpp"
#include <iostream>
#include <cstring>
#include "trivialserialize.hpp"
void dataeater::eat(std::vector<uint8_t> &data) {
for(const auto &i:data) m_internal_buffer.push(i);
}
void dataeater::eat(std::string &data) {
static_assert(sizeof(char) == sizeof(uint8_t), "size of char are different than size of uint8_t");
for(const auto i:data) {
uint8_t que_ele;
memcpy(&que_ele,&i,sizeof(uint8_t));
m_internal_buffer.push(que_ele);
}
}
void dataeater::process() {
if(!m_is_processing) {
processFresh();
} else {
continiueProcessing();
}
}
uint16_t dataeater::pop_msg_size() {
uint16_t msg_size;
msg_size = m_internal_buffer.front() << 8;
m_internal_buffer.pop();
msg_size += m_internal_buffer.front();
m_internal_buffer.pop();
return msg_size;
}
bool dataeater::processFresh() {
// change 4 to 2 because of no 0xff at the begin of packet
if (m_internal_buffer.size() < 2){
return false;
}
// Is it really neccessary?
//if(char (m_internal_buffer.front()) != char(0xff)) { //frame should start with 0xff. If not - something goes wrong
// m_internal_buffer.pop();
// return false;
//}
m_frame_size = pop_msg_size();
std::cout << "qframe size = " << m_frame_size << std::endl;
m_current_index = 0;
continiueProcessing();
return m_is_processing = true;
}
bool dataeater::continiueProcessing() {
while (!m_internal_buffer.empty()) {
if (m_frame_size == m_current_index) {
m_commands_list.push(m_last_command);
m_last_command.clear();
m_is_processing= false;
processFresh();
return true;
}else {
m_last_command.push_back(m_internal_buffer.front()); m_internal_buffer.pop();
}
m_current_index++;
}
return m_is_processing;
}
std::string dataeater::getLastCommand() {
if(m_commands_list.empty()) {
return std::string();
}
return std::string (m_commands_list.back());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<uint8_t> simple_packet_eater::serialize_msg(const std::string &msg) {
assert(msg.size() <= std::numeric_limits<uint16_t>::max() && "Too big message");
uint16_t msg_size = msg.size();
std::vector<uint8_t> packet(msg_size+2); // 2 is bytes for size
packet[0] = msg_size >> 8;
packet[1] = msg_size & 0xFF;
for (int i = 0; i < msg_size; ++i) {
packet.at(i + 2) = msg.at(i);
}
return packet;
}
std::string simple_packet_eater::deserialize_msg(const std::vector<uint8_t> &packet) {
uint16_t msg_size;
msg_size = packet[0] << 8;
msg_size += packet[1];
std::string msg(msg_size, 0);
for (size_t i = 0; i < msg_size; ++i) {
msg.at(i) = packet.at(i + 2);
}
return msg;
}
std::string simple_packet_eater::process_packet(const std::string &pck) {
trivialserialize::parser parser(trivialserialize::parser::tag_caller_must_keep_this_string_valid(),
pck);
uint64_t msg_size = parser.pop_integer_uvarint();
parser.pop_bytes_n(1);
std::cout << "Parsed msg size: " << msg_size << '\n';
return parser.pop_bytes_n(msg_size);
}
void simple_packet_eater::eat_packet(const std::string &pck) {
m_msg_queue.push(simple_packet_eater::process_packet(pck));
}
std::string simple_packet_eater::pop_last_message() {
if(m_msg_queue.empty()) {
return "";
}
std::string msg = m_msg_queue.front();
m_msg_queue.pop();
std::cout << "msg=[" << msg << "] size=[" << msg.size() << "]\n";
return msg;
}