Example #1
0
 public static async Task CopyToAsync(this Stream stream, IWritableChannel channel)
 {
     try
     {
         await stream.CopyToAsync(new StreamChannel(channel));
     }
     catch (Exception ex)
     {
         channel.Complete(ex);
     }
     finally
     {
         channel.Complete();
     }
 }
Example #2
0
        private static async Task WriteItems(IWritableChannel <int> channel, CancellationToken cancellationToken)
        {
            // Start generating items
            int counter = 0;

            while (!cancellationToken.IsCancellationRequested && counter < 5)
            {
                try
                {
                    var value = counter++;
                    Console.WriteLine($"Sending: {value}");
                    await channel.WriteAsync(value);

                    Console.WriteLine("Waiting 0.5sec");
                    await Task.Delay(500);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Sender Threw: {ex}");
                    return;
                }
            }

            Console.WriteLine("Completing channel");
            channel.Complete();
        }
Example #3
0
        private static async Task Pipes_ReadWriteValues <T>(bool firstWaitToRead, int numItems, Func <int, T> getValue)
        {
            using (AnonymousPipeServerStream serverPipe = new AnonymousPipeServerStream(PipeDirection.Out))
                using (AnonymousPipeClientStream clientPipe = new AnonymousPipeClientStream(PipeDirection.In, serverPipe.ClientSafePipeHandle))
                {
                    IWritableChannel <T> writer = Channel.WriteToStream <T>(serverPipe);
                    IReadableChannel <T> reader = Channel.ReadFromStream <T>(clientPipe);

                    for (int i = 0; i < numItems; i++)
                    {
                        T itemToWrite = getValue(i);

                        Task <T> readItem = firstWaitToRead ?
                                            reader.WaitToReadAsync().ContinueWith(_ => reader.ReadAsync().AsTask()).Unwrap() :
                                            reader.ReadAsync().AsTask();
                        Task writeItem = writer.WriteAsync(itemToWrite);
                        await Task.WhenAll(readItem, writeItem);

                        Assert.Equal(itemToWrite, readItem.Result);
                    }

                    writer.Complete();
                    Assert.False(await reader.WaitToReadAsync());
                    await reader.Completion;
                }
        }
Example #4
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));
                    }));
                }
        }
Example #5
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);
                        }
                    }));
                }
        }
Example #6
0
 public void Dispose()
 {
     State = WebSocketConnectionState.Closed;
     _pinger?.Dispose();
     _timerCts.Cancel();
     _terminateReceiveCts.Cancel();
     _inbound.Complete();
     _outbound.Complete();
 }
Example #7
0
        public void WaitToWriteAsync_Completed_SynchronousResult()
        {
            using (AnonymousPipeServerStream serverPipe = new AnonymousPipeServerStream(PipeDirection.Out))
            {
                IWritableChannel <int> writer = Channel.WriteToStream <int>(serverPipe);
                AssertSynchronousTrue(writer.WaitToWriteAsync());

                writer.Complete();
                AssertSynchronousFalse(writer.WaitToWriteAsync());

                var cts = new CancellationTokenSource();
                cts.Cancel();
                AssertSynchronouslyCanceled(writer.WaitToWriteAsync(cts.Token), cts.Token);
            }
        }
Example #8
0
            public async Task Execute(IReadableChannel input, IWritableChannel output)
            {
                while (true)
                {
                    var inputBuffer = await input.ReadAsync();

                    if (inputBuffer.IsEmpty && input.Reading.IsCompleted)
                    {
                        break;
                    }

                    var writerBuffer = output.Alloc(2048);
                    var memory       = inputBuffer.First;
                    if (memory.Length > 0)
                    {
                        unsafe
                        {
                            _inflater.SetInput((IntPtr)memory.UnsafePointer, memory.Length);

                            int written = _inflater.Inflate((IntPtr)memory.UnsafePointer, memory.Length);

                            writerBuffer.Advance(written);

                            var consumed = memory.Length - _inflater.AvailableInput;

                            inputBuffer = inputBuffer.Slice(0, consumed);
                        }
                    }

                    input.Advance(inputBuffer.End);

                    await writerBuffer.FlushAsync();
                }

                input.Complete();

                output.Complete();

                _inflater.Dispose();
            }
Example #9
0
            public async Task Execute(IReadableChannel input, IWritableChannel output)
            {
                while (true)
                {
                    var result = await input.ReadAsync();

                    var inputBuffer = result.Buffer;

                    if (inputBuffer.IsEmpty)
                    {
                        if (result.IsCompleted)
                        {
                            break;
                        }

                        input.Advance(inputBuffer.End);
                        continue;
                    }

                    var writerBuffer = output.Alloc();
                    var memory       = inputBuffer.First;

                    unsafe
                    {
                        // TODO: Pin pointer if not pinned
                        void *inPointer;
                        if (memory.TryGetPointer(out inPointer))
                        {
                            _deflater.SetInput((IntPtr)inPointer, memory.Length);
                        }
                        else
                        {
                            throw new InvalidOperationException("Pointer needs to be pinned");
                        }
                    }

                    while (!_deflater.NeedsInput())
                    {
                        unsafe
                        {
                            void *outPointer;
                            writerBuffer.Ensure();
                            if (writerBuffer.Memory.TryGetPointer(out outPointer))
                            {
                                int written = _deflater.ReadDeflateOutput((IntPtr)outPointer, writerBuffer.Memory.Length);
                                writerBuffer.Advance(written);
                            }
                            else
                            {
                                throw new InvalidOperationException("Pointer needs to be pinned");
                            }
                        }
                    }

                    var consumed = memory.Length - _deflater.AvailableInput;

                    inputBuffer = inputBuffer.Slice(0, consumed);

                    input.Advance(inputBuffer.End);

                    await writerBuffer.FlushAsync();
                }

                bool flushed = false;

                do
                {
                    // Need to do more stuff here
                    var writerBuffer = output.Alloc();

                    unsafe
                    {
                        void *pointer;
                        writerBuffer.Ensure();
                        var memory = writerBuffer.Memory;
                        if (memory.TryGetPointer(out pointer))
                        {
                            int compressedBytes;
                            flushed = _deflater.Flush((IntPtr)pointer, memory.Length, out compressedBytes);
                            writerBuffer.Advance(compressedBytes);
                        }
                        else
                        {
                            throw new InvalidOperationException("Pointer needs to be pinned");
                        }
                    }

                    await writerBuffer.FlushAsync();
                }while (flushed);

                bool finished = false;

                do
                {
                    // Need to do more stuff here
                    var writerBuffer = output.Alloc();

                    unsafe
                    {
                        void *pointer;
                        writerBuffer.Ensure();
                        var memory = writerBuffer.Memory;
                        if (memory.TryGetPointer(out pointer))
                        {
                            int compressedBytes;
                            finished = _deflater.Finish((IntPtr)pointer, memory.Length, out compressedBytes);
                            writerBuffer.Advance(compressedBytes);
                        }
                    }

                    await writerBuffer.FlushAsync();
                }while (!finished);

                input.Complete();

                output.Complete();

                _deflater.Dispose();
            }
Example #10
0
            public async Task Execute(IReadableChannel input, IWritableChannel output)
            {
                while (true)
                {
                    var result = await input.ReadAsync();

                    var inputBuffer = result.Buffer;

                    if (inputBuffer.IsEmpty)
                    {
                        if (result.IsCompleted)
                        {
                            break;
                        }

                        input.Advance(inputBuffer.End);
                        continue;
                    }

                    var writerBuffer = output.Alloc();
                    var memory       = inputBuffer.First;
                    if (memory.Length > 0)
                    {
                        unsafe
                        {
                            void *pointer;
                            if (memory.TryGetPointer(out pointer))
                            {
                                _inflater.SetInput((IntPtr)pointer, memory.Length);

                                void *writePointer;
                                writerBuffer.Ensure();
                                if (writerBuffer.Memory.TryGetPointer(out writePointer))
                                {
                                    int written = _inflater.Inflate((IntPtr)writePointer, writerBuffer.Memory.Length);
                                    writerBuffer.Advance(written);
                                }
                                else
                                {
                                    throw new InvalidOperationException("Pointer needs to be pinned");
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException("Pointer needs to be pinned");
                            }

                            var consumed = memory.Length - _inflater.AvailableInput;

                            inputBuffer = inputBuffer.Slice(0, consumed);
                        }
                    }

                    input.Advance(inputBuffer.End);

                    await writerBuffer.FlushAsync();
                }

                input.Complete();

                output.Complete();

                _inflater.Dispose();
            }
Example #11
0
            public async Task Execute(IReadableChannel input, IWritableChannel output)
            {
                while (true)
                {
                    var inputBuffer = await input.ReadAsync();

                    if (inputBuffer.IsEmpty && input.Reading.IsCompleted)
                    {
                        break;
                    }

                    var writerBuffer = output.Alloc(2048);
                    var memory       = inputBuffer.First;

                    unsafe
                    {
                        _deflater.SetInput((IntPtr)memory.UnsafePointer, memory.Length);
                    }

                    while (!_deflater.NeedsInput())
                    {
                        unsafe
                        {
                            int written = _deflater.ReadDeflateOutput((IntPtr)writerBuffer.Memory.UnsafePointer, writerBuffer.Memory.Length);
                            writerBuffer.Advance(written);
                        }
                    }

                    var consumed = memory.Length - _deflater.AvailableInput;

                    inputBuffer = inputBuffer.Slice(0, consumed);

                    input.Advance(inputBuffer.End);

                    await writerBuffer.FlushAsync();
                }

                bool flushed;

                do
                {
                    // Need to do more stuff here
                    var writerBuffer = output.Alloc(2048);
                    var memory       = writerBuffer.Memory;

                    unsafe
                    {
                        int compressedBytes;
                        flushed = _deflater.Flush((IntPtr)memory.UnsafePointer, memory.Length, out compressedBytes);
                        writerBuffer.Advance(compressedBytes);
                    }

                    await writerBuffer.FlushAsync();
                }while (flushed);

                bool finished;

                do
                {
                    // Need to do more stuff here
                    var writerBuffer = output.Alloc(2048);
                    var memory       = writerBuffer.Memory;

                    unsafe
                    {
                        int compressedBytes;
                        finished = _deflater.Finish((IntPtr)memory.UnsafePointer, memory.Length, out compressedBytes);
                        writerBuffer.Advance(compressedBytes);
                    }

                    await writerBuffer.FlushAsync();
                }while (!finished);

                input.Complete();

                output.Complete();

                _deflater.Dispose();
            }
Example #12
0
 public void OnCompleted() => _channel.Complete();