Updated
This commit is contained in:
1 parent
2c5b69bf2c
commit
22dee59f47
132 files changed
+19653
-7192
No files matched your search
@@ -0,0 +1,698 @@
|
||||
using EonaCat.Connections;
|
||||
using EonaCat.Connections.EventArguments;
|
||||
using EonaCat.Connections.Helpers;
|
||||
using EonaCat.Connections.Models;
|
||||
using EonaCat.Connections.gRPC.Proto;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
using System.Security.Authentication;
|
||||
using System.Text;
|
||||
using ErrorEventArgs = EonaCat.Connections.EventArguments.ErrorEventArgs;
|
||||
|
||||
namespace EonaCat.Connections.gRPC
|
||||
{
|
||||
public class GrpcNetworkServer : IDisposable, IAsyncDisposable
|
||||
{
|
||||
public static event EventHandler<LogMessage> OnLog;
|
||||
private readonly Configuration _config;
|
||||
private readonly Stats _stats;
|
||||
private readonly ConcurrentDictionary<string, GrpcClientConnection> _clients = new();
|
||||
private readonly ConcurrentDictionary<string, DateTime> _lastPingTimes = new();
|
||||
private WebApplication? _app;
|
||||
private CancellationTokenSource? _serverCts;
|
||||
private Task? _serverTask;
|
||||
private Task? _pingTask;
|
||||
private Task? _pongTask;
|
||||
private Task? _idleTask;
|
||||
private bool _disposed;
|
||||
|
||||
public bool IsStarted => _serverCts != null && !_serverCts.IsCancellationRequested;
|
||||
public int ActiveConnections => GetActiveConnectionCount();
|
||||
public int MaxConnections => _config.MaxConnections;
|
||||
public string IpAddress => _config.Host;
|
||||
public int Port => _config.Port;
|
||||
public long BytesSent => _stats.BytesSent;
|
||||
public long BytesReceived => _stats.BytesReceived;
|
||||
public long MessagesSent => _stats.MessagesSent;
|
||||
public long MessagesReceived => _stats.MessagesReceived;
|
||||
public double MessagesPerSecond => _stats.MessagesPerSecond;
|
||||
public long TotalConnections => _stats.TotalConnections;
|
||||
public long DroppedConnections => _stats.DroppedConnections;
|
||||
public TimeSpan Uptime => _stats.Uptime;
|
||||
public DateTime StartTime => _stats.StartTime;
|
||||
|
||||
public HealthApiServer HealthApi { get; }
|
||||
|
||||
public event EventHandler<ConnectionEventArgs>? OnConnected;
|
||||
public event EventHandler<DataReceivedEventArgs>? OnDataReceived;
|
||||
public event EventHandler<ConnectionEventArgs>? OnDisconnected;
|
||||
public event EventHandler<ErrorEventArgs>? OnGeneralError;
|
||||
public event EventHandler<PingEventArgs>? OnClientPingResponse;
|
||||
public event EventHandler<PingEventArgs>? OnPongMissed;
|
||||
public GrpcNetworkServer(Configuration config)
|
||||
{
|
||||
_config = config ?? throw new ArgumentNullException(nameof(config));
|
||||
_stats = new Stats { StartTime = DateTime.UtcNow };
|
||||
HealthApi = new HealthApiServer(BuildHealthJson, BuildStatusJson);
|
||||
}
|
||||
|
||||
public Stats GetStats()
|
||||
{
|
||||
_stats.ActiveConnections = GetActiveConnectionCount();
|
||||
return _stats;
|
||||
}
|
||||
|
||||
private int GetActiveConnectionCount()
|
||||
{
|
||||
if (_clients.IsEmpty)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int activeCount = 0;
|
||||
foreach (var client in _clients.Values)
|
||||
{
|
||||
if (client?.IsConnected == true)
|
||||
{
|
||||
activeCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return activeCount;
|
||||
}
|
||||
|
||||
internal void TrackDisconnect(DisconnectReason reason)
|
||||
{
|
||||
if (reason == DisconnectReason.Error
|
||||
|| reason == DisconnectReason.Timeout
|
||||
|| reason == DisconnectReason.SSLError
|
||||
|| reason == DisconnectReason.NoPongReceived
|
||||
|| reason == DisconnectReason.ProtocolError)
|
||||
{
|
||||
_stats.IncrementDroppedConnections();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
_serverCts?.Dispose();
|
||||
_serverCts = new CancellationTokenSource();
|
||||
|
||||
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
|
||||
{
|
||||
Args = Array.Empty<string>()
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton(this);
|
||||
builder.Services.AddGrpc(options =>
|
||||
{
|
||||
options.MaxReceiveMessageSize = _config.MAX_MESSAGE_SIZE;
|
||||
options.MaxSendMessageSize = _config.MAX_MESSAGE_SIZE;
|
||||
});
|
||||
|
||||
builder.WebHost.ConfigureKestrel(options =>
|
||||
{
|
||||
options.Listen(IPAddress.Parse(_config.Host), _config.Port, listenOptions =>
|
||||
{
|
||||
if (_config.Certificate != null && _config.UseSsl)
|
||||
{
|
||||
listenOptions.UseHttps(httpsOptions =>
|
||||
{
|
||||
httpsOptions.ServerCertificate = _config.Certificate;
|
||||
httpsOptions.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
|
||||
});
|
||||
}
|
||||
|
||||
listenOptions.Protocols = HttpProtocols.Http2;
|
||||
});
|
||||
});
|
||||
|
||||
_app = builder.Build();
|
||||
_app.MapGrpcService<ConnectorServiceImplementation>();
|
||||
|
||||
_serverTask = _app.RunAsync(_serverCts.Token);
|
||||
|
||||
if (_config.EnableHeartbeat)
|
||||
{
|
||||
_pingTask = Task.Run(() => PingLoopAsync(_serverCts.Token), _serverCts.Token);
|
||||
_pongTask = Task.Run(() => PongMonitorLoopAsync(_serverCts.Token), _serverCts.Token);
|
||||
}
|
||||
|
||||
if (_config.EnableHealthApi)
|
||||
{
|
||||
HealthApi.Start(_config.HealthApiPort, _config.HealthApiBindAddress);
|
||||
}
|
||||
|
||||
OnLog?.Invoke(this, new LogMessage { Type = LogType.Trace, Message = $"EonaCat gRPC server started on {_config.Host}:{_config.Port}" });
|
||||
}
|
||||
|
||||
internal bool CanAcceptConnection()
|
||||
{
|
||||
return _config.MaxConnections <= 0 || GetActiveConnectionCount() < _config.MaxConnections;
|
||||
}
|
||||
|
||||
internal void IncrementDroppedConnection() => _stats.IncrementDroppedConnections();
|
||||
|
||||
internal bool AddClient(string clientId, GrpcClientConnection connection)
|
||||
{
|
||||
if (_clients.TryAdd(clientId, connection))
|
||||
{
|
||||
_stats.IncrementTotalConnections();
|
||||
return true;
|
||||
}
|
||||
|
||||
_stats.IncrementDroppedConnections();
|
||||
return false;
|
||||
}
|
||||
|
||||
internal bool RemoveClient(string clientId, out GrpcClientConnection? client)
|
||||
{
|
||||
var removed = _clients.TryRemove(clientId, out client);
|
||||
_lastPingTimes.TryRemove(clientId, out _);
|
||||
return removed;
|
||||
}
|
||||
|
||||
internal void RaiseOnConnected(ConnectionEventArgs args) =>
|
||||
HandlingHelpers.SafeInvokeEventHandler(OnConnected, this, args, nameof(OnConnected));
|
||||
|
||||
internal void RaiseOnDataReceived(DataReceivedEventArgs args)
|
||||
{
|
||||
_stats.IncrementMessagesReceived();
|
||||
HandlingHelpers.SafeInvokeEventHandler(OnDataReceived, this, args, nameof(OnDataReceived));
|
||||
}
|
||||
|
||||
internal void RaiseOnDisconnected(ConnectionEventArgs args) =>
|
||||
HandlingHelpers.SafeInvokeEventHandler(OnDisconnected, this, args, nameof(OnDisconnected));
|
||||
|
||||
internal void RaiseOnGeneralError(ErrorEventArgs args) =>
|
||||
HandlingHelpers.SafeInvokeEventHandler(OnGeneralError, this, args, nameof(OnGeneralError));
|
||||
|
||||
internal void TrackBytesReceived(long bytes) => _stats.AddBytesReceived(bytes);
|
||||
internal void TrackBytesSent(long bytes) => _stats.AddBytesSent(bytes);
|
||||
|
||||
public async Task<bool> SendToClientAsync(string clientId, byte[] data)
|
||||
{
|
||||
if (data == null || data.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_clients.TryGetValue(clientId, out var client) || client.ResponseStream == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await client.WriteLock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var message = new ConnectorMessage
|
||||
{
|
||||
Type = MessageType.Data,
|
||||
Data = Google.Protobuf.ByteString.CopyFrom(data)
|
||||
};
|
||||
await client.ResponseStream.WriteAsync(message).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
client.WriteLock.Release();
|
||||
}
|
||||
|
||||
var sentAt = DateTime.UtcNow;
|
||||
client.LastDataSent = sentAt;
|
||||
client.LastActive = sentAt;
|
||||
client.BytesSent += data.Length;
|
||||
client.MessagesSent++;
|
||||
_stats.IncrementMessagesSent();
|
||||
_stats.AddBytesSent(data.Length);
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
HandlingHelpers.SafeInvokeEventHandler(
|
||||
OnGeneralError,
|
||||
this,
|
||||
new ErrorEventArgs
|
||||
{
|
||||
ClientId = clientId,
|
||||
Nickname = client.Nickname,
|
||||
Exception = exception,
|
||||
Message = "Error sending data to gRPC client"
|
||||
},
|
||||
nameof(OnGeneralError));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> SendToClientAsync(string clientId, string message)
|
||||
=> await SendToClientAsync(clientId, Encoding.UTF8.GetBytes(message)).ConfigureAwait(false);
|
||||
|
||||
public async Task<bool> BroadcastAsync(byte[] data)
|
||||
{
|
||||
if (data == null || data.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool any = false;
|
||||
foreach (var client in _clients)
|
||||
{
|
||||
if (await SendToClientAsync(client.Key, data).ConfigureAwait(false))
|
||||
{
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
return any;
|
||||
}
|
||||
|
||||
public async Task<bool> BroadcastAsync(string message)
|
||||
=> await BroadcastAsync(Encoding.UTF8.GetBytes(message)).ConfigureAwait(false);
|
||||
|
||||
public async Task<bool> BroadcastExceptAsync(byte[] data, string excludeClientId)
|
||||
{
|
||||
if (data == null || data.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool any = false;
|
||||
foreach (var client in _clients)
|
||||
{
|
||||
if (client.Key == excludeClientId)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (await SendToClientAsync(client.Key, data).ConfigureAwait(false))
|
||||
{
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
return any;
|
||||
}
|
||||
|
||||
public async Task<bool> BroadcastExceptAsync(string message, string excludeClientId)
|
||||
=> await BroadcastExceptAsync(Encoding.UTF8.GetBytes(message), excludeClientId).ConfigureAwait(false);
|
||||
|
||||
public ConcurrentDictionary<string, GrpcClientConnection> GetClients() => _clients;
|
||||
|
||||
public GrpcClientConnection? GetClient(string clientId)
|
||||
{
|
||||
_clients.TryGetValue(clientId, out var client);
|
||||
return client;
|
||||
}
|
||||
|
||||
public async Task DisconnectClientAsync(string clientId, DisconnectReason reason = DisconnectReason.ServerShutdown)
|
||||
{
|
||||
if (!RemoveClient(clientId, out var client))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
if (client?.ResponseStream != null)
|
||||
{
|
||||
await client.WriteLock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var msg = new ConnectorMessage { Type = MessageType.Disconnect };
|
||||
await client.ResponseStream.WriteAsync(msg).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
client.WriteLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
client?.CancellationSource?.Cancel();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
TrackDisconnect(reason);
|
||||
|
||||
HandlingHelpers.SafeInvokeEventHandler(
|
||||
OnDisconnected,
|
||||
this,
|
||||
new ConnectionEventArgs
|
||||
{
|
||||
ClientId = clientId,
|
||||
Nickname = client?.Nickname,
|
||||
RemoteEndPoint = client?.RemoteEndPoint,
|
||||
Reason = reason
|
||||
},
|
||||
nameof(OnDisconnected));
|
||||
}
|
||||
|
||||
private async Task PingLoopAsync(CancellationToken token)
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(_config.HeartbeatIntervalSeconds), token).ConfigureAwait(false);
|
||||
|
||||
foreach (var kvp in _clients)
|
||||
{
|
||||
var clientId = kvp.Key;
|
||||
var client = kvp.Value;
|
||||
|
||||
if (client.ResponseStream == null || !client.IsConnected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await client.WriteLock.WaitAsync(token).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var pingMsg = new ConnectorMessage { Type = MessageType.Ping };
|
||||
await client.ResponseStream.WriteAsync(pingMsg).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
client.WriteLock.Release();
|
||||
}
|
||||
|
||||
client.LastPingSent = DateTime.UtcNow;
|
||||
_lastPingTimes[clientId] = DateTime.UtcNow;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnLog?.Invoke(this, new LogMessage { Type = LogType.Error, Message = $"Error sending ping to {client.Nickname}: {ex.Message}" });
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
HandlingHelpers.SafeInvokeEventHandler(
|
||||
OnGeneralError,
|
||||
this,
|
||||
new ErrorEventArgs
|
||||
{
|
||||
Exception = ex,
|
||||
Message = "Ping loop error"
|
||||
},
|
||||
nameof(OnGeneralError));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PongMonitorLoopAsync(CancellationToken token)
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), token).ConfigureAwait(false);
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var timeout = _config.HeartbeatIntervalSeconds * 2;
|
||||
|
||||
foreach (var kvp in _lastPingTimes)
|
||||
{
|
||||
var clientId = kvp.Key;
|
||||
var lastPing = kvp.Value;
|
||||
|
||||
if (!_clients.TryGetValue(clientId, out var client))
|
||||
{
|
||||
_lastPingTimes.TryRemove(clientId, out _);
|
||||
continue;
|
||||
}
|
||||
|
||||
var elapsed = (now - client.LastPongReceived).TotalSeconds;
|
||||
if (client.LastPongReceived == default)
|
||||
{
|
||||
elapsed = (now - lastPing).TotalSeconds;
|
||||
}
|
||||
|
||||
if (elapsed > timeout)
|
||||
{
|
||||
OnPongMissed?.Invoke(this, new PingEventArgs
|
||||
{
|
||||
Id = client.Id,
|
||||
Nickname = client.Nickname,
|
||||
ReceivedTime = now,
|
||||
RemoteEndPoint = client.RemoteEndPoint
|
||||
});
|
||||
|
||||
if (_config.DisconectOnMissedPong)
|
||||
{
|
||||
OnLog?.Invoke(this, new LogMessage { Type = LogType.Trace, Message = $"gRPC client '{client.Nickname}' no PONG for {elapsed:F1}s, disconnecting." });
|
||||
_ = DisconnectClientAsync(clientId, DisconnectReason.NoPongReceived);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
HandlingHelpers.SafeInvokeEventHandler(
|
||||
OnGeneralError,
|
||||
this,
|
||||
new ErrorEventArgs
|
||||
{
|
||||
Exception = ex,
|
||||
Message = "Pong monitor loop error"
|
||||
},
|
||||
nameof(OnGeneralError));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void HandlePongReceived(string clientId)
|
||||
{
|
||||
if (_clients.TryGetValue(clientId, out var client))
|
||||
{
|
||||
client.LastPongReceived = DateTime.UtcNow;
|
||||
client.LastActive = DateTime.UtcNow;
|
||||
|
||||
OnClientPingResponse?.Invoke(this, new PingEventArgs
|
||||
{
|
||||
Id = clientId,
|
||||
Nickname = client.Nickname,
|
||||
ReceivedTime = DateTime.UtcNow,
|
||||
RemoteEndPoint = client.RemoteEndPoint
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private string BuildHealthJson()
|
||||
{
|
||||
var E = HealthApiServer.JsonEscape;
|
||||
var stats = GetStats();
|
||||
return "{" +
|
||||
"\"status\":\"ok\"," +
|
||||
"\"type\":\"grpc-server\"," +
|
||||
$"\"listenAddress\":{E(_config.Host + ":" + _config.Port)}," +
|
||||
$"\"isStarted\":{(IsStarted ? "true" : "false")}," +
|
||||
$"\"activeConnections\":{stats.ActiveConnections}," +
|
||||
$"\"maxConnections\":{_config.MaxConnections}," +
|
||||
$"\"uptimeSeconds\":{stats.Uptime.TotalSeconds:F1}," +
|
||||
$"\"startTimeUtc\":{E(stats.StartTime.ToString("O"))}" +
|
||||
"}";
|
||||
}
|
||||
|
||||
private string BuildStatusJson()
|
||||
{
|
||||
var E = HealthApiServer.JsonEscape;
|
||||
var stats = GetStats();
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("{");
|
||||
sb.Append("\"status\":\"ok\",");
|
||||
sb.Append("\"type\":\"grpc-server\",");
|
||||
sb.Append($"\"listenAddress\":{E(_config.Host + ":" + _config.Port)},");
|
||||
sb.Append($"\"isStarted\":{(IsStarted ? "true" : "false")},");
|
||||
sb.Append($"\"activeConnections\":{stats.ActiveConnections},");
|
||||
sb.Append($"\"maxConnections\":{_config.MaxConnections},");
|
||||
sb.Append($"\"totalConnections\":{stats.TotalConnections},");
|
||||
sb.Append($"\"droppedConnections\":{stats.DroppedConnections},");
|
||||
sb.Append($"\"bytesSent\":{stats.BytesSent},");
|
||||
sb.Append($"\"bytesReceived\":{stats.BytesReceived},");
|
||||
sb.Append($"\"messagesSent\":{stats.MessagesSent},");
|
||||
sb.Append($"\"messagesReceived\":{stats.MessagesReceived},");
|
||||
sb.Append($"\"messagesPerSecond\":{stats.MessagesPerSecond:F2},");
|
||||
sb.Append($"\"uptimeSeconds\":{stats.Uptime.TotalSeconds:F1},");
|
||||
sb.Append($"\"startTimeUtc\":{E(stats.StartTime.ToString("O"))},");
|
||||
|
||||
sb.Append("\"clients\":[");
|
||||
var clientList = _clients.Values.ToArray();
|
||||
for (int i = 0; i < clientList.Length; i++)
|
||||
{
|
||||
var c = clientList[i];
|
||||
if (i > 0)
|
||||
{
|
||||
sb.Append(",");
|
||||
}
|
||||
|
||||
sb.Append("{");
|
||||
sb.Append($"\"id\":{E(c.Id)},");
|
||||
sb.Append($"\"nickname\":{E(c.Nickname)},");
|
||||
sb.Append($"\"remoteEndPoint\":{E(c.RemoteEndPoint?.ToString())},");
|
||||
sb.Append($"\"connectedAtUtc\":{E(c.ConnectedAt.ToString("O"))},");
|
||||
sb.Append($"\"connectedTimeSeconds\":{c.ConnectedTime().TotalSeconds:F1},");
|
||||
sb.Append($"\"idleTimeSeconds\":{c.IdleTime().TotalSeconds:F1},");
|
||||
sb.Append($"\"bytesSent\":{c.BytesSent},");
|
||||
sb.Append($"\"bytesReceived\":{c.BytesReceived},");
|
||||
sb.Append($"\"messagesSent\":{c.MessagesSent},");
|
||||
sb.Append($"\"messagesReceived\":{c.MessagesReceived}");
|
||||
sb.Append("}");
|
||||
}
|
||||
sb.Append("]");
|
||||
sb.Append("}");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_serverCts?.Cancel();
|
||||
|
||||
foreach (var client in _clients.Keys.ToList())
|
||||
{
|
||||
DisconnectClientAsync(client, DisconnectReason.ServerShutdown).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
_clients.Clear();
|
||||
|
||||
HealthApi.Stop();
|
||||
|
||||
try
|
||||
{
|
||||
_app?.StopAsync().GetAwaiter().GetResult();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_app?.DisposeAsync().AsTask().GetAwaiter().GetResult();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
_app = null;
|
||||
}
|
||||
|
||||
public async Task StopAsync()
|
||||
{
|
||||
_serverCts?.Cancel();
|
||||
|
||||
foreach (var client in _clients.Keys.ToList())
|
||||
{
|
||||
await DisconnectClientAsync(client, DisconnectReason.ServerShutdown).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
_clients.Clear();
|
||||
|
||||
HealthApi.Stop();
|
||||
|
||||
try
|
||||
{
|
||||
if (_app != null)
|
||||
{
|
||||
await _app.StopAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (_app != null)
|
||||
{
|
||||
await _app.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
_app = null;
|
||||
|
||||
if (_serverTask != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _serverTask.ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
|
||||
Stop();
|
||||
|
||||
_serverCts?.Dispose();
|
||||
_stats.Dispose();
|
||||
|
||||
OnConnected = null;
|
||||
OnDisconnected = null;
|
||||
OnDataReceived = null;
|
||||
OnGeneralError = null;
|
||||
OnClientPingResponse = null;
|
||||
OnPongMissed = null;
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
|
||||
await StopAsync().ConfigureAwait(false);
|
||||
|
||||
_serverCts?.Dispose();
|
||||
_stats.Dispose();
|
||||
|
||||
OnConnected = null;
|
||||
OnDisconnected = null;
|
||||
OnDataReceived = null;
|
||||
OnGeneralError = null;
|
||||
OnClientPingResponse = null;
|
||||
OnPongMissed = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user