-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPacket.cs
More file actions
306 lines (257 loc) · 11 KB
/
Packet.cs
File metadata and controls
306 lines (257 loc) · 11 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#if USE_SOCKETIO && UNITY_EDITOR
namespace Dpoch.SocketIO {
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using UnityEngine;
public class Packet {
public enum EngineIOPacketType{
UNDEFINED = -1,
OPEN = 0,
CLOSE = 1,
PING = 2,
PONG = 3,
MESSAGE = 4,
UPGRADE = 5,
NOOP = 6
}
public enum SocketIOPacketType {
UNDEFINED = -1,
CONNECT = 0,
DISCONNECT = 1,
EVENT = 2,
ACK = 3,
ERROR = 4,
BINARY_EVENT = 5,
BINARY_ACK = 6
}
struct BinaryPlaceholder {
public bool _placeholder;
public int num;
}
int numAttachments;
public EngineIOPacketType EnginePacketType { get; private set; }
public SocketIOPacketType SocketPacketType { get; private set; }
public int ID { get; private set; }
public string Nsp { get; private set; }
public List<byte[]> Attachments { get; private set; }
public bool IsComplete { get{ return Attachments.Count >= numAttachments; } }
JToken _data = new JArray();
public JToken Data {
get {
if (!IsComplete) return null;
JToken dataCopy = _data.DeepClone();
if (numAttachments == 0) return dataCopy;
ReplaceData(dataCopy, (token) => {
if (token.Type == JTokenType.Object) {
var obj = (JObject)token;
var placeholder = obj["_placeholder"];
if (placeholder != null && placeholder.Type == JTokenType.Boolean && (bool)placeholder) {
var num = obj["num"];
if (num != null && num.Type == JTokenType.Integer && (int)num < Attachments.Count) {
return Attachments[(int)num].Skip(1).ToArray();
}
}
}
return null;
});
return dataCopy;
}
}
//private constructor - use factory methods instead
Packet() {
EnginePacketType = EngineIOPacketType.UNDEFINED;
SocketPacketType = SocketIOPacketType.UNDEFINED;
ID = -1;
Nsp = "/";
Attachments = new List<byte[]>();
}
public static Packet Ping() {
return new Packet() {
EnginePacketType = EngineIOPacketType.PING
};
}
public static Packet Pong() {
return new Packet() {
EnginePacketType = EngineIOPacketType.PONG
};
}
public static Packet Event(string ev, object[] data, int id = -1) {
JArray jData = new JArray(data);
jData.Insert(0, ev);
var packet = new Packet() {
EnginePacketType = EngineIOPacketType.MESSAGE,
SocketPacketType = SocketIOPacketType.EVENT,
_data = jData,
ID = id
};
packet.ExtractBinaryData();
if(packet.numAttachments > 0) {
packet.SocketPacketType = SocketIOPacketType.BINARY_EVENT;
}
return packet;
}
public static Packet Ack(int id, object[] data) {
JArray jData = new JArray(data);
var packet = new Packet() {
EnginePacketType = EngineIOPacketType.MESSAGE,
SocketPacketType = SocketIOPacketType.ACK,
_data = jData,
ID = id
};
packet.ExtractBinaryData();
if(packet.numAttachments > 0) {
packet.SocketPacketType = SocketIOPacketType.BINARY_ACK;
}
return packet;
}
public static byte[] BinaryPacket(byte[] data) {
byte[] binaryPacket = new byte[data.Length + 1];
data.CopyTo(binaryPacket, 1);
binaryPacket[0] = (byte)EngineIOPacketType.MESSAGE;
//Debug.Log("Packet.cs: BinaryPacket: data length: " + data.Length + " data: " + data[0] + " " + data[1] + " " + data[2] + " " + data[3] + " binaryPacket length: " + binaryPacket.Length + " binaryPacket: " + binaryPacket[0] + " " + binaryPacket[1] + " " + binaryPacket[2] + " " + binaryPacket[3]);
return binaryPacket;
}
void ExtractBinaryData() {
//Debug.Log("Packet.cs: ExtractBinaryData: _data: " + _data);
ReplaceData(_data, (token) => {
if (token.Type == JTokenType.Bytes) {
var placeholder = new BinaryPlaceholder() {
_placeholder = true,
num = Attachments.Count
};
Attachments.Add(token.ToObject<byte[]>());
return JObject.FromObject(placeholder);
}
return null;
});
numAttachments = Attachments.Count;
}
static void ReplaceData(JToken node, Func<JToken, JToken> getReplacement) {
if (node.Type == JTokenType.Object) {
var obj = (JObject)node;
foreach (var prop in obj.Properties()) {
var replacement = getReplacement(obj[prop.Name]);
if (replacement != null) {
obj[prop.Name] = replacement;
} else {
ReplaceData(obj[prop.Name], getReplacement);
}
}
} else if (node.Type == JTokenType.Array) {
var arr = (JArray)node;
for (int i = 0; i < arr.Count; i++) {
var replacement = getReplacement(arr[i]);
if (replacement != null) {
arr[i] = replacement;
} else {
ReplaceData(arr[i], getReplacement);
}
}
}
}
public string Encode() {
var packetStringBuilder = new StringBuilder();
//engineIO packet type
packetStringBuilder.Append((int)EnginePacketType);
//if it isn't a message packet we're done
if (EnginePacketType != EngineIOPacketType.MESSAGE) return packetStringBuilder.ToString();
//socketIO packet type
packetStringBuilder.Append((int)SocketPacketType);
//if type is binary append the number of attachments followed by a dash (-)
if(
SocketPacketType == SocketIOPacketType.BINARY_EVENT ||
SocketPacketType == SocketIOPacketType.BINARY_ACK
) {
packetStringBuilder.Append(numAttachments);
packetStringBuilder.Append("-");
}
//if the namespace isn't default ("/") or empty append it followed by a comma (,)
if (Nsp != "" && Nsp != "/") {
packetStringBuilder.Append(Nsp);
packetStringBuilder.Append(",");
}
//if the packet has an id (>= 0) append it
if(ID >= 0) {
packetStringBuilder.Append(ID);
}
//if the packet contains data append it
if(_data.Count() > 0) {
packetStringBuilder.Append(_data.ToString());
}
return packetStringBuilder.ToString();
}
public static Packet Parse(string packetData) {
try {
int parserIndex = 1;
var packet = new Packet();
//parse packet types
packet.EnginePacketType = (EngineIOPacketType)int.Parse(packetData[0].ToString());
if (packet.EnginePacketType == EngineIOPacketType.MESSAGE) {
packet.SocketPacketType = (SocketIOPacketType)int.Parse(packetData[1].ToString());
parserIndex++;
}
//done?
if (parserIndex >= packetData.Length) return packet;
//Debug.Log("Packet.cs: Parse: packet.SocketPacketType: " + packet.SocketPacketType + " parserIndex: " + parserIndex + " packetData.Length: " + packetData.Length + " left: " + (packetData.Length - parserIndex));
//parse numAttachments if type binary
if (
packet.SocketPacketType == SocketIOPacketType.BINARY_EVENT ||
packet.SocketPacketType == SocketIOPacketType.BINARY_ACK
) {
var numAttachmentsBuilder = new StringBuilder();
while (
parserIndex < packetData.Length &&
packetData[parserIndex] != '-'
) {
numAttachmentsBuilder.Append(packetData[parserIndex]);
parserIndex++;
}
packet.numAttachments = int.Parse(numAttachmentsBuilder.ToString());
parserIndex++;
//Debug.Log("Packet.cs: Parse: numAttachments: " + packet.numAttachments + " parserIndex: " + parserIndex + " packetData.Length: " + packetData.Length + " left: " + (packetData.Length - parserIndex));
}
//done?
if (parserIndex >= packetData.Length) return packet;
//parse namespace
if (packetData[parserIndex] == '/') {
var nspBuilder = new StringBuilder();
while (
parserIndex < packetData.Length &&
packetData[parserIndex] != ','
) {
nspBuilder.Append(packetData[parserIndex]);
parserIndex++;
}
packet.Nsp = nspBuilder.ToString();
parserIndex++;
}
//done?
if (parserIndex >= packetData.Length) return packet;
//parse id
if (char.IsNumber(packetData[parserIndex])) {
var idBuilder = new StringBuilder();
while (
parserIndex < packetData.Length &&
char.IsNumber(packetData[parserIndex])
) {
idBuilder.Append(packetData[parserIndex]);
parserIndex++;
}
packet.ID = int.Parse(idBuilder.ToString());
}
//done?
if (parserIndex >= packetData.Length) return packet;
//parse message data
packet._data = (JToken)JsonConvert.DeserializeObject(packetData.Substring(parserIndex));
return packet;
}catch(Exception e) {
throw new SocketIOException("Failed to parse packet: " + packetData, e);
}
}
}
}
#endif