Skip to content

Commit

Permalink
Implement data handling in RPCClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
zadockmaloba committed May 2, 2024
1 parent f80b7da commit 91aa178
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions RPCClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections;
using System.Text.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Text.Json.Nodes;

namespace jsonrpc;

Expand Down Expand Up @@ -85,26 +86,40 @@ public async Task PostRequests(List<RPCRequest> requests, bool async)

private void HandleData(byte[] data, List<RPCRequest> requests)
{
Debug.WriteLine("Received response, handling data");
try
{
var results = Encoding.UTF8.GetString(data);

var json_res = JsonSerializer.Deserialize<object>(results);
var json_res = JsonSerializer.Deserialize<Dictionary<string, object>>(results);

foreach (var request in requests)
{

if (request.Callback != null)
if (!json_res.TryGetValue("error", out var value))
{
request.Callback?.Invoke(new RPCResponse
{
Id = json_res["id"].ToString(),
Version = json_res["jsonrpc"].ToString(),
Result = json_res["result"]
});
}
else
{
request.Callback(new RPCResponse { });
var temp = value.ToString();
request.Callback?.Invoke(new RPCResponse
{
Id = json_res["id"].ToString(),
Error = new RPCError(JsonSerializer.Deserialize<Dictionary<string, object>>(temp)),
Version = json_res["jsonrpc"].ToString()
});
}
}
// Process results and invoke callbacks
}
catch (Exception ex)
{
Debug.WriteLine($"Error handling data: {ex.Message}");
HandleFailedRequests(requests, new RPCError(RPCErrorCode.ParseError, "Received invalid JSON response", Encoding.UTF8.GetString(data)));
HandleFailedRequests(requests, new RPCError(code: RPCErrorCode.ParseError, ex.Message , data: Encoding.UTF8.GetString(data)));
}
}

Expand Down

0 comments on commit 91aa178

Please sign in to comment.