Skip to content

Commit

Permalink
If the computer has multiple interfaces, we need to bind a socket to …
Browse files Browse the repository at this point in the history
…each one so that we can send a broadcast message to each interface.

svn path=/trunk/Mono.Nat/; revision=134568
  • Loading branch information
alanmcgovern committed May 21, 2009
1 parent e9eb518 commit 19706de
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions src/Mono.Nat/NatUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.Threading;
using System.Collections.Generic;
using System.IO;
using System.Net.NetworkInformation;

namespace Mono.Nat
{
Expand All @@ -40,7 +41,7 @@ public static class NatUtility
public static event EventHandler<DeviceEventArgs> DeviceLost;

public static event EventHandler<UnhandledExceptionEventArgs> UnhandledException;
private static UdpClient client = new UdpClient(0);
private static List<UdpClient> clients = new List<UdpClient>();

private static TextWriter logger;
private static List<ISearcher> controllers;
Expand All @@ -60,6 +61,7 @@ public static bool Verbose

static NatUtility()
{
CreateSockets();
controllers = new List<ISearcher>();
controllers.Add(new UpnpSearcher());
//controllers.Add(new PmpSearcher());
Expand All @@ -82,6 +84,24 @@ static NatUtility()
t.Start();
}

static void CreateSockets()
{
try
{
foreach (NetworkInterface n in NetworkInterface.GetAllNetworkInterfaces ())
{
foreach (UnicastIPAddressInformation address in n.GetIPProperties().UnicastAddresses)
{
clients.Add(new UdpClient(new IPEndPoint(address.Address, 0)));
}
}
}
catch (Exception ex)
{
clients.Add(new UdpClient(0));
}
}

internal static void Log(string format, params object[] args)
{
TextWriter logger = Logger;
Expand All @@ -96,20 +116,24 @@ private static void SearchAndListen()
{
try
{
if (client.Available > 0)
{
byte[] data = client.Receive(ref received);
foreach (UdpClient client in clients)
{
if (client.Available > 0)
{
byte[] data = client.Receive(ref received);

foreach (ISearcher s in controllers)
s.Handle(data, received);
continue;
}
foreach (ISearcher s in controllers)
s.Handle(data, received);
continue;
}
}

foreach (ISearcher s in controllers)
if (s.NextSearch < DateTime.Now && searching)
{
Log("Searching for: {0}", s.GetType().Name);
s.Search(client);
foreach (UdpClient client in clients)
s.Search(client);
}
System.Threading.Thread.Sleep(10);
}
Expand Down

0 comments on commit 19706de

Please sign in to comment.