-
Notifications
You must be signed in to change notification settings - Fork 11
/
UwpWebsocket.cs
386 lines (327 loc) · 9.7 KB
/
UwpWebsocket.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
#if WINDOWS_UWP
#define CUSTOM_WEBSOCKET
#endif
#if CUSTOM_WEBSOCKET
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Windows.Networking.Sockets;
using System.Threading.Tasks;
using Windows.Web;
using System; // System.Uri
using System.IO;
using Windows.System.Threading;
using System.Runtime.InteropServices.WindowsRuntime; // AsBuffer
using Windows.Storage.Streams; // DataWriter
// https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/WebSocket/cs/Scenario2_Binary.xaml.cs
namespace WebSocketSharp
{
public enum Opcode
{
TEXT,
BINARY
};
public class CloseEventArgs : EventArgs
{
public ushort Code { get; }
public string Reason { get; }
public bool WasClean { get; }
}
public class ErrorEventArgs : EventArgs
{
public string Message;
public ErrorEventArgs(string err)
{
this.Message = err;
}
}
public class MessageEventArgs : EventArgs
{
public string Data = null;
public byte[] RawData = null;
public Opcode Type
{
get
{
return ( RawData == null ) ? Opcode.TEXT : Opcode.BINARY;
}
}
public MessageEventArgs(string data)
{
this.Data = data;
}
public MessageEventArgs(byte[] data)
{
this.RawData = data;
}
}
public class WebSocket
{
MessageWebSocket socket;
Uri url;
DataWriter MessageWriter;
System.Threading.Mutex SendLock;
// one shared data writer; http://stackoverflow.com/a/39653730
public WebSocket(string Url)
{
url = TryGetUri( Url );
socket = new MessageWebSocket();
socket.MessageReceived += OnMessageRecieved;
socket.Closed += OnClosed;
MessageWriter = new DataWriter( socket.OutputStream );
SendLock = new System.Threading.Mutex();
}
public event EventHandler<CloseEventArgs> OnClose;
public event EventHandler<ErrorEventArgs> OnError;
public event EventHandler<MessageEventArgs> OnMessage;
public event EventHandler OnOpen;
public bool IsAlive
{
get
{
return true;
}
}
void OnMessageRecieved(MessageWebSocket FromSocket, MessageWebSocketMessageReceivedEventArgs InputMessage)
{
MessageEventArgs OutputMessage = null;
if (InputMessage.MessageType == SocketMessageType.Utf8)
{
var stringLength = InputMessage.GetDataReader().UnconsumedBufferLength;
string receivedMessage = InputMessage.GetDataReader().ReadString(stringLength);
OutputMessage = new MessageEventArgs( receivedMessage );
}
else
{
// todo!
//OutputMessage = new MessageEventArgs( InputMessage.GetDataStream().ReadAsync );
}
OnMessage.Invoke( this, OutputMessage );
}
async Task SendAsyncTask(string message)
{
// "flush before changing type"
await MessageWriter.FlushAsync();
socket.Control.MessageType = SocketMessageType.Utf8;
MessageWriter.WriteString(message);
await MessageWriter.StoreAsync();
}
async Task SendAsyncTask(byte[] Data)
{
// "flush before changing type"
await MessageWriter.FlushAsync();
socket.Control.MessageType = SocketMessageType.Binary;
MessageWriter.WriteBytes(Data);
await MessageWriter.StoreAsync();
}
public void Send(string data)
{
lock(SendLock)
{
var SendTask = SendAsyncTask( data );
SendTask.Wait();
};
}
public void Send(byte[] data)
{
lock(SendLock)
{
var SendTask = SendAsyncTask( data );
SendTask.Wait();
};
}
public void SendAsync(string data, System.Action<bool> completed)
{
ThreadPool.RunAsync( (Handler)=>
{
lock(SendLock)
{
var SendTask = SendAsyncTask( data );
SendTask.Wait();
completed.Invoke(true);
};
//await Send_Async(data);
} );
// todo: launch a task, wait and then send completed
//completed.Invoke(true);
}
public void SendAsync(byte[] data, System.Action<bool> completed)
{
ThreadPool.RunAsync( (Handler)=>
{
lock(SendLock)
{
var SendTask = SendAsyncTask( data );
SendTask.Wait();
completed.Invoke(true);
};
//await Send_Async(data);
});
//completed.Invoke(true);
}
public void ConnectAsync ()
{
StartAsync();
}
public void Close()
{
if (socket != null)
{
try
{
socket.Close(1000, "Closed due to user request.");
}
catch (Exception ex)
{
OnError.Invoke( this, new ErrorEventArgs(ex.Message) );
}
socket = null;
}
}
void OnClosed(IWebSocket Socket,WebSocketClosedEventArgs Event)
{
}
void OnRecvData(byte[] Data)
{
}
static System.Uri TryGetUri(string uriString)
{
Uri webSocketUri;
if (!Uri.TryCreate(uriString.Trim(), UriKind.Absolute, out webSocketUri))
throw new System.Exception("Error: Invalid URI");
// Fragments are not allowed in WebSocket URIs.
if (!String.IsNullOrEmpty(webSocketUri.Fragment))
throw new System.Exception("Error: URI fragments not supported in WebSocket URIs.");
// Uri.SchemeName returns the canonicalized scheme name so we can use case-sensitive, ordinal string
// comparison.
if ((webSocketUri.Scheme != "ws") && (webSocketUri.Scheme != "wss"))
throw new System.Exception("Error: WebSockets only support ws:// and wss:// schemes.");
return webSocketUri;
}
async Task StartAsync()
{
/*
// If we are connecting to wss:// endpoint, by default, the OS performs validation of
// the server certificate based on well-known trusted CAs. We can perform additional custom
// validation if needed.
if (SecureWebSocketCheckBox.IsChecked == true)
{
// WARNING: Only test applications should ignore SSL errors.
// In real applications, ignoring server certificate errors can lead to Man-In-The-Middle
// attacks. (Although the connection is secure, the server is not authenticated.)
// Note that not all certificate validation errors can be ignored.
// In this case, we are ignoring these errors since the certificate assigned to the localhost
// URI is self-signed and has subject name = fabrikam.com
streamWebSocket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
streamWebSocket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
// Add event handler to listen to the ServerCustomValidationRequested event. This enables performing
// custom validation of the server certificate. The event handler must implement the desired
// custom certificate validation logic.
streamWebSocket.ServerCustomValidationRequested += OnServerCustomValidationRequested;
// Certificate validation is meaningful only for secure connections.
if (server.Scheme != "wss")
{
AppendOutputLine("Note: Certificate validation is performed only for the wss: scheme.");
}
}
*/
try
{
await socket.ConnectAsync(url);
}
catch (Exception ex) // For debugging
{
socket.Dispose();
socket = null;
// gr: do error!
OnError.Invoke( this, new ErrorEventArgs(ex.Message) );
return;
}
OnOpen.Invoke( this, null );
}
/*
// Continuously read incoming data. For reading data we'll show how to use activeSocket.InputStream.AsStream()
// to get a .NET stream. Alternatively you could call readBuffer.AsBuffer() to use IBuffer with
// activeSocket.InputStream.ReadAsync.
private async Task ReceiveDataAsync(StreamWebSocket activeSocket)
{
Stream readStream = socket.InputStream.AsStreamForRead();
try
{
byte[] readBuffer = new byte[1000];
while (true)
{
if (socket != activeSocket)
{
// Our socket is no longer active. Stop reading.
return;
}
// gr: work out where messages split!
int BytesRead = await readStream.ReadAsync(readBuffer, 0, readBuffer.Length);
// Do something with the data.
// This sample merely reports that the data was received.
var PartBuffer = new byte[BytesRead];
Array.Copy(readBuffer, PartBuffer, BytesRead);
OnRecvData( PartBuffer );
}
}
catch (Exception ex)
{
WebErrorStatus status = WebSocketError.GetStatus(ex.GetBaseException().HResult);
switch (status)
{
case WebErrorStatus.OperationCanceled:
OnError.Invoke( this, new ErrorEventArgs("Background read canceled.") );
break;
default:
OnError.Invoke( this, new ErrorEventArgs("Read error: " + status + "; " + ex.Message) );
break;
}
}
}
private async Task SendDataAsync(StreamWebSocket activeSocket)
{
try
{
// Send until the socket gets closed/stopped
while (true)
{
if (socket != activeSocket)
{
// Our socket is no longer active. Stop sending.
return;
}
if ( SendQueue == null || SendQueue.Count == 0 )
{
// sleep thread plx
await Task.Delay( TimeSpan.FromMilliseconds(500) );
continue;
}
byte[] SendBuffer = null;
lock(SendQueue)
{
SendBuffer = SendQueue[0];
SendQueue.RemoveAt(0);
}
await activeSocket.OutputStream.WriteAsync( SendBuffer.AsBuffer() );
}
}
catch (Exception ex)
{
WebErrorStatus status = WebSocketError.GetStatus(ex.GetBaseException().HResult);
switch (status)
{
case WebErrorStatus.OperationCanceled:
OnError.Invoke( this, new ErrorEventArgs("Background write canceled.") );
break;
default:
OnError.Invoke( this, new ErrorEventArgs("Error " + status + " exception: " + ex.Message ) );
break;
}
}
}
*/
}
};
#endif