-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_packet_udp.h
229 lines (176 loc) · 6.24 KB
/
chat_packet_udp.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef CHAT_PACKET_UDP_H
#define CHAT_PACKET_UDP_H
#include <QObject>
class ChatPacketUDP
{
public:
enum ChatContentType
{
TEXT = 0, FILE = 1,
};
enum ClientMsgType
{
MSG_CLIENT_ACK = 10, // Reliable UDP
CHAT_CONTENT_CLIENT = 20,
LOGIN_REQUEST = 30, LOGOUT_REQUEST = 40,
CHAT_REQUEST = 100,
};
enum ServerMsgType
{
MSG_SERVER_ACK_TEXT = 11, // Reliable UDP
MSG_SERVER_ACK_FILE = 12,
CHAT_CONTENT_SERVER = 21,
LOGIN_REPLY = 31, LOGOUT_REPLY = 41,
CHAT_REQUEST_REPLY = 101,
};
enum Status
{
SUCCESS = 1,
ERROR_PASSWORD_WRONG = 10,
ERROR_CONFLICT = 11, // 重复操作
ERROR = 99
};
struct HeaderBase
{
quint16 headerSize;
quint16 packetSize;
quint8 msgType;
};
//------------------------CLIENT HEADERS-------------------------//
struct LoginRequestHeader
{
const quint16 headerSize = sizeof(LoginRequestHeader); // in bytes
const quint16 packetSize = sizeof(LoginRequestHeader); // in bytes (= headerSize + payloadSize)
const quint8 msgType = ClientMsgType::LOGIN_REQUEST;
quint16 thisUserID;
quint8 password[28];
};
struct LogoutRequestHeader
{
const quint16 headerSize = sizeof(LogoutRequestHeader); // in bytes
const quint16 packetSize = sizeof(LogoutRequestHeader); // in bytes (= headerSize + payloadSize)
const quint8 msgType = ClientMsgType::LOGOUT_REQUEST;
quint16 thisUserID;
};
struct TextMsgHeader
{
const quint16 headerSize = sizeof(TextMsgHeader); // in bytes
quint16 packetSize; // in bytes (= headerSize + payloadSize)
const quint8 msgType = ClientMsgType::CHAT_CONTENT_CLIENT;
// BLANK BYTE
quint16 fromUserID;
quint16 targetUserID;
const quint8 contentType = ChatContentType::TEXT;
// BLANK BYTE
quint16 textPacketSeq;
};
struct FileMsgHeader
{
const quint16 headerSize = sizeof(FileMsgHeader); // in bytes
quint16 packetSize; // in bytes (= headerSize + payloadSize)
const quint8 msgType = ClientMsgType::CHAT_CONTENT_CLIENT;
// BLANK BYTE
quint16 fromUserID;
quint16 targetUserID;
const quint8 contentType = ChatContentType::FILE;
quint8 fileNameLength;
// 分包信息
quint32 packetCountTotal;
quint32 packetCountCurrent;
// 包后紧跟fineName
// 然后才是payload
};
struct ChatRequestHeader
{
const quint16 headerSize = sizeof(ChatRequestHeader); // in bytes
const quint16 packetSize = sizeof(ChatRequestHeader); // in bytes (= headerSize + payloadSize)
const quint8 msgType = ClientMsgType::CHAT_REQUEST;
quint16 thisUserID;
};
//------------------------SERVER HEADERS-------------------------//
struct TextPacketReplyHeader
{
const quint16 headerSize = sizeof(TextPacketReplyHeader);
const quint16 packetSize = sizeof(TextPacketReplyHeader);
const quint8 msgType = ServerMsgType::MSG_SERVER_ACK_TEXT;
// const quint8 placeHolder = 0; // 字节对齐
// unsigned char md5Hash[16];
quint16 textPacketSeq = 0;
};
struct FilePacketReplyHeader
{
const quint16 headerSize = sizeof(FilePacketReplyHeader);
const quint16 packetSize = sizeof(FilePacketReplyHeader);
const quint8 msgType = ServerMsgType::MSG_SERVER_ACK_FILE;
// const quint8 placeHolder = 0; // 字节对齐
// unsigned char md5Hash[16];
quint32 filePacketSeq = 0;
};
struct LoginReplyHeader
{
const quint16 headerSize = sizeof(LoginReplyHeader);
quint16 packetSize;
const quint8 msgType = ServerMsgType::LOGIN_REPLY;
quint16 loginUserID;
quint8 status;
quint16 friendCount;
};
struct LogoutReplyHeader
{
const quint16 headerSize = sizeof(LogoutReplyHeader);
const quint16 packetSize = sizeof(LogoutReplyHeader);
const quint8 msgType = ServerMsgType::LOGOUT_REPLY;
quint16 logoutUserID;
quint8 status;
};
struct ChatRequestReplyHeader
{
const quint16 headerSize = sizeof(ChatRequestReplyHeader);
quint16 packetSize;
const quint8 msgType = ServerMsgType::CHAT_REQUEST_REPLY;
quint16 thisUserID;
quint16 pendingMsgTotalCount;
};
struct ChatContentServerHeader
{
// 为ChatRequestReplyHeader的次级Header
const quint16 headerSize = sizeof(ChatContentServerHeader);
quint16 currentPacketSize;
const quint8 msgType = ServerMsgType::CHAT_CONTENT_SERVER;
quint8 contentType = ChatContentType::TEXT;
quint16 fromUserID;
quint16 pendingMsgTotalCount;
quint16 currentMsgCount;
};
//------------------------HEADER FUNCTIONS-------------------------//
static bool isTextPacketReplyMsg(QByteArray& bytes)
{
HeaderBase headerBase = *(HeaderBase*)bytes.data();
return (headerBase.msgType == ServerMsgType::MSG_SERVER_ACK_TEXT);
}
static bool isFilePacketReplyMsg(QByteArray& bytes)
{
HeaderBase headerBase = *(HeaderBase*)bytes.data();
return (headerBase.msgType == ServerMsgType::MSG_SERVER_ACK_FILE);
}
static bool isLoginReplyMsg(QByteArray& bytes)
{
HeaderBase headerBase = *(HeaderBase*)bytes.data();
return (headerBase.msgType == ServerMsgType::LOGIN_REPLY &&
headerBase.headerSize == sizeof(LoginReplyHeader));
}
static bool isLogoutReplyMsg(QByteArray& bytes)
{
HeaderBase headerBase = *(HeaderBase*)bytes.data();
return (headerBase.msgType == ServerMsgType::LOGOUT_REPLY &&
headerBase.headerSize == sizeof(LogoutReplyHeader) &&
headerBase.packetSize == sizeof(LogoutReplyHeader));
}
static bool isChatRequestReplyMsg(QByteArray& bytes)
{
HeaderBase headerBase = *(HeaderBase*)bytes.data();
return (headerBase.msgType == ServerMsgType::CHAT_REQUEST_REPLY &&
headerBase.headerSize == sizeof(ChatRequestReplyHeader));
}
};
#endif // CHAT_PACKET_UDP_H