Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Janicki committed Aug 3, 2023
1 parent 85de844 commit e939795
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
25 changes: 8 additions & 17 deletions Consumer/RealTime/Services/ClientWebSocketWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using System.Net.WebSockets;
using System.Net.WebSockets;
using Consumer.Extensions;using Consumer.Models;
using Consumer.RealTime.Models;
using Microsoft.Extensions.Logging;
Expand All @@ -9,7 +8,6 @@ namespace Consumer.RealTime.Services;

public sealed class ClientWebSocketWrapper<TParam> : IClientWebSocketWrapper
{
private readonly ICredentials _credentials;
private readonly Uri _uri;
private readonly ILogger _logger;
private readonly TimeSpan _monitorDelay;
Expand All @@ -19,10 +17,9 @@ public sealed class ClientWebSocketWrapper<TParam> : IClientWebSocketWrapper
private readonly TimeSpan _minimalDelay;
private Argument<ClientWebSocketWrapper<TParam>>? _argument;

public ClientWebSocketWrapper(ICredentials credentials, Uri uri, ILogger logger, TimeSpan monitorDelay, Func<byte[], TParam, CancellationToken, Task> dataHandler,
public ClientWebSocketWrapper(Uri uri, ILogger logger, TimeSpan monitorDelay, Func<byte[], TParam, CancellationToken, Task> dataHandler,
Func<WebSocketState, TParam, CancellationToken, Task> monitorHandler, TParam param)
{
_credentials = credentials;
_uri = uri;
_logger = logger;
_monitorDelay = monitorDelay;
Expand All @@ -38,7 +35,7 @@ public sealed class ClientWebSocketWrapper<TParam> : IClientWebSocketWrapper
{
return await Connect(cancellationToken).ConfigureAwait(false);
}
_argument.ReplaceClient();
_argument.RecreateClient();

return await _argument.Connect(_uri, cancellationToken).ConfigureAwait(false);
}
Expand All @@ -49,8 +46,7 @@ public sealed class ClientWebSocketWrapper<TParam> : IClientWebSocketWrapper
{
return null;
}
_argument = new Argument<ClientWebSocketWrapper<TParam>>(_credentials,
new TaskData<ClientWebSocketWrapper<TParam>>(this, static (wrapper, source) => wrapper.ReceiveHandler(source.Token)),
_argument = new Argument<ClientWebSocketWrapper<TParam>>(new TaskData<ClientWebSocketWrapper<TParam>>(this, static (wrapper, source) => wrapper.ReceiveHandler(source.Token)),
new TaskData<ClientWebSocketWrapper<TParam>>(this, static (wrapper, source) => wrapper.MonitorHandler(source.Token)));

return await _argument.Connect(_uri, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -153,22 +149,17 @@ private sealed class Argument<T>
private readonly TaskData<T> _monitorTaskData;
private ClientWebSocket _clientWebSocket;

public Argument(ICredentials credentials, TaskData<T> receiveTaskData, TaskData<T> monitorTaskData)
public Argument(TaskData<T> receiveTaskData, TaskData<T> monitorTaskData)
{
var clientWebSocket = new ClientWebSocket();
clientWebSocket.Options.Credentials = credentials;
_clientWebSocket = clientWebSocket;
_clientWebSocket = new ClientWebSocket();
_receiveTaskData = receiveTaskData;
_monitorTaskData = monitorTaskData;
}

public void ReplaceClient()
public void RecreateClient()
{
var credentials = _clientWebSocket.Options.Credentials;
_clientWebSocket.Dispose();
var clientWebSocket = new ClientWebSocket();
clientWebSocket.Options.Credentials = credentials;
_clientWebSocket = clientWebSocket;
_clientWebSocket = new ClientWebSocket();
}

public ValueTask Send(byte[] utf8Bytes, CancellationToken cancellationToken = default) =>
Expand Down
7 changes: 2 additions & 5 deletions Consumer/RealTime/Services/ClientWebSocketWrapperFactory.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
using System.Net;
using System.Net.WebSockets;
using System.Net.WebSockets;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Consumer.RealTime.Services;

public sealed class ClientWebSocketWrapperFactory : IClientWebSocketWrapperFactory
{
private readonly ICredentials _credentials;
private readonly TimeSpan _monitorDelay;
private readonly Uri _uri;

public ClientWebSocketWrapperFactory(IOptions<ConfigurationSettings> options)
{
_credentials = new NetworkCredential(options.Value.UserName, options.Value.Password);
_monitorDelay = options.Value.WebSocketClientMonitorInterval;
_uri = options.Value.ApiUri;
}

public IClientWebSocketWrapper GetNewInstance<TParam>(ILogger logger, Func<byte[], TParam, CancellationToken, Task> dataHandler,
Func<WebSocketState, TParam, CancellationToken, Task> monitorHandler, TParam param) =>
new ClientWebSocketWrapper<TParam>(_credentials, _uri, logger, _monitorDelay, dataHandler, monitorHandler, param);
new ClientWebSocketWrapper<TParam>(_uri, logger, _monitorDelay, dataHandler, monitorHandler, param);
}

0 comments on commit e939795

Please sign in to comment.