led连接还原

master
tq 2 months ago
parent b3b36d93e4
commit 24abed6bd8

@ -136,43 +136,37 @@ public class LedClient
} }
} }
private static readonly object Locker = new object();
private static bool InnerSend(byte[] bytes) 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) if (_socket?.Connected ?? false)
{ {
try _socket.Send(bytes);
{ Log.Info($"Sent {BitUtls.BytesToHexStr(bytes)} after reconnect");
_socket.Send(bytes); return true;
Log.Info($"Sent {BitUtls.BytesToHexStr(bytes)}");
return true;
}
catch (SocketException ex)
{
Log.Error($"Failed to send data. Reason: {ex.Message}");
return false;
}
} }
else else
{ {
Log.Error($"Failed to reconnect and send data.");
Connect(); return false;
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;
}
} }
} }
} }

Loading…
Cancel
Save