Add desktop diagnostics and resilient message handling
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Text.Json;
|
||||
using Fleck;
|
||||
@@ -44,6 +45,7 @@ public sealed class WebSocketServerService : IDisposable
|
||||
{
|
||||
Port = port;
|
||||
LocalIpAddress = ResolveLocalIpAddress();
|
||||
AppLogger.Info($"Starting WebSocket server on 0.0.0.0:{Port}. Local IP: {LocalIpAddress}");
|
||||
|
||||
server = new WebSocketServer($"ws://0.0.0.0:{Port}");
|
||||
server.Start(socket =>
|
||||
@@ -55,6 +57,7 @@ public sealed class WebSocketServerService : IDisposable
|
||||
clients.Add(socket);
|
||||
}
|
||||
|
||||
AppLogger.Info($"Client connected: {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}");
|
||||
StatusChanged?.Invoke(this, EventArgs.Empty);
|
||||
};
|
||||
socket.OnClose = () =>
|
||||
@@ -64,6 +67,12 @@ public sealed class WebSocketServerService : IDisposable
|
||||
clients.Remove(socket);
|
||||
}
|
||||
|
||||
AppLogger.Info($"Client disconnected: {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}");
|
||||
StatusChanged?.Invoke(this, EventArgs.Empty);
|
||||
};
|
||||
socket.OnError = exception =>
|
||||
{
|
||||
AppLogger.Error("WebSocket connection error.", exception);
|
||||
StatusChanged?.Invoke(this, EventArgs.Empty);
|
||||
};
|
||||
socket.OnMessage = HandleRawMessage;
|
||||
@@ -91,26 +100,42 @@ public sealed class WebSocketServerService : IDisposable
|
||||
{
|
||||
try
|
||||
{
|
||||
AppLogger.Info($"Received message: {rawMessage}");
|
||||
var message = JsonSerializer.Deserialize<SyncMessage>(rawMessage, JsonOptions);
|
||||
if (message is null)
|
||||
{
|
||||
AppLogger.Info("Ignored empty sync message.");
|
||||
return;
|
||||
}
|
||||
|
||||
messageHandler.Handle(message);
|
||||
}
|
||||
catch (JsonException)
|
||||
catch (JsonException exception)
|
||||
{
|
||||
return;
|
||||
AppLogger.Error($"Invalid sync message: {rawMessage}", exception);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
AppLogger.Error($"Failed to handle sync message: {rawMessage}", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static string ResolveLocalIpAddress()
|
||||
{
|
||||
var host = Dns.GetHostEntry(Dns.GetHostName());
|
||||
var address = host.AddressList.FirstOrDefault(
|
||||
address => address.AddressFamily == AddressFamily.InterNetwork
|
||||
&& !IPAddress.IsLoopback(address));
|
||||
var address = NetworkInterface.GetAllNetworkInterfaces()
|
||||
.Where(networkInterface =>
|
||||
networkInterface.OperationalStatus == OperationalStatus.Up
|
||||
&& networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
|
||||
.Select(networkInterface => networkInterface.GetIPProperties())
|
||||
.Where(properties => properties.GatewayAddresses.Any(
|
||||
gateway => gateway.Address.AddressFamily == AddressFamily.InterNetwork
|
||||
&& !IPAddress.Any.Equals(gateway.Address)))
|
||||
.SelectMany(properties => properties.UnicastAddresses)
|
||||
.Select(unicast => unicast.Address)
|
||||
.FirstOrDefault(address =>
|
||||
address.AddressFamily == AddressFamily.InterNetwork
|
||||
&& !IPAddress.IsLoopback(address)
|
||||
&& !address.ToString().StartsWith("169.254.", StringComparison.Ordinal));
|
||||
|
||||
return address?.ToString() ?? "127.0.0.1";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user