2020-12-20 22:26:31 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
using System;
|
2021-02-01 16:57:32 +08:00
|
|
|
using System.Collections.Generic;
|
2020-12-20 22:26:31 +08:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Newtonsoft.Json;
|
2021-01-28 15:20:16 +08:00
|
|
|
using osu.Framework;
|
2020-12-20 22:26:31 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Game.Online.API;
|
2021-01-03 09:32:50 +08:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-12-20 22:26:31 +08:00
|
|
|
|
2020-12-25 12:38:11 +08:00
|
|
|
namespace osu.Game.Online.Multiplayer
|
2020-12-20 22:26:31 +08:00
|
|
|
{
|
2020-12-25 12:38:11 +08:00
|
|
|
public class MultiplayerClient : StatefulMultiplayerClient
|
2020-12-20 22:26:31 +08:00
|
|
|
{
|
|
|
|
public override IBindable<bool> IsConnected => isConnected;
|
|
|
|
|
|
|
|
private readonly Bindable<bool> isConnected = new Bindable<bool>();
|
|
|
|
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
|
|
|
|
|
2021-01-25 16:02:24 +08:00
|
|
|
private readonly SemaphoreSlim connectionLock = new SemaphoreSlim(1);
|
|
|
|
|
2020-12-20 22:26:31 +08:00
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; } = null!;
|
|
|
|
|
|
|
|
private HubConnection? connection;
|
|
|
|
|
2021-01-22 13:25:21 +08:00
|
|
|
private CancellationTokenSource connectCancelSource = new CancellationTokenSource();
|
|
|
|
|
2020-12-24 16:58:38 +08:00
|
|
|
private readonly string endpoint;
|
|
|
|
|
2020-12-25 12:38:11 +08:00
|
|
|
public MultiplayerClient(EndpointConfiguration endpoints)
|
2020-12-24 16:58:38 +08:00
|
|
|
{
|
2020-12-24 17:11:40 +08:00
|
|
|
endpoint = endpoints.MultiplayerEndpointUrl;
|
2020-12-24 16:58:38 +08:00
|
|
|
}
|
|
|
|
|
2020-12-20 22:26:31 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
apiState.BindTo(api.State);
|
|
|
|
apiState.BindValueChanged(apiStateChanged, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void apiStateChanged(ValueChangedEvent<APIState> state)
|
|
|
|
{
|
|
|
|
switch (state.NewValue)
|
|
|
|
{
|
|
|
|
case APIState.Failing:
|
|
|
|
case APIState.Offline:
|
2021-02-05 13:04:57 +08:00
|
|
|
Task.Run(() => disconnect(true));
|
2020-12-20 22:26:31 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case APIState.Online:
|
2021-02-05 13:04:57 +08:00
|
|
|
Task.Run(connect);
|
2020-12-20 22:26:31 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 16:02:24 +08:00
|
|
|
private async Task connect()
|
2020-12-20 22:26:31 +08:00
|
|
|
{
|
2021-01-22 13:25:21 +08:00
|
|
|
cancelExistingConnect();
|
2021-01-28 15:20:16 +08:00
|
|
|
|
2021-02-05 13:08:11 +08:00
|
|
|
if (!await connectionLock.WaitAsync(10000))
|
|
|
|
throw new TimeoutException("Could not obtain a lock to connect. A previous attempt is likely stuck.");
|
2020-12-20 22:26:31 +08:00
|
|
|
|
2021-01-22 13:25:21 +08:00
|
|
|
try
|
2020-12-20 22:26:31 +08:00
|
|
|
{
|
2021-01-22 13:34:58 +08:00
|
|
|
while (api.State.Value == APIState.Online)
|
2020-12-20 22:26:31 +08:00
|
|
|
{
|
2021-01-26 17:59:42 +08:00
|
|
|
// ensure any previous connection was disposed.
|
2021-01-27 04:26:50 +08:00
|
|
|
// this will also create a new cancellation token source.
|
2021-01-26 17:59:42 +08:00
|
|
|
await disconnect(false);
|
2021-01-02 15:47:00 +08:00
|
|
|
|
2021-01-27 04:26:50 +08:00
|
|
|
// this token will be valid for the scope of this connection.
|
|
|
|
// if cancelled, we can be sure that a disconnect or reconnect is handled elsewhere.
|
|
|
|
var cancellationToken = connectCancelSource.Token;
|
2020-12-20 22:26:31 +08:00
|
|
|
|
2021-01-22 13:34:58 +08:00
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2020-12-20 22:26:31 +08:00
|
|
|
|
2021-01-22 13:25:21 +08:00
|
|
|
Logger.Log("Multiplayer client connecting...", LoggingTarget.Network);
|
2020-12-20 22:26:31 +08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-01-22 13:25:21 +08:00
|
|
|
// importantly, rebuild the connection each attempt to get an updated access token.
|
|
|
|
connection = createConnection(cancellationToken);
|
2020-12-22 14:15:32 +08:00
|
|
|
|
2021-01-22 13:25:21 +08:00
|
|
|
await connection.StartAsync(cancellationToken);
|
2020-12-20 22:26:31 +08:00
|
|
|
|
2021-01-22 13:25:21 +08:00
|
|
|
Logger.Log("Multiplayer client connected!", LoggingTarget.Network);
|
2020-12-20 22:26:31 +08:00
|
|
|
isConnected.Value = true;
|
2021-01-22 13:25:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
//connection process was cancelled.
|
2021-01-22 13:34:58 +08:00
|
|
|
throw;
|
2020-12-20 22:26:31 +08:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Log($"Multiplayer client connection error: {e}", LoggingTarget.Network);
|
2021-01-22 13:25:21 +08:00
|
|
|
|
|
|
|
// retry on any failure.
|
|
|
|
await Task.Delay(5000, cancellationToken);
|
2020-12-20 22:26:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 13:25:21 +08:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
connectionLock.Release();
|
|
|
|
}
|
2020-12-20 22:26:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override Task<MultiplayerRoom> JoinRoom(long roomId)
|
|
|
|
{
|
|
|
|
if (!isConnected.Value)
|
2020-12-24 00:01:01 +08:00
|
|
|
return Task.FromCanceled<MultiplayerRoom>(new CancellationToken(true));
|
2020-12-20 22:26:31 +08:00
|
|
|
|
|
|
|
return connection.InvokeAsync<MultiplayerRoom>(nameof(IMultiplayerServer.JoinRoom), roomId);
|
|
|
|
}
|
|
|
|
|
2021-01-25 19:41:51 +08:00
|
|
|
protected override Task LeaveRoomInternal()
|
2020-12-20 22:26:31 +08:00
|
|
|
{
|
|
|
|
if (!isConnected.Value)
|
2021-01-25 19:41:51 +08:00
|
|
|
return Task.FromCanceled(new CancellationToken(true));
|
2020-12-20 22:26:31 +08:00
|
|
|
|
2021-01-25 19:41:51 +08:00
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.LeaveRoom));
|
2020-12-20 22:26:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override Task TransferHost(int userId)
|
|
|
|
{
|
|
|
|
if (!isConnected.Value)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.TransferHost), userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Task ChangeSettings(MultiplayerRoomSettings settings)
|
|
|
|
{
|
|
|
|
if (!isConnected.Value)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.ChangeSettings), settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Task ChangeState(MultiplayerUserState newState)
|
|
|
|
{
|
|
|
|
if (!isConnected.Value)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.ChangeState), newState);
|
|
|
|
}
|
|
|
|
|
2021-01-03 09:32:50 +08:00
|
|
|
public override Task ChangeBeatmapAvailability(BeatmapAvailability newBeatmapAvailability)
|
|
|
|
{
|
|
|
|
if (!isConnected.Value)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.ChangeBeatmapAvailability), newBeatmapAvailability);
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:57:32 +08:00
|
|
|
public override Task ChangeUserMods(IEnumerable<APIMod> newMods)
|
|
|
|
{
|
|
|
|
if (!isConnected.Value)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.ChangeUserMods), newMods);
|
|
|
|
}
|
|
|
|
|
2020-12-20 22:26:31 +08:00
|
|
|
public override Task StartMatch()
|
|
|
|
{
|
|
|
|
if (!isConnected.Value)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.StartMatch));
|
|
|
|
}
|
2021-01-22 13:25:21 +08:00
|
|
|
|
|
|
|
private async Task disconnect(bool takeLock)
|
|
|
|
{
|
|
|
|
cancelExistingConnect();
|
|
|
|
|
|
|
|
if (takeLock)
|
2021-02-05 13:08:11 +08:00
|
|
|
{
|
|
|
|
if (!await connectionLock.WaitAsync(10000))
|
|
|
|
throw new TimeoutException("Could not obtain a lock to disconnect. A previous attempt is likely stuck.");
|
|
|
|
}
|
2021-01-22 13:25:21 +08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (connection != null)
|
2021-01-25 16:11:04 +08:00
|
|
|
await connection.DisposeAsync();
|
2021-01-22 13:25:21 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
connection = null;
|
|
|
|
if (takeLock)
|
|
|
|
connectionLock.Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cancelExistingConnect()
|
|
|
|
{
|
|
|
|
connectCancelSource.Cancel();
|
|
|
|
connectCancelSource = new CancellationTokenSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
private HubConnection createConnection(CancellationToken cancellationToken)
|
|
|
|
{
|
2021-02-08 16:51:57 +08:00
|
|
|
var builder = new HubConnectionBuilder()
|
|
|
|
.WithUrl(endpoint, options => { options.Headers.Add("Authorization", $"Bearer {api.AccessToken}"); });
|
|
|
|
|
|
|
|
if (RuntimeInfo.SupportsJIT)
|
|
|
|
builder.AddMessagePackProtocol();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// eventually we will precompile resolvers for messagepack, but this isn't working currently
|
|
|
|
// see https://github.com/neuecc/MessagePack-CSharp/issues/780#issuecomment-768794308.
|
|
|
|
builder.AddNewtonsoftJsonProtocol(options => { options.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; });
|
|
|
|
}
|
|
|
|
|
|
|
|
var newConnection = builder.Build();
|
2021-01-22 13:25:21 +08:00
|
|
|
|
|
|
|
// this is kind of SILLY
|
|
|
|
// https://github.com/dotnet/aspnetcore/issues/15198
|
|
|
|
newConnection.On<MultiplayerRoomState>(nameof(IMultiplayerClient.RoomStateChanged), ((IMultiplayerClient)this).RoomStateChanged);
|
|
|
|
newConnection.On<MultiplayerRoomUser>(nameof(IMultiplayerClient.UserJoined), ((IMultiplayerClient)this).UserJoined);
|
|
|
|
newConnection.On<MultiplayerRoomUser>(nameof(IMultiplayerClient.UserLeft), ((IMultiplayerClient)this).UserLeft);
|
|
|
|
newConnection.On<int>(nameof(IMultiplayerClient.HostChanged), ((IMultiplayerClient)this).HostChanged);
|
|
|
|
newConnection.On<MultiplayerRoomSettings>(nameof(IMultiplayerClient.SettingsChanged), ((IMultiplayerClient)this).SettingsChanged);
|
|
|
|
newConnection.On<int, MultiplayerUserState>(nameof(IMultiplayerClient.UserStateChanged), ((IMultiplayerClient)this).UserStateChanged);
|
|
|
|
newConnection.On(nameof(IMultiplayerClient.LoadRequested), ((IMultiplayerClient)this).LoadRequested);
|
|
|
|
newConnection.On(nameof(IMultiplayerClient.MatchStarted), ((IMultiplayerClient)this).MatchStarted);
|
|
|
|
newConnection.On(nameof(IMultiplayerClient.ResultsReady), ((IMultiplayerClient)this).ResultsReady);
|
2021-02-08 12:43:24 +08:00
|
|
|
newConnection.On<int, IEnumerable<APIMod>>(nameof(IMultiplayerClient.UserModsChanged), ((IMultiplayerClient)this).UserModsChanged);
|
2021-02-08 18:08:53 +08:00
|
|
|
newConnection.On<int, BeatmapAvailability>(nameof(IMultiplayerClient.UserBeatmapAvailabilityChanged), ((IMultiplayerClient)this).UserBeatmapAvailabilityChanged);
|
2021-01-22 13:25:21 +08:00
|
|
|
|
2021-01-22 13:34:58 +08:00
|
|
|
newConnection.Closed += ex =>
|
2021-01-22 13:25:21 +08:00
|
|
|
{
|
|
|
|
isConnected.Value = false;
|
|
|
|
|
|
|
|
Logger.Log(ex != null ? $"Multiplayer client lost connection: {ex}" : "Multiplayer client disconnected", LoggingTarget.Network);
|
|
|
|
|
|
|
|
// make sure a disconnect wasn't triggered (and this is still the active connection).
|
|
|
|
if (!cancellationToken.IsCancellationRequested)
|
2021-02-05 13:04:57 +08:00
|
|
|
Task.Run(connect, default);
|
2021-01-22 13:34:58 +08:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2021-01-22 13:25:21 +08:00
|
|
|
};
|
|
|
|
return newConnection;
|
|
|
|
}
|
2021-01-25 16:17:04 +08:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
cancelExistingConnect();
|
|
|
|
}
|
2020-12-20 22:26:31 +08:00
|
|
|
}
|
|
|
|
}
|