1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 11:42:54 +08:00

Merge pull request #18421 from peppy/reconnect-on-server-shutdown-exception

Handle server shutdown messages in room creation and spectator initialisation
This commit is contained in:
Dean Herbert 2022-05-28 03:52:24 +09:00 committed by GitHub
commit ca9b2648a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 25 deletions

View File

@ -20,6 +20,8 @@ namespace osu.Game.Online
{
public class HubClientConnector : IHubClientConnector
{
public const string SERVER_SHUTDOWN_MESSAGE = "Server is shutting down.";
/// <summary>
/// Invoked whenever a new hub connection is built, to configure it before it's started.
/// </summary>
@ -64,20 +66,28 @@ namespace osu.Game.Online
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()

View File

@ -30,5 +30,10 @@ namespace osu.Game.Online
/// Invoked whenever a new hub connection is built, to configure it before it's started.
/// </summary>
public Action<HubConnection>? ConfigureConnection { get; set; }
/// <summary>
/// Reconnect if already connected.
/// </summary>
void Reconnect();
}
}

View File

@ -25,12 +25,7 @@ namespace osu.Game.Online.Multiplayer
Debug.Assert(exception != null);
string message = exception is HubException
// HubExceptions arrive with additional message context added, but we want to display the human readable message:
// "An unexpected error occurred invoking 'AddPlaylistItem' on the server.InvalidStateException: Can't enqueue more than 3 items at once."
// We generally use the message field for a user-parseable error (eventually to be replaced), so drop the first part for now.
? exception.Message.Substring(exception.Message.IndexOf(':') + 1).Trim()
: exception.Message;
string message = exception.GetHubExceptionMessage() ?? exception.Message;
Logger.Log(message, level: LogLevel.Important);
onError?.Invoke(exception);
@ -40,5 +35,16 @@ namespace osu.Game.Online.Multiplayer
onSuccess?.Invoke();
}
});
public static string? GetHubExceptionMessage(this Exception exception)
{
if (exception is HubException hubException)
// HubExceptions arrive with additional message context added, but we want to display the human readable message:
// "An unexpected error occurred invoking 'AddPlaylistItem' on the server.InvalidStateException: Can't enqueue more than 3 items at once."
// We generally use the message field for a user-parseable error (eventually to be replaced), so drop the first part for now.
return hubException.Message.Substring(exception.Message.IndexOf(':') + 1).Trim();
return null;
}
}
}

View File

@ -3,10 +3,12 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
@ -71,14 +73,23 @@ namespace osu.Game.Online.Multiplayer
}
}
protected override Task<MultiplayerRoom> JoinRoom(long roomId, string? password = null)
protected override async Task<MultiplayerRoom> JoinRoom(long roomId, string? password = null)
{
if (!IsConnected.Value)
return Task.FromCanceled<MultiplayerRoom>(new CancellationToken(true));
throw new OperationCanceledException();
Debug.Assert(connection != null);
return connection.InvokeAsync<MultiplayerRoom>(nameof(IMultiplayerServer.JoinRoomWithPassword), roomId, password ?? string.Empty);
try
{
return await connection.InvokeAsync<MultiplayerRoom>(nameof(IMultiplayerServer.JoinRoomWithPassword), roomId, password ?? string.Empty);
}
catch (HubException exception)
{
if (exception.GetHubExceptionMessage() == HubClientConnector.SERVER_SHUTDOWN_MESSAGE)
connector?.Reconnect();
throw;
}
}
protected override Task LeaveRoomInternal()

View File

@ -5,10 +5,12 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Online.API;
using osu.Game.Online.Multiplayer;
namespace osu.Game.Online.Spectator
{
@ -47,14 +49,23 @@ namespace osu.Game.Online.Spectator
}
}
protected override Task BeginPlayingInternal(SpectatorState state)
protected override async Task BeginPlayingInternal(SpectatorState state)
{
if (!IsConnected.Value)
return Task.CompletedTask;
return;
Debug.Assert(connection != null);
return connection.SendAsync(nameof(ISpectatorServer.BeginPlaySession), state);
try
{
await connection.SendAsync(nameof(ISpectatorServer.BeginPlaySession), state);
}
catch (HubException exception)
{
if (exception.GetHubExceptionMessage() == HubClientConnector.SERVER_SHUTDOWN_MESSAGE)
connector?.Reconnect();
throw;
}
}
protected override Task SendFramesInternal(FrameDataBundle bundle)