-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket.cpp
More file actions
137 lines (116 loc) · 3.01 KB
/
Packet.cpp
File metadata and controls
137 lines (116 loc) · 3.01 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
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <cstring>
#include <iostream>
#include "Packet.h"
#include "TCPStream.h"
#include "Engine.h"
#define maxPacketSize 10000
Packet::Packet(TCPStream* stream, char type)
{
this->type = type;
this->stream = stream;
this->dataIndex = INITIALPACKETSIZE; //the header is ignored
}
Packet::Packet(char* byteArray, char type, unsigned int size)
{
this->type = type;
this->size = size;
this->dataIndex = INITIALPACKETSIZE;
data = new char[size];
memcpy(data, byteArray, size);
}
Packet::Packet(char type, unsigned int _size)
{
this->type = type;
this->size = _size < INITIALPACKETSIZE ? INITIALPACKETSIZE : _size; //must atleast be large enought to hold the header
this->dataIndex = INITIALPACKETSIZE;
data = new char[size];
}
Packet::~Packet()
{
delete[] data;
}
void Packet::writeHeader()
{
dataIndex = dataIndex < INITIALPACKETSIZE ? INITIALPACKETSIZE : dataIndex;
data[0] = type;
char* Cdata = reinterpret_cast<char* >(&dataIndex);
memcpy(data + 1, Cdata, sizeof(dataIndex));
}
char* Packet::toByteArray()
{
writeHeader();
return data;
}
bool Packet::read()
{
if (!stream)
{
return false;
}
//read data size from stream
char* streamData[sizeof(unsigned int)];
stream->read(streamData, sizeof(unsigned int));
//convert to unsigned int
char* Cdata = reinterpret_cast<char* >(&size);
memcpy(Cdata, streamData, sizeof(unsigned int));
//read the rest of the data but dont interprete it
unsigned int readDataLen = INITIALPACKETSIZE;
data = new char[size];
while (readDataLen < size)
{
int curRead = stream->read(data + readDataLen, size - readDataLen);
readDataLen += curRead > 0 ? curRead : 0;
}
return true;
}
void Packet::exicute(Engine* engine)
{
}
//getters
char Packet::getType()
{
return type;
}
char* Packet::getDataArray()
{
return data;
}
unsigned int Packet::getPacketSize()
{
return size;
}
unsigned int Packet::getDataSize()
{
return dataIndex;
}
void Packet::addToByteArray(void* Tdata, long unsigned length)
{
//convert data type char pointer and coppy bytes
char* Cdata = reinterpret_cast<char* >(Tdata);
memcpy(data + dataIndex, Cdata, length);
dataIndex += length;
}
void Packet::readFromByteArray(void* Tdata, long unsigned length)
{
//convert data type to char pointer and then copy data from array to pointer
char* Cdata = reinterpret_cast<char* >(Tdata);
memcpy(Cdata, data + dataIndex, length);
dataIndex += length;
}
void Packet::addToByteArray(std::string& string)
{
unsigned int length = string.size();
addToByteArray(&length, sizeof(length));
char* cstring = &string[0];
addToByteArray(cstring, length);
}
void Packet::readFromByteArray(std::string& string)
{
unsigned int length;
readFromByteArray(&length, sizeof(length));
char* cstring = new char[length+1];
readFromByteArray(cstring, length);
cstring[length] = '\0';
string = std::string(cstring);
delete[] cstring;
}