-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathWebsocketConnection.cs
338 lines (286 loc) · 10.4 KB
/
WebsocketConnection.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
using System.Net.WebSockets;
using Newtonsoft.Json;
using WalletConnectSharp.Common;
using WalletConnectSharp.Common.Utils;
using WalletConnectSharp.Network.Models;
using Websocket.Client;
namespace WalletConnectSharp.Network.Websocket
{
/// <summary>
/// A JSON RPC connection using Websocket.Client library + EventDelegator
/// </summary>
public class WebsocketConnection : IJsonRpcConnection, IModule
{
private WebsocketClient _socket;
private string _url;
private bool _registering;
private Guid _context;
protected bool Disposed;
/// <summary>
/// The Open timeout
/// </summary>
public TimeSpan OpenTimeout = TimeSpan.FromSeconds(60);
/// <summary>
/// The Url to connect to
/// </summary>
public string Url
{
get
{
return _url;
}
}
public bool IsPaused
{
get;
internal set;
}
/// <summary>
/// The name of this websocket connection module
/// </summary>
public string Name
{
get
{
return "websocket-connection";
}
}
/// <summary>
/// The context string of this Websocket module
/// </summary>
public string Context
{
get
{
return _context.ToString();
}
}
public event EventHandler<string> PayloadReceived;
public event EventHandler Closed;
public event EventHandler<Exception> ErrorReceived;
public event EventHandler<object> Opened;
public event EventHandler<Exception> RegisterErrored;
/// <summary>
/// Whether this websocket connection is connected
/// </summary>
public bool Connected => _socket != null && _socket.NativeClient.State == WebSocketState.Open;
/// <summary>
/// Whether this websocket connection is currently connecting
/// </summary>
public bool Connecting
{
get
{
return _registering;
}
}
/// <summary>
/// Create a new websocket connection that will connect to the given URL
/// </summary>
/// <param name="url">The URL to connect to</param>
/// <exception cref="ArgumentException">If the given URL is invalid</exception>
public WebsocketConnection(string url)
{
if (!Validation.IsWsUrl(url))
throw new ArgumentException("Provided URL is not compatible with WebSocket connection: " + url);
_context = Guid.NewGuid();
this._url = url;
}
/// <summary>
/// Open this connection
/// </summary>
public async Task Open()
{
await Register(_url);
}
/// <summary>
/// Open this connection using a string url
/// </summary>
/// <param name="options">Must be a string url. If any other type, then normal Open() is invoked</param>
/// <typeparam name="T">The type of the options. Should always be string</typeparam>
public async Task Open<T>(T options)
{
if (typeof(string).IsAssignableFrom(typeof(T)))
{
await Register(options as string);
}
await Open();
}
private async Task<WebsocketClient> Register(string url)
{
if (!Validation.IsWsUrl(url))
{
throw new ArgumentException("Provided URL is not compatible with WebSocket connection: " + url);
}
if (_registering)
{
TaskCompletionSource<WebsocketClient> registeringTask =
new TaskCompletionSource<WebsocketClient>(TaskCreationOptions.None);
RegisterErrored.ListenOnce((sender, args) =>
{
registeringTask.SetException(args);
});
Opened.ListenOnce((sender, args) =>
{
registeringTask.SetResult((WebsocketClient)args);
});
await registeringTask.Task;
return registeringTask.Task.Result;
}
this._url = url;
this._registering = true;
try
{
_socket = new WebsocketClient(new Uri(_url));
_socket.ReconnectTimeout = null;
await _socket.Start().WithTimeout(OpenTimeout, "Unavailable WS RPC url at " + _url);
OnOpen(_socket);
return _socket;
}
catch (Exception e)
{
this.RegisterErrored?.Invoke(this, e);
OnClose(new DisconnectionInfo(DisconnectionType.Error, WebSocketCloseStatus.Empty, e.Message, null, e));
throw;
}
}
private void OnOpen(WebsocketClient socket)
{
if (socket == null)
return;
socket.MessageReceived.Subscribe(OnPayload);
socket.DisconnectionHappened.Subscribe(OnDisconnect);
this._socket = socket;
this._registering = false;
this.Opened?.Invoke(this, _socket);
}
private void OnDisconnect(DisconnectionInfo obj)
{
if (obj.Exception != null)
this.ErrorReceived?.Invoke(this, obj.Exception);
OnClose(obj);
}
private void OnClose(DisconnectionInfo obj)
{
if (this._socket == null)
return;
//_socket.Dispose();
this._socket = null;
this._registering = false;
this.Closed?.Invoke(this, EventArgs.Empty);
}
private void OnPayload(ResponseMessage obj)
{
string json = null;
switch (obj.MessageType)
{
case WebSocketMessageType.Binary:
return;
case WebSocketMessageType.Text:
json = obj.Text;
break;
case WebSocketMessageType.Close:
return;
}
if (string.IsNullOrWhiteSpace(json)) return;
//Console.WriteLine($"[{Name}] Got payload {json}");
this.PayloadReceived?.Invoke(this, json);
}
/// <summary>
/// Close this connection
/// </summary>
/// <exception cref="IOException">If this connection was already closed</exception>
public async Task Close()
{
if (_socket == null)
throw new IOException("Connection already closed");
await _socket.Stop(WebSocketCloseStatus.NormalClosure, "Close Invoked");
OnClose(new DisconnectionInfo(DisconnectionType.Exit, WebSocketCloseStatus.Empty, "Close Invoked", null,
null));
}
/// <summary>
/// Send a Json RPC request through this websocket connection, using the given context
/// </summary>
/// <param name="requestPayload">The request payload to encode and send</param>
/// <param name="context">The context to use when sending</param>
/// <typeparam name="T">The type of the Json RPC request parameter</typeparam>
public async Task SendRequest<T>(IJsonRpcRequest<T> requestPayload, object context)
{
if (_socket == null)
_socket = await Register(_url);
try
{
_socket.Send(JsonConvert.SerializeObject(requestPayload));
}
catch (Exception e)
{
OnError<T>(requestPayload, e);
}
}
/// <summary>
/// Send a Json RPC response through this websocket connection, using the given context
/// </summary>
/// <param name="requestPayload">The response payload to encode and send</param>
/// <param name="context">The context to use when sending</param>
/// <typeparam name="T">The type of the Json RPC response result</typeparam>
public async Task SendResult<T>(IJsonRpcResult<T> requestPayload, object context)
{
if (_socket == null)
_socket = await Register(_url);
try
{
_socket.Send(JsonConvert.SerializeObject(requestPayload));
}
catch (Exception e)
{
OnError<T>(requestPayload, e);
}
}
/// <summary>
/// Send a Json RPC error response through this websocket connection, using the given context
/// </summary>
/// <param name="requestPayload">The error response payload to encode and send</param>
/// <param name="context">The context to use when sending</param>
public async Task SendError(IJsonRpcError requestPayload, object context)
{
if (_socket == null)
_socket = await Register(_url);
try
{
_socket.Send(JsonConvert.SerializeObject(requestPayload));
}
catch (Exception e)
{
OnError<object>(requestPayload, e);
}
}
public async void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (Disposed)
return;
if (disposing)
{
_socket.Dispose();
}
Disposed = true;
}
private const string AddressNotFoundError = "getaddrinfo ENOTFOUND";
private const string ConnectionRefusedError = "connect ECONNREFUSED";
private void OnError<T>(IJsonRpcPayload ogPayload, Exception e)
{
var exception = e.Message.Contains(AddressNotFoundError) || e.Message.Contains(ConnectionRefusedError)
? new IOException("Unavailable WS RPC url at " + _url)
: e;
var message = exception.Message;
var payload = new JsonRpcResponse<T>(ogPayload.Id,
new Error() { Code = exception.HResult, Data = null, Message = message }, default(T));
//Trigger the payload event, converting the new JsonRpcResponse object to JSON string
this.PayloadReceived?.Invoke(this, JsonConvert.SerializeObject(payload));
}
}
}