-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.h
69 lines (57 loc) · 1.77 KB
/
packet.h
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
#ifndef PACKET_H
#define PACKET_H
#include <QTypeInfo>
#include <QTextStream>
#include <QTcpSocket>
class Packet
{
public:
typedef enum{
NOP = 0,
PUBLIC = 1,
CHANGE_NICK = 2,
PRIVATE = 3,
ACK_CHANGE_NICK = 4,
MESSAGE = 5,
JOIN = 6,
PING = 7,
PONG = 8
}PacketType_t;
struct message_header_t {
quint32 length;
quint32 type; /* message_type_e */
quint16 body_checksum;
quint16 head_checksum;
} __attribute__((packed)); /* do not use padding for alignment */
static const quint32 HEADER_LENGTH = sizeof(message_header_t);
protected:
message_header_t header_;
char* payload_; //message content
public:
Packet(PacketType_t type = NOP);
~Packet();
void build();
friend QDataStream& operator<<(QDataStream& stream, Packet& pack);
friend QTcpSocket& operator<<(QTcpSocket& sock, Packet& pack);
friend QDataStream& operator>>(QDataStream& stream, Packet& pack);
quint32 getLength();
quint32 getType();
virtual int payloadSize() = 0;
virtual const char* getPayload() = 0;
void getHeader(QDataStream& stream);
void setHeader(QDataStream& stream);
void setHeader(message_header_t h);
bool checkHeader();
/* source : http://en.wikipedia.org/wiki/Fletcher%27s_checksum */
inline quint16 fletcher16(quint8* data, int count) {
qint16 sum1 = 0;
qint16 sum2 = 0;
int index;
for (index = 0; index < count; ++index) {
sum1 = (sum1 + data[index]) % 255;
sum2 = (sum2 + sum1) % 255;
}
return (sum2 << 8) | sum1;
}
};
#endif // PACKET_H