1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 03:07:26 +08:00
osu-lazer/osu.Game/Online/PollingComponent.cs

124 lines
3.6 KiB
C#
Raw Normal View History

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