private void runTCPReadThread(object args_) { ConsoleConnectArgs args = (ConsoleConnectArgs)args_; var client = args.client; var readQueue = args.read; var stream = client.GetStream(); int bytes; Byte[] data = new Byte[256]; try { while ((bytes = stream.Read(data, 0, data.Length)) != 0) { bytes = stream.Read(data, 0, data.Length); var response = System.Text.Encoding.ASCII.GetString(data, 0, bytes); readQueue.Enqueue(response); Debug.Log("response: " + response); } } catch (SocketException e) { Debug.Log("ReadThread: " + e); } finally { client.Close(); Debug.Log("Leaving ReadThread"); } }
private void runTCPThread(object args_) { ConsoleConnectArgs args = (ConsoleConnectArgs)args_; Node node = args.node; var read = args.read; var write = args.write; TcpClient client = new TcpClient(); Debug.Log("Attempting to connect to console at " + node.console_host + ":" + node.console.ToString()); try { IPAddress ipAddress = IPAddress.Parse(node.console_host); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, node.console); client.Connect(ipEndPoint); args.client = client; var readThread = new Thread(start: new ParameterizedThreadStart(runTCPReadThread)); var writeThread = new Thread(start: new ParameterizedThreadStart(runTCPWriteThread)); readThread.Start(args); Thread.Sleep(15000); writeThread.Start(args); } catch (ArgumentNullException e) { Debug.Log("ArgumentNullException: " + e); } catch (SocketException e) { Debug.Log("SocketException: " + e); } }
private void runTCPWriteThread(object args_) { ConsoleConnectArgs args = (ConsoleConnectArgs)args_; var client = args.client; var writeQueue = args.write; var stream = client.GetStream(); while (true) { var toWrite = writeQueue.Dequeue(); Byte[] data = System.Text.Encoding.ASCII.GetBytes(toWrite); try { stream.Write(data, 0, data.Length); } catch (Exception e) { Debug.Log("write Exception: " + e); return; } } }
// This starts a thread that runs a TCP connection to the input node console and returns a read queue and a write queue public void ConnectTo(ConsoleConnectArgs args) { var thread = new Thread(start: new ParameterizedThreadStart(runTCPThread)); thread.Start(args); }