-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunication_protocol.cpp
More file actions
78 lines (50 loc) · 2.8 KB
/
communication_protocol.cpp
File metadata and controls
78 lines (50 loc) · 2.8 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
#include "communication_protocol.h"
//-------------------------------------------------------------------------------------
CP::CommunicationProtocol::CommunicationProtocol()
: body_length_(0) {}
//-------------------------------------------------------------------------------------
const char *CP::CommunicationProtocol::Data() const { return data_; }
//-------------------------------------------------------------------------------------
char *CP::CommunicationProtocol::Data() { return data_; }
//-------------------------------------------------------------------------------------
std::size_t CP::CommunicationProtocol::Length() const { return header_length + body_length_; }
//-------------------------------------------------------------------------------------
const char *CP::CommunicationProtocol::Body() const { return data_ + header_length; }
//-------------------------------------------------------------------------------------
char *CP::CommunicationProtocol::Body() { return data_ + header_length; }
//-------------------------------------------------------------------------------------
std::size_t CP::CommunicationProtocol::BodyLength() const { return body_length_; }
//-------------------------------------------------------------------------------------
void CP::CommunicationProtocol::BodyLength(std::size_t new_length) {
body_length_ = new_length;
if (body_length_ > max_body_length)
body_length_ = max_body_length;
}
//-------------------------------------------------------------------------------------
bool CP::CommunicationProtocol::DecodeHeader() {
char header[header_length + 1] = "";
std::strncat(header, data_, header_length);
body_length_ = std::atoi(header);
if (body_length_ > max_body_length) {
body_length_ = 0;
return false;
}
return true;
}
//-------------------------------------------------------------------------------------
void CP::CommunicationProtocol::EncodeHeader() {
char header[header_length + 1] = "";
std::sprintf(header, "%4d", static_cast<int>(body_length_));
std::memcpy(data_, header, header_length);
}
//-------------------------------------------------------------------------------------
std::string CP::CommunicationProtocol::UintToStringConvert(std::size_t str) {
std::stringstream ss;
ss << str;
return ss.str();
}
//-------------------------------------------------------------------------------------
char *CP::CommunicationProtocol::StringToCharConvert(std::string &&str) { return strdup(str.c_str()); }
//-------------------------------------------------------------------------------------
std::string CP::CommunicationProtocol::CharToStringConvert(char *str) { return std::string(str); }
//-------------------------------------------------------------------------------------