-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.h
More file actions
125 lines (102 loc) · 3.14 KB
/
Message.h
File metadata and controls
125 lines (102 loc) · 3.14 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once
#include <vector>
#include <cstdint>
#include <string>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
using MessageID = uint32_t;
struct MessageHeader
{
MessageID id;
std::size_t size;
};
struct MessagePacket
{
MessageHeader header;
std::vector<uint8_t> body;
public:
template <typename T, typename ...Args>
void AddData(T const& data, Args const& ...args)
{
addData(data);
addData(args...);
}
template <typename T>
void AddData(T const& data)
{
static_assert(std::is_standard_layout<T>::value, "Data is too complex to be pushed into vector");
std::size_t bodySize = body.size();
body.resize(body.size() + sizeof(T));
// add at the end of body
std::memcpy(body.data() + bodySize, &data, sizeof(T));
// update size in header
header.size = body.size();
}
void AddBuffer(const void* buffer, std::size_t bufSize)
{
std::size_t bodySize = body.size();
body.resize(body.size() + bufSize);
std::memcpy(body.data() + bodySize, buffer, bufSize);
header.size = body.size();
// store size so that the receiver can know the buffer size
AddData(bufSize);
}
template <typename T, typename ...Args>
void RetrieveData(T& data, Args& ...args)
{
retrieveData(data);
retrieveData(args...);
}
template <typename T>
void RetrieveData(T& data)
{
static_assert(std::is_standard_layout<T>::value, "Data is too complex to be read from vector");
if (body.size() < sizeof(T)) {
std::cerr << "-- buffer size is lower than the size that is being retrieved --\n";
return;
}
std::size_t dataLocation = body.size() - sizeof(T);
std::memcpy(&data, body.data() + dataLocation, sizeof(T));
body.resize(dataLocation);
header.size = body.size();
}
void RetrieveBuffer(void* buffer, std::size_t bufSize)
{
std::size_t currBodySize = body.size();
if (currBodySize <= 0 || bufSize <= 0 || bufSize > currBodySize)
{
std::cerr << "-- incorrect buffer size when retrieving buffer --\n";
return;
}
std::size_t bufferLocation = body.size() - (bufSize);
std::memcpy(buffer, body.data() + bufferLocation, bufSize);
body.resize(bufferLocation);
header.size = body.size();
}
// wont manage buffer memory, should free where used
uint8_t* GetByteBuffer() const
{
std::size_t packetSize = sizeof(MessageHeader) + header.size;
uint8_t* buffer = (uint8_t*)malloc(packetSize);
memcpy(buffer, (void*)&header, sizeof(MessageHeader));
memcpy(buffer + sizeof(MessageHeader), body.data(), header.size);
return buffer;
}
std::size_t GetBufferSize() const
{
return sizeof(MessageHeader) + body.size();
}
void Clear()
{
body.clear();
body.resize(0);
header.size = 0;
}
};
struct Message
{
virtual bool Serialize(MessagePacket& msg) = 0;
virtual bool Deserialize(MessagePacket& msg) = 0;
};