Remove necessity of making hub client connector a component

This commit is contained in:
Salman Ahmed
2021-02-09 07:53:22 +03:00
parent 71e564d399
commit 848b81e952

View File

@ -4,15 +4,14 @@
#nullable enable #nullable enable
using System; using System;
using System.Diagnostics;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client; using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json; using Newtonsoft.Json;
using osu.Framework; using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Game.Online.API; using osu.Game.Online.API;
@ -21,7 +20,7 @@ namespace osu.Game.Online
/// <summary> /// <summary>
/// A component that maintains over a hub connection between client and server. /// A component that maintains over a hub connection between client and server.
/// </summary> /// </summary>
public class HubClientConnector : Component public class HubClientConnector : IDisposable
{ {
/// <summary> /// <summary>
/// Invoked whenever a new hub connection is built. /// Invoked whenever a new hub connection is built.
@ -30,6 +29,7 @@ namespace osu.Game.Online
private readonly string clientName; private readonly string clientName;
private readonly string endpoint; private readonly string endpoint;
private readonly IAPIProvider? api;
/// <summary> /// <summary>
/// The current connection opened by this connector. /// The current connection opened by this connector.
@ -45,9 +45,6 @@ namespace osu.Game.Online
private readonly SemaphoreSlim connectionLock = new SemaphoreSlim(1); private readonly SemaphoreSlim connectionLock = new SemaphoreSlim(1);
private CancellationTokenSource connectCancelSource = new CancellationTokenSource(); private CancellationTokenSource connectCancelSource = new CancellationTokenSource();
[Resolved]
private IAPIProvider api { get; set; } = null!;
private readonly IBindable<APIState> apiState = new Bindable<APIState>(); private readonly IBindable<APIState> apiState = new Bindable<APIState>();
/// <summary> /// <summary>
@ -55,30 +52,32 @@ namespace osu.Game.Online
/// </summary> /// </summary>
/// <param name="clientName">The name of the client this connector connects for, used for logging.</param> /// <param name="clientName">The name of the client this connector connects for, used for logging.</param>
/// <param name="endpoint">The endpoint to the hub.</param> /// <param name="endpoint">The endpoint to the hub.</param>
public HubClientConnector(string clientName, string endpoint) /// <param name="api">The API provider for listening to state changes, or null to not listen.</param>
public HubClientConnector(string clientName, string endpoint, IAPIProvider? api)
{ {
this.clientName = clientName; this.clientName = clientName;
this.endpoint = endpoint; this.endpoint = endpoint;
}
[BackgroundDependencyLoader] this.api = api;
private void load()
{ if (api != null)
apiState.BindTo(api.State);
apiState.BindValueChanged(state =>
{ {
switch (state.NewValue) apiState.BindTo(api.State);
apiState.BindValueChanged(state =>
{ {
case APIState.Failing: switch (state.NewValue)
case APIState.Offline: {
Task.Run(() => disconnect(true)); case APIState.Failing:
break; case APIState.Offline:
Task.Run(() => disconnect(true));
break;
case APIState.Online: case APIState.Online:
Task.Run(connect); Task.Run(connect);
break; break;
} }
}); }, true);
}
} }
private async Task connect() private async Task connect()
@ -137,6 +136,8 @@ namespace osu.Game.Online
private HubConnection createConnection(CancellationToken cancellationToken) private HubConnection createConnection(CancellationToken cancellationToken)
{ {
Debug.Assert(api != null);
var builder = new HubConnectionBuilder() var builder = new HubConnectionBuilder()
.WithUrl(endpoint, options => { options.Headers.Add("Authorization", $"Bearer {api.AccessToken}"); }); .WithUrl(endpoint, options => { options.Headers.Add("Authorization", $"Bearer {api.AccessToken}"); });
@ -200,9 +201,9 @@ namespace osu.Game.Online
public override string ToString() => $"Connector for {clientName} ({(IsConnected.Value ? "connected" : "not connected")}"; public override string ToString() => $"Connector for {clientName} ({(IsConnected.Value ? "connected" : "not connected")}";
protected override void Dispose(bool isDisposing) public void Dispose()
{ {
base.Dispose(isDisposing); apiState.UnbindAll();
cancelExistingConnect(); cancelExistingConnect();
} }
} }