Exemplo n.º 1
0
        private async Task ProcessCommands()
        {
            try {
                if (_namedPipeServerStream == null)
                {
                    throw new NullReferenceException();
                }
                using var reader       = new CliReader(_namedPipeServerStream);
                await using var writer = new CliWriter(_namedPipeServerStream);
                var cliMode  = false;
                var firstCmd = true;
                while (_namedPipeServerStream.IsConnected)
                {
                    string?line = await reader.ReadLineAsync();

                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }
                    if (firstCmd)
                    {
                        firstCmd = false;
                        if (line == "cli-mode")
                        {
                            cliMode = true;
                            await writer.WriteAsync(">");

                            await writer.FlushAsync();

                            continue;
                        }
                    }
                    if (line == "exit")
                    {
                        await writer.WriteLineAsync("Exiting...");

                        await writer.FlushAsync();

                        return;
                    }
                    Logger.LogInfo($">{line}");
                    string[] data = line.Split(" ", 2);
                    if (_commands.TryGetValue(data[0], out var command))
                    {
                        await command.Process(reader, writer, data.Length > 1?data[1] : null);
                    }
                    else
                    {
                        await writer.WriteLineAsync($"Unknown command '{data[0]}'.");
                    }
                    if (cliMode)
                    {
                        await writer.WriteAsync(">");
                    }
                    await writer.FlushAsync();
                }
            } catch (Exception ex) {
                Logger.LogError(ex);
            }
        }
Exemplo n.º 2
0
 public void CliWriterWriteTest()
 {
     using (var writer = new CliWriter())
     {
         Assert.DoesNotThrow(() => writer.Write("Write test"));
     }
 }