Rolled UDP port support back to v4 only.
This commit is contained in:
@@ -13,8 +13,8 @@ namespace TimberWinR.Inputs
|
||||
{
|
||||
public class UdpInputListener : InputListener
|
||||
{
|
||||
private UdpClient _udpListenerV6;
|
||||
private readonly Thread _listenThreadV6;
|
||||
private UdpClient _udpListenerV4;
|
||||
private readonly Thread _listenThreadV4;
|
||||
|
||||
private readonly int _port;
|
||||
private long _receivedMessages;
|
||||
@@ -37,12 +37,10 @@ namespace TimberWinR.Inputs
|
||||
: base(cancelToken, "Win32-Udp")
|
||||
{
|
||||
_port = port;
|
||||
|
||||
|
||||
_receivedMessages = 0;
|
||||
|
||||
_listenThreadV6 = new Thread(StartListener);
|
||||
_listenThreadV6.Start();
|
||||
_listenThreadV4 = new Thread(StartListener);
|
||||
_listenThreadV4.Start();
|
||||
}
|
||||
|
||||
|
||||
@@ -51,31 +49,21 @@ namespace TimberWinR.Inputs
|
||||
LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType);
|
||||
|
||||
// close UDP listeners, which will end the listener threads
|
||||
_udpListenerV6.Close();
|
||||
_udpListenerV4.Close();
|
||||
|
||||
// wait for completion of the threads
|
||||
_listenThreadV6.Join();
|
||||
_listenThreadV4.Join();
|
||||
|
||||
base.Shutdown();
|
||||
}
|
||||
|
||||
private void StartListener()
|
||||
{
|
||||
var groupV6 = new IPEndPoint(IPAddress.IPv6Any, _port);
|
||||
// Create the socket as IPv6
|
||||
var dualModeSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
|
||||
var groupV4 = new IPEndPoint(IPAddress.Any, _port);
|
||||
|
||||
//
|
||||
// Now, disable the IPV6only flag to make it compatable with both ipv4 and ipv6
|
||||
// See: http://blogs.msdn.com/b/malarch/archive/2005/11/18/494769.aspx
|
||||
//
|
||||
dualModeSocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);
|
||||
dualModeSocket.Bind(groupV6);
|
||||
_udpListenerV4 = new UdpClient(_port);
|
||||
|
||||
_udpListenerV6 = new UdpClient();
|
||||
_udpListenerV6.Client = dualModeSocket;
|
||||
|
||||
LogManager.GetCurrentClassLogger().Info("Udp Input on Port {0} Ready", groupV6);
|
||||
LogManager.GetCurrentClassLogger().Info("Udp Input on Port {0} Ready", groupV4);
|
||||
|
||||
string lastMessage = "";
|
||||
try
|
||||
@@ -84,13 +72,18 @@ namespace TimberWinR.Inputs
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] bytes = _udpListenerV6.Receive(ref groupV6);
|
||||
byte[] bytes = _udpListenerV4.Receive(ref groupV4);
|
||||
var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
|
||||
lastMessage = data;
|
||||
var json = JObject.Parse(data);
|
||||
ProcessJson(json);
|
||||
Interlocked.Increment(ref _receivedMessages);
|
||||
}
|
||||
catch(ArgumentException aex)
|
||||
{
|
||||
LogManager.GetCurrentClassLogger().Error(aex);
|
||||
break;
|
||||
}
|
||||
catch(SocketException)
|
||||
{
|
||||
break;
|
||||
@@ -107,7 +100,7 @@ namespace TimberWinR.Inputs
|
||||
Interlocked.Increment(ref _parsedErrors);
|
||||
}
|
||||
}
|
||||
_udpListenerV6.Close();
|
||||
_udpListenerV4.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user