Removed use of cancellation token
This commit is contained in:
@@ -9,7 +9,6 @@ namespace CommonSocketLibrary.Abstract
|
||||
public abstract class SocketClient<Message> : IDisposable where Message : class
|
||||
{
|
||||
protected ClientWebSocket? _socket;
|
||||
protected CancellationTokenSource? _cts;
|
||||
|
||||
private readonly int ReceiveBufferSize = 8192;
|
||||
protected readonly ILogger _logger;
|
||||
@@ -42,21 +41,18 @@ namespace CommonSocketLibrary.Abstract
|
||||
_socket.Options.RemoteCertificateValidationCallback = (o, c, ch, er) => true;
|
||||
_socket.Options.UseDefaultCredentials = false;
|
||||
_disposed = false;
|
||||
if (_cts != null) _cts.Dispose();
|
||||
_cts = new CancellationTokenSource();
|
||||
await _socket.ConnectAsync(new Uri(url), _cts.Token);
|
||||
await Task.Factory.StartNew(ReceiveLoop, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
||||
await _socket.ConnectAsync(new Uri(url), CancellationToken.None);
|
||||
await Task.Factory.StartNew(ReceiveLoop, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
||||
OnConnected?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public async Task DisconnectAsync(SocketDisconnectionEventArgs args)
|
||||
{
|
||||
if (_disposed || _socket == null || _cts == null)
|
||||
if (_disposed || _socket == null)
|
||||
return;
|
||||
|
||||
if (_socket.State == WebSocketState.Open)
|
||||
{
|
||||
_cts.CancelAfter(TimeSpan.FromMilliseconds(500));
|
||||
await _socket.CloseOutputAsync(WebSocketCloseStatus.Empty, "", CancellationToken.None);
|
||||
await _socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
|
||||
}
|
||||
@@ -64,11 +60,9 @@ namespace CommonSocketLibrary.Abstract
|
||||
OnDisconnected?.Invoke(this, args);
|
||||
_socket.Dispose();
|
||||
_socket = null;
|
||||
_cts.Dispose();
|
||||
_cts = null;
|
||||
}
|
||||
|
||||
protected virtual async Task<T> Deserialize<T>(Stream stream)
|
||||
protected virtual async Task<T?> Deserialize<T>(Stream stream)
|
||||
{
|
||||
return await JsonSerializer.DeserializeAsync<T>(stream, _options);
|
||||
}
|
||||
@@ -83,21 +77,19 @@ namespace CommonSocketLibrary.Abstract
|
||||
|
||||
private async Task ReceiveLoop()
|
||||
{
|
||||
if (_socket == null || _cts == null) return;
|
||||
if (_socket == null) return;
|
||||
|
||||
var loopToken = _cts.Token;
|
||||
MemoryStream? outputStream = null;
|
||||
WebSocketReceiveResult? receiveResult = null;
|
||||
var buffer = new byte[ReceiveBufferSize];
|
||||
try
|
||||
{
|
||||
while (!loopToken.IsCancellationRequested)
|
||||
while (_socket.State == WebSocketState.Open || _socket.State == WebSocketState.CloseSent)
|
||||
{
|
||||
outputStream = new MemoryStream(ReceiveBufferSize);
|
||||
do
|
||||
{
|
||||
receiveResult = await _socket.ReceiveAsync(buffer, _cts.Token);
|
||||
if (receiveResult.MessageType != WebSocketMessageType.Close)
|
||||
receiveResult = await _socket.ReceiveAsync(buffer, CancellationToken.None);
|
||||
outputStream.Write(buffer, 0, receiveResult.Count);
|
||||
}
|
||||
while (!receiveResult.EndOfMessage);
|
||||
@@ -109,7 +101,7 @@ namespace CommonSocketLibrary.Abstract
|
||||
catch (WebSocketException wse)
|
||||
{
|
||||
string data = string.Join(string.Empty, wse.Data.Cast<DictionaryEntry>().Select(e => e.Key + "=" + e.Value));
|
||||
_logger.Error($"Websocket connection problem while receiving data [state: {_socket.State}][code: {wse.ErrorCode}][data: {data}]");
|
||||
_logger.Error(wse, $"Websocket connection problem while receiving data [state: {_socket.State}][code: {wse.ErrorCode}][data: {data}]");
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace CommonSocketLibrary.Common
|
||||
while (current < total)
|
||||
{
|
||||
var size = Encoding.UTF8.GetBytes(content.Substring(current), array);
|
||||
await _socket!.SendAsync(array, WebSocketMessageType.Text, current + size >= total, _cts!.Token);
|
||||
await _socket!.SendAsync(array, WebSocketMessageType.Text, current + size >= total, CancellationToken.None);
|
||||
current += size;
|
||||
}
|
||||
_logger.Verbose("TX #" + opcode + ": " + content);
|
||||
|
||||
Reference in New Issue
Block a user