Rolled UDP port support back to v4 only.

This commit is contained in:
Eric Fontana
2015-04-13 06:16:04 -04:00
parent 0b3204efe8
commit 44104f1e59

View File

@@ -13,8 +13,8 @@ namespace TimberWinR.Inputs
{ {
public class UdpInputListener : InputListener public class UdpInputListener : InputListener
{ {
private UdpClient _udpListenerV6; private UdpClient _udpListenerV4;
private readonly Thread _listenThreadV6; private readonly Thread _listenThreadV4;
private readonly int _port; private readonly int _port;
private long _receivedMessages; private long _receivedMessages;
@@ -36,13 +36,11 @@ namespace TimberWinR.Inputs
public UdpInputListener(CancellationToken cancelToken, int port = 5140) public UdpInputListener(CancellationToken cancelToken, int port = 5140)
: base(cancelToken, "Win32-Udp") : base(cancelToken, "Win32-Udp")
{ {
_port = port; _port = port;
_receivedMessages = 0; _receivedMessages = 0;
_listenThreadV6 = new Thread(StartListener); _listenThreadV4 = new Thread(StartListener);
_listenThreadV6.Start(); _listenThreadV4.Start();
} }
@@ -51,31 +49,21 @@ namespace TimberWinR.Inputs
LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType); LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType);
// close UDP listeners, which will end the listener threads // close UDP listeners, which will end the listener threads
_udpListenerV6.Close(); _udpListenerV4.Close();
// wait for completion of the threads // wait for completion of the threads
_listenThreadV6.Join(); _listenThreadV4.Join();
base.Shutdown(); base.Shutdown();
} }
private void StartListener() private void StartListener()
{ {
var groupV6 = new IPEndPoint(IPAddress.IPv6Any, _port); var groupV4 = new IPEndPoint(IPAddress.Any, _port);
// Create the socket as IPv6
var dualModeSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
//
// 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);
_udpListenerV6 = new UdpClient(); _udpListenerV4 = new UdpClient(_port);
_udpListenerV6.Client = dualModeSocket;
LogManager.GetCurrentClassLogger().Info("Udp Input on Port {0} Ready", groupV4);
LogManager.GetCurrentClassLogger().Info("Udp Input on Port {0} Ready", groupV6);
string lastMessage = ""; string lastMessage = "";
try try
@@ -84,13 +72,18 @@ namespace TimberWinR.Inputs
{ {
try try
{ {
byte[] bytes = _udpListenerV6.Receive(ref groupV6); byte[] bytes = _udpListenerV4.Receive(ref groupV4);
var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length); var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
lastMessage = data; lastMessage = data;
var json = JObject.Parse(data); var json = JObject.Parse(data);
ProcessJson(json); ProcessJson(json);
Interlocked.Increment(ref _receivedMessages); Interlocked.Increment(ref _receivedMessages);
} }
catch(ArgumentException aex)
{
LogManager.GetCurrentClassLogger().Error(aex);
break;
}
catch(SocketException) catch(SocketException)
{ {
break; break;
@@ -107,7 +100,7 @@ namespace TimberWinR.Inputs
Interlocked.Increment(ref _parsedErrors); Interlocked.Increment(ref _parsedErrors);
} }
} }
_udpListenerV6.Close(); _udpListenerV4.Close();
} }
catch (Exception ex) catch (Exception ex)
{ {