Java Code Examples for io.netty.channel.ChannelPromise#setUncancellable()

The following examples show how to use io.netty.channel.ChannelPromise#setUncancellable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public void register(EventLoop eventLoop, ChannelPromise promise) {
    if (!promise.setUncancellable()) {
        return;
    }
    if (registered) {
        throw new UnsupportedOperationException("Re-register is not supported");
    }

    registered = true;

    if (!outbound) {
        // Add the handler to the pipeline now that we are registered.
        pipeline().addLast(inboundStreamHandler);
    }

    promise.setSuccess();

    pipeline().fireChannelRegistered();
    if (isActive()) {
        pipeline().fireChannelActive();
    }
}
 
Example 2
Source File: AbstractOioChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public void connect(
        final SocketAddress remoteAddress,
        final SocketAddress localAddress, final ChannelPromise promise) {
    if (!promise.setUncancellable() || !ensureOpen(promise)) {
        return;
    }

    try {
        boolean wasActive = isActive();
        doConnect(remoteAddress, localAddress);

        // Get the state as trySuccess() may trigger an ChannelFutureListener that will close the Channel.
        // We still need to ensure we call fireChannelActive() in this case.
        boolean active = isActive();

        safeSetSuccess(promise);
        if (!wasActive && active) {
            pipeline().fireChannelActive();
        }
    } catch (Throwable t) {
        safeSetFailure(promise, annotateConnectException(t, remoteAddress));
        closeIfClosed();
    }
}
 
Example 3
Source File: UkcpClientChannel.java    From kcp-netty with MIT License 6 votes vote down vote up
@Override
public void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
    if (!promise.setUncancellable() || !ensureOpen(promise)) {
        return;
    }

    try {
        boolean wasActive = isActive();
        if (doConnect(remoteAddress, localAddress)) {
            fulfillConnectPromise(promise, wasActive);
        } else {
            throw new Error();
        }
    } catch (Throwable t) {
        promise.tryFailure(annotateConnectException(t, remoteAddress));
        closeIfClosed();
    }
}
 
Example 4
Source File: AbstractOioChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public void connect(
        final SocketAddress remoteAddress,
        final SocketAddress localAddress, final ChannelPromise promise) {
    if (!promise.setUncancellable() || !ensureOpen(promise)) {
        return;
    }

    try {
        boolean wasActive = isActive();
        doConnect(remoteAddress, localAddress);
        safeSetSuccess(promise);
        if (!wasActive && isActive()) {
            pipeline().fireChannelActive();
        }
    } catch (Throwable t) {
        safeSetFailure(promise, annotateConnectException(t, remoteAddress));
        closeIfClosed();
    }
}
 
Example 5
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(final SocketAddress remoteAddress,
                    SocketAddress localAddress, final ChannelPromise promise) {
    if (!promise.setUncancellable()) {
        return;
    }
    promise.setFailure(new UnsupportedOperationException());
}
 
Example 6
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void bind(SocketAddress localAddress, ChannelPromise promise) {
    if (!promise.setUncancellable()) {
        return;
    }
    promise.setFailure(new UnsupportedOperationException());
}
 
Example 7
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void deregister(ChannelPromise promise) {
    if (!promise.setUncancellable()) {
        return;
    }
    if (registered) {
        registered = true;
        promise.setSuccess();
        pipeline().fireChannelUnregistered();
    } else {
        promise.setFailure(new IllegalStateException("Not registered"));
    }
}
 
Example 8
Source File: VirtualChannel.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(final SocketAddress remoteAddress,
        SocketAddress localAddress, final ChannelPromise promise) {
    if (!promise.setUncancellable() || !ensureOpen(promise)) {
        return;
    }

    if (state == State.CONNECTED) {
        Exception cause = new AlreadyConnectedException();
        safeSetFailure(promise, cause);
        pipeline().fireExceptionCaught(cause);
        return;
    }

    if (connectPromise != null) {
        throw new ConnectionPendingException();
    }

    connectPromise = promise;

    if (state != State.BOUND) {
        // Not bound yet and no localAddress specified - get one.
        if (localAddress == null) {
            localAddress = new VirtualAddress(VirtualChannel.this);
        }
    }

    if (localAddress != null) {
        try {
            doBind(localAddress);
        } catch (Throwable t) {
            safeSetFailure(promise, t);
            close(voidPromise());
            return;
        }
    }

}
 
Example 9
Source File: CommandEncoder.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (acceptOutboundMessage(msg)) {
        if (!promise.setUncancellable()) {
            return;
        }
    }

    try {
        super.write(ctx, msg, promise);
    } catch (Exception e) {
        promise.tryFailure(e);
        throw e;
    }
}
 
Example 10
Source File: CommandBatchEncoder.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (acceptOutboundMessage(msg)) {
        if (!promise.setUncancellable()) {
            return;
        }
    }

    super.write(ctx, msg, promise);
}
 
Example 11
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public void close(final ChannelPromise promise) {
    if (!promise.setUncancellable()) {
        return;
    }
    if (closeInitiated) {
        if (closePromise.isDone()) {
            // Closed already.
            promise.setSuccess();
        } else if (!(promise instanceof VoidChannelPromise)) { // Only needed if no VoidChannelPromise.
            // This means close() was called before so we just register a listener and return
            closePromise.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    promise.setSuccess();
                }
            });
        }
        return;
    }
    closeInitiated = true;

    closePending = false;
    fireChannelReadPending = false;

    // Only ever send a reset frame if the connection is still alive as otherwise it makes no sense at
    // all anyway.
    if (parent().isActive() && !streamClosedWithoutError && isStreamIdValid(stream().id())) {
        Http2StreamFrame resetFrame = new DefaultHttp2ResetFrame(Http2Error.CANCEL).stream(stream());
        write(resetFrame, unsafe().voidPromise());
        flush();
    }

    if (inboundBuffer != null) {
        for (;;) {
            Object msg = inboundBuffer.poll();
            if (msg == null) {
                break;
            }
            ReferenceCountUtil.release(msg);
        }
    }

    // The promise should be notified before we call fireChannelInactive().
    outboundClosed = true;
    closePromise.setSuccess();
    promise.setSuccess();

    pipeline().fireChannelInactive();
    if (isRegistered()) {
        deregister(unsafe().voidPromise());
    }
}