-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cs
More file actions
319 lines (272 loc) · 9.23 KB
/
Copy pathParser.cs
File metadata and controls
319 lines (272 loc) · 9.23 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
307
308
309
310
311
312
313
314
315
316
317
318
319
using Newtonsoft.Json.Linq;
using Socket.IO.NET35.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
namespace Socket.IO.NET35
{
public class Parser
{
public const int CONNECT = 0;
public const int DISCONNECT = 1;
public const int EVENT = 2;
public const int ACK = 3;
public const int ERROR = 4;
public const int BINARY_EVENT = 5;
public const int BINARY_ACK = 6;
public const int protocol = 4;
/// <summary>
/// Packet types
/// </summary>
public static List<string> types = new List<string>()
{
"CONNECT",
"DISCONNECT",
"EVENT",
"BINARY_EVENT",
"ACK",
"BINARY_ACK",
"ERROR"
};
private Parser() { }
private static Packet ErrorPacket = new Packet(ERROR, "parser error");
public class Encoder
{
public Encoder() { }
public interface ICallback
{
void Call(object[] data);
}
public void Encode(Packet obj, ICallback callback)
{
var log = LogManager.GetLogger(GlobalHelper.CallerName());
log.Info(string.Format("encoding packet {0}", obj));
if (BINARY_EVENT == obj.Type || BINARY_ACK == obj.Type)
{
EncodeAsBinary(obj, callback);
}
else
{
String encoding = EncodeAsString(obj);
callback.Call(new object[] { encoding });
}
}
private string EncodeAsString(Packet obj)
{
var str = new StringBuilder();
bool nsp = false;
str.Append(obj.Type);
if (BINARY_EVENT == obj.Type || BINARY_ACK == obj.Type)
{
str.Append(obj.Attachments);
str.Append("-");
}
if (!string.IsNullOrEmpty(obj.Nsp) && !"/".Equals(obj.Nsp))
{
nsp = true;
str.Append(obj.Nsp);
}
if (obj.Id >= 0)
{
if (nsp)
{
str.Append(",");
nsp = false;
}
str.Append(obj.Id);
}
if (obj.Data != null)
{
if (nsp) str.Append(",");
str.Append(obj.Data);
}
var log = LogManager.GetLogger(GlobalHelper.CallerName());
log.Info(string.Format("encoded {0} as {1}", obj, str));
return str.ToString();
}
private void EncodeAsBinary(Packet obj, ICallback callback)
{
Binary.DeconstructedPacket deconstruction = Binary.DeconstructPacket(obj);
String pack = EncodeAsString(deconstruction.Packet);
var buffers = new List<object>();
foreach (var item in deconstruction.Buffers)
{
buffers.Add(item);
}
buffers.Insert(0, pack);
callback.Call(buffers.ToArray());
}
public class CallbackImp : ICallback
{
private readonly Action<object[]> Fn;
public CallbackImp(Action<object[]> fn)
{
Fn = fn;
}
public void Call(object[] data)
{
Fn(data);
}
}
}
public class Decoder : Emitter
{
public const string EVENT_DECODED = "decoded";
/*package*/
public BinaryReconstructor Reconstructor = null;
public Decoder()
{
}
public void Add(string obj)
{
Packet packet = decodeString(obj);
if (packet.Type == BINARY_EVENT || packet.Type == BINARY_ACK)
{
this.Reconstructor = new BinaryReconstructor(packet);
if (this.Reconstructor.reconPack.Attachments == 0)
{
this.Emit(EVENT_DECODED, packet);
}
}
else
{
this.Emit(EVENT_DECODED, packet);
}
}
public void Add(byte[] obj)
{
if (this.Reconstructor == null)
{
throw new SocketException("got binary data when not reconstructing a packet");
}
else
{
var packet = this.Reconstructor.TakeBinaryData(obj);
if (packet != null)
{
this.Reconstructor = null;
this.Emit(EVENT_DECODED, packet);
}
}
}
private Packet decodeString(string str)
{
Packet p = new Packet();
int i = 0;
p.Type = int.Parse(str.Substring(0, 1));
if (p.Type < 0 || p.Type > types.Count - 1) return ErrorPacket;
if (BINARY_EVENT == p.Type || BINARY_ACK == p.Type)
{
StringBuilder attachments = new StringBuilder();
while (str.Substring(++i, 1) != "-")
{
attachments.Append(str.Substring(i, 1));
}
p.Attachments = int.Parse(attachments.ToString());
}
if (str.Length > i + 1 && "/" == str.Substring(i + 1, 1))
{
var nsp = new StringBuilder();
while (true)
{
++i;
string c = str.Substring(i, 1);
if ("," == c)
{
break;
}
nsp.Append(c);
if (i + 1 == str.Length)
{
break;
}
}
p.Nsp = nsp.ToString();
}
else
{
p.Nsp = "/";
}
var next = (i + 1) >= str.Length ? null : str.Substring(i + 1, 1);
int unused;
if (null != next && int.TryParse(next, out unused))
{
var id = new StringBuilder();
while (true)
{
++i;
var c = str.Substring(i, 1);
if (!int.TryParse(c, out unused))
{
--i;
break;
}
id.Append(c);
if (i + 1 >= str.Length)
{
break;
}
}
p.Id = int.Parse(id.ToString());
}
if (i++ < str.Length)
{
try
{
var t = str.Substring(i);
p.Data = new JValue(t);
}
catch (ArgumentOutOfRangeException)
{
// do nothing
}
catch (Exception)
{
return ErrorPacket;
}
}
var log = LogManager.GetLogger(GlobalHelper.CallerName());
log.Info(string.Format("decoded {0} as {1}", str, p));
return p;
}
public void Destroy()
{
if (Reconstructor != null)
{
Reconstructor.FinishReconstruction();
}
}
}
/*package*/
public class BinaryReconstructor
{
public Packet reconPack;
/*package*/
public List<byte[]> Buffers;
public BinaryReconstructor(Packet packet)
{
this.reconPack = packet;
this.Buffers = new List<byte[]>();
}
public Packet TakeBinaryData(byte[] binData)
{
this.Buffers.Add(binData);
if (this.Buffers.Count == this.reconPack.Attachments)
{
Packet packet = Binary.ReconstructPacket(this.reconPack,
this.Buffers.ToArray());
this.FinishReconstruction();
return packet;
}
return null;
}
public void FinishReconstruction()
{
this.reconPack = null;
this.Buffers = new List<byte[]>();
}
}
}
}