我在我的C#网络项目中使用无状态,主要是因为它是一种很好的方法来添加功能,如套接字连接后的有线级授权、重新连接的延迟等。
话虽如此,但我自己却陷入了一些竞争状态和僵局--我向以下各州寻求解决问题的最佳方法:
enum State { Stopped, Disconnected, Connecting, Connected, Resetting }
enum Trigger { Start, Stop, Connect, SetConnectComplete, Reset, SetResetComplete }
class StateMachine : StateMachine<State, Trigger>
{
public StateMachine(Action OnDisconnected, Action OnConnecting, Action OnConnected, Action OnResetting) : base(State.Stopped)
{
this.Configure(State.Stopped)
.Permit(Trigger.Start, State.Disconnected);
this.Configure(State.Disconnected)
.OnEntry(OnDisconnected)
.Permit(Trigger.Connect, State.Connecting);
this.Configure(State.Connecting)
.OnEntry(OnConnecting)
.Permit(Trigger.SetConnectComplete, State.Connected)
.Permit(Trigger.Reset, State.Resetting);
this.Configure(State.Connected)
.OnEntry(OnConnected)
.Permit(Trigger.Reset, State.Resetting);
this.Configure(State.Resetting)
.OnEntry(OnResetting)
.Permit(Trigger.SetResetComplete, State.Disconnected);
}
}
它的功能是套接字将自动重新连接,并在连接时启动接收循环。如果出现套接字错误,它应该返回以释放资源,然后循环返回以重新启动。
然而,当我处理对象时,连接的套接字中止,这也释放了资源,并且它尝试自己等待。
我相信这是因为线程在等待自己,所以我的设计/状态结构从根本上肯定是不正确的,我很欣赏更好的结构的指针,它可以完全避免死锁。
public class ManagedWebSocket : IDisposable
{
readonly StateMachine stateMachine;
Task backgroundReaderTask;
private ClientWebSocket webSocket;
private readonly ITargetBlock<byte[]> target;
private readonly ILogger<ManagedWebSocket> logger;
private CancellationTokenSource cancellationTokenSource;
bool isDisposing;
public ManagedWebSocket(string uri, ITargetBlock<byte[]> target, ILogger<ManagedWebSocket> logger)
{
this.stateMachine = new StateMachine(OnDisconnected, OnConnecting, OnConnected, OnResetting);
this.target = target;
this.logger = logger;
}
private void OnConnecting()
{
this.backgroundReaderTask = Task.Run(async () =>
{
this.cancellationTokenSource = new CancellationTokenSource();
this.webSocket = new ClientWebSocket();
webSocket.Options.KeepAliveInterval = KeepAliveInterval;
try
{
await this.webSocket.ConnectAsync(this.uri, cancellationTokenSource.Token);
}
catch(WebSocketException ex)
{
this.logger.LogError(ex.Message, ex);
await this.stateMachine.FireAsync(Trigger.Reset);
}
this.stateMachine.Fire(Trigger.SetConnectComplete);
});
}
private void OnDisconnected()
{
if (isDisposing == false)
this.stateMachine.Fire(Trigger.Connect);
}
private void OnResetting()
{
FreeResources();
this.stateMachine.Fire(Trigger.SetResetComplete);
}
private void OnConnected()
{
this.backgroundReaderTask = Task.Run( async () => {
try
{
// returns when the internal frame loop completes with websocket close, or by throwing an exception
await this.webSocket.ReceiveFramesLoopAsync(target.SendAsync, 2048, this.cancellationTokenSource.Token);
}
catch (Exception ex)
{
this.logger.LogError(ex.Message, ex);
}
await this.stateMachine.FireAsync(Trigger.Reset);
});
}
public async Task SendAsync(byte[] data, WebSocketMessageType webSocketMessageType)
{
if (this.stateMachine.State != State.Connected)
throw new Exception($"{nameof(ManagedWebSocket)} is not yet connected.");
try
{
await webSocket
.SendAsChunksAsync(data, webSocketMessageType, 2048, this.cancellationTokenSource.Token)
.ConfigureAwait(false);
}
catch (Exception ex)
{
this.logger.LogError(ex, ex.Message);
await this.stateMachine.FireAsync(Trigger.Reset);
}
}
public void Start()
{
this.stateMachine.Fire(Trigger.Start);
}
public void FreeResources()
{
this.logger.LogDebug($"{nameof(ManagedWebSocket.FreeResources)}");
this.cancellationTokenSource?.Cancel();
this.backgroundReaderTask?.Wait();
this.cancellationTokenSource?.Dispose();
this.backgroundReaderTask?.Dispose();
}
public void Dispose()
{
if (isDisposing)
return;
isDisposing = true;
FreeResources();
}
}
我猜想死锁是由于在onResetting()
中调用freeResources();
引起的,因为freeResources();
正在等待BackgroundReaderTask
但在BackgroundReaderTask
中,您正在通过Await this.StateMachine.Fireasync(Trigger.Reset);
等待onResetting()
。
作为某种变通方法,您可以省略触发重置的“await”关键字,因为它将处理整个对象。
还请注意,如果以前在onconnecting()
中抛出了异常,那么似乎没有理由调用this.stateMachine.fire(Trigger.SetConnectComplete);
-只需将其移动到try-block中即可。
此外,作为某种最佳实践和附带说明,请尝试遵循推荐的dispose模式