public static void Complete <T>(this ITerminatableWritableChannel <T> channel)
 {
     if (!channel.TryComplete())
     {
         throw new OperationCanceledException();
     }
 }
 public static void Terminate <T>(
     this ITerminatableWritableChannel <T> channel,
     Exception error = null)
 {
     if (!channel.TryTerminate(error))
     {
         throw new OperationCanceledException();
     }
 }
        public static void PropagateExceptionFrom <T>(this ITerminatableWritableChannel <T> channel, Task completion)
        {
            void OnCompleted(Task task, object state)
            {
                var c = (ITerminatableWritableChannel <T>)state;

                if (task.IsFaulted)
                {
                    c.TryTerminate(task.Exception.ExtractInner());
                }
                else if (task.IsCanceled)
                {
                    c.TryTerminate();
                }
            }

            Task.WhenAny(channel.Completion, completion).Unwrap().ContinueWithSynchronously((Action <Task, object>)OnCompleted, channel);
        }
 public static async Task TerminateAsync <T>(this ITerminatableWritableChannel <T> channel, Exception error = null)
 {
     channel.TryTerminate(error);
     await channel.Completion.ConfigureAwait(false);
 }
 public static async Task CompleteAsync <T>(this ITerminatableWritableChannel <T> channel)
 {
     channel.TryComplete();
     await channel.Completion.ConfigureAwait(false);
 }
 public static bool IsCompleted <T>(this ITerminatableWritableChannel <T> channel)
 {
     return(channel.Completion.IsCompleted);
 }
        private static async Task PropagateAsync(IReadableChannel <TransportMessageFrame> channel1, ITerminatableWritableChannel <TransportMessageFrame> channel2)
        {
            try
            {
                while (true)
                {
                    var result = await channel1.TryReadAsync().ConfigureAwait(false);

                    if (!result.HasValue)
                    {
                        break;
                    }
                    await channel2.WriteAsync(result.Value).ConfigureAwait(false);
                }

                channel2.TryComplete();
            }
            catch (Exception ex)
            {
                channel2.TryTerminate(ex);
            }
        }