Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions BACnetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,10 +1545,7 @@ public IAsyncResult BeginSubscribeCOVRequest(BacnetAddress adr, BacnetObjectId o
public void EndSubscribeCOVRequest(IAsyncResult result, out Exception ex)
{
var res = (BacnetAsyncResult)result;
ex = res.Error;
if (ex == null && !res.WaitForDone(Timeout))
ex = new Exception("Wait Timeout");

ex = res.WaitForDone(Timeout) ? res.Error : new Exception("Wait Timeout");
res.Dispose();
}

Expand Down
96 changes: 51 additions & 45 deletions Transport/BacnetIpUdpProtocolTransport.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/**************************************************************************
* MIT License
*
* Copyright (C) 2014 Morten Kvistgaard <mk@pch-engineering.dk>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*********************************************************************/

/**************************************************************************
* MIT License
*
* Copyright (C) 2014 Morten Kvistgaard <mk@pch-engineering.dk>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*********************************************************************/
namespace System.IO.BACnet;

/// <summary>
/// This is the standard BACNet udp transport
/// </summary>
/// <summary>
/// This is the standard BACNet udp transport
/// </summary>
public class BacnetIpUdpProtocolTransport : BacnetTransportBase
{
private UdpClient _sharedConn;
Expand Down Expand Up @@ -215,15 +215,21 @@ private void OnReceiveData(IAsyncResult asyncResult)
{
if (connection.Client != null && !_disposing)
{
try
{
// BeginReceive ASAP to enable parallel processing (e.g. query additional data while processing a notification)
connection.BeginReceive(OnReceiveData, connection);
}
catch (Exception ex)
{
Log.Error($"Failed to restart data receive. {ex}");
}
// BeginReceive ASAP to enable parallel processing (e.g. query additional data while processing a notification)
ThreadPool.UnsafeQueueUserWorkItem(static state =>
{
var (transport, udp) = ((BacnetIpUdpProtocolTransport transport, UdpClient udp)) state!;
if (udp.Client == null || transport._disposing) return;

try
{
udp.BeginReceive(transport.OnReceiveData, udp);
}
catch (Exception ex)
{
transport.Log.Error($"Failed to restart data receive. {ex}");
}
}, (this, connection));
}
}

Expand Down Expand Up @@ -415,12 +421,12 @@ protected virtual BacnetAddress _GetBroadcastAddress()
else
{
// restricted local broadcast (directed ... routable)
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
foreach (var ip in adapter.GetIPProperties().UnicastAddresses)
if (LocalEndPoint.Address.Equals(ip.Address))
{
ipAddr = ip;
break;
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
foreach (var ip in adapter.GetIPProperties().UnicastAddresses)
if (LocalEndPoint.Address.Equals(ip.Address))
{
ipAddr = ip;
break;
}
}

Expand Down Expand Up @@ -462,4 +468,4 @@ public override void Dispose()
_sharedConn?.Close();
_sharedConn = null;
}
}
}