From 63dd9bd49372f0f051d86124fcd6b0a71a95ea1e Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 26 May 2022 18:02:50 +0900 Subject: [PATCH] Expose `Reconnect` logic in `HubClientConnector` --- osu.Game/Online/HubClientConnector.cs | 34 ++++++++++++++++---------- osu.Game/Online/IHubClientConnector.cs | 5 ++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/osu.Game/Online/HubClientConnector.cs b/osu.Game/Online/HubClientConnector.cs index ca9bf00b23..5c11eed381 100644 --- a/osu.Game/Online/HubClientConnector.cs +++ b/osu.Game/Online/HubClientConnector.cs @@ -64,20 +64,28 @@ public HubClientConnector(string clientName, string endpoint, IAPIProvider api, this.preferMessagePack = preferMessagePack; apiState.BindTo(api.State); - apiState.BindValueChanged(state => - { - switch (state.NewValue) - { - case APIState.Failing: - case APIState.Offline: - Task.Run(() => disconnect(true)); - break; + apiState.BindValueChanged(state => connectIfPossible(), true); + } - case APIState.Online: - Task.Run(connect); - break; - } - }, true); + public void Reconnect() + { + Logger.Log($"{clientName} reconnecting...", LoggingTarget.Network); + Task.Run(connectIfPossible); + } + + private void connectIfPossible() + { + switch (apiState.Value) + { + case APIState.Failing: + case APIState.Offline: + Task.Run(() => disconnect(true)); + break; + + case APIState.Online: + Task.Run(connect); + break; + } } private async Task connect() diff --git a/osu.Game/Online/IHubClientConnector.cs b/osu.Game/Online/IHubClientConnector.cs index d2ceb1f030..b168e4669f 100644 --- a/osu.Game/Online/IHubClientConnector.cs +++ b/osu.Game/Online/IHubClientConnector.cs @@ -30,5 +30,10 @@ public interface IHubClientConnector : IDisposable /// Invoked whenever a new hub connection is built, to configure it before it's started. /// public Action? ConfigureConnection { get; set; } + + /// + /// Reconnect if already connected. + /// + void Reconnect(); } }