-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSparsePointStorage.cs
More file actions
84 lines (66 loc) · 2.72 KB
/
SparsePointStorage.cs
File metadata and controls
84 lines (66 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using NModbus.BasicServer.Interfaces;
using NModbus.Interfaces;
using System.Collections.Concurrent;
namespace NModbus.BasicServer
{
/// <summary>
/// Stores points using a Dictionary rather than an array.
/// </summary>
/// <remarks>
/// Rather than allocate a full array of <see cref="UInt16.MaxValue"/> values, we use a <see cref="ConcurrentDictionary{UInt16, TValue}"/>
/// to store only those addresses which have been set.
/// </remarks>
/// <typeparam name="T"></typeparam>
public class SparsePointStorage<T> : IPointStorage<T>
where T : struct
{
private readonly ConcurrentDictionary<ushort, T> values = new ConcurrentDictionary<ushort, T>();
public T this[ushort address]
{
get
{
if (values.TryGetValue(address, out var value))
return value;
return default;
}
set
{
values[address] = value;
}
}
public event EventHandler<DeviceReadArgs>? BeforeDeviceRead;
public event EventHandler<DeviceReadArgs>? AfterDeviceRead;
public event EventHandler<DeviceWriteArgs<T>>? BeforeDeviceWrite;
public event EventHandler<DeviceWriteArgs<T>>? AfterDeviceWrite;
private void VerifyPointsAreInRange(ushort startingAddress, ushort numberOfPoints)
{
if (startingAddress + numberOfPoints > ushort.MaxValue)
throw new ModbusServerException(ModbusExceptionCode.IllegalDataAddress);
}
T[] IDevicePointStorage<T>.ReadPoints(ushort startingAddress, ushort numberOfPoints)
{
VerifyPointsAreInRange(startingAddress, numberOfPoints);
var args = new DeviceReadArgs(startingAddress, numberOfPoints);
BeforeDeviceRead?.Invoke(this, args);
var points = new T[numberOfPoints];
for (var index = 0; index < numberOfPoints; index++)
{
values.TryGetValue((ushort)(startingAddress + index), out var value);
points[index] = value;
}
AfterDeviceRead?.Invoke(this, args);
return points;
}
void IDevicePointStorage<T>.WritePoints(ushort startingAddress, T[] values)
{
VerifyPointsAreInRange(startingAddress, (ushort)values.Length);
var args = new DeviceWriteArgs<T>(startingAddress, values);
BeforeDeviceWrite?.Invoke(this, args);
for (var index = 0; index < values.Length; index++)
{
this.values[(ushort)(startingAddress + index)] = values[index];
}
AfterDeviceWrite?.Invoke(this, args);
}
}
}