Skip to content

Commit

Permalink
修复握手超时出队问题 (#659)
Browse files Browse the repository at this point in the history
Co-authored-by: shenghao <[email protected]>
  • Loading branch information
shenghao0219 and shenghao authored Jun 1, 2023
1 parent 86263bc commit a2b3b4c
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions src/SuperSocket.WebSocket.Server/WebSocketServerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,50 +85,58 @@ private void HandshakePendingQueueCheckingCallback(object state)
{
_checkingTimer.Change(Timeout.Infinite, Timeout.Infinite);

while (true)
var openHandshakeTimeTask = Task.Run(() =>
{
WebSocketSession session;
while (true)
{
WebSocketSession session;

if (!_openHandshakePendingQueue.TryPeek(out session))
break;
if (!_openHandshakePendingQueue.TryPeek(out session))
break;

if (session.Handshaked || session.State == SessionState.Closed || (session is IAppSession appSession && appSession.Channel.IsClosed))
{
//Handshaked or not connected
if (session.Handshaked || session.State == SessionState.Closed || (session is IAppSession appSession && appSession.Channel.IsClosed))
{
//Handshaked or not connected
_openHandshakePendingQueue.TryDequeue(out session);
continue;
}

if (DateTime.Now < session.StartTime.AddSeconds(_options.OpenHandshakeTimeOut))
break;

//Timeout, dequeue and then close
_openHandshakePendingQueue.TryDequeue(out session);
continue;
session.CloseWithoutHandshake();
}
});

if (DateTime.Now < session.StartTime.AddSeconds(_options.OpenHandshakeTimeOut))
break;
var closeHandshakeTimeTask = Task.Run(() =>
{
while (true)
{
WebSocketSession session;

//Timeout, dequeue and then close
_openHandshakePendingQueue.TryDequeue(out session);
session.CloseWithoutHandshake();
}
if (!_closeHandshakePendingQueue.TryPeek(out session))
break;

while (true)
{
WebSocketSession session;
if (session.State == SessionState.Closed)
{
//the session has been closed
_closeHandshakePendingQueue.TryDequeue(out session);
continue;
}

if (!_closeHandshakePendingQueue.TryPeek(out session))
break;
if (DateTime.Now < session.CloseHandshakeStartTime.AddSeconds(_options.CloseHandshakeTimeOut))
break;

if (session.State == SessionState.Closed)
{
//the session has been closed
//Timeout, dequeue and then close
_closeHandshakePendingQueue.TryDequeue(out session);
continue;
//Needn't send closing handshake again
session.CloseWithoutHandshake();
}
});

if (DateTime.Now < session.CloseHandshakeStartTime.AddSeconds(_options.CloseHandshakeTimeOut))
break;

//Timeout, dequeue and then close
_closeHandshakePendingQueue.TryDequeue(out session);
//Needn't send closing handshake again
session.CloseWithoutHandshake();
}
Task.WhenAll(openHandshakeTimeTask, closeHandshakeTimeTask);

_checkingTimer?.Change(_options.CheckingInterval * 1000, _options.CheckingInterval * 1000);
}
Expand Down

0 comments on commit a2b3b4c

Please sign in to comment.