-
Notifications
You must be signed in to change notification settings - Fork 62
/
JsonRpcProvider.cs
310 lines (261 loc) · 10.6 KB
/
JsonRpcProvider.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
using Newtonsoft.Json;
using WalletConnectSharp.Common;
using WalletConnectSharp.Common.Events;
using WalletConnectSharp.Common.Logging;
using WalletConnectSharp.Common.Model.Errors;
using WalletConnectSharp.Network.Models;
namespace WalletConnectSharp.Network
{
/// <summary>
/// A full implementation of the IJsonRpcProvider interface using the EventDelegator
/// </summary>
public class JsonRpcProvider : IJsonRpcProvider, IModule
{
private IJsonRpcConnection _connection;
private bool _hasRegisteredEventListeners;
private Guid _context;
private bool _connectingStarted;
private TaskCompletionSource<bool> Connecting = new TaskCompletionSource<bool>();
private long _lastId;
public event EventHandler<JsonRpcPayload> PayloadReceived;
public event EventHandler<IJsonRpcConnection> Connected;
public event EventHandler Disconnected;
public event EventHandler<Exception> ErrorReceived;
public event EventHandler<string> RawMessageReceived;
private GenericEventHolder jsonResponseEventHolder = new();
/// <summary>
/// Whether the provider is currently connecting or not
/// </summary>
public bool IsConnecting
{
get
{
return _connectingStarted && !Connecting.Task.IsCompleted;
}
}
/// <summary>
/// The current Connection for this provider
/// </summary>
public IJsonRpcConnection Connection
{
get
{
return _connection;
}
}
/// <summary>
/// The name of this provider module
/// </summary>
public string Name
{
get
{
return "json-rpc-provider";
}
}
/// <summary>
/// The context string of this provider module
/// </summary>
public string Context
{
get
{
//TODO Get context from logger
return _context.ToString();
}
}
/// <summary>
/// Create a new JsonRpcProvider with the given connection
/// </summary>
/// <param name="connection">The IJsonRpcConnection to use</param>
public JsonRpcProvider(IJsonRpcConnection connection)
{
_context = Guid.NewGuid();
this._connection = connection;
if (this._connection.Connected)
{
RegisterEventListeners();
}
}
/// <summary>
/// Connect this provider using the given connection string
/// </summary>
/// <param name="connection">The connection string to use to connect, usually a URI</param>
public async Task Connect(string connection)
{
if (this._connection.Connected)
{
await this._connection.Close();
}
// Reset connecting task
Connecting = new TaskCompletionSource<bool>();
_connectingStarted = true;
WCLogger.Log("[JsonRpcProvider] Opening connection");
await this._connection.Open(connection);
FinalizeConnection(this._connection);
}
/// <summary>
/// Connect this provider with the given IJsonRpcConnection connection
/// </summary>
/// <param name="connection">The connection object to use to connect</param>
public async Task Connect(IJsonRpcConnection connection)
{
if (this._connection.Url == connection.Url && connection.Connected) return;
if (this._connection.Connected)
{
WCLogger.Log("Current connection still open, closing connection");
await this._connection.Close();
}
// Reset connecting task
Connecting = new TaskCompletionSource<bool>();
_connectingStarted = true;
WCLogger.Log("[JsonRpcProvider] Opening connection");
await connection.Open();
FinalizeConnection(connection);
}
private void FinalizeConnection(IJsonRpcConnection connection)
{
WCLogger.Log("[JsonRpcProvider] Finalizing Connection, registering event listeners");
this._connection = connection;
RegisterEventListeners();
this.Connected?.Invoke(this, connection);
Connecting.SetResult(true);
_connectingStarted = false;
}
/// <summary>
/// Connect this provider using the backing IJsonRpcConnection that was set in the
/// constructor
/// </summary>
public async Task Connect()
{
if (_connection == null)
throw new Exception("No connection is set");
WCLogger.Log("[JsonRpcProvider] Connecting with given connection object");
await Connect(_connection);
}
/// <summary>
/// Disconnect this provider
/// </summary>
public async Task Disconnect()
{
await _connection.Close();
// Reset connecting task
Connecting = new TaskCompletionSource<bool>();
}
/// <summary>
/// Send a request and wait for a response. The response is returned as a task and can
/// be awaited
/// </summary>
/// <param name="requestArgs">The request arguments to send</param>
/// <param name="context">The context to use during sending</param>
/// <typeparam name="T">The type of request arguments</typeparam>
/// <typeparam name="TR">The type of the response</typeparam>
/// <returns>A Task that will resolve when a response is received</returns>
public async Task<TR> Request<T, TR>(IRequestArguments<T> requestArgs, object context = null)
{
WCLogger.Log("[JsonRpcProvider] Checking if connected");
if (IsConnecting)
await Connecting.Task;
else if (!_connectingStarted && !_connection.Connected)
{
WCLogger.Log("[JsonRpcProvider] Not connected, connecting now");
await Connect(_connection);
}
long? id = null;
if (requestArgs is IJsonRpcRequest<T>)
{
id = ((IJsonRpcRequest<T>)requestArgs).Id;
if (id == 0)
id = null; // An id of 0 is null
}
var request = new JsonRpcRequest<T>(requestArgs.Method, requestArgs.Params, id);
TaskCompletionSource<TR> requestTask = new TaskCompletionSource<TR>(TaskCreationOptions.None);
jsonResponseEventHolder.OfType<string>()[request.Id.ToString()] += (sender, responseJson) =>
{
if (requestTask.Task.IsCompleted)
return;
var result = JsonConvert.DeserializeObject<JsonRpcResponse<TR>>(responseJson);
if (result.Error != null)
{
requestTask.SetException(new IOException(result.Error.Message));
}
else
{
requestTask.SetResult(result.Result);
}
};
jsonResponseEventHolder.OfType<WalletConnectException>()[request.Id.ToString()] += (sender, exception) =>
{
if (requestTask.Task.IsCompleted)
return;
//Console.WriteLine($"[{Name}] Got Response Error {exception}");
if (exception != null)
{
requestTask.SetException(exception);
}
};
_lastId = request.Id;
WCLogger.Log($"[JsonRpcProvider] Sending request {request.Method} with data {JsonConvert.SerializeObject(request)}");
//Console.WriteLine($"[{Name}] Sending request {request.Method} with data {JsonConvert.SerializeObject(request)}");
await _connection.SendRequest(request, context);
WCLogger.Log("[JsonRpcProvider] Awaiting request result");
await requestTask.Task;
return requestTask.Task.Result;
}
protected void RegisterEventListeners()
{
if (_hasRegisteredEventListeners) return;
WCLogger.Log($"[JsonRpcProvider] Registering event listeners on connection object with context {_connection.ToString()}");
_connection.PayloadReceived += OnPayload;
_connection.Closed += OnConnectionDisconnected;
_connection.ErrorReceived += OnConnectionError;
/*_connection.On<string>("payload", OnPayload);
_connection.On<object>("close", OnConnectionDisconnected);
_connection.On<Exception>("error", OnConnectionError);*/
_hasRegisteredEventListeners = true;
}
private void OnConnectionError(object sender, Exception e)
{
this.ErrorReceived?.Invoke(this, e);
}
private void OnConnectionDisconnected(object sender, EventArgs e)
{
this.Disconnected?.Invoke(this, e);
}
private void OnPayload(object sender, string json)
{
WCLogger.Log($"[JsonRpcProvider] Got payload {json}");
var payload = JsonConvert.DeserializeObject<JsonRpcPayload>(json);
if (payload == null)
{
throw new IOException("Invalid payload: " + json);
}
if (payload.Id == 0)
payload.Id = _lastId;
WCLogger.Log($"[JsonRpcProvider] Payload has ID {payload.Id}");
this.PayloadReceived?.Invoke(this, payload);
if (payload.IsRequest)
{
this.RawMessageReceived?.Invoke(this, json);
}
else
{
if (payload.IsError)
{
var errorPayload = JsonConvert.DeserializeObject<JsonRpcError>(json);
jsonResponseEventHolder.OfType<WalletConnectException>()[payload.Id.ToString()](this,
errorPayload.Error.ToException());
}
else
{
WCLogger.Log($"Triggering event for ID {payload.Id.ToString()}");
jsonResponseEventHolder.OfType<string>()[payload.Id.ToString()](this, json);
}
}
}
public void Dispose()
{
_connection?.Dispose();
}
}
}