Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 47 additions & 28 deletions SteamKit2/SteamKit2/Networking/Steam3/WebSocketContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public WebSocketContext(WebSocketConnection connection, EndPoint endPoint)

cts = new CancellationTokenSource();
socket = new ClientWebSocket();
semaphore = new SemaphoreSlim( 1, 1 );
connectionUri = ConstructUri(endPoint);
}

readonly WebSocketConnection connection;
readonly CancellationTokenSource cts;
readonly ClientWebSocket socket;
readonly SemaphoreSlim semaphore;
readonly Uri connectionUri;
Task? runloopTask;
int disposed;
Expand Down Expand Up @@ -66,38 +68,47 @@ async Task RunCore(HttpMessageInvoker invoker, TimeSpan connectionTimeout, Cance
connection.log.LogDebug( nameof(WebSocketContext), "Connected to {0}", connectionUri);
connection.Connected?.Invoke(connection, EventArgs.Empty);

while (!cancellationToken.IsCancellationRequested && socket.State == WebSocketState.Open)
{
byte[]? packet = null;
await semaphore.WaitAsync(CancellationToken.None).ConfigureAwait( false );

try
{
packet = await ReadMessageAsync( cancellationToken ).ConfigureAwait( false );
}
catch ( Exception ex )
try
{
while (!cancellationToken.IsCancellationRequested && socket.State == WebSocketState.Open)
{
connection.log.LogDebug( nameof( WebSocketContext ), "Exception reading from websocket: {0} - {1}", ex.GetType().FullName, ex.Message );
connection.DisconnectCore( userInitiated: false, specificContext: this );
return;
byte[]? packet = null;

try
{
packet = await ReadMessageAsync( cancellationToken ).ConfigureAwait( false );
}
catch ( Exception ex )
{
connection.log.LogDebug( nameof( WebSocketContext ), "Exception reading from websocket: {0} - {1}", ex.GetType().FullName, ex.Message );
connection.DisconnectCore( userInitiated: false, specificContext: this );
return;
}

if (packet != null && packet.Length > 0)
{
connection.NetMsgReceived?.Invoke(connection, new NetMsgEventArgs(packet, EndPoint));
}
}

if (packet != null && packet.Length > 0)
if (!cancellationToken.IsCancellationRequested && socket.State == WebSocketState.Open)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will socket.Dispose close cleanly, or should it still let CloseAsync be called here?

{
connection.NetMsgReceived?.Invoke(connection, new NetMsgEventArgs(packet, EndPoint));
connection.log.LogDebug( nameof(WebSocketContext), "Closing connection...");
try
{
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, default).ConfigureAwait(false);
}
catch (Win32Exception ex)
{
connection.log.LogDebug( nameof(WebSocketContext), "Error closing connection: {0}", ex.Message);
}
}
}

if (socket.State == WebSocketState.Open)
finally
{
connection.log.LogDebug( nameof(WebSocketContext), "Closing connection...");
try
{
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, default).ConfigureAwait(false);
}
catch (Win32Exception ex)
{
connection.log.LogDebug( nameof(WebSocketContext), "Error closing connection: {0}", ex.Message);
}
semaphore.Release();
}
}

Expand All @@ -117,16 +128,24 @@ public async Task SendAsync(Memory<byte> data)

public void Dispose()
{
if (Interlocked.Exchange(ref disposed, 1) == 1)
if ( Interlocked.Exchange( ref disposed, 1 ) == 1 )
{
return;
}

cts.Cancel();
cts.Dispose();
runloopTask = null;
semaphore.Wait();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't mind an explanatory comment here as to what flow is happening in regards to the semaphore and the cancellation.

try
{
cts.Dispose();
runloopTask = null;

socket.Dispose();
socket.Dispose();
}
finally
{
semaphore.Dispose();
}
}

async Task<byte[]?> ReadMessageAsync( CancellationToken cancellationToken )
Expand Down