Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions ResgateIO.Client.UnitTests/ResgateIO.Client.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
27 changes: 14 additions & 13 deletions ResgateIO.Client/ItemCache.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

[assembly: InternalsVisibleTo("ResgateIO.Client.UnitTests")]
namespace ResgateIO.Client
{

internal class ItemCache
{
private delegate TraverseState TraverseCallback(CacheItem traversedCI, TraverseState ret);

delegate TraverseState TraverseCallback(CacheItem traversedCI, TraverseState ret);

readonly struct TraverseState
private readonly struct TraverseState
{
public readonly ReferenceState State;
public readonly string ResourceID;
Expand All @@ -33,11 +32,11 @@ public TraverseState(ReferenceState state)
}
}

private static TraverseState TraverseStop = new TraverseState(ReferenceState.Stop);
private static TraverseState TraverseContinue = new TraverseState(ReferenceState.None);
private static readonly TraverseState TraverseStop = new TraverseState(ReferenceState.Stop);
private static readonly TraverseState TraverseContinue = new TraverseState(ReferenceState.None);

private Dictionary<string, CacheItem> cache = new Dictionary<string, CacheItem>();
private object cacheLock = new object();
private readonly Dictionary<string, CacheItem> cache = new Dictionary<string, CacheItem>();
private readonly object cacheLock = new object();
private IResourceType[] resourceTypes;
private PatternMap<ResourceFactory> resourcePatterns;

Expand Down Expand Up @@ -208,7 +207,8 @@ public async Task Unsubscribe(string rid, Action<string, ResponseCallback> unsub
}
});

await tcs.Task;
await tcs.Task
.ConfigureAwait(false);
}

public CacheItem AddResourcesAndSubscribe(JToken result, string rid)
Expand Down Expand Up @@ -375,7 +375,7 @@ private CacheItem getRefItem(object value)
private Dictionary<string, CacheItemReference> getRefState(CacheItem ci)
{
var refs = new Dictionary<string, CacheItemReference>();

// Quick exit if directly subscribed
if (ci.Subscriptions > 0)
{
Expand Down Expand Up @@ -782,7 +782,8 @@ private ResourceEventArgs handleUnsubscribeEvent(CacheItem ci, ResourceEventArgs
}
}

if (reason == null) {
if (reason == null)
{
reason = new ResError("Missing unsubscribe reason.");
}

Expand Down
91 changes: 49 additions & 42 deletions ResgateIO.Client/ResClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public class ResClient : IResClient
private JsonSerializerSettings _serializerSettings;
private Func<ResClient, Task> _onConnectCallback;
private int _reconnectDelay = 3000;
private CancellationTokenSource _reconnectTokenSource;
private CancellationTokenSource _reconnectCancellationTokenSource;
private Task _connectTask;
private int _protocol;
private bool _isOnline;
private bool _tryReconnect;
private bool _disposedValue;
private bool _isDisposed;
private ItemCache _cache;

/// <summary>
Expand Down Expand Up @@ -151,11 +151,7 @@ public async Task ConnectAsync()
{
_tryReconnect = true;

if (_reconnectTokenSource != null)
{
_reconnectTokenSource.Cancel();
_reconnectTokenSource = null;
}
CancelOngoingReconnect();

if (_connectTask == null)
{
Expand All @@ -168,7 +164,7 @@ public async Task ConnectAsync()

try
{
await task;
await task.ConfigureAwait(false);
}
catch
{
Expand All @@ -183,7 +179,8 @@ public async Task ConnectAsync()

private async Task ConnectInternalAsync()
{
var webSocket = await _webSocketFactory();
var webSocket = await _webSocketFactory()
.ConfigureAwait(false);

webSocket.ConnectionStatusChanged += WebSocket_ConnectionStatusChanged;

Expand All @@ -193,11 +190,13 @@ private async Task ConnectInternalAsync()

try
{
await HandshakeAsync();
await HandshakeAsync()
.ConfigureAwait(false);

if (_onConnectCallback != null)
{
await _onConnectCallback(this);
await _onConnectCallback(this)
.ConfigureAwait(false);
}

_isOnline = true;
Expand Down Expand Up @@ -233,11 +232,7 @@ public async Task DisconnectAsync()
lock (_connectLock)
{
_tryReconnect = false;
if (_reconnectTokenSource != null)
{
_reconnectTokenSource.Cancel();
_reconnectTokenSource = null;
}
CancelOngoingReconnect();
}

if (_rpc == null)
Expand All @@ -247,7 +242,8 @@ public async Task DisconnectAsync()

try
{
await _rpc.WebSocket.DisconnectAsync();
await _rpc.DisconnectAsync()
.ConfigureAwait(false);
}
finally
{
Expand Down Expand Up @@ -291,21 +287,21 @@ private void StartReconnectTimer()
return;
}

if (_reconnectTokenSource != null)
{
_reconnectTokenSource.Cancel();
_reconnectTokenSource = null;
}
CancelOngoingReconnect();

_reconnectTokenSource = new CancellationTokenSource();
Task.Delay(_reconnectDelay, _reconnectTokenSource.Token).ContinueWith(async _ => await Reconnect());
_reconnectCancellationTokenSource = new CancellationTokenSource();
Task.Delay(
_reconnectDelay,
_reconnectCancellationTokenSource.Token)
.ContinueWith(async _ => await Reconnect().ConfigureAwait(false));
}

private async Task Reconnect()
{
try
{
await ConnectAsync();
await ConnectAsync()
.ConfigureAwait(false);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -334,7 +330,9 @@ public Task<ResResource> SubscribeAsync(string rid)
/// <param name="rid">Resource ID.</param>
public async Task UnsubscribeAsync(string rid)
{
await _cache.Unsubscribe(rid, Unsubscribe);
await _cache
.Unsubscribe(rid, Unsubscribe)
.ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -445,7 +443,8 @@ private async Task<object> RequestAsync(string type, string rid, string method,
}
});

return await tcs.Task;
return await tcs.Task
.ConfigureAwait(false);
}

private object HandleRequestResult(RequestResult result)
Expand Down Expand Up @@ -474,7 +473,9 @@ private object HandleRequestResult(RequestResult result)

private async Task<T> RequestAsync<T>(string type, string rid, string method, object parameters)
{
var o = await RequestAsync(type, rid, method, parameters);
var o = await RequestAsync(type, rid, method, parameters)
.ConfigureAwait(false);

if (o is T)
{
return (T)o;
Expand Down Expand Up @@ -533,7 +534,8 @@ private async Task HandshakeAsync()
tcs.SetResult(null);
});

await tcs.Task;
await tcs.Task
.ConfigureAwait(false);
}

private void Unsubscribe(string rid, ResponseCallback callback)
Expand All @@ -551,7 +553,8 @@ private void Send(string action, string rid, string method, object parameters, R

try
{
await ConnectAsync();
await ConnectAsync()
.ConfigureAwait(false);
}
catch (ResException e)
{
Expand Down Expand Up @@ -583,7 +586,8 @@ private void DisposeRpc()
private async Task<IWebSocket> CreateWebSocket()
{
var webSocket = new WebSocket();
await webSocket.ConnectAsync(_hostUrl);
await webSocket.ConnectAsync(_hostUrl)
.ConfigureAwait(false);
return webSocket;
}

Expand Down Expand Up @@ -632,22 +636,25 @@ private void OnCacheResourceEvent(object sender, ResourceEventArgs ev)
ResourceEvent?.Invoke(this, ev);
}

protected virtual void Dispose(bool disposing)
private void CancelOngoingReconnect()
{
if (!_disposedValue)
{
if (disposing)
{
_rpc?.Dispose();
}
_disposedValue = true;
}
_reconnectCancellationTokenSource?.Cancel();
_reconnectCancellationTokenSource?.Dispose();
_reconnectCancellationTokenSource = null;
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
if (_isDisposed)
return;

DisposeRpc();

ResourceEvent = null;
Error = null;
ConnectionStatusChanged = null;

_isDisposed = true;
}
}
}
Loading