From 94b8184f7b7ac892feac369b13489990f1c8cd10 Mon Sep 17 00:00:00 2001 From: Jan Korf Date: Sat, 30 Apr 2022 16:14:17 +0200 Subject: [PATCH] Added handling for websocket send failing --- .../Sockets/SocketConnection.cs | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index 297c95d..25d9298 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -254,12 +254,12 @@ namespace CryptoExchange.Net.Sockets private void StartProcessingTask() { - log.Write(LogLevel.Trace, "Starting processing task"); + log.Write(LogLevel.Trace, $"Starting {SocketId} processing task"); _socketProcessReconnectTask = Task.Run(async () => { await _socket.ProcessAsync().ConfigureAwait(false); await ReconnectAsync().ConfigureAwait(false); - log.Write(LogLevel.Trace, "Processing task finished"); + log.Write(LogLevel.Trace, $"Processing {SocketId} task finished"); }); } @@ -566,7 +566,10 @@ namespace CryptoExchange.Net.Sockets { pendingRequests.Add(pending); } - Send(obj); + var sendOk = Send(obj); + if(!sendOk) + pending.Fail(); + return pending.Event.WaitAsync(timeout); } @@ -576,22 +579,30 @@ namespace CryptoExchange.Net.Sockets /// The type of the object to send /// The object to send /// How null values should be serialized - public virtual void Send(T obj, NullValueHandling nullValueHandling = NullValueHandling.Ignore) + public virtual bool Send(T obj, NullValueHandling nullValueHandling = NullValueHandling.Ignore) { if(obj is string str) - Send(str); + return Send(str); else - Send(JsonConvert.SerializeObject(obj, Formatting.None, new JsonSerializerSettings { NullValueHandling = nullValueHandling })); + return Send(JsonConvert.SerializeObject(obj, Formatting.None, new JsonSerializerSettings { NullValueHandling = nullValueHandling })); } /// /// Send string data over the websocket connection /// /// The data to send - public virtual void Send(string data) + public virtual bool Send(string data) { log.Write(LogLevel.Trace, $"Socket {SocketId} sending data: {data}"); - _socket.Send(data); + try + { + _socket.Send(data); + return true; + } + catch(Exception) + { + return false; + } } ///