1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 02:22:56 +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)
{
// do not pass API to prevent attempting failing connections on an actual hub.
return base.CreateConnector(name, endpoint, null);
}
protected override HubClientConnector CreateConnector(string name, string endpoint, IAPIProvider api) => null;
public void StartPlay(int beatmapId)
{

View File

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

View File

@ -29,7 +29,7 @@ namespace osu.Game.Online
private readonly string clientName;
private readonly string endpoint;
private readonly IAPIProvider? api;
private readonly IAPIProvider api;
/// <summary>
/// The current connection opened by this connector.
@ -52,32 +52,28 @@ namespace osu.Game.Online
/// </summary>
/// <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="api"> An API provider used to react to connection state changes, or null to not establish connection at all (for testing purposes).</param>
public HubClientConnector(string clientName, string endpoint, IAPIProvider? api)
/// <param name="api"> An API provider used to react to connection state changes.</param>
public HubClientConnector(string clientName, string endpoint, IAPIProvider api)
{
this.clientName = clientName;
this.endpoint = endpoint;
this.api = api;
if (api != null)
apiState.BindTo(api.State);
apiState.BindValueChanged(state =>
{
apiState.BindTo(api.State);
apiState.BindValueChanged(state =>
switch (state.NewValue)
{
switch (state.NewValue)
{
case APIState.Failing:
case APIState.Offline:
Task.Run(() => disconnect(true));
break;
case APIState.Failing:
case APIState.Offline:
Task.Run(() => disconnect(true));
break;
case APIState.Online:
Task.Run(connect);
break;
}
}, true);
}
case APIState.Online:
Task.Run(connect);
break;
}
}, true);
}
private async Task connect()
@ -136,8 +132,6 @@ namespace osu.Game.Online
private HubConnection buildConnection(CancellationToken cancellationToken)
{
Debug.Assert(api != null);
var builder = new HubConnectionBuilder()
.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;
[CanBeNull]
private HubClientConnector connector;
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>();
@ -86,42 +87,46 @@ namespace osu.Game.Online.Spectator
private void load(IAPIProvider 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);
isConnected.BindValueChanged(connected =>
if (connector != null)
{
if (connected.NewValue)
connector.ConfigureConnection = connection =>
{
// get all the users that were previously being watched
int[] users;
// 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);
};
lock (userLock)
isConnected.BindTo(connector.IsConnected);
isConnected.BindValueChanged(connected =>
{
if (connected.NewValue)
{
users = watchingUsers.ToArray();
watchingUsers.Clear();
// get all the users that were previously being watched
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();
}
// resubscribe to watched users.
foreach (var userId in users)
WatchUser(userId);
// re-send state in case it wasn't received
if (isPlaying)
beginPlaying();
}
else
{
playingUsers.Clear();
}
}, true);
else
{
playingUsers.Clear();
}
}, true);
}
}
protected virtual HubClientConnector CreateConnector(string name, string endpoint, IAPIProvider api) => new HubClientConnector(name, endpoint, api);