diff --git a/osu.Game/Online/HubClientConnector.cs b/osu.Game/Online/HubClientConnector.cs index b740aabb92..65285882d9 100644 --- a/osu.Game/Online/HubClientConnector.cs +++ b/osu.Game/Online/HubClientConnector.cs @@ -25,7 +25,7 @@ namespace osu.Game.Online /// /// Invoked whenever a new hub connection is built. /// - public Action? OnNewConnection; + public Action? ConfigureConnection; private readonly string clientName; private readonly string endpoint; @@ -106,7 +106,7 @@ namespace osu.Game.Online try { // importantly, rebuild the connection each attempt to get an updated access token. - CurrentConnection = createConnection(cancellationToken); + CurrentConnection = buildConnection(cancellationToken); await CurrentConnection.StartAsync(cancellationToken); @@ -134,7 +134,7 @@ namespace osu.Game.Online } } - private HubConnection createConnection(CancellationToken cancellationToken) + private HubConnection buildConnection(CancellationToken cancellationToken) { Debug.Assert(api != null); @@ -152,24 +152,25 @@ namespace osu.Game.Online var newConnection = builder.Build(); - OnNewConnection?.Invoke(newConnection); - - newConnection.Closed += ex => - { - isConnected.Value = false; - - Logger.Log(ex != null ? $"{clientName} lost connection: {ex}" : $"{clientName} disconnected", LoggingTarget.Network); - - // make sure a disconnect wasn't triggered (and this is still the active connection). - if (!cancellationToken.IsCancellationRequested) - Task.Run(connect, default); - - return Task.CompletedTask; - }; + ConfigureConnection?.Invoke(newConnection); + newConnection.Closed += ex => onConnectionClosed(ex, cancellationToken); return newConnection; } + private Task onConnectionClosed(Exception? ex, CancellationToken cancellationToken) + { + isConnected.Value = false; + + Logger.Log(ex != null ? $"{clientName} lost connection: {ex}" : $"{clientName} disconnected", LoggingTarget.Network); + + // make sure a disconnect wasn't triggered (and this is still the active connection). + if (!cancellationToken.IsCancellationRequested) + Task.Run(connect, default); + + return Task.CompletedTask; + } + private async Task disconnect(bool takeLock) { cancelExistingConnect(); diff --git a/osu.Game/Online/Multiplayer/MultiplayerClient.cs b/osu.Game/Online/Multiplayer/MultiplayerClient.cs index f025a5b429..ba2a8d7246 100644 --- a/osu.Game/Online/Multiplayer/MultiplayerClient.cs +++ b/osu.Game/Online/Multiplayer/MultiplayerClient.cs @@ -33,7 +33,7 @@ namespace osu.Game.Online.Multiplayer { connector = new HubClientConnector(nameof(MultiplayerClient), endpoint, api) { - OnNewConnection = connection => + ConfigureConnection = connection => { // this is kind of SILLY // https://github.com/dotnet/aspnetcore/issues/15198 diff --git a/osu.Game/Online/Spectator/SpectatorStreamingClient.cs b/osu.Game/Online/Spectator/SpectatorStreamingClient.cs index 4ef59b5e47..532f717f2c 100644 --- a/osu.Game/Online/Spectator/SpectatorStreamingClient.cs +++ b/osu.Game/Online/Spectator/SpectatorStreamingClient.cs @@ -86,7 +86,7 @@ namespace osu.Game.Online.Spectator private void load(IAPIProvider api) { connector = CreateConnector(nameof(SpectatorStreamingClient), endpoint, api); - connector.OnNewConnection = connection => + connector.ConfigureConnection = connection => { // until strong typed client support is added, each method must be manually bound // (see https://github.com/dotnet/aspnetcore/issues/15198)