-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessage.cpp
More file actions
46 lines (40 loc) · 987 Bytes
/
Message.cpp
File metadata and controls
46 lines (40 loc) · 987 Bytes
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
#include "Message.h"
Message* Message::Allocate(int id, int len)
{
int total_len = sizeof(Message)+len;
Message* msg = (Message*)malloc(total_len);
memset(msg, 0, total_len);
msg->header.magic[0] = 'f';
msg->header.magic[1] = 'm';
msg->header.msgid = id;
msg->header.length = len;
return msg;
}
Message* Message::Allocate(int id, int len, const void* body)
{
Message* msg = Allocate(id, len);
memcpy(&(msg->body[0]), body, len);
return msg;
}
Message* Message::Allocate(int id, const char* body)
{
int len = int(strlen(body));
Message* msg = Allocate(id, len);
strcpy((char*)&(msg->body[0]), body);
return msg;
}
Message* Message::Allocate(int id, const std::string& body)
{
int len = int(body.length());
Message* msg = Allocate(id, len);
strcpy((char*)&(msg->body[0]), body.c_str());
return msg;
}
void Message::Free(Message* msg)
{
free((void*)msg);
}
bool Message::Verify(Message* msg)
{
return msg->header.magic[0] == 'f' && msg->header.magic[1] == 'm';
}