コード例 #1
0
        public override void Invoke(string[] param)
        {
            var filename = QueryParam <string>("Filename", GetParam(param, 0));

            if (!System.IO.File.Exists(filename))
            {
                throw new CommandFailedException($"File {filename} does not exist.");
            }

            var fileLines = System.IO.File.ReadAllLines(filename);

            OutputInformation($"There are {fileLines.Length} commands in file {filename}.");

            var index = 0;

            foreach (var line in fileLines)
            {
                OutputInformation($"Command {++index}: {line}");
                if (!line.StartsWith("#"))
                {
                    var success = _rootCommand.Execute(line);
                    if (!success)
                    {
                        throw new CommandFailedException("Terminating command chain.");
                    }
                }
            }
        }
コード例 #2
0
        private void DoAcceptTcpClientCallback(IAsyncResult ar)
        {
            _tcpListener   = (TcpListener)ar.AsyncState;
            _clientSocket  = _tcpListener.EndAcceptTcpClient(ar);
            _networkStream = _clientSocket.GetStream();

            _networkStream.ReadTimeout  = 10;
            _networkStream.WriteTimeout = 10;

            //TODO: Respond with a handshake message.
            System.Console.WriteLine("Client connected to service via TCP.");
            var handshakeMessage = "HANDSHAKE" + Environment.NewLine;
            var handshakeBytes   = Encoding.ASCII.GetBytes(handshakeMessage);

            _networkStream.Write(handshakeBytes, 0, handshakeBytes.Length);
            _networkStream.Flush();

            _running = true;
            while (_running)
            {
                try
                {
                    var dataFromClient = ReadFromClient();
                    _rootCommandBase.Execute(dataFromClient);
                }
                catch (IOException exception)
                {
                    System.Console.WriteLine(exception.Message);
                    break;
                }
                catch (Exception exception)
                {
                    System.Console.WriteLine(exception.Message);
                    break;
                }
                finally
                {
                    _networkStream?.Dispose();
                }
            }

            System.Console.WriteLine("Client disconnected from service.");
            _serverSocket.BeginAcceptTcpClient(DoAcceptTcpClientCallback, _serverSocket);
        }