| | |
| | | public class TRegion : MemoryRegion, IMemoryRegion |
| | | { |
| | | /// <summary> |
| | | /// 标识对象是否已被释放 |
| | | /// </summary> |
| | | private bool _disposed = false; |
| | | |
| | | /// <summary> |
| | | /// 区域类型 |
| | | /// </summary> |
| | | public override string RegionType => "T"; |
| | |
| | | private const int TimerSize = 2; |
| | | |
| | | /// <summary> |
| | | /// 定时器数量 |
| | | /// </summary> |
| | | public int TimerCount => Size / TimerSize; |
| | | |
| | | /// <summary> |
| | | /// 构造函数 |
| | | /// </summary> |
| | | /// <param name="timerCount">定时器数量</param> |
| | | /// <exception cref="ArgumentOutOfRangeException">当timerCount小于或等于0时抛出</exception> |
| | | /// <exception cref="OverflowException">当timerCount * TimerSize超出int范围时抛出</exception> |
| | | public TRegion(int timerCount) : base(timerCount * TimerSize) |
| | | { |
| | | if (timerCount <= 0) |
| | | throw new ArgumentOutOfRangeException(nameof(timerCount), "定时器数量必须大于0"); |
| | | |
| | | // 验证没有溢出 |
| | | if (timerCount > Size / TimerSize) |
| | | throw new OverflowException($"定时器数量{timerCount}过大,超出内存限制"); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | /// <param name="timerNumber">定时器号(从1开始)</param> |
| | | /// <returns>定时器值(毫秒)</returns> |
| | | /// <exception cref="ArgumentOutOfRangeException">当timerNumber为0或超出定时器数量时抛出</exception> |
| | | public ushort ReadTimer(ushort timerNumber) |
| | | { |
| | | if (_disposed) |
| | | throw new ObjectDisposedException(nameof(TRegion)); |
| | | if (timerNumber == 0) |
| | | throw new ArgumentOutOfRangeException(nameof(timerNumber), "定时器号必须大于0(从1开始)"); |
| | | |
| | | if (timerNumber > TimerCount) |
| | | throw new ArgumentOutOfRangeException(nameof(timerNumber), |
| | | $"定时器号{timerNumber}超出范围,当前定时器总数为{TimerCount}"); |
| | | |
| | | ushort offset = (ushort)((timerNumber - 1) * TimerSize); |
| | | var data = Read(offset, TimerSize); |
| | |
| | | /// </summary> |
| | | /// <param name="timerNumber">定时器号(从1开始)</param> |
| | | /// <param name="value">定时器值(毫秒)</param> |
| | | /// <exception cref="ArgumentOutOfRangeException">当timerNumber为0或超出定时器数量时抛出</exception> |
| | | public void WriteTimer(ushort timerNumber, ushort value) |
| | | { |
| | | if (_disposed) |
| | | throw new ObjectDisposedException(nameof(TRegion)); |
| | | if (timerNumber == 0) |
| | | throw new ArgumentOutOfRangeException(nameof(timerNumber), "定时器号必须大于0(从1开始)"); |
| | | |
| | | if (timerNumber > TimerCount) |
| | | throw new ArgumentOutOfRangeException(nameof(timerNumber), |
| | | $"定时器号{timerNumber}超出范围,当前定时器总数为{TimerCount}"); |
| | | |
| | | ushort offset = (ushort)((timerNumber - 1) * TimerSize); |
| | | var data = new byte[] { (byte)(value >> 8), (byte)(value & 0xFF) }; |
| | | Write(offset, data); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 释放资源 |
| | | /// </summary> |
| | | /// <param name="disposing">是否正在释放托管资源</param> |
| | | protected override void Dispose(bool disposing) |
| | | { |
| | | if (!_disposed) |
| | | { |
| | | if (disposing) |
| | | { |
| | | // 释放托管资源 |
| | | } |
| | | _disposed = true; |
| | | } |
| | | base.Dispose(disposing); |
| | | } |
| | | } |
| | | } |