-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacketSerialiser.cpp
More file actions
85 lines (71 loc) · 1.73 KB
/
Copy pathPacketSerialiser.cpp
File metadata and controls
85 lines (71 loc) · 1.73 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
//
// Created by Matt on 13/09/2016.
//
#include "PacketSerialiser.h"
PacketSerialiser::PacketSerialiser(char *data, unsigned int dataSize) {
buffer = data;
bufferSize = dataSize;
byteIndex = 0;
//std::cout << data[0] << std::endl;
}
int PacketSerialiser::writeBool(bool data) {
if (byteIndex + sizeof(bool) < bufferSize) {
buffer[byteIndex] = data;
byteIndex += sizeof(bool);
return 0;
}
return 1;
}
bool PacketSerialiser::readBool() {
bool read = buffer[byteIndex];
byteIndex += sizeof(bool);
return read;
}
int PacketSerialiser::writeInt(int data) {
if (byteIndex + sizeof(int) < bufferSize) {
buffer[byteIndex] = data;
byteIndex += sizeof(int);
return 0;
}
return 1;
}
int PacketSerialiser::readInt() {
int read = buffer[byteIndex];
byteIndex += sizeof(int);
return read;
}
int PacketSerialiser::writeFloat(float data) {
if (byteIndex + sizeof(float) < bufferSize) {
buffer[byteIndex] = data;
byteIndex += sizeof(float);
return 0;
}
return 1;
}
float PacketSerialiser::readFloat() {
float read = buffer[byteIndex];
byteIndex += sizeof(float);
return read;
}
int PacketSerialiser::writeChar(char data) {
if (byteIndex + sizeof(char) < bufferSize) {
buffer[byteIndex] = data;
byteIndex += sizeof(char);
return 0;
}
return 1;
}
char PacketSerialiser::readChar() {
char read = buffer[byteIndex];
byteIndex += sizeof(char);
return read;
}
char *PacketSerialiser::getBuffer() const {
return buffer;
}
unsigned int PacketSerialiser::getBufferSize() const {
return bufferSize;
}
unsigned int PacketSerialiser::getLength() {
return byteIndex;
}