From 4b4b4e16a80c2d188eccdc6441ab4f7dc4e84ea8 Mon Sep 17 00:00:00 2001 From: Jason Elie Bou Kheir <5115126+jasonboukheir@users.noreply.github.com> Date: Tue, 20 Dec 2022 12:28:39 -0800 Subject: [PATCH] fix(walletconnect): handle server error messages in payloads Also sets default ping-pong to infinity (timespan = 0) fix #173 --- Algorand.Unity.Package/Assets/Resources.meta | 8 ------ .../JsonRpcClient.cs | 5 ++++ .../NativeWebSocketClient.cs | 25 +++++++++++++------ 3 files changed, 22 insertions(+), 16 deletions(-) delete mode 100644 Algorand.Unity.Package/Assets/Resources.meta diff --git a/Algorand.Unity.Package/Assets/Resources.meta b/Algorand.Unity.Package/Assets/Resources.meta deleted file mode 100644 index 24a413290..000000000 --- a/Algorand.Unity.Package/Assets/Resources.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 5b22df488ba6442d7b4b7c40d5d0401e -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Runtime/Algorand.Unity.WalletConnect/JsonRpcClient.cs b/Runtime/Algorand.Unity.WalletConnect/JsonRpcClient.cs index 90c6093aa..fa039c6d5 100644 --- a/Runtime/Algorand.Unity.WalletConnect/JsonRpcClient.cs +++ b/Runtime/Algorand.Unity.WalletConnect/JsonRpcClient.cs @@ -1,6 +1,7 @@ using System; using System.Net.WebSockets; using System.Threading; +using Algorand.Unity.Json; using Algorand.Unity.WebSocket; using Cysharp.Threading.Tasks; using UnityEngine; @@ -188,6 +189,10 @@ private async UniTaskVoid PollJsonRpcMessages(CancellationToken cancellationToke Debug.LogWarning(inner.ToString()); throw; } + catch (JsonReadException ex) + { + Debug.LogWarning($"Response from JsonRpcServer was not Json:\n\"{System.Text.Encoding.UTF8.GetString(evt.Payload)}\"\nError:\n{ex}"); + } break; case WebSocketEventType.Error: Debug.LogWarning(evt.Error); diff --git a/Runtime/Algorand.Unity.WebSocket/NativeWebSocketClient.cs b/Runtime/Algorand.Unity.WebSocket/NativeWebSocketClient.cs index 1b49330af..a07b8017f 100644 --- a/Runtime/Algorand.Unity.WebSocket/NativeWebSocketClient.cs +++ b/Runtime/Algorand.Unity.WebSocket/NativeWebSocketClient.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Net.WebSockets; using System.Threading; using Cysharp.Threading.Tasks; +using UnityEngine; namespace Algorand.Unity.WebSocket { @@ -12,6 +14,7 @@ public class NativeWebSocketClient : IWebSocketClient private ClientWebSocket Connection; private readonly Uri uri; + private readonly ArraySegment buffer; public Queue EventQueue { get; } = new Queue(); public WebSocketState ReadyState => Connection?.State ?? WebSocketState.Closed; @@ -20,6 +23,8 @@ public NativeWebSocketClient(string url) { uri = new Uri(url); Connection = new ClientWebSocket(); + Connection.Options.KeepAliveInterval = TimeSpan.Zero; + buffer = System.Net.WebSockets.WebSocket.CreateClientBuffer(8 * 1024, 8 * 1024); } public void Connect() @@ -48,23 +53,27 @@ private async UniTaskVoid ConnectAsync() { await Connection.ConnectAsync(uri, CancellationToken.None); OnOpen(); - var buffer = new byte[1024 * 4]; try { while (true) { - var result = await Connection.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None); - if (Connection.CloseStatus.HasValue) + using var ms = new MemoryStream(); + WebSocketReceiveResult result; + do + { + result = await Connection.ReceiveAsync(buffer, CancellationToken.None); + ms.Write(buffer.Array, buffer.Offset, result.Count); + } while (!result.EndOfMessage); + + ms.Seek(0, SeekOrigin.Begin); + + if (Connection.CloseStatus.HasValue || result.CloseStatus.HasValue) { OnClose(); break; } - var payload = new byte[result.Count]; - for (var i = 0; i < payload.Length; i++) - { - payload[i] = buffer[i]; - } + var payload = ms.ToArray(); OnMessage(payload); } }