public TcpServer(int port, Driver driver, Thread httpThread) { this.httpThread = httpThread; this.driver = driver; tcpListener = new TcpListener(IPAddress.Any, port); DriveCommand.CreateFile(); }
private bool HandleRequest(string request) { if (httpThread != null && httpThread.IsAlive) { httpThread?.Abort(); } DriveCommand.AppendCommand(request); if (request.StartsWith("Start")) { return(false); } return(true); }
public void Drive() { this.logThread = new Thread(positionLogger.Start);; this.logThread.Start(); foreach (RobotCommand command in DriveCommand.ReadCommands()) { command.Execute(this.robot); tbLog.BeginInvoke(new AddLog(SetLabel), new object[] { command.ToString() + " started" }); while (!this.robot.Drive.Done) { Thread.Sleep(100); } ; } this.logThread.Abort(); this.httpLogServerThread?.Abort(); httpLogServerThread = new Thread(httpLogServer.Start); this.httpLogServerThread.Start(); }
public void Start() { isActive = true; tcpListener.Start(); Log?.Invoke("Tcp Server started", new EventArgs()); while (true) { using (TcpClient client = tcpListener.AcceptTcpClient()) { //using (NetworkStream stream = client.GetStream()) { if (client.ReceiveBufferSize > 0) { StreamReader inputStream = new StreamReader(client.GetStream()); StreamWriter outputStream = new StreamWriter(client.GetStream()); string request = inputStream.ReadLine(); Log?.Invoke(request, new EventArgs()); isActive = HandleRequest(request); outputStream.WriteLine("OK"); outputStream.Flush(); inputStream.Close(); outputStream.Close(); } } } if (!isActive) { Thread driveThread = new Thread(new ThreadStart(driver.Drive)); driveThread.Start(); driveThread.Join(); DriveCommand.CreateFile(); isActive = true; } } }