Inheritance: IDisposable
Ejemplo n.º 1
0
        public static Task <NamedPipeClient> Connect(
            [NotNull] string description,
            [CanBeNull] NamedPipeServerInfo server,
            [NotNull] Action <Message> onReceive,
            CancellationToken token = default(CancellationToken))
        {
            if (server == null ||
                !server.IsValid)
            {
                return(TaskResult <NamedPipeClient> .Default);
            }

            NamedPipeClient npc = new NamedPipeClient(description, server, onReceive, token);
            TaskCompletionSource <NamedPipeClient> ccs = npc._connectionCompletionSource;

            // ReSharper disable once AssignNullToNotNullAttribute
            return(ccs != null ? ccs.Task : Task.FromResult(npc));
        }
Ejemplo n.º 2
0
        public static async Task RunAsync(
            [NotNull] string description,
            [CanBeNull] NamedPipeServerInfo service = null,
            CancellationToken token = default(CancellationToken))
        {
            if (description == null)
            {
                throw new ArgumentNullException("description");
            }

            if (!ConsoleHelper.IsConsole)
            {
                return;
            }

            try
            {
                Log.SetTrace(validLevels: LoggingLevels.None);
                Log.SetConsole(_logFormatBuilder);

                Console.Title = description;
                NamedPipeClient client = null;
                while (client == null)
                {
                    token.ThrowIfCancellationRequested();
                    while (service == null ||
                           !service.IsValid)
                    {
                        Console.Clear();
                        NamedPipeServerInfo[] services = null;
                        await Log.Flush(token).ConfigureAwait(false);

                        // ReSharper disable once AssignNullToNotNullAttribute
                        ConsoleTextWriter.Default.WriteLine(ClientResources.ConsoleClient_RunAsync_ScanningForService);
                        while (services == null ||
                               services.Length < 1)
                        {
                            services = NamedPipeClient.GetServices().ToArray();
                            if (Console.KeyAvailable)
                            {
                                return;
                            }
                            // ReSharper disable once PossibleNullReferenceException
                            await Task.Delay(500, token).ConfigureAwait(false);

                            token.ThrowIfCancellationRequested();
                        }

                        if (services.Length > 0)
                        {
                            WriteServerList(services);
                        }

                        // ReSharper disable once AssignNullToNotNullAttribute
                        ConsoleTextWriter.Default.WriteLine(ClientResources.ConsoleClient_RunAsync_EnterServiceName);
                        string serviceName = Console.ReadLine();
                        service = !string.IsNullOrWhiteSpace(serviceName)
                            ? NamedPipeClient.FindService(serviceName)
                            : NamedPipeClient.GetServices().FirstOrDefault();
                    }

                    Console.Clear();
                    // ReSharper disable once AssignNullToNotNullAttribute
                    ConsoleTextWriter.Default.WriteLine(
                        ClientResources.ConsoleClient_RunAsync_ConnectingToService,
                        service.Name);

                    try
                    {
                        // TODO Remove constant timeout
                        using (ITokenSource tokenSource = token.WithTimeout(10000))
                            client =
                                await
                                NamedPipeClient.Connect(
                                    description,
                                    service,
                                    OnReceive,
                                    tokenSource.Token)
                                .ConfigureAwait(false);
                    }
                    catch (TaskCanceledException)
                    {
                        // ReSharper disable once AssignNullToNotNullAttribute
                        ConsoleTextWriter.Default.WriteLine(
                            ClientResources.ConsoleClient_RunAsync_TimedOut,
                            service.Name);
                        // ReSharper disable once AssignNullToNotNullAttribute
                        ConsoleTextWriter.Default.WriteLine(ClientResources.ConsoleClient_RunAsync_PressAnyKeyContinue);
                        client  = null;
                        service = null;
                        Console.ReadKey(true);
                    }
                }

                // ReSharper disable once AssignNullToNotNullAttribute
                Console.Title = string.Format(
                    ClientResources.ConsoleClient_RunAsync_ConnectedTitle,
                    description,
                    service.Name);
                _connected.WriteToConsole(
                    null,
                    new Dictionary <string, object>
                {
                    { "ServiceName", client.ServiceName }
                });

                // ReSharper disable once PossibleNullReferenceException
                await Task.Delay(1100, token).ConfigureAwait(false);

                await Log.Flush(token).ConfigureAwait(false);

                while (client.State != PipeState.Closed)
                {
                    token.ThrowIfCancellationRequested();
                    WritePrompt(service);
                    string command = Console.ReadLine();

                    if (!string.IsNullOrWhiteSpace(command))
                    {
                        Guid commandGuid;
                        bool completed = false;
                        client.Execute(command, out commandGuid, token)
                        .Subscribe(
                            c => new FormatBuilder(c).WriteToConsole(),
                            e =>
                        {
                            Debug.Assert(e != null);
                            if (!(e is TaskCanceledException))
                            {
                                new FormatBuilder()
                                .AppendForegroundColor(ConsoleColor.Red)
                                .AppendLine(e.Message)
                                .AppendResetForegroundColor()
                                .WriteToConsole();
                            }
                            completed = true;
                        },
                            () => { completed = true; },
                            token);

                        if (commandGuid != Guid.Empty)
                        {
                            do
                            {
                                if (Console.KeyAvailable &&
                                    Console.ReadKey(true).Key == ConsoleKey.Escape)
                                {
                                    // Cancel command
                                    ConsoleTextWriter.Default.Write(ClientResources.ConsoleClient_RunAsync_Cancelling);
                                    await client.CancelCommand(commandGuid, token).ConfigureAwait(false);

                                    break;
                                }
                                // ReSharper disable once PossibleNullReferenceException
                                await Task.Delay(100, token).ConfigureAwait(false);
                            } while (!completed);
                        }
                    }

                    // Wait to allow any disconnects or logs to come through.
                    // ReSharper disable once PossibleNullReferenceException
                    await Task.Delay(1100, token).ConfigureAwait(false);

                    await Log.Flush(token).ConfigureAwait(false);
                }
            }
            catch (TaskCanceledException)
            {
            }
            catch (Exception e)
            {
                if (!token.IsCancellationRequested)
                {
                    Log.Add(e);
                }
            }
            await Log.Flush(token).ConfigureAwait(false);

            // ReSharper disable once PossibleNullReferenceException
            await Task.Delay(200, token).ConfigureAwait(false);

            Console.WriteLine(ClientResources.ConsoleClient_RunAsync_PressAnyKeyExit);
            Console.ReadKey(true);
        }
Ejemplo n.º 3
0
        public static Task<NamedPipeClient> Connect(
            [NotNull] string description,
            [CanBeNull] NamedPipeServerInfo server,
            [NotNull] Action<Message> onReceive,
            CancellationToken token = default(CancellationToken))
        {
            if (server == null ||
                !server.IsValid)
                return TaskResult<NamedPipeClient>.Default;

            NamedPipeClient npc = new NamedPipeClient(description, server, onReceive, token);
            TaskCompletionSource<NamedPipeClient> ccs = npc._connectionCompletionSource;
            // ReSharper disable once AssignNullToNotNullAttribute
            return (ccs != null ? ccs.Task : Task.FromResult(npc));
        }