daily update on 2024.03.18 by itload youmu
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="TimeSpanProvider.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharpDebug.Logic;
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
public delegate void TimeSpanRefeshCallback();
|
||||
|
||||
/// <summary>
|
||||
/// A microsecond-accuracy timer. It will refresh every a certain time span.
|
||||
/// </summary>
|
||||
public class TimeSpanProvider
|
||||
{
|
||||
|
||||
private readonly int _maxSpanCount;
|
||||
private TimeSpanRefeshCallback _refeshCallback;
|
||||
private long _spanBegin;
|
||||
private readonly long _spanLength;
|
||||
private bool _subscribed;
|
||||
private Counter<uint> timeSpanCount = new Counter<uint>(0, 1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="TimeSpanProvider"/> and define the length of time span. Length of a single time span is defined in microseconds.
|
||||
/// </summary>
|
||||
/// <param name="spanLength"></param>
|
||||
/// <parma name="provider"></parma>
|
||||
public TimeSpanProvider(long spanLength, TimeSourceProviderBase provider)
|
||||
{
|
||||
_spanLength = spanLength;
|
||||
provider.AddSubscriber(this);
|
||||
_subscribed = true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Create a new <see cref="TimeSpanProvider"/> and define the length of time span. Length of a single time span is defined in microseconds.
|
||||
/// </summary>
|
||||
/// <param name="spanLength"></param>
|
||||
/// <parma name="provider"></parma>
|
||||
public TimeSpanProvider(long spanLength, uint maxSpan, TimeSourceProviderBase provider)
|
||||
{
|
||||
_spanLength = spanLength;
|
||||
provider.AddSubscriber(this);
|
||||
_subscribed = true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Time source provider provided in <see cref="StgSharp"/> internal framework.
|
||||
/// </summary>
|
||||
public static TimeSourceProviderBase DefaultProvider => StgSharpTime.OnlyInstance;
|
||||
|
||||
/// <summary>
|
||||
/// The method this <see cref="TimeSpanProvider"/> will call every time a new time span started.
|
||||
/// </summary>
|
||||
public TimeSpanRefeshCallback SpanRefreshCallback
|
||||
{
|
||||
get => _refeshCallback;
|
||||
set => _refeshCallback = value;
|
||||
}
|
||||
|
||||
|
||||
public void SubcsribeToTimeSource(TimeSourceProviderBase serviceHandler)
|
||||
{
|
||||
if (_subscribed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_subscribed = true;
|
||||
serviceHandler.AddSubscriber(this);
|
||||
}
|
||||
|
||||
internal bool CheckTime(long microseconds)
|
||||
{
|
||||
if (microseconds - _spanBegin > _spanLength)
|
||||
{
|
||||
_spanBegin = microseconds;
|
||||
timeSpanCount++;
|
||||
}
|
||||
if (timeSpanCount > _maxSpanCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static implicit operator uint(TimeSpanProvider provider)
|
||||
{
|
||||
return provider.timeSpanCount.Value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user