예제 #1
0
 public TcpServer(int port, Driver driver, Thread httpThread)
 {
     this.httpThread = httpThread;
     this.driver     = driver;
     tcpListener     = new TcpListener(IPAddress.Any, port);
     DriveCommand.CreateFile();
 }
예제 #2
0
        private bool HandleRequest(string request)
        {
            if (httpThread != null && httpThread.IsAlive)
            {
                httpThread?.Abort();
            }

            DriveCommand.AppendCommand(request);

            if (request.StartsWith("Start"))
            {
                return(false);
            }
            return(true);
        }
예제 #3
0
        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();
        }
예제 #4
0
        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;
                }
            }
        }