-
Notifications
You must be signed in to change notification settings - Fork 15
/
Network.cs
1249 lines (1168 loc) · 27.5 KB
/
Network.cs
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityObject = UnityEngine.Object;
namespace TinyNet
{
#region 发送和接收缓冲区
/// <summary>
/// 填充数据包
/// </summary>
public struct BufferSegment
{
public byte[] Array;
public int Length;
public int Offset;
public IntPtr IntPtr;
public byte Byte;
public static BufferSegment Make(byte[] array)
{
return Make(array, 0, array.Length);
}
public static BufferSegment Make(byte[] array, int length)
{
return Make(array, 0, length);
}
public static BufferSegment Make(byte[] array, int offset, int length)
{
BufferSegment seg = new BufferSegment
{
Array = array,
Length = length,
Offset = offset,
};
return seg;
}
public static BufferSegment Make(IntPtr intptr, int length)
{
BufferSegment seg = new BufferSegment
{
Array = null,
IntPtr = intptr,
Length = length,
};
return seg;
}
public static BufferSegment Make(byte onebyte)
{
BufferSegment seg = new BufferSegment
{
Array = null,
IntPtr = IntPtr.Zero,
Byte = onebyte,
};
return seg;
}
}
/// <summary>
/// 复用缓冲区
/// </summary>
public class ByteBuffer
{
private byte[] _array;
public byte[] array
{
get { return _array; }
}
private int _length;
public int length
{
get { return _length; }
}
private int _offset;
public int offset
{
get { return _offset; }
}
public ByteBuffer()
{
_array = new byte[1024];
_length = 0;
_offset = 0;
}
private static readonly Stack<ByteBuffer> freelist = new Stack<ByteBuffer>();
public static ByteBuffer New()
{
if (freelist.Count > 0)
{
lock (freelist)
{
if (freelist.Count > 0)
return freelist.Pop();
}
}
return new ByteBuffer();
}
public void Release()
{
Reset();
lock (freelist)
{
freelist.Push(this);
}
}
public void Write(BufferSegment seg)
{
if (seg.Array != null)
Write(seg.Array, seg.Offset, seg.Length);
else if (seg.IntPtr != IntPtr.Zero)
Write(seg.IntPtr, seg.Length);
else
Write(seg.Byte);
}
private void checksize(int length)
{
if (_offset + _length + length > _array.Length)
{
int need = _length + length;
if (need <= _array.Length && _offset >= (_array.Length + 1) >> 1)
{
Array.Copy(_array, _offset, _array, 0, _length);
_offset = 0;
}
else
{
int size = _array.Length << 1;
while (size < need)
{
size = size << 1;
}
byte[] array = new byte[size];
Array.Copy(_array, _offset, array, 0, _length);
_offset = 0;
_array = array;
}
}
}
public void Write(byte[] bytes, int offset, int length)
{
checksize(length);
Array.Copy(bytes, offset, _array, _offset + _length, length);
_length += length;
}
public void Write(IntPtr bytes, int length)
{
checksize(length);
Marshal.Copy(bytes, _array, _offset + _length, length);
_length += length;
}
public void Write(byte onebyte)
{
checksize(1);
_array[_offset + _length] = onebyte;
_length += 1;
}
public void Pop(int length)
{
if (length > _length)
length = _length;
_offset += length;
_length -= length;
if (_length == 0)
{
Reset();
}
}
public void Reset()
{
_offset = 0;
_length = 0;
}
}
#endregion
#region 解析网络请求
/// <summary>
/// 基础请求类
/// </summary>
public abstract class Request
{
private readonly ByteBuffer buffer;
protected Request()
{
buffer = ByteBuffer.New();
}
~Request()
{
buffer.Release();
}
internal bool Input(byte[] bytes, int offset, ref int length)
{
int len = length;
while (len > 0)
{
int l = PreTest(buffer, bytes, offset, len);
offset += l;
len -= l;
if (Test(buffer))
{
length -= len;
return true;
}
}
return false;
}
protected abstract int PreTest(ByteBuffer buffer, byte[] bytes, int offset, int length);
protected abstract bool Test(ByteBuffer buffer);
internal abstract void Execute(NetHandler handler);
public abstract bool Send(NetHandler handler);
public virtual void Reset()
{
buffer.Reset();
}
}
/// <summary>
/// 无ID分组
/// </summary>
public abstract class Request<T> : Request
{
public T Value;
internal override void Execute(NetHandler handler)
{
Handle(handler, Value);
}
protected virtual void Handle(NetHandler handler, T t)
{
if (DefaultHandler != null)
DefaultHandler(handler, t);
}
public static event Action<NetHandler, T> DefaultHandler;
}
/// <summary>
/// 按ID分组
/// </summary>
public abstract class Request<TKey, T> : Request
{
public TKey ID;
public T Value;
internal override void Execute(NetHandler handler)
{
Handle(handler, ID, Value);
}
protected virtual void Handle(NetHandler handler, TKey k, T t)
{
Action<NetHandler, T> action;
if (_Handlers.TryGetValue(k, out action))
{
action(handler, t);
}
else
{
if (DefaultHandler != null)
DefaultHandler(handler, k, t);
}
}
private static readonly Dictionary<TKey, Action<NetHandler, T>> _Handlers = new Dictionary<TKey, Action<NetHandler, T>>();
public static Dictionary<TKey, Action<NetHandler, T>> Handlers
{
get
{
return _Handlers;
}
}
public static event Action<NetHandler, TKey, T> DefaultHandler;
}
#endregion
#region 设置相关
public class Settings
{
public uint timeout;
public int capacity;
public int worklimit;
public Events events;
public struct Events
{
public Action<NetHandler, Request> request;
public Action<NetHandler, int> read;
public Action<NetHandler, int> write;
public Action<NetHandler> close;
public Action<NetHandler, Exception> exception;
}
public Settings()
{
timeout = 5;
capacity = 2 << 24;
worklimit = 4;
}
}
#endregion
/// <summary>
/// 网络连接抽象句柄
/// </summary>
public abstract class NetHandler
{
public abstract bool Send(BufferSegment seg);
public abstract bool Send(BufferSegment seg1, BufferSegment seg2);
public abstract bool Send(BufferSegment seg1, BufferSegment seg2, BufferSegment seg3);
public abstract bool Send(BufferSegment seg, params BufferSegment[] segs);
public abstract void Close();
public abstract bool Connected { get; }
public abstract IPEndPoint Remote { get; }
public abstract IPEndPoint Local { get; }
public abstract short TTL { get; set; }
public abstract bool NoDelay { get; set; }
}
/// <summary>
/// 网络管理全局接口
/// </summary>
public abstract class NetManager : IDisposable
{
protected readonly Settings settings = null;
private bool running = true;
private int workcount = 0;
private readonly Queue<NetHandlerImpl> workqueue = new Queue<NetHandlerImpl>();
private readonly Dictionary<NetHandlerImpl, bool> workindex = new Dictionary<NetHandlerImpl, bool>();
private readonly Dictionary<int, Control> listens = new Dictionary<int, Control>();
private readonly Dictionary<int, Control> listensv6 = new Dictionary<int, Control>();
private readonly Dictionary<NetHandlerImpl, bool> sockets = new Dictionary<NetHandlerImpl, bool>();
private static readonly Dictionary<NetManager, bool> managers = new Dictionary<NetManager, bool>();
#region 内部使用的类
private class Control
{
public bool running;
public Action<NetHandler> action;
}
#endregion
protected NetManager(Settings settings)
{
this.settings = settings;
lock (managers)
{
managers.Add(this, true);
}
}
public static void Initialize()
{
Loop.Initialize();
}
public void Listen(int port, Action<NetHandler> callback)
{
if (!running)
throw new ObjectDisposedException(ToString());
new Thread(delegate()
{
Thread.CurrentThread.Name = "Network Server";
TcpListener server = new TcpListener(IPAddress.Any, port);
server.Start();
Control ctrl;
lock (listens)
{
if (listens.TryGetValue(port, out ctrl))
{
ctrl.running = true;
}
else
{
ctrl = new Control { running = true };
listens[port] = ctrl;
}
}
ctrl.action = callback;
while (running)
{
if (!server.Server.Poll(1000, SelectMode.SelectRead))
{
if (!ctrl.running)
break;
continue;
}
if (!server.Pending())
break;
NetHandlerImpl socket = new NetHandlerImpl(server.AcceptSocket(), this);
socket.Socket.Blocking = false;
socket.OnConnected();
Loop.Run(() =>
{
ctrl.action(socket);
});
}
lock (listens)
{
Control ctrlnow;
if (listens.TryGetValue(port, out ctrlnow) && ctrlnow == ctrl)
{
listens.Remove(port);
}
}
server.Stop();
}).Start();
new Thread(delegate()
{
Thread.CurrentThread.Name = "Network Server";
TcpListener server = new TcpListener(IPAddress.IPv6Any, port);
server.Start();
Control ctrl;
lock (listensv6)
{
if (listensv6.TryGetValue(port, out ctrl))
{
ctrl.running = true;
}
else
{
ctrl = new Control { running = true };
listensv6[port] = ctrl;
}
}
ctrl.action = callback;
while (running)
{
if (!server.Server.Poll(1000, SelectMode.SelectRead))
{
if (!ctrl.running)
break;
continue;
}
if (!server.Pending())
break;
NetHandlerImpl socket = new NetHandlerImpl(server.AcceptSocket(), this);
socket.Socket.Blocking = false;
socket.OnConnected();
Loop.Run(() =>
{
ctrl.action(socket);
});
}
lock (listensv6)
{
Control ctrlnow;
if (listensv6.TryGetValue(port, out ctrlnow) && ctrlnow == ctrl)
{
listensv6.Remove(port);
}
}
server.Stop();
}).Start();
}
public void Stop(int port)
{
if (!running)
throw new ObjectDisposedException(ToString());
lock (listens)
{
Control ctrl;
if (listens.TryGetValue(port, out ctrl))
{
ctrl.running = false;
listens.Remove(port);
}
}
lock (listensv6)
{
Control ctrl;
if (listensv6.TryGetValue(port, out ctrl))
{
ctrl.running = false;
listensv6.Remove(port);
}
}
}
public NetHandler Connect(string hostport, int timeout)
{
Regex regex = new Regex("^(?<host>.+):(?<port>\\d+)$", RegexOptions.Multiline);
Match match = regex.Match(hostport);
if (!match.Success)
throw new ArgumentException();
return Connect(match.Groups["host"].Captures[0].Value, Convert.ToInt32(match.Groups["port"].Captures[0].Value), timeout);
}
public void Connect(string hostport, int timeout, Action<NetHandler, Exception> callback)
{
Regex regex = new Regex("^(?<host>.+):(?<port>\\d+)$", RegexOptions.Multiline);
Match match = regex.Match(hostport);
if (match.Success)
{
Connect(match.Groups["host"].Captures[0].Value, Convert.ToInt32(match.Groups["port"].Captures[0].Value), timeout, callback);
}
else
{
Exception e = new ArgumentException();
Loop.RunDelay(() =>
{
callback(null, e);
});
}
}
public NetHandler Connect(string host, int port, int timeout)
{
AutoResetEvent reset = new AutoResetEvent(false);
NetHandler socket = null;
Exception e = null;
ConnectImpl(host, port, timeout, (handler, exception) =>
{
socket = handler;
e = exception;
reset.Set();
});
reset.WaitOne();
reset.Close();
if (e != null)
throw e;
return socket;
}
public void Connect(string host, int port, int timeout, Action<NetHandler, Exception> callback)
{
ConnectImpl(host, port, timeout, (handler, exception) =>
{
Loop.Run(() =>
{
callback(handler, exception);
});
});
}
public void Dispose()
{
lock (managers)
{
if (!managers.Remove(this))
return;
}
running = false;
lock (workqueue)
{
Monitor.PulseAll(workqueue);
}
List<NetHandlerImpl> keys;
lock (sockets)
{
keys = new List<NetHandlerImpl>(sockets.Count);
foreach (var kv in sockets)
{
keys.Add(kv.Key);
}
}
foreach (var handler in keys)
{
handler.Close();
}
}
public void Close()
{
Dispose();
}
#region 连接
private void ConnectImpl(string host, int port, int timeout, Action<NetHandler, Exception> callback)
{
if (!running)
throw new ObjectDisposedException(ToString());
Stopwatch clock = new Stopwatch();
clock.Start();
Dns.BeginGetHostAddresses(host, ar =>
{
clock.Stop();
timeout -= (int)clock.ElapsedMilliseconds;
try
{
IPAddress[] iplist = Dns.EndGetHostAddresses(ar);
if (timeout > 0)
{
AddressFamily family = AddressFamily.InterNetwork;
for (int i = 0; i < iplist.Length; ++i)
{
if (iplist[i].AddressFamily == AddressFamily.InterNetworkV6)
{
family = AddressFamily.InterNetworkV6;
break;
}
}
NetHandlerImpl socket = new NetHandlerImpl(family, this);
Timer timer = new Timer(state =>
{
socket.Socket.Close();
}, null, Timeout.Infinite, Timeout.Infinite);
socket.Socket.BeginConnect(iplist, port, result =>
{
try
{
timer.Dispose();
}
catch
{
}
Exception exception = null;
try
{
socket.Socket.EndConnect(result);
socket.Socket.Blocking = false;
socket.OnConnected();
}
catch (ObjectDisposedException)
{
exception = new SocketException((int)SocketError.TimedOut);
}
catch (Exception e)
{
exception = e;
socket.Socket.Close();
}
callback(exception == null ? socket : null, exception);
}, null);
timer.Change(timeout, Timeout.Infinite);
}
else
{
callback(null, new SocketException((int)SocketError.TimedOut));
}
}
catch (Exception e)
{
callback(null, e);
}
}, null);
}
#endregion
private void SendQueue(NetHandlerImpl handler)
{
lock (workqueue)
{
if (!workindex.ContainsKey(handler))
{
workindex.Add(handler, true);
workqueue.Enqueue(handler);
Monitor.Pulse(workqueue);
}
}
if (workcount >= settings.worklimit)
return;
if (Interlocked.Increment(ref workcount) > settings.worklimit)
{
Interlocked.Decrement(ref workcount);
return;
}
new Thread(delegate()
{
Thread.CurrentThread.Name = "Network Write";
while (running)
{
handler = null;
lock (workqueue)
{
while (running)
{
if (workqueue.Count != 0)
{
handler = workqueue.Dequeue();
workindex.Remove(handler);
break;
}
Monitor.Wait(workqueue);
}
}
if (handler != null)
{
bool result;
try
{
result = handler.Socket.Poll((int)settings.timeout * 1000, SelectMode.SelectWrite);
}
catch (ObjectDisposedException)
{
continue;
}
if (result)
{
if (!handler.SendReady())
continue;
}
lock (workqueue)
{
if (!workindex.ContainsKey(handler))
{
workindex.Add(handler, true);
workqueue.Enqueue(handler);
}
}
}
}
}).Start();
}
#region 网络连接的具体类
private class NetHandlerImpl : NetHandler
{
public readonly Socket Socket;
public readonly ByteBuffer buffer = new ByteBuffer();
private readonly NetManager manager;
private int closed;
private Request request;
public NetHandlerImpl(AddressFamily family, NetManager manager)
{
this.manager = manager;
this.closed = 0;
Socket = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(false, 0));
}
public NetHandlerImpl(Socket socket, NetManager manager)
{
this.manager = manager;
this.closed = 0;
Socket = socket;
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(false, 0));
}
public void OnConnected()
{
lock (manager.sockets)
{
manager.sockets.Add(this, true);
}
new Thread(delegate()
{
Thread.CurrentThread.Name = "Network Read";
byte[] bytes = new byte[64 * 1024];
while (manager.running)
{
bool result;
try
{
result = Socket.Poll(-1, SelectMode.SelectRead);
}
catch (ObjectDisposedException)
{
lock (manager.sockets)
{
manager.sockets.Remove(this);
}
break;
}
if (result)
{
int total = 0;
try
{
while (Socket.Available > 0)
{
int length = Socket.Receive(bytes);
if (length == 0)
{
break;
}
total += length;
OnReceive(bytes, length);
}
if (total == 0)
{
Close();
break;
}
}
catch (SocketException e)
{
if (e.SocketErrorCode == SocketError.ConnectionReset ||
e.SocketErrorCode == SocketError.ConnectionAborted ||
e.SocketErrorCode == SocketError.NotConnected ||
e.SocketErrorCode == SocketError.Shutdown)
{
Close();
break;
}
if (manager.settings.events.exception != null)
{
Loop.Run(() =>
{
if (manager.settings.events.exception != null)
manager.settings.events.exception(this, e);
});
}
}
catch (ObjectDisposedException)
{
lock (manager.sockets)
{
manager.sockets.Remove(this);
}
break;
}
catch (Exception e)
{
if (manager.settings.events.exception != null)
{
Loop.Run(() =>
{
if (manager.settings.events.exception != null)
manager.settings.events.exception(this, e);
});
}
}
if (total != 0 && manager.settings.events.read != null)
{
Loop.Run(() =>
{
if (manager.settings.events.read != null)
manager.settings.events.read(this, total);
});
}
}
}
}).Start();
}
public override void Close()
{
if (Interlocked.CompareExchange(ref closed, 1, 0) != 0)
return;
lock (manager.sockets)
{
manager.sockets.Remove(this);
}
if (manager.settings.events.close != null)
{
Loop.Run(() =>
{
if (manager.settings.events.close != null)
manager.settings.events.close(this);
Socket.Close();
});
}
else
{
Socket.Close();
}
}
public override bool Send(BufferSegment seg)
{
lock (buffer)
{
if (buffer.length >= manager.settings.capacity)
return false;
buffer.Write(seg);
}
manager.SendQueue(this);
return true;
}
public override bool Send(BufferSegment seg1, BufferSegment seg2)
{
lock (buffer)
{
if (buffer.length >= manager.settings.capacity)
return false;
buffer.Write(seg1);
buffer.Write(seg2);
}
manager.SendQueue(this);
return true;
}
public override bool Send(BufferSegment seg1, BufferSegment seg2, BufferSegment seg3)
{
lock (buffer)
{
if (buffer.length >= manager.settings.capacity)
return false;
buffer.Write(seg1);
buffer.Write(seg2);
buffer.Write(seg3);
}
manager.SendQueue(this);
return true;
}
public override bool Send(BufferSegment seg, params BufferSegment[] segs)
{
lock (buffer)
{
if (buffer.length >= manager.settings.capacity)
return false;
buffer.Write(seg);
for (int i = 0; i < segs.Length; ++i)
{
buffer.Write(segs[i]);
}
}
manager.SendQueue(this);
return true;
}
public override bool Connected
{
get { return Socket.Connected; }
}
public override IPEndPoint Remote
{
get
{
return Socket.RemoteEndPoint as IPEndPoint;
}
}
public override IPEndPoint Local
{
get
{
return Socket.LocalEndPoint as IPEndPoint;
}
}
public override short TTL
{
get { return Socket.Ttl; }
set { Socket.Ttl = value; }
}
public override bool NoDelay
{
get { return !Socket.NoDelay; }
set { Socket.NoDelay = !value; }
}
public bool SendReady()
{
int length = 0;
bool needsend = false;
bool sended = false;
lock (buffer)
{
if (buffer.length != 0)
{
try
{
length = Socket.Send(buffer.array, buffer.offset, buffer.length, SocketFlags.None);
}
catch (ObjectDisposedException)
{
return false;
}
sended = true;
buffer.Pop(length);
needsend = buffer.length != 0;
}
}
if (sended && manager.settings.events.write != null)
{
Loop.Run(() =>
{
if (manager.settings.events.write != null)
manager.settings.events.write(this, length);
});
}
return needsend;
}
private void OnReceive(byte[] bytes, int length)