Skip to content

Commit

Permalink
fix: don't set UdpClient.DontFragment on OSX (#116)
Browse files Browse the repository at this point in the history
* Wrap DontFragment assignment in a catch for OSX

dotnet/runtime#27653

Fixes #115

* Don't attempt to set fragmentation on OSX
  • Loading branch information
YarekTyshchenko authored Nov 21, 2022
1 parent e28cda9 commit 3411ed7
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions Transport/BacnetIpUdpProtocolTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void Open()
if (!string.IsNullOrEmpty(_localEndpoint)) ep = new IPEndPoint(IPAddress.Parse(_localEndpoint), SharedPort);
DisableConnReset(_sharedConn);
_sharedConn.Client.Bind(ep);
_sharedConn.DontFragment = _dontFragment;
SetDontFragment(_sharedConn, _dontFragment);
Log.Info($"Binded shared {ep} using UDP");
}
/* This is our own exclusive port. We'll recieve everything sent to this. */
Expand All @@ -120,10 +120,9 @@ private void Open()
// minutes ... yes it's like this at least on several systems
_exclusiveConn = new UdpClient(ep)
{
DontFragment = _dontFragment,
EnableBroadcast = true
};

SetDontFragment(_exclusiveConn, _dontFragment);
DisableConnReset(_exclusiveConn);
}
}
Expand All @@ -134,20 +133,43 @@ private void Open()
_exclusiveConn = new UdpClient { ExclusiveAddressUse = true };
DisableConnReset(_exclusiveConn);
_exclusiveConn.Client.Bind(ep);
_exclusiveConn.DontFragment = _dontFragment;
SetDontFragment(_exclusiveConn, _dontFragment);
_exclusiveConn.EnableBroadcast = true;
Log.Info($"Binded exclusively to {ep} using UDP");
}

Bvlc = new BVLC(this);
}

/// <summary>
/// Done to prevent exceptions in Socket.BeginReceive()
/// </summary>
/// <remarks>
/// http://microsoft.public.win32.programmer.networks.narkive.com/RlxW2V6m/udp-comms-and-connection-reset-problem
/// </remarks>
/// <summary>
/// Prevent exception on setting Don't Fragment on OSX
/// </summary>
/// <remarks>
/// https://github.com/dotnet/runtime/issues/27653
/// </remarks>
/// <param name="client"></param>
/// <param name="dontFragment"></param>
private void SetDontFragment(UdpClient client, bool dontFragment)
{
if (Environment.OSVersion.Platform != PlatformID.MacOSX)
{
try
{
client.DontFragment = dontFragment;
}
catch (SocketException e)
{
Log.WarnFormat("Unable to set DontFragment", e);
}
}
}

/// <summary>
/// Done to prevent exceptions in Socket.BeginReceive()
/// </summary>
/// <remarks>
/// http://microsoft.public.win32.programmer.networks.narkive.com/RlxW2V6m/udp-comms-and-connection-reset-problem
/// </remarks>
private static void DisableConnReset(UdpClient client)
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
Expand Down

0 comments on commit 3411ed7

Please sign in to comment.