-
Notifications
You must be signed in to change notification settings - Fork 5
/
message.hpp
57 lines (55 loc) · 1.71 KB
/
message.hpp
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
#ifndef message_HPP
#define message_HPP
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
class message {
public:
enum { headerLength = 4 };
enum { maxBodyLength = 512 };
message() : bodyLength_(0) {
}
const char* data() const {
return data_;
}
char* data() {
return data_;
}
size_t length() const {
return headerLength + bodyLength_;
}
const char* body() const {
return data_ + headerLength;
}
char* body() {
return data_ + headerLength;
}
size_t bodyLength() const {
return bodyLength_;
}
void bodyLength(size_t new_length) {
bodyLength_ = new_length;
if(bodyLength_ > maxBodyLength)
bodyLength_ = maxBodyLength;
}
bool decodeHeader() {
char header[headerLength + 1] = "";
strncat(header, data_, headerLength);
bodyLength_ = atoi(header);
if(bodyLength_ > maxBodyLength) {
bodyLength_ = 0;
return false;
}
return true;
}
void encodeHeader() {
char header[headerLength + 1] = "";
sprintf(header, "%4d", static_cast<int>(bodyLength_));
memcpy(data_, header, headerLength);
}
private:
char data_[headerLength + maxBodyLength];
size_t bodyLength_;
};
#endif