mirror of
https://github.com/ppy/osu.git
synced 2024-11-08 15:27:25 +08:00
109 lines
2.9 KiB
C#
109 lines
2.9 KiB
C#
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|||
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|||
|
|
|||
|
using System;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using osu.Framework.Graphics;
|
|||
|
using osu.Framework.Threading;
|
|||
|
|
|||
|
namespace osu.Game.Online
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// A component which requires a constant polling process.
|
|||
|
/// </summary>
|
|||
|
public abstract class PollingComponent : Component
|
|||
|
{
|
|||
|
private double? lastTimePolled;
|
|||
|
|
|||
|
private ScheduledDelegate scheduledPoll;
|
|||
|
|
|||
|
private bool pollingActive;
|
|||
|
|
|||
|
private double timeBetweenPolls;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// The time that should be waited between polls.
|
|||
|
/// </summary>
|
|||
|
public double TimeBetweenPolls
|
|||
|
{
|
|||
|
get => timeBetweenPolls;
|
|||
|
set
|
|||
|
{
|
|||
|
timeBetweenPolls = value;
|
|||
|
scheduledPoll?.Cancel();
|
|||
|
pollIfNecessary();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
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.
|
|||
|
if (timeBetweenPolls == 0) return false;
|
|||
|
|
|||
|
if (!lastTimePolled.HasValue)
|
|||
|
{
|
|||
|
doPoll();
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
if (Time.Current - lastTimePolled.Value > timeBetweenPolls)
|
|||
|
{
|
|||
|
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>
|
|||
|
/// Perform the polling in this method. Call <see cref="pollComplete"/> when done.
|
|||
|
/// </summary>
|
|||
|
protected virtual Task Poll()
|
|||
|
{
|
|||
|
return Task.CompletedTask;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Call when a poll operation has completed.
|
|||
|
/// </summary>
|
|||
|
private void pollComplete()
|
|||
|
{
|
|||
|
lastTimePolled = Time.Current;
|
|||
|
pollingActive = false;
|
|||
|
|
|||
|
if (scheduledPoll == null)
|
|||
|
scheduleNextPoll();
|
|||
|
}
|
|||
|
|
|||
|
private void scheduleNextPoll()
|
|||
|
{
|
|||
|
scheduledPoll?.Cancel();
|
|||
|
|
|||
|
double lastPollDuration = lastTimePolled.HasValue ? Time.Current - lastTimePolled.Value : 0;
|
|||
|
|
|||
|
scheduledPoll = Scheduler.AddDelayed(doPoll, Math.Max(0, timeBetweenPolls - lastPollDuration));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|