1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 11:42:56 +08:00

Rename some members and extract connection closure to separate method

This commit is contained in:
Salman Ahmed 2021-02-11 10:49:16 +03:00
parent 0c5e66205b
commit 5fb99fdc52
3 changed files with 20 additions and 19 deletions

View File

@ -25,7 +25,7 @@ namespace osu.Game.Online
/// <summary>
/// Invoked whenever a new hub connection is built.
/// </summary>
public Action<HubConnection>? OnNewConnection;
public Action<HubConnection>? 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();

View File

@ -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

View File

@ -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)