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> /// <summary>
/// Invoked whenever a new hub connection is built. /// Invoked whenever a new hub connection is built.
/// </summary> /// </summary>
public Action<HubConnection>? OnNewConnection; public Action<HubConnection>? ConfigureConnection;
private readonly string clientName; private readonly string clientName;
private readonly string endpoint; private readonly string endpoint;
@ -106,7 +106,7 @@ namespace osu.Game.Online
try try
{ {
// importantly, rebuild the connection each attempt to get an updated access token. // importantly, rebuild the connection each attempt to get an updated access token.
CurrentConnection = createConnection(cancellationToken); CurrentConnection = buildConnection(cancellationToken);
await CurrentConnection.StartAsync(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); Debug.Assert(api != null);
@ -152,24 +152,25 @@ namespace osu.Game.Online
var newConnection = builder.Build(); var newConnection = builder.Build();
OnNewConnection?.Invoke(newConnection); ConfigureConnection?.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;
};
newConnection.Closed += ex => onConnectionClosed(ex, cancellationToken);
return newConnection; 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) private async Task disconnect(bool takeLock)
{ {
cancelExistingConnect(); cancelExistingConnect();

View File

@ -33,7 +33,7 @@ namespace osu.Game.Online.Multiplayer
{ {
connector = new HubClientConnector(nameof(MultiplayerClient), endpoint, api) connector = new HubClientConnector(nameof(MultiplayerClient), endpoint, api)
{ {
OnNewConnection = connection => ConfigureConnection = connection =>
{ {
// this is kind of SILLY // this is kind of SILLY
// https://github.com/dotnet/aspnetcore/issues/15198 // https://github.com/dotnet/aspnetcore/issues/15198

View File

@ -86,7 +86,7 @@ namespace osu.Game.Online.Spectator
private void load(IAPIProvider api) private void load(IAPIProvider api)
{ {
connector = CreateConnector(nameof(SpectatorStreamingClient), endpoint, 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 // until strong typed client support is added, each method must be manually bound
// (see https://github.com/dotnet/aspnetcore/issues/15198) // (see https://github.com/dotnet/aspnetcore/issues/15198)