From a56758f5a6a1f63356bfdd5c6490ea11f08b3632 Mon Sep 17 00:00:00 2001 From: rootdarkarchon Date: Sun, 8 Oct 2023 12:11:18 +0200 Subject: [PATCH] fix reconnection to server --- MareSynchronos/WebAPI/SignalR/ApiController.cs | 14 ++++++++++---- MareSynchronos/WebAPI/SignalR/HubFactory.cs | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/MareSynchronos/WebAPI/SignalR/ApiController.cs b/MareSynchronos/WebAPI/SignalR/ApiController.cs index 0f1b80d..11492ee 100644 --- a/MareSynchronos/WebAPI/SignalR/ApiController.cs +++ b/MareSynchronos/WebAPI/SignalR/ApiController.cs @@ -149,7 +149,7 @@ public sealed partial class ApiController : DisposableMediatorSubscriberBase, IM { new KeyValuePair("auth", auth), new KeyValuePair("charaIdent", await _dalamudUtil.GetPlayerNameHashedAsync().ConfigureAwait(false)), - })).ConfigureAwait(false); + }), token).ConfigureAwait(false); AuthFailureMessage = await result.Content.ReadAsStringAsync().ConfigureAwait(false); result.EnsureSuccessStatusCode(); _serverManager.SaveToken(await result.Content.ReadAsStringAsync().ConfigureAwait(false)); @@ -202,6 +202,11 @@ public sealed partial class ApiController : DisposableMediatorSubscriberBase, IM await LoadOnlinePairs().ConfigureAwait(false); } + catch (OperationCanceledException) + { + Logger.LogWarning("Connection attempt cancelled"); + return; + } catch (HttpRequestException ex) { Logger.LogWarning(ex, "HttpRequestException on Connection"); @@ -230,7 +235,7 @@ public sealed partial class ApiController : DisposableMediatorSubscriberBase, IM { CancellationTokenSource cts = new(); cts.CancelAfter(TimeSpan.FromSeconds(5)); - Task.Run(async () => + _ = Task.Run(async () => { var pair = _pairManager.GetOnlineUserPairs().Single(p => p.UserPair != null && p.UserData == userData); var perm = pair.UserPair!.OwnPermissions; @@ -404,17 +409,18 @@ public sealed partial class ApiController : DisposableMediatorSubscriberBase, IM { ServerState = ServerState.Disconnecting; + Logger.LogInformation("Stopping existing connection"); + await _hubFactory.DisposeHubAsync().ConfigureAwait(false); + if (_mareHub is not null) { _initialized = false; _healthCheckTokenSource?.Cancel(); - Logger.LogInformation("Stopping existing connection"); Mediator.Publish(new DisconnectedMessage()); _mareHub = null; _connectionDto = null; } - await _hubFactory.DisposeHubAsync().ConfigureAwait(false); ServerState = state; } diff --git a/MareSynchronos/WebAPI/SignalR/HubFactory.cs b/MareSynchronos/WebAPI/SignalR/HubFactory.cs index bf1a43b..bc7a9ac 100644 --- a/MareSynchronos/WebAPI/SignalR/HubFactory.cs +++ b/MareSynchronos/WebAPI/SignalR/HubFactory.cs @@ -117,5 +117,7 @@ public class HubFactory : MediatorSubscriberBase await _instance.DisposeAsync().ConfigureAwait(false); _instance = null; + + Logger.LogDebug("Current HubConnection disposed"); } }