-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket.h
More file actions
53 lines (42 loc) · 1.63 KB
/
Packet.h
File metadata and controls
53 lines (42 loc) · 1.63 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
#ifndef H_PACKET
#define H_PACKET
#include <string>
//size of defualt packet header
#define INITIALPACKETSIZE (sizeof(char) + sizeof(unsigned int))
/* types
0 something
1 connection closed
2 write file
3 delete file
*/
class TCPStream;
class Engine;
class Packet
{
protected:
char type;
unsigned int size = 50;
TCPStream* stream = nullptr;
char* data;
unsigned int dataIndex = 0;
public:
Packet(TCPStream* stream, char type); //create packet that reads data from a TCPStream
Packet(char* byteArray, char type, unsigned int size); //create packet from a byte array and length
Packet(char type, unsigned int size); //creates a packet with no data to write to
virtual ~Packet();
void writeHeader(); //write type and size header
virtual char* toByteArray(); //convert packet to byte array
virtual void exicute(Engine*); //do whatever the packet does
virtual bool read(); //read the byte array to packet data
//getters
char getType();
char* getDataArray();
unsigned int getPacketSize();
unsigned int getDataSize();
//DOESNT WORK WITH OBJECTS THAT CONTAIN INTERNAL POINTERS, but everything else is fine ignoring significant byte order
void addToByteArray(void* data, long unsigned length); //write data to byte array
void readFromByteArray(void* data, long unsigned length); //read data from byte array
void addToByteArray(std::string& string); //a special set just for strings
void readFromByteArray(std::string& string); //strings are just to usefull
};
#endif