-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestMessage.cpp
executable file
·52 lines (44 loc) · 975 Bytes
/
RequestMessage.cpp
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
#include "RequestMessage.h"
RequestMessage::RequestMessage() : body_length_(0)
{
}
char* RequestMessage::data()
{
return data_;
}
size_t RequestMessage::length()
{
return header_length + body_length_;
}
char* RequestMessage::body()
{
return data_ + header_length;
}
size_t RequestMessage::body_length()
{
return body_length_;
}
void RequestMessage::body_length(size_t length)
{
body_length_ = length;
if ( body_length_ > max_body_length )
body_length_ = max_body_length;
}
bool RequestMessage::decode_header()
{
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 RequestMessage::encode_header()
{
char header[header_length + 1] = "";
std::sprintf(header, "%4d", body_length_);
std::memcpy(data_, header, header_length);
}