From 24abed6bd823ea7ba1c0bcd78bcc964fbc900470 Mon Sep 17 00:00:00 2001 From: tq <1916474859@qq,com> Date: Sat, 28 Sep 2024 17:18:36 +0800 Subject: [PATCH] =?UTF-8?q?led=E8=BF=9E=E6=8E=A5=E8=BF=98=E5=8E=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Led/LedClient.cs | 52 ++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/Service/Led/LedClient.cs b/Service/Led/LedClient.cs index 53e20fd..6047cea 100644 --- a/Service/Led/LedClient.cs +++ b/Service/Led/LedClient.cs @@ -135,45 +135,39 @@ public class LedClient _socket = null; } } - - private static readonly object Locker = new object(); private static bool InnerSend(byte[] bytes) { - lock (Locker) + if (_socket?.Connected ?? false) { + try + { + _socket.Send(bytes); + Log.Info($"Sent {BitUtls.BytesToHexStr(bytes)}"); + return true; + } + catch (SocketException ex) + { + Log.Error($"Failed to send data. Reason: {ex.Message}"); + + return false; + } + } + else + { + Connect(); if (_socket?.Connected ?? false) { - try - { - _socket.Send(bytes); - Log.Info($"Sent {BitUtls.BytesToHexStr(bytes)}"); - return true; - } - catch (SocketException ex) - { - Log.Error($"Failed to send data. Reason: {ex.Message}"); - - return false; - } + _socket.Send(bytes); + Log.Info($"Sent {BitUtls.BytesToHexStr(bytes)} after reconnect"); + return true; } else { - - Connect(); - if (_socket?.Connected ?? false) - { - _socket.Send(bytes); - Log.Info($"Sent {BitUtls.BytesToHexStr(bytes)} after reconnect"); - return true; - } - else - { - Log.Error($"Failed to reconnect and send data."); - return false; - } - } + Log.Error($"Failed to reconnect and send data."); + return false; + } } }