cancel web socket operations on dispose

This commit is contained in:
Luke Pulverenti 2013-09-27 17:08:51 -04:00
parent 51a6ee5650
commit df29ed5a6f

View file

@ -25,6 +25,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer
/// <value>The web socket.</value> /// <value>The web socket.</value>
private System.Net.WebSockets.WebSocket WebSocket { get; set; } private System.Net.WebSockets.WebSocket WebSocket { get; set; }
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="NativeWebSocket" /> class. /// Initializes a new instance of the <see cref="NativeWebSocket" /> class.
/// </summary> /// </summary>
@ -79,7 +81,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
try try
{ {
bytes = await ReceiveBytesAsync(CancellationToken.None).ConfigureAwait(false); bytes = await ReceiveBytesAsync(_cancellationTokenSource.Token).ConfigureAwait(false);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@ -144,7 +146,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer
_logger.Warn("Unrecognized WebSocketMessageType: {0}", type.ToString()); _logger.Warn("Unrecognized WebSocketMessageType: {0}", type.ToString());
} }
return WebSocket.SendAsync(new ArraySegment<byte>(bytes), nativeType, true, cancellationToken); var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationTokenSource.Token);
return WebSocket.SendAsync(new ArraySegment<byte>(bytes), nativeType, true, linkedTokenSource.Token);
} }
/// <summary> /// <summary>
@ -163,6 +167,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer
{ {
if (dispose) if (dispose)
{ {
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
WebSocket.Dispose(); WebSocket.Dispose();
} }
} }