-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.hpp
More file actions
75 lines (62 loc) · 1.89 KB
/
message.hpp
File metadata and controls
75 lines (62 loc) · 1.89 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
#ifndef MESSAGE_HPP
#define MESSAGE_HPP
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
class Message {
public:
Message() : bodyLength_(0) {}
enum {maxBytes = 512};
enum {header = 4};
Message(std::string message){
bodyLength_ = getNewBodyLength(message.size());
encodeHeader();
std::memcpy(data + header, message.c_str(), bodyLength_);
};
void printMessage(){
std::string message = getData();
std::cout<<"Message recieved: "<<message<<std::endl;
}
std::string getData(){
int length = header + bodyLength_;
std::string result(data, length);
return result;
}
std::string getBody(){
std::string dataStr = getData();
std::string result = dataStr.substr(header, bodyLength_);
return result;
}
size_t getNewBodyLength(size_t newLength){
if(newLength > maxBytes){
return maxBytes;
}
return newLength;
}
void encodeHeader(){
char new_header[header+1] = "";
sprintf(new_header, "%4d", static_cast<int>(bodyLength_));
memcpy(data, new_header, header);
}
bool decodeHeader(){
char new_header[header+1] = "";
strncpy(new_header, data, header);
new_header[header] = '\0';
int headerValue = atoi(new_header);
if(headerValue > maxBytes){
bodyLength_ = 0;
return false;
}
bodyLength_ = headerValue;
return true;
}
size_t getBodyLength(){
return bodyLength_;
}
private:
char data[header+maxBytes];
size_t bodyLength_;
};
#endif MESSAGE_HPP