forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWssClient.cs
More file actions
408 lines (344 loc) · 17.6 KB
/
WssClient.cs
File metadata and controls
408 lines (344 loc) · 17.6 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
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace NetCoreServer
{
/// <summary>
/// WebSocket secure client
/// </summary>
/// <remarks>WebSocket secure client is used to communicate with secure WebSocket server. Thread-safe.</remarks>
public class WssClient : HttpsClient, IWebSocket
{
internal readonly WebSocket WebSocket;
/// <summary>
/// Initialize WebSocket client with a given IP address and port number
/// </summary>
/// <param name="context">SSL context</param>
/// <param name="address">IP address</param>
/// <param name="port">Port number</param>
public WssClient(SslContext context, IPAddress address, int port) : base(context, address, port) { WebSocket = new WebSocket(this); }
/// <summary>
/// Initialize WebSocket client with a given IP address and port number
/// </summary>
/// <param name="context">SSL context</param>
/// <param name="address">IP address</param>
/// <param name="port">Port number</param>
public WssClient(SslContext context, string address, int port) : base(context, address, port) { WebSocket = new WebSocket(this); }
/// <summary>
/// Initialize WebSocket client with a given DNS endpoint
/// </summary>
/// <param name="context">SSL context</param>
/// <param name="endpoint">DNS endpoint</param>
public WssClient(SslContext context, DnsEndPoint endpoint) : base(context, endpoint) { WebSocket = new WebSocket(this); }
/// <summary>
/// Initialize WebSocket client with a given IP endpoint
/// </summary>
/// <param name="context">SSL context</param>
/// <param name="endpoint">IP endpoint</param>
public WssClient(SslContext context, IPEndPoint endpoint) : base(context, endpoint) { WebSocket = new WebSocket(this); }
/// <summary>
/// WebSocket random nonce
/// </summary>
public byte[] WsNonce => WebSocket.WsNonce;
#region WebSocket connection methods
public override bool Connect() { _syncConnect = true; return base.Connect(); }
public override bool ConnectAsync() { _syncConnect = false; return base.ConnectAsync(); }
public virtual bool Close() => Close(0, Span<byte>.Empty);
public virtual bool Close(int status) => Close(status, Span<byte>.Empty);
public virtual bool Close(int status, string text) => Close(status, Encoding.UTF8.GetBytes(text));
public virtual bool Close(int status, ReadOnlySpan<char> text) => Close(status, Encoding.UTF8.GetBytes(text.ToArray()));
public virtual bool Close(int status, byte[] buffer) => Close(status, buffer.AsSpan());
public virtual bool Close(int status, byte[] buffer, long offset, long size) => Close(status, buffer.AsSpan((int)offset, (int)size));
public virtual bool Close(int status, ReadOnlySpan<byte> buffer) { SendClose(status, buffer); base.Disconnect(); return true; }
public virtual bool CloseAsync() => CloseAsync(0, Span<byte>.Empty);
public virtual bool CloseAsync(int status) => CloseAsync(status, Span<byte>.Empty);
public virtual bool CloseAsync(int status, string text) => CloseAsync(status, Encoding.UTF8.GetBytes(text));
public virtual bool CloseAsync(int status, ReadOnlySpan<char> text) => CloseAsync(status, Encoding.UTF8.GetBytes(text.ToArray()));
public virtual bool CloseAsync(int status, byte[] buffer) => CloseAsync(status, buffer.AsSpan());
public virtual bool CloseAsync(int status, byte[] buffer, long offset, long size) => CloseAsync(status, buffer.AsSpan((int)offset, (int)size));
public virtual bool CloseAsync(int status, ReadOnlySpan<byte> buffer) { SendClose(status, buffer); base.DisconnectAsync(); return true; }
#endregion
#region WebSocket send text methods
public long SendText(string text) => SendText(Encoding.UTF8.GetBytes(text));
public long SendText(ReadOnlySpan<char> text) => SendText(Encoding.UTF8.GetBytes(text.ToArray()));
public long SendText(byte[] buffer) => SendText(buffer.AsSpan());
public long SendText(byte[] buffer, long offset, long size) => SendText(buffer.AsSpan((int)offset, (int)size));
public long SendText(ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_TEXT, true, buffer);
return base.Send(WebSocket.WsSendBuffer.AsSpan());
}
}
public bool SendTextAsync(string text) => SendTextAsync(Encoding.UTF8.GetBytes(text));
public bool SendTextAsync(ReadOnlySpan<char> text) => SendTextAsync(Encoding.UTF8.GetBytes(text.ToArray()));
public bool SendTextAsync(byte[] buffer) => SendTextAsync(buffer.AsSpan());
public bool SendTextAsync(byte[] buffer, long offset, long size) => SendTextAsync(buffer.AsSpan((int)offset, (int)size));
public bool SendTextAsync(ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_TEXT, true, buffer);
return base.SendAsync(WebSocket.WsSendBuffer.AsSpan());
}
}
#endregion
#region WebSocket send binary methods
public long SendBinary(string text) => SendBinary(Encoding.UTF8.GetBytes(text));
public long SendBinary(ReadOnlySpan<char> text) => SendBinary(Encoding.UTF8.GetBytes(text.ToArray()));
public long SendBinary(byte[] buffer) => SendBinary(buffer.AsSpan());
public long SendBinary(byte[] buffer, long offset, long size) => SendBinary(buffer.AsSpan((int)offset, (int)size));
public long SendBinary(ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_BINARY, true, buffer);
return base.Send(WebSocket.WsSendBuffer.AsSpan());
}
}
public bool SendBinaryAsync(string text) => SendBinaryAsync(Encoding.UTF8.GetBytes(text));
public bool SendBinaryAsync(ReadOnlySpan<char> text) => SendBinaryAsync(Encoding.UTF8.GetBytes(text.ToArray()));
public bool SendBinaryAsync(byte[] buffer) => SendBinaryAsync(buffer.AsSpan());
public bool SendBinaryAsync(byte[] buffer, long offset, long size) => SendBinaryAsync(buffer.AsSpan((int)offset, (int)size));
public bool SendBinaryAsync(ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_BINARY, true, buffer);
return base.SendAsync(WebSocket.WsSendBuffer.AsSpan());
}
}
#endregion
#region WebSocket send close methods
public long SendClose(int status, string text) => SendClose(status, Encoding.UTF8.GetBytes(text));
public long SendClose(int status, ReadOnlySpan<char> text) => SendClose(status, Encoding.UTF8.GetBytes(text.ToArray()));
public long SendClose(int status, byte[] buffer) => SendClose(status, buffer.AsSpan());
public long SendClose(int status, byte[] buffer, long offset, long size) => SendClose(status, buffer.AsSpan((int)offset, (int)size));
public long SendClose(int status, ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_CLOSE, true, buffer, status);
return base.Send(WebSocket.WsSendBuffer.AsSpan());
}
}
public bool SendCloseAsync(int status, string text) => SendCloseAsync(status, Encoding.UTF8.GetBytes(text));
public bool SendCloseAsync(int status, ReadOnlySpan<char> text) => SendCloseAsync(status, Encoding.UTF8.GetBytes(text.ToArray()));
public bool SendCloseAsync(int status, byte[] buffer) => SendCloseAsync(status, buffer.AsSpan());
public bool SendCloseAsync(int status, byte[] buffer, long offset, long size) => SendCloseAsync(status, buffer.AsSpan((int)offset, (int)size));
public bool SendCloseAsync(int status, ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_CLOSE, true, buffer, status);
return base.SendAsync(WebSocket.WsSendBuffer.AsSpan());
}
}
#endregion
#region WebSocket send ping methods
public long SendPing(string text) => SendPing(Encoding.UTF8.GetBytes(text));
public long SendPing(ReadOnlySpan<char> text) => SendPing(Encoding.UTF8.GetBytes(text.ToArray()));
public long SendPing(byte[] buffer) => SendPing(buffer.AsSpan());
public long SendPing(byte[] buffer, long offset, long size) => SendPing(buffer.AsSpan((int)offset, (int)size));
public long SendPing(ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_PING, true, buffer);
return base.Send(WebSocket.WsSendBuffer.AsSpan());
}
}
public bool SendPingAsync(string text) => SendPingAsync(Encoding.UTF8.GetBytes(text));
public bool SendPingAsync(ReadOnlySpan<char> text) => SendPingAsync(Encoding.UTF8.GetBytes(text.ToArray()));
public bool SendPingAsync(byte[] buffer) => SendPingAsync(buffer.AsSpan());
public bool SendPingAsync(byte[] buffer, long offset, long size) => SendPingAsync(buffer.AsSpan((int)offset, (int)size));
public bool SendPingAsync(ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_PING, true, buffer);
return base.SendAsync(WebSocket.WsSendBuffer.AsSpan());
}
}
#endregion
#region WebSocket send pong methods
public long SendPong(string text) => SendPong(Encoding.UTF8.GetBytes(text));
public long SendPong(ReadOnlySpan<char> text) => SendPong(Encoding.UTF8.GetBytes(text.ToArray()));
public long SendPong(byte[] buffer) => SendPong(buffer.AsSpan());
public long SendPong(byte[] buffer, long offset, long size) => SendPong(buffer.AsSpan((int)offset, (int)size));
public long SendPong(ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_PONG, true, buffer);
return base.Send(WebSocket.WsSendBuffer.AsSpan());
}
}
public bool SendPongAsync(string text) => SendPongAsync(Encoding.UTF8.GetBytes(text));
public bool SendPongAsync(ReadOnlySpan<char> text) => SendPongAsync(Encoding.UTF8.GetBytes(text.ToArray()));
public bool SendPongAsync(byte[] buffer) => SendPongAsync(buffer.AsSpan());
public bool SendPongAsync(byte[] buffer, long offset, long size) => SendPongAsync(buffer.AsSpan((int)offset, (int)size));
public bool SendPongAsync(ReadOnlySpan<byte> buffer)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_PONG, true, buffer);
return base.SendAsync(WebSocket.WsSendBuffer.AsSpan());
}
}
#endregion
#region WebSocket receive methods
public string ReceiveText()
{
var result = new Buffer();
if (!WebSocket.WsHandshaked)
return result.ExtractString(0, result.Data.Length);
var cache = new Buffer();
// Receive WebSocket frame data
while (!WebSocket.WsFinalReceived)
{
while (!WebSocket.WsFrameReceived)
{
long required = WebSocket.RequiredReceiveFrameSize();
cache.Resize(required);
long received = (int)base.Receive(cache.Data, 0, required);
if (received != required)
return result.ExtractString(0, result.Data.Length);
WebSocket.PrepareReceiveFrame(cache.Data, 0, received);
}
if (!WebSocket.WsFinalReceived)
WebSocket.PrepareReceiveFrame(null, 0, 0);
}
// Copy WebSocket frame data
result.Append(WebSocket.WsReceiveFinalBuffer);
WebSocket.PrepareReceiveFrame(null, 0, 0);
return result.ExtractString(0, result.Data.Length);
}
public Buffer ReceiveBinary()
{
var result = new Buffer();
if (!WebSocket.WsHandshaked)
return result;
var cache = new Buffer();
// Receive WebSocket frame data
while (!WebSocket.WsFinalReceived)
{
while (!WebSocket.WsFrameReceived)
{
long required = WebSocket.RequiredReceiveFrameSize();
cache.Resize(required);
long received = (int)base.Receive(cache.Data, 0, required);
if (received != required)
return result;
WebSocket.PrepareReceiveFrame(cache.Data, 0, received);
}
if (!WebSocket.WsFinalReceived)
WebSocket.PrepareReceiveFrame(null, 0, 0);
}
// Copy WebSocket frame data
result.Append(WebSocket.WsReceiveFinalBuffer);
WebSocket.PrepareReceiveFrame(null, 0, 0);
return result;
}
#endregion
#region Session handlers
protected override void OnHandshaked()
{
// Clear WebSocket send/receive buffers
WebSocket.ClearWsBuffers();
// Fill the WebSocket upgrade HTTP request
OnWsConnecting(Request);
// Send the WebSocket upgrade HTTP request
if (_syncConnect)
SendRequest(Request);
else
SendRequestAsync(Request);
}
protected override void OnDisconnecting()
{
if (WebSocket.WsHandshaked)
OnWsDisconnecting();
}
protected override void OnDisconnected()
{
// Disconnect WebSocket
if (WebSocket.WsHandshaked)
{
WebSocket.WsHandshaked = false;
OnWsDisconnected();
}
// Reset WebSocket upgrade HTTP request and response
Request.Clear();
Response.Clear();
// Clear WebSocket send/receive buffers
WebSocket.ClearWsBuffers();
// Initialize new WebSocket random nonce
WebSocket.InitWsNonce();
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
// Check for WebSocket handshaked status
if (WebSocket.WsHandshaked)
{
// Prepare receive frame
WebSocket.PrepareReceiveFrame(buffer, offset, size);
return;
}
base.OnReceived(buffer, offset, size);
}
protected override void OnReceivedResponseHeader(HttpResponse response)
{
// Check for WebSocket handshaked status
if (WebSocket.WsHandshaked)
return;
// Try to perform WebSocket upgrade
if (!WebSocket.PerformClientUpgrade(response, Id))
{
base.OnReceivedResponseHeader(response);
return;
}
}
protected override void OnReceivedResponse(HttpResponse response)
{
// Check for WebSocket handshaked status
if (WebSocket.WsHandshaked)
{
// Prepare receive frame from the remaining response body
var body = Response.Body;
var data = Encoding.UTF8.GetBytes(body);
WebSocket.PrepareReceiveFrame(data, 0, data.Length);
return;
}
base.OnReceivedResponse(response);
}
protected override void OnReceivedResponseError(HttpResponse response, string error)
{
// Check for WebSocket handshaked status
if (WebSocket.WsHandshaked)
{
OnError(new SocketError());
return;
}
base.OnReceivedResponseError(response, error);
}
#endregion
#region Web socket handlers
public virtual void OnWsConnecting(HttpRequest request) {}
public virtual void OnWsConnected(HttpResponse response) {}
public virtual bool OnWsConnecting(HttpRequest request, HttpResponse response) { return true; }
public virtual void OnWsConnected(HttpRequest request) {}
public virtual void OnWsDisconnecting() {}
public virtual void OnWsDisconnected() {}
public virtual void OnWsReceived(byte[] buffer, long offset, long size) {}
public virtual void OnWsClose(byte[] buffer, long offset, long size, int status = 1000) { CloseAsync(); }
public virtual void OnWsPing(byte[] buffer, long offset, long size) { SendPongAsync(buffer, offset, size); }
public virtual void OnWsPong(byte[] buffer, long offset, long size) {}
public virtual void OnWsError(string error) { OnError(SocketError.SocketError); }
public virtual void OnWsError(SocketError error) { OnError(error); }
#endregion
// Sync connect flag
private bool _syncConnect;
}
}