Add support for multiple interfaces.
This commit is contained in:
parent
8e3c90d5aa
commit
53b9dbdafd
5 changed files with 66 additions and 7 deletions
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
#include "Utilities.h"
|
#include "Utilities.h"
|
||||||
|
|
||||||
char ssid[] = "evenstar-2.4";
|
char ssid[] = "ZimWeaver";
|
||||||
char password[] = "***REMOVED***";
|
char password[] = "***REMOVED***";
|
||||||
|
|
||||||
// The address that the PixelHub beacon is broadcasting on.
|
// The address that the PixelHub beacon is broadcasting on.
|
||||||
|
|
|
@ -40,6 +40,13 @@ namespace PixelHub.Net
|
||||||
/// Setting this value affects the IP address that is broadcast by the beacon.
|
/// Setting this value affects the IP address that is broadcast by the beacon.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public IPAddress LocalIP { get; set; }
|
public IPAddress LocalIP { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The name of the entwork interface to bind to.
|
||||||
|
/// If you don't want to bind to a specific interface, leave this property alone.
|
||||||
|
/// </summary>
|
||||||
|
public string NetworkInterfaceName { get; set; } = string.Empty;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The (multicast) IP address that the beacon should broadcast to.
|
/// The (multicast) IP address that the beacon should broadcast to.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -57,7 +64,7 @@ namespace PixelHub.Net
|
||||||
/// The role that the program hosted at this beacon plays. Default: server.
|
/// The role that the program hosted at this beacon plays. Default: server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Role {
|
public string Role {
|
||||||
get{
|
get {
|
||||||
return role;
|
return role;
|
||||||
}
|
}
|
||||||
private set {
|
private set {
|
||||||
|
@ -95,7 +102,6 @@ namespace PixelHub.Net
|
||||||
MulticastChannel = new IPEndPoint(DestinationAddress, Port);
|
MulticastChannel = new IPEndPoint(DestinationAddress, Port);
|
||||||
|
|
||||||
LocalIP = findLocalIP();
|
LocalIP = findLocalIP();
|
||||||
logger.WriteLine("Using {0} as the local IP Address.", LocalIP);
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="PixelHub.Net.DiscoveryBeacon"/> class.
|
/// Initializes a new instance of the <see cref="PixelHub.Net.DiscoveryBeacon"/> class.
|
||||||
|
@ -118,6 +124,7 @@ namespace PixelHub.Net
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task Emit()
|
public async Task Emit()
|
||||||
{
|
{
|
||||||
|
logger.WriteLine("Using {0} as the local IP Address.", LocalIP);
|
||||||
connect();
|
connect();
|
||||||
logger.WriteLine("Beacon activated with broadcast interval of {0}ms.", EmitFrequency);
|
logger.WriteLine("Beacon activated with broadcast interval of {0}ms.", EmitFrequency);
|
||||||
while(true)
|
while(true)
|
||||||
|
@ -143,6 +150,16 @@ namespace PixelHub.Net
|
||||||
private void connect()
|
private void connect()
|
||||||
{
|
{
|
||||||
beacon = new UdpClient(Port);
|
beacon = new UdpClient(Port);
|
||||||
|
|
||||||
|
if(NetworkInterfaceName != string.Empty)
|
||||||
|
{
|
||||||
|
beacon.Client.SetSocketOption(
|
||||||
|
SocketOptionLevel.IP,
|
||||||
|
SocketOptionName.MulticastInterface,
|
||||||
|
(int)IPAddress.HostToNetworkOrder(NetTools.GetNetworkIndexByName4(NetworkInterfaceName))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Active = true;
|
Active = true;
|
||||||
beacon.JoinMulticastGroup(DestinationAddress);
|
beacon.JoinMulticastGroup(DestinationAddress);
|
||||||
logger.WriteLine("One way beacon set up pointing at {0}.", MulticastChannel);
|
logger.WriteLine("One way beacon set up pointing at {0}.", MulticastChannel);
|
||||||
|
|
29
PixelHub-Server/PixelHub/NetTools.cs
Normal file
29
PixelHub-Server/PixelHub/NetTools.cs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
using System;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace SBRL.Utilities
|
||||||
|
{
|
||||||
|
public static class NetTools
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// case-insensitively gets the IPv4 index of the network interface with the given name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="interfaceName">The interface name to search for.</param>
|
||||||
|
/// <returns>The IPv4 index of the network interface with the given name.</returns>
|
||||||
|
public static int GetNetworkIndexByName4(string targetInterfaceName)
|
||||||
|
{
|
||||||
|
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
|
||||||
|
foreach (NetworkInterface nic in nics)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Id: {0}, Description: {1}", nic.Id, nic.Description);
|
||||||
|
IPInterfaceProperties ipProps = nic.GetIPProperties();
|
||||||
|
IPv4InterfaceProperties ip4Props = ipProps.GetIPv4Properties();
|
||||||
|
if (nic.Id == targetInterfaceName)
|
||||||
|
return ip4Props.Index;
|
||||||
|
}
|
||||||
|
throw new Exception($"Error: Can't find network interface with the name {targetInterfaceName}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@
|
||||||
<Compile Include="SBRL.Utilities\PrefixedWriter.cs" />
|
<Compile Include="SBRL.Utilities\PrefixedWriter.cs" />
|
||||||
<Compile Include="PixelCommand.cs" />
|
<Compile Include="PixelCommand.cs" />
|
||||||
<Compile Include="PixelBot.cs" />
|
<Compile Include="PixelBot.cs" />
|
||||||
|
<Compile Include="NetTools.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
using SBRL.Utilities;
|
using SBRL.Utilities;
|
||||||
using PixelHub.Net;
|
using PixelHub.Net;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
|
||||||
namespace PixelHub.Server
|
namespace PixelHub.Server
|
||||||
{
|
{
|
||||||
|
@ -22,13 +23,24 @@ namespace PixelHub.Server
|
||||||
IPAddress.Parse("239.62.148.30"), port,
|
IPAddress.Parse("239.62.148.30"), port,
|
||||||
beaconWriter
|
beaconWriter
|
||||||
);
|
);
|
||||||
|
beacon.LocalIP = IPAddress.Parse("10.42.0.1");
|
||||||
|
beacon.NetworkInterfaceName = "wlan0";
|
||||||
|
|
||||||
PixelHub server = new PixelHub(port, serverWriter);
|
PixelHub server = new PixelHub(port, serverWriter);
|
||||||
|
//server.BindAddress = IPAddress.Parse("10.42.0.1");
|
||||||
|
|
||||||
systemWriter.WriteLine("Server booting complete. Beginning async loop.");
|
systemWriter.WriteLine("Server booting complete. Beginning async loop.");
|
||||||
//AsyncTools.ForgetTask(beacon.Emit());
|
//AsyncTools.ForgetTask(beacon.Emit());
|
||||||
|
try {
|
||||||
Task.WaitAll(new Task[] {
|
Task.WaitAll(new Task[] {
|
||||||
beacon.Emit(),
|
beacon.Emit(),
|
||||||
server.Listen()
|
server.Listen()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
catch(AggregateException error) {
|
||||||
|
foreach (Exception innerErr in error.InnerExceptions)
|
||||||
|
Console.WriteLine(innerErr);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue