Exemplo n.º 1
0
        private void MainLoop()
        {
            _logger.Info($"Start listening on port={_port}...");
            var endPoint    = new IPEndPoint(IPAddress.Any, _port);
            var tcpListener = new TcpListener(endPoint);

            tcpListener.Start();
            while (_running)
            {
                using (var socket = tcpListener.AcceptSocket2())
                {
                    _logger.Info($" Received connection from {socket.RemoteEndPoint}");
                    var buffer  = new byte[1024];
                    int nbBytes = socket.Receive(buffer);
                    _logger.Info($"Received: {nbBytes} bytes");
                    var    token = Encoding.ASCII.GetString(buffer, 0, nbBytes);
                    byte[] response;
                    if (token == _token)
                    {
                        response = GetResponse(ResponseOk);
                        MySystemReboot(_forceAppsClosed, _dryRun);
                    }
                    else
                    {
                        response = GetResponse(ResponseNotOk);
                    }
                    socket.Send(response);
                    socket.Shutdown(SocketShutdown.Both);
                }
            }
            _logger.Info("Exiting main loop");
        }