Removed use of cancellation token

This commit is contained in:
Tom
2026-01-03 05:13:01 +00:00
parent ac967dcb1d
commit 0131cca278
2 changed files with 10 additions and 18 deletions

View File

@@ -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,22 +77,20 @@ 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)
outputStream.Write(buffer, 0, receiveResult.Count);
receiveResult = await _socket.ReceiveAsync(buffer, CancellationToken.None);
outputStream.Write(buffer, 0, receiveResult.Count);
}
while (!receiveResult.EndOfMessage);
if (receiveResult.MessageType == WebSocketMessageType.Close) break;
@@ -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)
{

View File

@@ -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);