Skip to content

Commit

Permalink
Automatically support multiple networks / interfaces with Remoting
Browse files Browse the repository at this point in the history
  • Loading branch information
nwoolls committed Aug 25, 2014
1 parent d238896 commit 77f7c63
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
7 changes: 4 additions & 3 deletions MultiMiner.Discovery/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ private void ProcessReceived(IPEndPoint source, byte[] bytes)

if (packet.Verb.Equals(Verbs.Online))
{
string ipAddress = source.Address.ToString();
if (!instances.Any(i => i.IpAddress.Equals(ipAddress)))
//search by MachineName and Fingerprint - these are unique while IP address may not be
//reasoning - the same machine may have multiple IP addresses as discovery supports multiple interfaces
if (!instances.Any(i => i.MachineName.Equals(packet.MachineName) && (i.Fingerprint == packet.Fingerprint)))
{
Data.Instance instance = new Data.Instance
{
IpAddress = ipAddress,
IpAddress = source.Address.ToString(),
MachineName = packet.MachineName,
Fingerprint = packet.Fingerprint
};
Expand Down
6 changes: 6 additions & 0 deletions MultiMiner.Discovery/MultiMiner.Discovery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sender.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MultiMiner.Utility\MultiMiner.Utility.csproj">
<Project>{99b0f266-eff7-4416-8c36-8356396a11ca}</Project>
<Name>MultiMiner.Utility</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
14 changes: 12 additions & 2 deletions MultiMiner.Discovery/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
using System.Net.Sockets;
using System.Text;
using System.Web.Script.Serialization;
using System.Collections.Generic;

namespace MultiMiner.Discovery
{
class Sender
{
public static void Send(IPAddress ipAddress, string verb, int fingerprint)
{
using (UdpClient client = new UdpClient())
//send from each local interface
//reasoning: virtual network adapters may be the chosen interface otherwise
List<string> localIPAddresses = Utility.Net.LocalNetwork.GetLocalIPAddresses();
foreach (string localIPAddress in localIPAddresses)
Send(IPAddress.Parse(localIPAddress), ipAddress, verb, fingerprint);
}

public static void Send(IPAddress source, IPAddress destination, string verb, int fingerprint)
{
using (UdpClient client = new UdpClient(new IPEndPoint(source, 0)))
{
Data.Packet packet = new Data.Packet();
packet.MachineName = Environment.MachineName;
Expand All @@ -21,7 +31,7 @@ public static void Send(IPAddress ipAddress, string verb, int fingerprint)
string jsonData = serializer.Serialize(packet);
byte[] bytes = Encoding.ASCII.GetBytes(jsonData);

IPEndPoint ip = new IPEndPoint(ipAddress, Config.Port);
IPEndPoint ip = new IPEndPoint(destination, Config.Port);
client.Send(bytes, bytes.Length, ip);
client.Close();
}
Expand Down
14 changes: 12 additions & 2 deletions MultiMiner.Remoting/Broadcast/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@
using System.Net.Sockets;
using System.Text;
using System.Web.Script.Serialization;
using System.Collections.Generic;

namespace MultiMiner.Remoting.Broadcast
{
public class Sender
{
public static void Send(IPAddress ipAddress, object payload)
{
using (UdpClient client = new UdpClient())
//send from each local interface
//reasoning: virtual network adapters may be the chosen interface otherwise
List<string> localIPAddresses = Utility.Net.LocalNetwork.GetLocalIPAddresses();
foreach (string localIPAddress in localIPAddresses)
Send(IPAddress.Parse(localIPAddress), ipAddress, payload);
}

private static void Send(IPAddress source, IPAddress destination, object payload)
{
using (UdpClient client = new UdpClient(new IPEndPoint(source, 0)))
{
JavaScriptSerializer serializer = new JavaScriptSerializer();

Expand All @@ -23,7 +33,7 @@ public static void Send(IPAddress ipAddress, object payload)

byte[] bytes = Encoding.ASCII.GetBytes(jsonPacket);

IPEndPoint ip = new IPEndPoint(ipAddress, Config.BroadcastPort);
IPEndPoint ip = new IPEndPoint(destination, Config.BroadcastPort);
client.Send(bytes, bytes.Length, ip);
client.Close();
}
Expand Down

0 comments on commit 77f7c63

Please sign in to comment.