1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 08:55:35 +08:00

Replace nullable API with null connector instead

This commit is contained in:
Salman Ahmed 2021-02-11 12:32:54 +03:00
parent 18acd7f080
commit d3c1b47592
4 changed files with 53 additions and 62 deletions

View File

@ -244,11 +244,7 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
} }
protected override HubClientConnector CreateConnector(string name, string endpoint, IAPIProvider api) protected override HubClientConnector CreateConnector(string name, string endpoint, IAPIProvider api) => null;
{
// do not pass API to prevent attempting failing connections on an actual hub.
return base.CreateConnector(name, endpoint, null);
}
public void StartPlay(int beatmapId) public void StartPlay(int beatmapId)
{ {

View File

@ -106,11 +106,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
this.totalUsers = totalUsers; this.totalUsers = totalUsers;
} }
protected override HubClientConnector CreateConnector(string name, string endpoint, IAPIProvider api) protected override HubClientConnector CreateConnector(string name, string endpoint, IAPIProvider api) => null;
{
// do not pass API to prevent attempting failing connections on an actual hub.
return base.CreateConnector(name, endpoint, null);
}
public void Start(int beatmapId) public void Start(int beatmapId)
{ {

View File

@ -29,7 +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; private readonly IAPIProvider api;
/// <summary> /// <summary>
/// The current connection opened by this connector. /// The current connection opened by this connector.
@ -52,32 +52,28 @@ 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>
/// <param name="api"> An API provider used to react to connection state changes, or null to not establish connection at all (for testing purposes).</param> /// <param name="api"> An API provider used to react to connection state changes.</param>
public HubClientConnector(string clientName, string endpoint, IAPIProvider? api) public HubClientConnector(string clientName, string endpoint, IAPIProvider api)
{ {
this.clientName = clientName; this.clientName = clientName;
this.endpoint = endpoint; this.endpoint = endpoint;
this.api = api; this.api = api;
if (api != null) apiState.BindTo(api.State);
apiState.BindValueChanged(state =>
{ {
apiState.BindTo(api.State); switch (state.NewValue)
apiState.BindValueChanged(state =>
{ {
switch (state.NewValue) case APIState.Failing:
{ case APIState.Offline:
case APIState.Failing: Task.Run(() => disconnect(true));
case APIState.Offline: break;
Task.Run(() => disconnect(true));
break;
case APIState.Online: case APIState.Online:
Task.Run(connect); Task.Run(connect);
break; break;
} }
}, true); }, true);
}
} }
private async Task connect() private async Task connect()
@ -136,8 +132,6 @@ namespace osu.Game.Online
private HubConnection buildConnection(CancellationToken cancellationToken) private HubConnection buildConnection(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}"); });

View File

@ -32,11 +32,12 @@ namespace osu.Game.Online.Spectator
private readonly string endpoint; private readonly string endpoint;
[CanBeNull]
private HubClientConnector connector; private HubClientConnector connector;
private readonly IBindable<bool> isConnected = new BindableBool(); private readonly IBindable<bool> isConnected = new BindableBool();
private HubConnection connection => connector.CurrentConnection; private HubConnection connection => connector?.CurrentConnection;
private readonly List<int> watchingUsers = new List<int>(); private readonly List<int> watchingUsers = new List<int>();
@ -86,42 +87,46 @@ 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.ConfigureConnection = connection =>
{
// until strong typed client support is added, each method must be manually bound
// (see https://github.com/dotnet/aspnetcore/issues/15198)
connection.On<int, SpectatorState>(nameof(ISpectatorClient.UserBeganPlaying), ((ISpectatorClient)this).UserBeganPlaying);
connection.On<int, FrameDataBundle>(nameof(ISpectatorClient.UserSentFrames), ((ISpectatorClient)this).UserSentFrames);
connection.On<int, SpectatorState>(nameof(ISpectatorClient.UserFinishedPlaying), ((ISpectatorClient)this).UserFinishedPlaying);
};
isConnected.BindTo(connector.IsConnected); if (connector != null)
isConnected.BindValueChanged(connected =>
{ {
if (connected.NewValue) connector.ConfigureConnection = connection =>
{ {
// get all the users that were previously being watched // until strong typed client support is added, each method must be manually bound
int[] users; // (see https://github.com/dotnet/aspnetcore/issues/15198)
connection.On<int, SpectatorState>(nameof(ISpectatorClient.UserBeganPlaying), ((ISpectatorClient)this).UserBeganPlaying);
connection.On<int, FrameDataBundle>(nameof(ISpectatorClient.UserSentFrames), ((ISpectatorClient)this).UserSentFrames);
connection.On<int, SpectatorState>(nameof(ISpectatorClient.UserFinishedPlaying), ((ISpectatorClient)this).UserFinishedPlaying);
};
lock (userLock) isConnected.BindTo(connector.IsConnected);
isConnected.BindValueChanged(connected =>
{
if (connected.NewValue)
{ {
users = watchingUsers.ToArray(); // get all the users that were previously being watched
watchingUsers.Clear(); int[] users;
lock (userLock)
{
users = watchingUsers.ToArray();
watchingUsers.Clear();
}
// resubscribe to watched users.
foreach (var userId in users)
WatchUser(userId);
// re-send state in case it wasn't received
if (isPlaying)
beginPlaying();
} }
else
// resubscribe to watched users. {
foreach (var userId in users) playingUsers.Clear();
WatchUser(userId); }
}, true);
// re-send state in case it wasn't received }
if (isPlaying)
beginPlaying();
}
else
{
playingUsers.Clear();
}
}, true);
} }
protected virtual HubClientConnector CreateConnector(string name, string endpoint, IAPIProvider api) => new HubClientConnector(name, endpoint, api); protected virtual HubClientConnector CreateConnector(string name, string endpoint, IAPIProvider api) => new HubClientConnector(name, endpoint, api);