Exemplo n.º 1
0
        private static void Pipes_WaitForReadThenTryReadValues()
        {
            using (AnonymousPipeServerStream serverPipe = new AnonymousPipeServerStream(PipeDirection.Out))
                using (AnonymousPipeClientStream clientPipe = new AnonymousPipeClientStream(PipeDirection.In, serverPipe.ClientSafePipeHandle))
                {
                    IWritableChannel <int> writer = Channel.WriteToStream <int>(serverPipe);
                    IReadableChannel <int> reader = Channel.ReadFromStream <int>(clientPipe);

                    Task.WaitAll(
                        Task.Run(async() =>
                    {
                        for (int i = 0; i < 100; i++)
                        {
                            await writer.WriteAsync(i);
                        }
                        writer.Complete();
                        Assert.False(writer.TryWrite(100));
                    }),
                        Task.Run(async() =>
                    {
                        int result;
                        int i = 0;
                        while (await reader.WaitToReadAsync())
                        {
                            if (reader.TryRead(out result))
                            {
                                Assert.Equal(i++, result);
                            }
                        }
                        Assert.False(reader.TryRead(out result));
                    }));
                }
        }
Exemplo n.º 2
0
        private static void Pipes_EnumerateValues()
        {
            using (AnonymousPipeServerStream serverPipe = new AnonymousPipeServerStream(PipeDirection.Out))
                using (AnonymousPipeClientStream clientPipe = new AnonymousPipeClientStream(PipeDirection.In, serverPipe.ClientSafePipeHandle))
                {
                    IWritableChannel <int> writer = Channel.WriteToStream <int>(serverPipe);
                    IReadableChannel <int> reader = Channel.ReadFromStream <int>(clientPipe);

                    Task.WaitAll(
                        Task.Run(async() =>
                    {
                        for (int i = 0; i < 100; i++)
                        {
                            await writer.WriteAsync(i);
                        }
                        writer.Complete();
                        Assert.False(writer.TryWrite(100));
                    }),
                        Task.Run(async() =>
                    {
                        int i = 0;
                        IAsyncEnumerator <int> e = reader.GetAsyncEnumerator();
                        while (await e.MoveNextAsync())
                        {
                            Assert.Equal(i++, e.Current);
                        }
                    }));
                }
        }
Exemplo n.º 3
0
 public static async Task <bool> TryWriteAsync <T>(this IWritableChannel <T> channel, T item, CancellationToken cancellationToken = default)
 {
     do
     {
         if (channel.TryWrite(item))
         {
             return(true);
         }
     } while (await channel.WaitWriteAvailableAsync(cancellationToken).ConfigureAwait(false));
     return(false);
 }
Exemplo n.º 4
0
        public async Task TryWrite_AfterComplete()
        {
            IWritableChannel <int> c = Channel.WriteToStream <int>(new MemoryStream());

            Assert.True(c.TryComplete());
            Assert.False(c.TryWrite(42));
            AssertSynchronousFalse(c.WaitToWriteAsync());
            await Assert.ThrowsAnyAsync <InvalidOperationException>(() => c.WriteAsync(42));

            Assert.False(c.TryComplete());
        }
        private async Task HandleResponseMessageAsync(TResponse msg)
        {
            var header = _protocol.MessageFactory.CreateInvocationMessageReceived();

            if (!_sender.TryWrite(header))
            {
                await _sender.WriteOrDisposeAsync(header, CancellationToken).ConfigureAwait(false);
            }
            Log.Debug("Sent confirmation about received response of type {0}", msg.GetType().Name);
            if (!_responses.TryWrite(msg))
            {
                await _responses.WriteAsync(msg, CancellationToken).ConfigureAwait(false);
            }
            Log.Debug("Consumed response of type {0}", msg.GetType().Name);
        }
Exemplo n.º 6
0
 public override Task <bool> TryInvokeAsync()
 {
     if (_channel.TryWrite(_item))
     {
         try
         {
             _action();
             return(s_trueTask);
         }
         catch (Exception exc)
         {
             return(Task.FromException <bool>(exc));
         }
     }
     return(s_falseTask);
 }
Exemplo n.º 7
0
 public override Task <bool> TryInvokeAsync()
 {
     if (_channel.TryWrite(_item))
     {
         try
         {
             return(_action().ContinueWith(t =>
             {
                 t.GetAwaiter().GetResult();
                 return true;
             }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default));
         }
         catch (Exception exc)
         {
             return(Task.FromException <bool>(exc));
         }
     }
     return(s_falseTask);
 }