Exemplo n.º 1
0
        protected override async Task <int> Execute(ICommandManager commandManager)
        {
            ExecutionContext context = LifetimeScope.Resolve <ExecutionContext>();

            context.WriteInformation(string.Format(CultureInfo.InvariantCulture, MessageResources.ClientStarting, ServerName));
            using (ICommunicationProtocol protocol = await NamedPipeCommunicationProtocol.Connect(ServerName,
                                                                                                  LifetimeScope.Resolve <StreamFactory>(),
                                                                                                  LifetimeScope.Resolve <ILog>(),
                                                                                                  actAsClient: true)
                                                     .ConfigureAwait(false))
                using (ManualResetEvent serverStoppedEvent = new ManualResetEvent(false))
                {
                    context.WriteInformation(MessageResources.ClientStarted);
                    protocol.CommunicationError += OnError;
                    protocol.MessageReceived    += OnMessageReceived;
                    protocol.Start();

                    Task.Run(ReadConsoleAsync);
                    serverStoppedEvent.WaitOne(-1, true);

                    return(0);

                    void OnError(object sender, EventArgs e)
                    {
                        protocol.CommunicationError -= OnError;
                        context.WriteInformation(MessageResources.ClientServerDisconnectedMessage);
                        serverStoppedEvent.Set();
                    }

                    void OnMessageReceived(object sender, MessageReceivedEventArgs e)
                    {
                        context.WriteInformation(MessageResources.ClientMessageReceived);
                        context.WriteInformation(Encoding.UTF8.GetString(e.Message.ReadToEnd()));
                    }

                    void ReadConsoleAsync()
                    {
                        string serverMessage = Console.ReadLine();

                        if (serverMessage?.Equals("kill", StringComparison.OrdinalIgnoreCase) == true)
                        {
                            serverStoppedEvent.Set();
                        }
                        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(serverMessage)))
                        {
                            context.WriteInformation(MessageResources.ClientSendingMessage);
                            protocol.SendMessage(stream);
                        }

                        Task.Run(ReadConsoleAsync);
                    }
                }
        }
        public NamedPipeServerProtocolSplitMessagesTest(ITestOutputHelper output)
        {
            NamedPipeServerFeature serverFeature = new NamedPipeServerFeature();

            if (!serverFeature.FeatureEnabled)
            {
                throw new SkipTestException("Disabled named pipe communication");
            }
            streamFactory = PageStreamFactory.CreateDefault(16);
            string serverName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                                    ? Guid.NewGuid().ToByteString()
                                    : $"/tmp/{Guid.NewGuid().ToByteString()}";
            Task <ICommunicationProtocol> creationTask = NamedPipeCommunicationProtocol.Connect(serverName, streamFactory, new LogTracer(output));

            simulator = NamedPipeCommunicationProtocolSimulator.Connect(serverName, streamFactory, new LogTracer(output));
            creationTask.Wait();
            protocol = creationTask.Result;
            protocol.MessageReceived    += OnMessageReceived;
            protocol.CommunicationError += OnCommunicationError;
            protocol.Start();
        }