This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SHMessage.cpp
71 lines (53 loc) · 2.33 KB
/
SHMessage.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstring>
#include "SHMessage.h"
#include "loader.h"
using namespace std;
//______ CONSTRUCTORS _______
// Save in memory constructor
SHMessage::SHMessage (string message, int begPos) {
this->operation = 'w';
this->message = message;
this->begPos = begPos;
}
// Read from memory constructor
SHMessage::SHMessage(int start, int length) {
this->operation = 'r';
this->message = "";
this->begPos = start;
this->readLen = length;
}
SHMessage::SHMessage () {}
//============================
string SHMessage::getMessage() {
return this->message;
}
void SHMessage::setMessage(string content) {
this->message = content;
}
// serialize SHMessage object returning byte array
// format: operation (1 byte) + readLen (4 bytes) + begPos (4 bytes) + msgLen (4 bytes) + msg (msgLen bytes)
const char * SHMessage::serialize() {
int msgLen = (int) this->message.size();
char * serial = (char *) malloc( sizeof(char) + (3 * sizeof(int)) + (msgLen * sizeof(char)) );
memcpy( serial, &operation, sizeof( char ) );
memcpy( serial+sizeof(char), &readLen, sizeof( int ) );
memcpy( serial+sizeof(char)+sizeof(int), &begPos, sizeof( int ) );
memcpy( serial+sizeof(char)+2*sizeof(int), &msgLen, sizeof( int ) );
memcpy( serial+sizeof(char)+3*sizeof(int), this->message.c_str(), msgLen * sizeof(char) );
return serial;
}
// deserialize byte array setting this SHMessage object
// format: operation (1 byte) + readLen (4 bytes) + begPos (4 bytes) + msgLen (4 bytes) + msg (msgLen bytes)
void SHMessage::deserialize(const char * serial) {
int msgLen;
memcpy( &(this->operation), serial, sizeof( char ) );
memcpy( &(this->readLen), serial+sizeof(char), sizeof( int ) );
memcpy( &(this->begPos), serial+sizeof(char)+sizeof(int), sizeof( int ) );
memcpy( &msgLen, serial+sizeof(char)+2*sizeof(int), sizeof( int ) );
char *message = (char *) malloc(sizeof(char) * (msgLen+1));
memcpy(message, serial+(3*sizeof(int) + sizeof(char)), (size_t) msgLen * sizeof(char));
message[msgLen] = '\0';
this->message = string(message);
log("\tSHMessage: deserialized message: '" + this->message + "'");
}