| | |
| | | public class CRegion : MemoryRegion, IMemoryRegion |
| | | { |
| | | /// <summary> |
| | | /// 标识对象是否已被释放 |
| | | /// </summary> |
| | | private bool _disposed = false; |
| | | |
| | | /// <summary> |
| | | /// 区域类型 |
| | | /// </summary> |
| | | public override string RegionType => "C"; |
| | |
| | | private const int CounterSize = 2; |
| | | |
| | | /// <summary> |
| | | /// 计数器数量 |
| | | /// </summary> |
| | | public int CounterCount => Size / CounterSize; |
| | | |
| | | /// <summary> |
| | | /// 构造函数 |
| | | /// </summary> |
| | | /// <param name="counterCount">计数器数量</param> |
| | | /// <exception cref="ArgumentOutOfRangeException">当counterCount小于或等于0时抛出</exception> |
| | | /// <exception cref="OverflowException">当counterCount * CounterSize超出int范围时抛出</exception> |
| | | public CRegion(int counterCount) : base(counterCount * CounterSize) |
| | | { |
| | | if (counterCount <= 0) |
| | | throw new ArgumentOutOfRangeException(nameof(counterCount), "计数器数量必须大于0"); |
| | | |
| | | // 验证没有溢出 |
| | | if (counterCount > Size / CounterSize) |
| | | throw new OverflowException($"计数器数量{counterCount}过大,超出内存限制"); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | /// <param name="counterNumber">计数器号(从1开始)</param> |
| | | /// <returns>计数器值</returns> |
| | | /// <exception cref="ArgumentOutOfRangeException">当counterNumber为0或超出计数器数量时抛出</exception> |
| | | public ushort ReadCounter(ushort counterNumber) |
| | | { |
| | | if (_disposed) |
| | | throw new ObjectDisposedException(nameof(CRegion)); |
| | | if (counterNumber == 0) |
| | | throw new ArgumentOutOfRangeException(nameof(counterNumber), "计数器号必须大于0(从1开始)"); |
| | | |
| | | if (counterNumber > CounterCount) |
| | | throw new ArgumentOutOfRangeException(nameof(counterNumber), |
| | | $"计数器号{counterNumber}超出范围,当前计数器总数为{CounterCount}"); |
| | | |
| | | ushort offset = (ushort)((counterNumber - 1) * CounterSize); |
| | | var data = Read(offset, CounterSize); |
| | |
| | | /// </summary> |
| | | /// <param name="counterNumber">计数器号(从1开始)</param> |
| | | /// <param name="value">计数器值</param> |
| | | /// <exception cref="ArgumentOutOfRangeException">当counterNumber为0或超出计数器数量时抛出</exception> |
| | | public void WriteCounter(ushort counterNumber, ushort value) |
| | | { |
| | | if (_disposed) |
| | | throw new ObjectDisposedException(nameof(CRegion)); |
| | | if (counterNumber == 0) |
| | | throw new ArgumentOutOfRangeException(nameof(counterNumber), "计数器号必须大于0(从1开始)"); |
| | | |
| | | if (counterNumber > CounterCount) |
| | | throw new ArgumentOutOfRangeException(nameof(counterNumber), |
| | | $"计数器号{counterNumber}超出范围,当前计数器总数为{CounterCount}"); |
| | | |
| | | ushort offset = (ushort)((counterNumber - 1) * CounterSize); |
| | | 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); |
| | | } |
| | | } |
| | | } |