forked from ela-compil/BACnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BacnetAsyncResult.cs
160 lines (138 loc) · 5.53 KB
/
BacnetAsyncResult.cs
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
using System.Threading;
namespace System.IO.BACnet
{
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;
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 { get; }
public Exception Error
{
get => _error;
set
{
_error = value;
CompletedSynchronously = true;
_waitHandle.Set();
}
}
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();
}
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();
}
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();
}
/// <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;
}
}
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;
}
}
}
}