-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathBacnetAsyncResult.cs
More file actions
230 lines (201 loc) · 8.22 KB
/
Copy pathBacnetAsyncResult.cs
File metadata and controls
230 lines (201 loc) · 8.22 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
namespace System.IO.BACnet;
/// <summary>
/// Tracks a single outstanding confirmed request and correlates the reply to it.
/// Per ASHRAE 135 (20.1.2.6) the invoke-id is source-device-unique — unique across all of this
/// device's outstanding requests — and is the value used to "associate the response ... with the
/// original request". Replies are therefore matched by invoke-id alone; the transport source
/// address is intentionally not compared, because a conformant reply routed via a BACnet
/// router/BBMD arrives with the router's address (not the addressed device's), and a strict
/// address check would wrongly drop it (issues #141, #149).
/// </summary>
public class BacnetAsyncResult : IAsyncResult, IDisposable
{
private BacnetClient _comm;
private readonly byte _waitInvokeId;
private Exception _error;
private readonly byte[] _transmitBuffer;
private readonly int _transmitLength;
private readonly bool _waitForTransmit;
private readonly int _transmitTimeout;
private ManualResetEvent _waitHandle;
private readonly BacnetAddress _address;
// Non-blocking waiting path used by the *Async request methods. It runs in parallel with the
// ManualResetEvent above (which still drives the synchronous WaitForDone), so the public API is
// unchanged: both are signalled at the same points. Continuations run asynchronously so a waiter
// resuming never executes on — and thus never stalls — the transport's receive thread.
private readonly object _asyncLock = new();
private TaskCompletionSource<bool> _completionSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
private bool _done;
public bool Segmented { get; private set; }
public byte[] Result { get; private set; }
public object AsyncState { get; set; }
public bool CompletedSynchronously { get; private set; }
public WaitHandle AsyncWaitHandle => _waitHandle;
public bool IsCompleted => _waitHandle.WaitOne(0);
public BacnetAddress Address => _address;
public Exception Error
{
get => _error;
set
{
_error = value;
CompletedSynchronously = true;
_waitHandle.Set();
MarkDone();
}
}
public BacnetAsyncResult(BacnetClient comm, BacnetAddress adr, byte invokeId, byte[] transmitBuffer, int transmitLength, bool waitForTransmit, int transmitTimeout)
{
_transmitTimeout = transmitTimeout;
_address = adr;
_waitForTransmit = waitForTransmit;
_transmitBuffer = transmitBuffer;
_transmitLength = transmitLength;
_comm = comm;
_waitInvokeId = invokeId;
_comm.OnComplexAck += OnComplexAck;
_comm.OnError += OnError;
_comm.OnAbort += OnAbort;
_comm.OnReject += OnReject;
_comm.OnSimpleAck += OnSimpleAck;
_comm.OnSegment += OnSegment;
_waitHandle = new ManualResetEvent(false);
}
public void Resend()
{
try
{
if (_comm.Transport.Send(_transmitBuffer, _comm.Transport.HeaderLength, _transmitLength, _address, _waitForTransmit, _transmitTimeout) < 0)
{
Error = new IOException("Write Timeout");
}
}
catch (Exception ex)
{
Error = new Exception($"Write Exception: {ex.Message}");
}
}
private void OnSegment(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invokeId, BacnetMaxSegments maxSegments, BacnetMaxAdpu maxAdpu, byte sequenceNumber, byte[] buffer, int offset, int length)
{
if (invokeId != _waitInvokeId)
return;
Segmented = true;
_waitHandle.Set();
MarkActivity();
}
private void OnSimpleAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invokeId, byte[] data, int dataOffset, int dataLength)
{
if (invokeId != _waitInvokeId)
return;
_waitHandle.Set();
MarkDone();
}
private void OnAbort(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, byte invokeId, BacnetAbortReason reason, byte[] buffer, int offset, int length)
{
if (invokeId != _waitInvokeId)
return;
Error = new Exception($"Abort from device, reason: {reason}");
}
private void OnReject(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, byte invokeId, BacnetRejectReason reason, byte[] buffer, int offset, int length)
{
if (invokeId != _waitInvokeId)
return;
Error = new Exception($"Reject from device, reason: {reason}");
}
private void OnError(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invokeId, BacnetErrorClasses errorClass, BacnetErrorCodes errorCode, byte[] buffer, int offset, int length)
{
if (invokeId != _waitInvokeId)
return;
Error = new Exception($"Error from device: {errorClass} - {errorCode}");
}
private void OnComplexAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invokeId, byte[] buffer, int offset, int length)
{
if (invokeId != _waitInvokeId)
return;
Segmented = false;
Result = new byte[length];
if (length > 0)
Array.Copy(buffer, offset, Result, 0, length);
//notify waiter even if segmented
_waitHandle.Set();
MarkDone();
}
/// <summary>
/// Will continue waiting until all segments are recieved
/// </summary>
public bool WaitForDone(int timeout)
{
while (true)
{
if (!AsyncWaitHandle.WaitOne(timeout))
return false;
if (Segmented)
_waitHandle.Reset();
else
return true;
}
}
/// <summary>
/// Non-blocking equivalent of <see cref="WaitForDone"/>. Completes when a terminal reply (simple
/// ack, final complex ack, error, abort or reject) for this invoke-id arrives, mirroring the
/// segmented semantics of <see cref="WaitForDone"/>: each incoming segment re-arms the timeout so
/// the whole transfer is bounded per segment, not in total.
/// </summary>
/// <returns><c>true</c> if the request completed within the timeout; <c>false</c> on timeout.</returns>
public async Task<bool> GetResultOrTimeout(int timeoutMs, CancellationToken cancellationToken = default)
{
while (true)
{
Task signal;
lock (_asyncLock)
{
if (_done)
return true;
if (_completionSource.Task.IsCompleted)
_completionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
signal = _completionSource.Task;
}
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var completed = await Task.WhenAny(signal, Task.Delay(timeoutMs, timeoutCts.Token)).ConfigureAwait(false);
if (completed != signal)
{
cancellationToken.ThrowIfCancellationRequested();
return false;
}
// A terminal reply or a segment arrived; stop the timer and loop to re-evaluate: _done
// ends the wait, a bare segment re-arms it (the completed source is replaced above).
timeoutCts.Cancel();
}
}
private void MarkActivity()
{
lock (_asyncLock)
_completionSource.TrySetResult(true);
}
private void MarkDone()
{
lock (_asyncLock)
{
_done = true;
_completionSource.TrySetResult(true);
}
}
public void Dispose()
{
if (_comm != null)
{
_comm.OnComplexAck -= OnComplexAck;
_comm.OnError -= OnError;
_comm.OnAbort -= OnAbort;
_comm.OnReject -= OnReject;
_comm.OnSimpleAck -= OnSimpleAck;
_comm.OnSegment -= OnSegment;
_comm = null;
}
if (_waitHandle != null)
{
_waitHandle.Dispose();
_waitHandle = null;
}
}
}