forked from kerryjiang/WebSocket4Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocket.cs
More file actions
779 lines (588 loc) · 21.9 KB
/
WebSocket.cs
File metadata and controls
779 lines (588 loc) · 21.9 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using SuperSocket.ClientEngine;
using WebSocket4Net.Common;
using WebSocket4Net.Protocol;
namespace WebSocket4Net
{
public partial class WebSocket : IDisposable
{
internal TcpClientSession Client { get; private set; }
private EndPoint m_RemoteEndPoint;
/// <summary>
/// Gets the version of the websocket protocol.
/// </summary>
public WebSocketVersion Version { get; private set; }
/// <summary>
/// Gets the last active time of the websocket.
/// </summary>
public DateTime LastActiveTime { get; internal set; }
/// <summary>
/// Gets or sets a value indicating whether [enable auto send ping].
/// </summary>
/// <value>
/// <c>true</c> if [enable auto send ping]; otherwise, <c>false</c>.
/// </value>
public bool EnableAutoSendPing { get; set; }
/// <summary>
/// Gets or sets the interval of ping auto sending, in seconds.
/// </summary>
/// <value>
/// The auto send ping internal.
/// </value>
public int AutoSendPingInterval { get; set; }
protected const string UserAgentKey = "UserAgent";
internal IProtocolProcessor ProtocolProcessor { get; private set; }
public bool SupportBinary
{
get { return ProtocolProcessor.SupportBinary; }
}
internal Uri TargetUri { get; private set; }
internal string SubProtocol { get; private set; }
internal IDictionary<string, object> Items { get; private set; }
internal List<KeyValuePair<string, string>> Cookies { get; private set; }
internal List<KeyValuePair<string, string>> CustomHeaderItems { get; private set; }
public const int DefaultReceiveBufferSize = 4096;
private int m_StateCode;
internal int StateCode
{
get { return m_StateCode; }
}
public WebSocketState State
{
get { return (WebSocketState)m_StateCode; }
}
public bool Handshaked { get; private set; }
public IProxyConnector Proxy { get; set; }
private EndPoint m_HttpConnectProxy;
internal EndPoint HttpConnectProxy
{
get { return m_HttpConnectProxy; }
}
protected IClientCommandReader<WebSocketCommandInfo> CommandReader { get; private set; }
private Dictionary<string, ICommand<WebSocket, WebSocketCommandInfo>> m_CommandDict
= new Dictionary<string, ICommand<WebSocket, WebSocketCommandInfo>>(StringComparer.OrdinalIgnoreCase);
private static ProtocolProcessorFactory m_ProtocolProcessorFactory;
internal bool NotSpecifiedVersion { get; private set; }
/// <summary>
/// It is used for ping/pong and closing handshake checking
/// </summary>
private Timer m_WebSocketTimer;
internal string LastPongResponse { get; set; }
private string m_LastPingRequest;
private const string m_UriScheme = "ws";
private const string m_UriPrefix = m_UriScheme + "://";
private const string m_SecureUriScheme = "wss";
private const int m_SecurePort = 443;
private const string m_SecureUriPrefix = m_SecureUriScheme + "://";
internal string HandshakeHost { get; private set; }
internal string Origin { get; private set; }
#if !__IOS__
public bool NoDelay { get; set; }
#endif
#if !SILVERLIGHT
/// <summary>
/// set/get the local bind endpoint
/// </summary>
public EndPoint LocalEndPoint
{
get
{
if (Client == null)
return null;
return Client.LocalEndPoint;
}
set
{
if (Client == null)
throw new Exception("Websocket client is not initilized.");
Client.LocalEndPoint = value;
}
}
#endif
#if !SILVERLIGHT
private SecurityOption m_Security;
/// <summary>
/// get the websocket's security options
/// </summary>
public SecurityOption Security
{
get
{
if (m_Security != null)
return m_Security;
var secureClient = Client as SslStreamTcpSession;
if (secureClient == null)
return m_Security = new SecurityOption();
return m_Security = secureClient.Security;
}
}
#endif
private bool m_Disposed = false;
static WebSocket()
{
m_ProtocolProcessorFactory = new ProtocolProcessorFactory(new Rfc6455Processor(), new DraftHybi10Processor(), new DraftHybi00Processor());
}
private EndPoint ResolveUri(string uri, int defaultPort, out int port)
{
TargetUri = new Uri(uri);
if (string.IsNullOrEmpty(Origin))
Origin = TargetUri.GetOrigin();
IPAddress ipAddress;
EndPoint remoteEndPoint;
port = TargetUri.Port;
if (port <= 0)
port = defaultPort;
if (IPAddress.TryParse(TargetUri.Host, out ipAddress))
remoteEndPoint = new IPEndPoint(ipAddress, port);
else
remoteEndPoint = new DnsEndPoint(TargetUri.Host, port);
return remoteEndPoint;
}
TcpClientSession CreateClient(string uri)
{
int port;
var targetEndPoint = m_RemoteEndPoint = ResolveUri(uri, 80, out port);
if (port == 80)
HandshakeHost = TargetUri.Host;
else
HandshakeHost = TargetUri.Host + ":" + port;
return new AsyncTcpSession();
}
#if !NETFX_CORE
TcpClientSession CreateSecureClient(string uri)
{
int hostPos = uri.IndexOf('/', m_SecureUriPrefix.Length);
if (hostPos < 0)//wss://localhost or wss://localhost:xxxx
{
hostPos = uri.IndexOf(':', m_SecureUriPrefix.Length, uri.Length - m_SecureUriPrefix.Length);
if (hostPos < 0)
uri = uri + ":" + m_SecurePort + "/";
else
uri = uri + "/";
}
else if (hostPos == m_SecureUriPrefix.Length)//wss://
{
throw new ArgumentException("Invalid uri", "uri");
}
else//wss://xxx/xxx
{
int colonPos = uri.IndexOf(':', m_SecureUriPrefix.Length, hostPos - m_SecureUriPrefix.Length);
if (colonPos < 0)
{
uri = uri.Substring(0, hostPos) + ":" + m_SecurePort + uri.Substring(hostPos);
}
}
int port;
var targetEndPoint = m_RemoteEndPoint = ResolveUri(uri, m_SecurePort, out port);
if (m_HttpConnectProxy != null)
{
m_RemoteEndPoint = m_HttpConnectProxy;
}
if (port == m_SecurePort)
HandshakeHost = TargetUri.Host;
else
HandshakeHost = TargetUri.Host + ":" + port;
return CreateSecureTcpSession();
}
#endif
private void Initialize(string uri, string subProtocol, List<KeyValuePair<string, string>> cookies, List<KeyValuePair<string, string>> customHeaderItems, string userAgent, string origin, WebSocketVersion version, EndPoint httpConnectProxy, int receiveBufferSize)
{
if (version == WebSocketVersion.None)
{
NotSpecifiedVersion = true;
version = WebSocketVersion.Rfc6455;
}
Version = version;
ProtocolProcessor = GetProtocolProcessor(version);
Cookies = cookies;
Origin = origin;
if (!string.IsNullOrEmpty(userAgent))
{
if (customHeaderItems == null)
customHeaderItems = new List<KeyValuePair<string, string>>();
customHeaderItems.Add(new KeyValuePair<string, string>(UserAgentKey, userAgent));
}
if (customHeaderItems != null && customHeaderItems.Count > 0)
CustomHeaderItems = customHeaderItems;
var handshakeCmd = new Command.Handshake();
m_CommandDict.Add(handshakeCmd.Name, handshakeCmd);
var textCmd = new Command.Text();
m_CommandDict.Add(textCmd.Name, textCmd);
var dataCmd = new Command.Binary();
m_CommandDict.Add(dataCmd.Name, dataCmd);
var closeCmd = new Command.Close();
m_CommandDict.Add(closeCmd.Name, closeCmd);
var pingCmd = new Command.Ping();
m_CommandDict.Add(pingCmd.Name, pingCmd);
var pongCmd = new Command.Pong();
m_CommandDict.Add(pongCmd.Name, pongCmd);
var badRequestCmd = new Command.BadRequest();
m_CommandDict.Add(badRequestCmd.Name, badRequestCmd);
m_StateCode = WebSocketStateConst.None;
SubProtocol = subProtocol;
Items = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
m_HttpConnectProxy = httpConnectProxy;
TcpClientSession client;
if (uri.StartsWith(m_UriPrefix, StringComparison.OrdinalIgnoreCase))
{
client = CreateClient(uri);
}
else if (uri.StartsWith(m_SecureUriPrefix, StringComparison.OrdinalIgnoreCase))
{
#if !NETFX_CORE
client = CreateSecureClient(uri);
#else
throw new NotSupportedException("WebSocket4Net still has not supported secure websocket for UWP yet.");
#endif
}
else
{
throw new ArgumentException("Invalid uri", "uri");
}
client.ReceiveBufferSize = receiveBufferSize > 0 ? receiveBufferSize : DefaultReceiveBufferSize;
client.Connected += new EventHandler(client_Connected);
client.Closed += new EventHandler(client_Closed);
client.Error += new EventHandler<ErrorEventArgs>(client_Error);
client.DataReceived += new EventHandler<DataEventArgs>(client_DataReceived);
Client = client;
//Ping auto sending is enabled by default
EnableAutoSendPing = true;
}
void client_DataReceived(object sender, DataEventArgs e)
{
OnDataReceived(e.Data, e.Offset, e.Length);
}
void client_Error(object sender, ErrorEventArgs e)
{
OnError(e);
//Also fire close event if the connection fail to connect
OnClosed();
}
void client_Closed(object sender, EventArgs e)
{
OnClosed();
}
void client_Connected(object sender, EventArgs e)
{
OnConnected();
}
internal bool GetAvailableProcessor(int[] availableVersions)
{
var processor = m_ProtocolProcessorFactory.GetPreferedProcessorFromAvialable(availableVersions);
if (processor == null)
return false;
this.ProtocolProcessor = processor;
return true;
}
public int ReceiveBufferSize
{
get { return Client.ReceiveBufferSize; }
set { Client.ReceiveBufferSize = value; }
}
public void Open()
{
m_StateCode = WebSocketStateConst.Connecting;
if (Proxy != null)
Client.Proxy = Proxy;
#if !__IOS__
Client.NoDelay = NoDelay;
#endif
#if SILVERLIGHT
#if !WINDOWS_PHONE
Client.ClientAccessPolicyProtocol = ClientAccessPolicyProtocol;
#endif
#endif
Client.Connect(m_RemoteEndPoint);
}
private static IProtocolProcessor GetProtocolProcessor(WebSocketVersion version)
{
var processor = m_ProtocolProcessorFactory.GetProcessorByVersion(version);
if (processor == null)
throw new ArgumentException("Invalid websocket version");
return processor;
}
void OnConnected()
{
CommandReader = ProtocolProcessor.CreateHandshakeReader(this);
if (Items.Count > 0)
Items.Clear();
ProtocolProcessor.SendHandshake(this);
}
protected internal virtual void OnHandshaked()
{
m_StateCode = WebSocketStateConst.Open;
Handshaked = true;
if (EnableAutoSendPing && ProtocolProcessor.SupportPingPong)
{
//Ping auto sending interval's default value is 60 seconds
if (AutoSendPingInterval <= 0)
AutoSendPingInterval = 60;
m_WebSocketTimer = new Timer(OnPingTimerCallback, ProtocolProcessor, AutoSendPingInterval * 1000, AutoSendPingInterval * 1000);
}
if (m_Opened != null)
m_Opened(this, EventArgs.Empty);
}
private void OnPingTimerCallback(object state)
{
var protocolProcessor = state as IProtocolProcessor;
if (!string.IsNullOrEmpty(m_LastPingRequest) && !m_LastPingRequest.Equals(LastPongResponse))
{
// have not got last response
// Verify that the remote endpoint is still responsive
// by sending an un-solicited PONG frame:
try
{
protocolProcessor.SendPong(this, "");
}
catch (Exception e)
{
OnError(e);
return;
}
}
m_LastPingRequest = DateTime.Now.ToString();
try
{
protocolProcessor.SendPing(this, m_LastPingRequest);
}
catch (Exception e)
{
OnError(e);
}
}
private EventHandler m_Opened;
public event EventHandler Opened
{
add { m_Opened += value; }
remove { m_Opened -= value; }
}
private EventHandler<MessageReceivedEventArgs> m_MessageReceived;
public event EventHandler<MessageReceivedEventArgs> MessageReceived
{
add { m_MessageReceived += value; }
remove { m_MessageReceived -= value; }
}
internal void FireMessageReceived(string message)
{
if (m_MessageReceived == null)
return;
m_MessageReceived(this, new MessageReceivedEventArgs(message));
}
private EventHandler<DataReceivedEventArgs> m_DataReceived;
public event EventHandler<DataReceivedEventArgs> DataReceived
{
add { m_DataReceived += value; }
remove { m_DataReceived -= value; }
}
internal void FireDataReceived(byte[] data)
{
if (m_DataReceived == null)
return;
m_DataReceived(this, new DataReceivedEventArgs(data));
}
private const string m_NotOpenSendingMessage = "You must send data by websocket after websocket is opened!";
private bool EnsureWebSocketOpen()
{
if (!Handshaked)
{
OnError(new Exception(m_NotOpenSendingMessage));
return false;
}
return true;
}
public void Send(string message)
{
if (!EnsureWebSocketOpen())
return;
ProtocolProcessor.SendMessage(this, message);
}
public void Send(byte[] data, int offset, int length)
{
if (!EnsureWebSocketOpen())
return;
ProtocolProcessor.SendData(this, data, offset, length);
}
public void Send(IList<ArraySegment<byte>> segments)
{
if (!EnsureWebSocketOpen())
return;
ProtocolProcessor.SendData(this, segments);
}
private ClosedEventArgs m_ClosedArgs;
private void OnClosed()
{
var fireBaseClose = false;
if (m_StateCode == WebSocketStateConst.Closing || m_StateCode == WebSocketStateConst.Open || m_StateCode == WebSocketStateConst.Connecting)
fireBaseClose = true;
m_StateCode = WebSocketStateConst.Closed;
if (fireBaseClose)
FireClosed();
}
public void Close()
{
Close(string.Empty);
}
public void Close(string reason)
{
Close(ProtocolProcessor.CloseStatusCode.NormalClosure, reason);
}
public void Close(int statusCode, string reason)
{
m_ClosedArgs = new ClosedEventArgs((short)statusCode, reason);
//The websocket never be opened
if (Interlocked.CompareExchange(ref m_StateCode, WebSocketStateConst.Closed, WebSocketStateConst.None)
== WebSocketStateConst.None)
{
OnClosed();
return;
}
//The websocket is connecting or in handshake
if (Interlocked.CompareExchange(ref m_StateCode, WebSocketStateConst.Closing, WebSocketStateConst.Connecting)
== WebSocketStateConst.Connecting)
{
var client = Client;
if (client != null && client.IsConnected)
{
client.Close();
return;
}
OnClosed();
return;
}
m_StateCode = WebSocketStateConst.Closing;
//Disable auto ping
ClearTimer();
//Set closing hadnshake checking timer
m_WebSocketTimer = new Timer(CheckCloseHandshake, null, 5 * 1000, Timeout.Infinite);
ProtocolProcessor.SendCloseHandshake(this, statusCode, reason);
}
private void CheckCloseHandshake(object state)
{
if (m_StateCode == WebSocketStateConst.Closed)
return;
try
{
CloseWithoutHandshake();
}
catch (Exception e)
{
OnError(e);
}
}
internal void CloseWithoutHandshake()
{
var client = Client;
if (client != null)
client.Close();
}
protected void ExecuteCommand(WebSocketCommandInfo commandInfo)
{
ICommand<WebSocket, WebSocketCommandInfo> command;
if (m_CommandDict.TryGetValue(commandInfo.Key, out command))
{
command.ExecuteCommand(this, commandInfo);
}
}
private void OnDataReceived(byte[] data, int offset, int length)
{
while (true)
{
int left;
var commandInfo = CommandReader.GetCommandInfo(data, offset, length, out left);
if (CommandReader.NextCommandReader != null)
CommandReader = CommandReader.NextCommandReader;
if (commandInfo != null)
ExecuteCommand(commandInfo);
if (left <= 0)
break;
offset = offset + length - left;
length = left;
}
}
internal void FireError(Exception error)
{
OnError(error);
}
private EventHandler m_Closed;
public event EventHandler Closed
{
add { m_Closed += value; }
remove { m_Closed -= value; }
}
private void ClearTimer()
{
var timer = m_WebSocketTimer;
if (timer == null)
return;
lock (this)
{
if (m_WebSocketTimer == null)
return;
timer.Change(Timeout.Infinite, Timeout.Infinite);
timer.Dispose();
m_WebSocketTimer = null;
}
}
private void FireClosed()
{
ClearTimer();
var handler = m_Closed;
if (handler != null)
handler(this, m_ClosedArgs ?? EventArgs.Empty);
}
private EventHandler<ErrorEventArgs> m_Error;
public event EventHandler<ErrorEventArgs> Error
{
add { m_Error += value; }
remove { m_Error -= value; }
}
private void OnError(ErrorEventArgs e)
{
var handler = m_Error;
if (handler == null)
return;
handler(this, e);
}
private void OnError(Exception e)
{
OnError(new ErrorEventArgs(e));
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (m_Disposed)
return;
if (disposing)
{
var client = Client;
if (client != null)
{
client.Connected -= new EventHandler(client_Connected);
client.Closed -= new EventHandler(client_Closed);
client.Error -= new EventHandler<ErrorEventArgs>(client_Error);
client.DataReceived -= new EventHandler<DataEventArgs>(client_DataReceived);
if (client.IsConnected)
client.Close();
Client = null;
}
ClearTimer();
}
m_Disposed = true;
}
~WebSocket()
{
Dispose(false);
}
}
}