public Task DisconnectAsync()
            {
                var promise = new TaskCompletionSource();

                if (!promise.setUncancellable())
                {
                    return(promise.Task);
                }

                bool wasActive = this.channel.Active;

                try
                {
                    this.channel.DoDisconnect();
                }
                catch (Exception t)
                {
                    this.SafeSetFailure(promise, t);
                    this.CloseIfClosed();
                    return(promise.Task);
                }

                if (wasActive && !this.channel.Active)
                {
                    this.InvokeLater(() => this.channel.pipeline.FireChannelInactive());
                }

                this.SafeSetSuccess(promise);
                this.CloseIfClosed(); // doDisconnect() might have closed the channel

                return(promise.Task);
            }
 void Register0(TaskCompletionSource promise)
 {
     try
     {
         // check if the channel is still open as it could be closed input the mean time when the register
         // call was outside of the eventLoop
         if (!promise.setUncancellable() || !this.EnsureOpen(promise))
         {
             Util.SafeSetFailure(promise, ClosedChannelException, Logger);
             return;
         }
         bool firstRegistration = this.neverRegistered;
         this.channel.DoRegister();
         this.neverRegistered    = false;
         this.channel.registered = true;
         this.channel.eventLoop.AcceptNewTasks();
         Util.SafeSetSuccess(promise, Logger);
         this.channel.pipeline.FireChannelRegistered();
         // Only fire a channelActive if the channel has never been registered. This prevents firing
         // multiple channel actives if the channel is deregistered and re-registered.
         if (firstRegistration && this.channel.Active)
         {
             this.channel.pipeline.FireChannelActive();
         }
     }
     catch (Exception t)
     {
         // Close the channel directly to avoid FD leak.
         this.CloseForcibly();
         this.channel.closeFuture.Complete();
         Util.SafeSetFailure(promise, t, Logger);
     }
 }
            public Task CloseAsync() //CancellationToken cancellationToken)
            {
                var promise = new TaskCompletionSource();

                if (!promise.setUncancellable())
                {
                    return(promise.Task);
                }
                //if (cancellationToken.IsCancellationRequested)
                //{
                //    return TaskEx.Cancelled;
                //}

                if (this.outboundBuffer == null)
                {
                    // Only needed if no VoidChannelPromise.
                    if (promise != TaskCompletionSource.Void)
                    {
                        // This means close() was called before so we just register a listener and return
                        return(this.channel.closeFuture.Task);
                    }
                    return(promise.Task);
                }

                if (this.channel.closeFuture.Task.IsCompleted)
                {
                    // Closed already.
                    Util.SafeSetSuccess(promise, Logger);
                    return(promise.Task);
                }

                bool wasActive = this.channel.Active;
                ChannelOutboundBuffer buffer = this.outboundBuffer;

                this.outboundBuffer = null;          // Disallow adding any messages and flushes to outboundBuffer.
                IEventExecutor closeExecutor = null; // todo closeExecutor();

                if (closeExecutor != null)
                {
                    closeExecutor.Execute(() =>
                    {
                        try
                        {
                            // Execute the close.
                            this.DoClose0(promise);
                        }
                        finally
                        {
                            // Call invokeLater so closeAndDeregister is executed input the EventLoop again!
                            this.InvokeLater(() =>
                            {
                                // Fail all the queued messages
                                buffer.FailFlushed(ClosedChannelException,
                                                   false);
                                buffer.Close(ClosedChannelException);
                                this.FireChannelInactiveAndDeregister(wasActive);
                            });
                        }
                    });
                }
                else
                {
                    try
                    {
                        // Close the channel and fail the queued messages input all cases.
                        this.DoClose0(promise);
                    }
                    finally
                    {
                        // Fail all the queued messages.
                        buffer.FailFlushed(ClosedChannelException, false);
                        buffer.Close(ClosedChannelException);
                    }
                    if (this.inFlush0)
                    {
                        this.InvokeLater(() => this.FireChannelInactiveAndDeregister(wasActive));
                    }
                    else
                    {
                        this.FireChannelInactiveAndDeregister(wasActive);
                    }
                }

                return(promise.Task);
            }
Example #4
0
            public Task CloseAsync() //CancellationToken cancellationToken)
            {
                var promise = new TaskCompletionSource();
                if (!promise.setUncancellable())
                {
                    return promise.Task;
                }
                //if (cancellationToken.IsCancellationRequested)
                //{
                //    return TaskEx.Cancelled;
                //}

                if (this.outboundBuffer == null)
                {
                    // Only needed if no VoidChannelPromise.
                    if (promise != TaskCompletionSource.Void)
                    {
                        // This means close() was called before so we just register a listener and return
                        return this.channel.closeFuture.Task;
                    }
                    return promise.Task;
                }

                if (this.channel.closeFuture.Task.IsCompleted)
                {
                    // Closed already.
                    Util.SafeSetSuccess(promise);
                    return promise.Task;
                }

                bool wasActive = this.channel.Active;
                ChannelOutboundBuffer buffer = this.outboundBuffer;
                this.outboundBuffer = null; // Disallow adding any messages and flushes to outboundBuffer.
                IEventExecutor closeExecutor = null; // todo closeExecutor();
                if (closeExecutor != null)
                {
                    closeExecutor.Execute(() =>
                    {
                        try
                        {
                            // Execute the close.
                            this.DoClose0(promise);
                        }
                        finally
                        {
                            // Call invokeLater so closeAndDeregister is executed input the EventLoop again!
                            this.InvokeLater(() =>
                            {
                                // Fail all the queued messages
                                buffer.FailFlushed(ClosedChannelException,
                                    false);
                                buffer.Close(ClosedChannelException);
                                this.FireChannelInactiveAndDeregister(wasActive);
                            });
                        }
                    });
                }
                else
                {
                    try
                    {
                        // Close the channel and fail the queued messages input all cases.
                        this.DoClose0(promise);
                    }
                    finally
                    {
                        // Fail all the queued messages.
                        buffer.FailFlushed(ClosedChannelException, false);
                        buffer.Close(ClosedChannelException);
                    }
                    if (this.inFlush0)
                    {
                        this.InvokeLater(() => this.FireChannelInactiveAndDeregister(wasActive));
                    }
                    else
                    {
                        this.FireChannelInactiveAndDeregister(wasActive);
                    }
                }

                return promise.Task;
            }
Example #5
0
            public Task DisconnectAsync()
            {
                var promise = new TaskCompletionSource();
                if (!promise.setUncancellable())
                {
                    return promise.Task;
                }

                bool wasActive = this.channel.Active;
                try
                {
                    this.channel.DoDisconnect();
                }
                catch (Exception t)
                {
                    this.SafeSetFailure(promise, t);
                    this.CloseIfClosed();
                    return promise.Task;
                }

                if (wasActive && !this.channel.Active)
                {
                    this.InvokeLater(() => this.channel.pipeline.FireChannelInactive());
                }

                this.SafeSetSuccess(promise);
                this.CloseIfClosed(); // doDisconnect() might have closed the channel

                return promise.Task;
            }
Example #6
0
 void Register0(TaskCompletionSource promise)
 {
     try
     {
         // check if the channel is still open as it could be closed input the mean time when the register
         // call was outside of the eventLoop
         if (!promise.setUncancellable() || !this.EnsureOpen(promise))
         {
             Util.SafeSetFailure(promise, ClosedChannelException);
             return;
         }
         bool firstRegistration = this.neverRegistered;
         this.channel.DoRegister();
         this.neverRegistered = false;
         this.channel.registered = true;
         this.channel.eventLoop.AcceptNewTasks();
         Util.SafeSetSuccess(promise);
         this.channel.pipeline.FireChannelRegistered();
         // Only fire a channelActive if the channel has never been registered. This prevents firing
         // multiple channel actives if the channel is deregistered and re-registered.
         if (firstRegistration && this.channel.Active)
         {
             this.channel.pipeline.FireChannelActive();
         }
     }
     catch (Exception t)
     {
         // Close the channel directly to avoid FD leak.
         this.CloseForcibly();
         this.channel.closeFuture.Complete();
         Util.SafeSetFailure(promise, t);
     }
 }