-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdataeater.cpp
More file actions
89 lines (73 loc) · 2.12 KB
/
dataeater.cpp
File metadata and controls
89 lines (73 loc) · 2.12 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
#include "dataeater.hpp"
#include <iostream>
#include <cstring>
#include "trivialserialize.hpp"
void dataeater::eat(const std::vector<uint8_t> &data) {
for(const auto &i:data) m_internal_buffer.push(i);
}
void dataeater::eat(const 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) {
eat(i);
}
}
void dataeater::eat(const char &ch) {
static_assert(sizeof(char) == sizeof(uint8_t), "size of char are different than size of uint8_t");
uint8_t que_ele;
memcpy(&que_ele,&ch,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 = static_cast<uint16_t>(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 (true) {
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 {
if(m_internal_buffer.empty()) break;
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());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////