1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 06:07:25 +08:00
osu-lazer/osu.Game/Online/Metadata/OnlineMetadataClient.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

246 lines
9.0 KiB
C#
Raw Normal View History

2022-07-04 16:18:33 +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.
using System;
2022-07-04 16:49:08 +08:00
using System.Diagnostics;
using System.Threading;
2022-07-04 16:18:33 +08:00
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using osu.Framework.Allocation;
2022-07-04 16:49:08 +08:00
using osu.Framework.Bindables;
2022-07-04 16:18:33 +08:00
using osu.Framework.Logging;
using osu.Game.Configuration;
2022-07-04 16:18:33 +08:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Users;
2022-07-04 16:18:33 +08:00
namespace osu.Game.Online.Metadata
{
public partial class OnlineMetadataClient : MetadataClient
{
public override IBindable<bool> IsConnected { get; } = new Bindable<bool>();
public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence;
private readonly BindableBool isWatchingUserPresence = new BindableBool();
public override IBindableDictionary<int, UserPresence> UserStates => userStates;
private readonly BindableDictionary<int, UserPresence> userStates = new BindableDictionary<int, UserPresence>();
2022-07-04 16:18:33 +08:00
private readonly string endpoint;
private IHubClientConnector? connector;
private Bindable<int> lastQueueId = null!;
2022-07-04 16:49:08 +08:00
private IBindable<APIUser> localUser = null!;
private IBindable<UserActivity?> userActivity = null!;
private IBindable<UserStatus?>? userStatus;
2022-07-04 16:18:33 +08:00
private HubConnection? connection => connector?.CurrentConnection;
public OnlineMetadataClient(EndpointConfiguration endpoints)
2022-07-04 16:18:33 +08:00
{
endpoint = endpoints.MetadataEndpointUrl;
}
[BackgroundDependencyLoader]
private void load(IAPIProvider api, OsuConfigManager config)
2022-07-04 16:18:33 +08:00
{
// Importantly, we are intentionally not using MessagePack here to correctly support derived class serialization.
// More information on the limitations / reasoning can be found in osu-server-spectator's initialisation code.
connector = api.GetHubConnector(nameof(OnlineMetadataClient), endpoint, false);
2022-07-04 16:18:33 +08:00
if (connector != null)
{
connector.ConfigureConnection = connection =>
{
// this is kind of SILLY
// https://github.com/dotnet/aspnetcore/issues/15198
connection.On<BeatmapUpdates>(nameof(IMetadataClient.BeatmapSetsUpdated), ((IMetadataClient)this).BeatmapSetsUpdated);
connection.On<int, UserPresence?>(nameof(IMetadataClient.UserPresenceUpdated), ((IMetadataClient)this).UserPresenceUpdated);
connection.On(nameof(IStatefulUserHubClient.DisconnectRequested), ((IMetadataClient)this).DisconnectRequested);
2022-07-04 16:18:33 +08:00
};
2022-07-04 16:49:08 +08:00
IsConnected.BindTo(connector.IsConnected);
IsConnected.BindValueChanged(isConnectedChanged, true);
2022-07-04 16:49:08 +08:00
}
lastQueueId = config.GetBindable<int>(OsuSetting.LastProcessedMetadataId);
localUser = api.LocalUser.GetBoundCopy();
userActivity = api.Activity.GetBoundCopy()!;
}
protected override void LoadComplete()
{
base.LoadComplete();
localUser.BindValueChanged(_ =>
{
if (localUser.Value is not GuestUser)
{
userStatus = localUser.Value.Status.GetBoundCopy();
userStatus.BindValueChanged(status => UpdateStatus(status.NewValue), true);
}
else
userStatus = null;
}, true);
userActivity.BindValueChanged(activity =>
{
if (localUser.Value is not GuestUser)
UpdateActivity(activity.NewValue);
}, true);
2022-07-04 16:49:08 +08:00
}
private bool catchingUp;
private void isConnectedChanged(ValueChangedEvent<bool> connected)
{
if (!connected.NewValue)
{
Schedule(() =>
{
isWatchingUserPresence.Value = false;
userStates.Clear();
});
2022-07-04 16:49:08 +08:00
return;
}
if (localUser.Value is not GuestUser)
{
UpdateActivity(userActivity.Value);
UpdateStatus(userStatus?.Value);
}
2022-07-04 16:49:08 +08:00
if (lastQueueId.Value >= 0)
2022-07-04 16:49:08 +08:00
{
catchingUp = true;
Task.Run(async () =>
{
try
{
while (true)
{
Logger.Log($"Requesting catch-up from {lastQueueId.Value}");
var catchUpChanges = await GetChangesSince(lastQueueId.Value).ConfigureAwait(true);
2022-07-04 16:49:08 +08:00
lastQueueId.Value = catchUpChanges.LastProcessedQueueID;
2022-07-04 16:49:08 +08:00
if (catchUpChanges.BeatmapSetIDs.Length == 0)
{
Logger.Log($"Catch-up complete at {lastQueueId.Value}");
break;
}
await ProcessChanges(catchUpChanges.BeatmapSetIDs).ConfigureAwait(true);
2022-07-04 16:49:08 +08:00
}
}
catch (Exception e)
{
Logger.Log($"Error while processing catch-up of metadata ({e.Message})");
}
2022-07-04 16:49:08 +08:00
finally
{
catchingUp = false;
}
});
2022-07-04 16:18:33 +08:00
}
}
2022-07-04 16:49:08 +08:00
public override async Task BeatmapSetsUpdated(BeatmapUpdates updates)
2022-07-04 16:18:33 +08:00
{
Logger.Log($"Received beatmap updates {updates.BeatmapSetIDs.Length} updates with last id {updates.LastProcessedQueueID}");
2022-07-04 16:49:08 +08:00
// If we're still catching up, avoid updating the last ID as it will interfere with catch-up efforts.
if (!catchingUp)
lastQueueId.Value = updates.LastProcessedQueueID;
2022-07-04 16:49:08 +08:00
await ProcessChanges(updates.BeatmapSetIDs).ConfigureAwait(false);
2022-07-04 16:49:08 +08:00
}
public override Task<BeatmapUpdates> GetChangesSince(int queueId)
2022-07-04 16:18:33 +08:00
{
2022-07-04 16:49:08 +08:00
if (connector?.IsConnected.Value != true)
return Task.FromCanceled<BeatmapUpdates>(default);
Logger.Log($"Requesting any changes since last known queue id {queueId}");
Debug.Assert(connection != null);
return connection.InvokeAsync<BeatmapUpdates>(nameof(IMetadataServer.GetChangesSince), queueId);
2022-07-04 16:18:33 +08:00
}
public override Task UpdateActivity(UserActivity? activity)
{
if (connector?.IsConnected.Value != true)
return Task.FromCanceled(new CancellationToken(true));
Debug.Assert(connection != null);
return connection.InvokeAsync(nameof(IMetadataServer.UpdateActivity), activity);
}
public override Task UpdateStatus(UserStatus? status)
{
if (connector?.IsConnected.Value != true)
return Task.FromCanceled(new CancellationToken(true));
Debug.Assert(connection != null);
return connection.InvokeAsync(nameof(IMetadataServer.UpdateStatus), status);
}
public override Task UserPresenceUpdated(int userId, UserPresence? presence)
{
Schedule(() =>
{
if (presence?.Status != null)
userStates[userId] = presence.Value;
else
userStates.Remove(userId);
});
return Task.CompletedTask;
}
public override async Task BeginWatchingUserPresence()
{
if (connector?.IsConnected.Value != true)
throw new OperationCanceledException();
Debug.Assert(connection != null);
await connection.InvokeAsync(nameof(IMetadataServer.BeginWatchingUserPresence)).ConfigureAwait(false);
2023-12-20 18:42:05 +08:00
Schedule(() => isWatchingUserPresence.Value = true);
}
public override async Task EndWatchingUserPresence()
{
try
{
if (connector?.IsConnected.Value != true)
throw new OperationCanceledException();
2023-12-20 18:42:05 +08:00
// must be scheduled before any remote calls to avoid mis-ordering.
Schedule(() => userStates.Clear());
Debug.Assert(connection != null);
await connection.InvokeAsync(nameof(IMetadataServer.EndWatchingUserPresence)).ConfigureAwait(false);
}
finally
{
2023-12-20 18:42:05 +08:00
Schedule(() => isWatchingUserPresence.Value = false);
}
}
public override async Task DisconnectRequested()
{
await base.DisconnectRequested().ConfigureAwait(false);
if (connector != null)
await connector.Disconnect().ConfigureAwait(false);
}
2022-07-04 16:18:33 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
connector?.Dispose();
}
}
}