Skip to content

Commit

Permalink
Minor changes but vital for managing network change in graceful manne…
Browse files Browse the repository at this point in the history
…r. Sudden network change can result in crash if no try/catch when trying to send udp message or no check for array element swapping.
  • Loading branch information
ak-76 committed Nov 6, 2017
1 parent 27f4dc0 commit 30a4b79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/NetMQ/Core/Patterns/Utils/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static T[] Resize<T>([NotNull] this T[] src, int size, bool ended)

public static void Swap<T>([NotNull] this List<T> items, int index1, int index2) where T : class
{
if (index1 == index2)
return;
if (index1 == index2) { return; }
if (items.Count <= index1 || items.Count <= index2) { return; }

T item1 = items[index1];
T item2 = items[index2];
Expand Down
14 changes: 13 additions & 1 deletion src/NetMQ/NetMQBeacon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,19 @@ private void OnPipeReady(object sender, NetMQSocketEventArgs e)

private void SendUdpFrame(NetMQFrame frame)
{
m_udpSocket.SendTo(frame.Buffer, 0, frame.MessageSize, SocketFlags.None, m_broadcastAddress);
try
{
m_udpSocket.SendTo(frame.Buffer, 0, frame.MessageSize, SocketFlags.None, m_broadcastAddress);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode != SocketError.AddressNotAvailable) { throw; }

// Initiate Creation of new Udp here to solve issue related to 'sudden' network change.
// On windows (7 OR 10) incorrect/previous ip address might still exist instead of new Ip
// due to network change which causes crash (if no try/catch and keep trying to send to incorrect/not available address.
// This approach would solve the issue...
}
}

private NetMQFrame ReceiveUdpFrame(out string peerName)
Expand Down

0 comments on commit 30a4b79

Please sign in to comment.