2019-01-24 08:43:03 +00: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.
|
2018-12-10 12:08:14 +00:00
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2018-12-10 12:08:14 +00:00
|
|
|
|
using System;
|
2022-01-28 04:44:09 +00:00
|
|
|
|
using System.Diagnostics;
|
2018-12-10 12:08:14 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-12-20 09:34:54 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2022-01-28 04:44:09 +00:00
|
|
|
|
using osu.Framework.Development;
|
2020-04-19 06:12:36 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-12-10 12:08:14 +00:00
|
|
|
|
using osu.Framework.Threading;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A component which requires a constant polling process.
|
|
|
|
|
/// </summary>
|
2022-08-19 11:43:15 +00:00
|
|
|
|
public abstract partial class PollingComponent : CompositeComponent
|
2018-12-10 12:08:14 +00:00
|
|
|
|
{
|
|
|
|
|
private double? lastTimePolled;
|
|
|
|
|
|
|
|
|
|
private ScheduledDelegate scheduledPoll;
|
|
|
|
|
|
|
|
|
|
private bool pollingActive;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-12-14 10:17:21 +00:00
|
|
|
|
/// The time in milliseconds to wait between polls.
|
|
|
|
|
/// Setting to zero stops all polling.
|
2018-12-10 12:08:14 +00:00
|
|
|
|
/// </summary>
|
2020-12-20 09:34:54 +00:00
|
|
|
|
public readonly Bindable<double> TimeBetweenPolls = new Bindable<double>();
|
2018-12-10 12:08:14 +00:00
|
|
|
|
|
2018-12-14 10:17:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-12-27 16:43:20 +00:00
|
|
|
|
/// <param name="timeBetweenPolls">The initial time in milliseconds to wait between polls. Setting to zero stops all polling.</param>
|
2018-12-14 10:17:21 +00:00
|
|
|
|
protected PollingComponent(double timeBetweenPolls = 0)
|
|
|
|
|
{
|
2020-12-20 09:34:54 +00:00
|
|
|
|
TimeBetweenPolls.BindValueChanged(_ =>
|
|
|
|
|
{
|
|
|
|
|
scheduledPoll?.Cancel();
|
|
|
|
|
pollIfNecessary();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
TimeBetweenPolls.Value = timeBetweenPolls;
|
2018-12-14 10:17:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-10 12:08:14 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
pollIfNecessary();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 08:04:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Immediately performs a <see cref="Poll"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void PollImmediately()
|
2018-12-10 12:08:14 +00:00
|
|
|
|
{
|
2021-08-17 08:04:32 +00:00
|
|
|
|
lastTimePolled = Time.Current - TimeBetweenPolls.Value;
|
2018-12-10 12:08:14 +00:00
|
|
|
|
scheduleNextPoll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-12-19 09:01:21 +00:00
|
|
|
|
/// Performs a poll. Implement but do not call this.
|
2018-12-10 12:08:14 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual Task Poll()
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 08:04:32 +00:00
|
|
|
|
private void doPoll()
|
2018-12-19 09:01:21 +00:00
|
|
|
|
{
|
2022-01-28 04:44:09 +00:00
|
|
|
|
Debug.Assert(ThreadSafety.IsUpdateThread);
|
|
|
|
|
|
2021-08-17 08:04:32 +00:00
|
|
|
|
scheduledPoll = null;
|
|
|
|
|
pollingActive = true;
|
|
|
|
|
Poll().ContinueWith(_ => pollComplete());
|
2018-12-19 09:01:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-10 12:08:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Call when a poll operation has completed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void pollComplete()
|
|
|
|
|
{
|
|
|
|
|
lastTimePolled = Time.Current;
|
|
|
|
|
pollingActive = false;
|
|
|
|
|
|
|
|
|
|
if (scheduledPoll == null)
|
2018-12-27 11:03:32 +00:00
|
|
|
|
pollIfNecessary();
|
2018-12-10 12:08:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 08:04:32 +00:00
|
|
|
|
private void pollIfNecessary()
|
|
|
|
|
{
|
|
|
|
|
// we must be loaded so we have access to clock.
|
|
|
|
|
if (!IsLoaded) return;
|
|
|
|
|
|
|
|
|
|
// there's already a poll process running.
|
|
|
|
|
if (pollingActive) return;
|
|
|
|
|
|
|
|
|
|
// don't try polling if the time between polls hasn't been set.
|
|
|
|
|
if (TimeBetweenPolls.Value == 0) return;
|
|
|
|
|
|
|
|
|
|
if (!lastTimePolled.HasValue)
|
|
|
|
|
{
|
2022-01-28 04:44:09 +00:00
|
|
|
|
Scheduler.AddOnce(doPoll);
|
2021-08-17 08:04:32 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Time.Current - lastTimePolled.Value > TimeBetweenPolls.Value)
|
|
|
|
|
{
|
2022-01-28 04:44:09 +00:00
|
|
|
|
Scheduler.AddOnce(doPoll);
|
2021-08-17 08:04:32 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// not enough time has passed since the last poll. we do want to schedule a poll to happen, though.
|
|
|
|
|
scheduleNextPoll();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-10 12:08:14 +00:00
|
|
|
|
private void scheduleNextPoll()
|
|
|
|
|
{
|
|
|
|
|
scheduledPoll?.Cancel();
|
|
|
|
|
|
|
|
|
|
double lastPollDuration = lastTimePolled.HasValue ? Time.Current - lastTimePolled.Value : 0;
|
|
|
|
|
|
2020-12-20 09:34:54 +00:00
|
|
|
|
scheduledPoll = Scheduler.AddDelayed(doPoll, Math.Max(0, TimeBetweenPolls.Value - lastPollDuration));
|
2018-12-10 12:08:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|