Listen on both IPV4 and IPV6 interfaces

This commit is contained in:
Eric Fontana
2014-08-19 11:40:02 -04:00
parent 8d4ae9d7f9
commit 660bdb52dc

View File

@@ -13,31 +13,44 @@ namespace TimberWinR.Inputs
{ {
public class TcpInputListener : InputListener public class TcpInputListener : InputListener
{ {
private readonly System.Net.Sockets.TcpListener _tcpListener; private readonly System.Net.Sockets.TcpListener _tcpListenerV4;
private Thread _listenThread; private readonly System.Net.Sockets.TcpListener _tcpListenerV6;
private Thread _listenThreadV4;
private Thread _listenThreadV6;
private readonly int _port; private readonly int _port;
public TcpInputListener(CancellationToken cancelToken, int port = 5140) public TcpInputListener(CancellationToken cancelToken, int port = 5140)
: base(cancelToken, "Win32-Tcp") : base(cancelToken, "Win32-Tcp")
{ {
_port = port; _port = port;
_tcpListener = new System.Net.Sockets.TcpListener(IPAddress.Any, port); LogManager.GetCurrentClassLogger().Info("Tcp Input on Port: {0}", _port);
_listenThread = new Thread(new ThreadStart(ListenForClients));
_listenThread.Start(); _tcpListenerV6 = new System.Net.Sockets.TcpListener(IPAddress.IPv6Any, port);
_tcpListenerV4 = new System.Net.Sockets.TcpListener(IPAddress.Any, port);
_listenThreadV4 = new Thread(new ParameterizedThreadStart(ListenForClients));
_listenThreadV4.Start(_tcpListenerV4);
_listenThreadV6 = new Thread(new ParameterizedThreadStart(ListenForClients));
_listenThreadV6.Start(_tcpListenerV6);
} }
public override void Shutdown() public override void Shutdown()
{ {
this._tcpListener.Stop(); this._tcpListenerV4.Stop();
this._tcpListenerV6.Stop();
Finished(); Finished();
base.Shutdown(); base.Shutdown();
} }
private void ListenForClients() private void ListenForClients(object olistener)
{ {
this._tcpListener.Start(); System.Net.Sockets.TcpListener listener = olistener as System.Net.Sockets.TcpListener;
listener.Start();
LogManager.GetCurrentClassLogger().Info("Tcp Input on Port {0} Ready", _port); LogManager.GetCurrentClassLogger().Info("Tcp Input on Port {0} Ready", _port);
@@ -46,7 +59,7 @@ namespace TimberWinR.Inputs
try try
{ {
//blocks until a client has connected to the server //blocks until a client has connected to the server
TcpClient client = this._tcpListener.AcceptTcpClient(); TcpClient client = listener.AcceptTcpClient();
// Wait for a client, spin up a thread. // Wait for a client, spin up a thread.
var clientThread = new Thread(new ParameterizedThreadStart(HandleNewClient)); var clientThread = new Thread(new ParameterizedThreadStart(HandleNewClient));
@@ -66,7 +79,8 @@ namespace TimberWinR.Inputs
{ {
var tcpClient = (TcpClient)client; var tcpClient = (TcpClient)client;
NetworkStream clientStream = null; NetworkStream clientStream = null;
do
try
{ {
clientStream = tcpClient.GetStream(); clientStream = tcpClient.GetStream();
var stream = new StreamReader(clientStream); var stream = new StreamReader(clientStream);
@@ -85,9 +99,15 @@ namespace TimberWinR.Inputs
if (CancelToken.IsCancellationRequested) if (CancelToken.IsCancellationRequested)
break; break;
} }
} while (!CancelToken.IsCancellationRequested); }
catch (Exception ex)
{
LogManager.GetCurrentClassLogger().Error("Tcp Exception", ex);
}
if (clientStream != null)
clientStream.Close();
clientStream.Close();
tcpClient.Close(); tcpClient.Close();
Finished(); Finished();
} }