Java Code Examples for io.netty.channel.ChannelHandlerContext#newPromise()

The following examples show how to use io.netty.channel.ChannelHandlerContext#newPromise() . 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: SslHandler.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
private void closeOutboundAndChannel(
        final ChannelHandlerContext ctx, final ChannelPromise promise, boolean disconnect) throws Exception {
    if (!ctx.channel().isActive()) {
        if (disconnect) {
            ctx.disconnect(promise);
        } else {
            ctx.close(promise);
        }
        return;
    }

    engine.closeOutbound();

    ChannelPromise closeNotifyFuture = ctx.newPromise();
    write(ctx, Unpooled.EMPTY_BUFFER, closeNotifyFuture);
    flush(ctx);
    safeClose(ctx, closeNotifyFuture, promise);
}
 
Example 2
Source File: JdkZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture close(final ChannelPromise promise) {
    ChannelHandlerContext ctx = ctx();
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        return finishEncode(ctx, promise);
    } else {
        final ChannelPromise p = ctx.newPromise();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                ChannelFuture f = finishEncode(ctx(), p);
                f.addListener(new ChannelPromiseNotifier(promise));
            }
        });
        return p;
    }
}
 
Example 3
Source File: MainAndStaticFileHandler.java    From crate with Apache License 2.0 5 votes vote down vote up
private void writeResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse resp) {
    Netty4CorsHandler.setCorsResponseHeaders(req, resp, corsConfig);
    ChannelPromise promise = ctx.newPromise();
    if (isCloseConnection(req)) {
        promise.addListener(ChannelFutureListener.CLOSE);
    } else {
        Headers.setKeepAlive(req.protocolVersion(), resp);
    }
    ctx.channel().writeAndFlush(resp, promise);
}
 
Example 4
Source File: SslHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void closeOutboundAndChannel(
        final ChannelHandlerContext ctx, final ChannelPromise promise, boolean disconnect) throws Exception {
    if (!ctx.channel().isActive()) {
        if (disconnect) {
            ctx.disconnect(promise);
        } else {
            ctx.close(promise);
        }
        return;
    }

    outboundClosed = true;
    engine.closeOutbound();

    ChannelPromise closeNotifyPromise = ctx.newPromise();
    try {
        flush(ctx, closeNotifyPromise);
    } finally {
        // It's important that we do not pass the original ChannelPromise to safeClose(...) as when flush(....)
        // throws an Exception it will be propagated to the AbstractChannelHandlerContext which will try
        // to fail the promise because of this. This will then fail as it was already completed by safeClose(...).
        // We create a new ChannelPromise and try to notify the original ChannelPromise
        // once it is complete. If we fail to do so we just ignore it as in this case it was failed already
        // because of a propagated Exception.
        //
        // See https://github.com/netty/netty/issues/5931
        safeClose(ctx, closeNotifyPromise, ctx.newPromise().addListener(
                new ChannelPromiseNotifier(false, promise)));
    }
}
 
Example 5
Source File: KeyValueSelectBucketHandler.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
                    ChannelPromise promise) throws Exception {
    originalPromise = promise;
    ChannelPromise downPromise = ctx.newPromise();
    downPromise.addListener(new GenericFutureListener<Future<Void>>() {
        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess() && !originalPromise.isDone()) {
                originalPromise.setFailure(future.cause());
            }
        }
    });
    ctx.connect(remoteAddress, localAddress, downPromise);
}
 
Example 6
Source File: KeyValueErrorMapHandler.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
                    ChannelPromise promise) throws Exception {
    originalPromise = promise;
    ChannelPromise downPromise = ctx.newPromise();
    downPromise.addListener(new GenericFutureListener<Future<Void>>() {
        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess() && !originalPromise.isDone()) {
                originalPromise.setFailure(future.cause());
            }
        }
    });
    ctx.connect(remoteAddress, localAddress, downPromise);
}
 
Example 7
Source File: SpdySessionHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void issueStreamError(ChannelHandlerContext ctx, int streamId, SpdyStreamStatus status) {
    boolean fireChannelRead = !spdySession.isRemoteSideClosed(streamId);
    ChannelPromise promise = ctx.newPromise();
    removeStream(streamId, promise);

    SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, status);
    ctx.writeAndFlush(spdyRstStreamFrame, promise);
    if (fireChannelRead) {
        ctx.fireChannelRead(spdyRstStreamFrame);
    }
}
 
Example 8
Source File: GatewayHandler.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public void handlerAdded(@Nullable ChannelHandlerContext ctx) throws Exception {
   Preconditions.checkNotNull(ctx);

   this.ctx = ctx;
   this.handshakeFuture = ctx.newPromise();
}
 
Example 9
Source File: NettyServerHandler.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
void secondGoAwayAndClose(ChannelHandlerContext ctx) {
  if (pingAckedOrTimeout) {
    return;
  }
  pingAckedOrTimeout = true;

  checkNotNull(pingFuture, "pingFuture");
  pingFuture.cancel(false);

  // send the second GOAWAY with last stream id
  goAway(
      ctx,
      connection().remote().lastStreamCreated(),
      Http2Error.NO_ERROR.code(),
      ByteBufUtil.writeAscii(ctx.alloc(), goAwayMessage),
      ctx.newPromise());

  // gracefully shutdown with specified grace time
  long savedGracefulShutdownTimeMillis = gracefulShutdownTimeoutMillis();
  long overriddenGraceTime = graceTimeOverrideMillis(savedGracefulShutdownTimeMillis);
  try {
    gracefulShutdownTimeoutMillis(overriddenGraceTime);
    NettyServerHandler.super.close(ctx, ctx.newPromise());
  } catch (Exception e) {
    onError(ctx, /* outbound= */ true, e);
  } finally {
    gracefulShutdownTimeoutMillis(savedGracefulShutdownTimeMillis);
  }
}
 
Example 10
Source File: WebSocketClientHandler.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
Example 11
Source File: CompressorHttp2ConnectionEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelFuture writeData(final ChannelHandlerContext ctx, final int streamId, ByteBuf data, int padding,
        final boolean endOfStream, ChannelPromise promise) {
    final Http2Stream stream = connection().stream(streamId);
    final EmbeddedChannel channel = stream == null ? null : (EmbeddedChannel) stream.getProperty(propertyKey);
    if (channel == null) {
        // The compressor may be null if no compatible encoding type was found in this stream's headers
        return super.writeData(ctx, streamId, data, padding, endOfStream, promise);
    }

    try {
        // The channel will release the buffer after being written
        channel.writeOutbound(data);
        ByteBuf buf = nextReadableBuf(channel);
        if (buf == null) {
            if (endOfStream) {
                if (channel.finish()) {
                    buf = nextReadableBuf(channel);
                }
                return super.writeData(ctx, streamId, buf == null ? Unpooled.EMPTY_BUFFER : buf, padding,
                        true, promise);
            }
            // END_STREAM is not set and the assumption is data is still forthcoming.
            promise.setSuccess();
            return promise;
        }

        PromiseCombiner combiner = new PromiseCombiner();
        for (;;) {
            ByteBuf nextBuf = nextReadableBuf(channel);
            boolean compressedEndOfStream = nextBuf == null && endOfStream;
            if (compressedEndOfStream && channel.finish()) {
                nextBuf = nextReadableBuf(channel);
                compressedEndOfStream = nextBuf == null;
            }

            ChannelPromise bufPromise = ctx.newPromise();
            combiner.add(bufPromise);
            super.writeData(ctx, streamId, buf, padding, compressedEndOfStream, bufPromise);
            if (nextBuf == null) {
                break;
            }

            padding = 0; // Padding is only communicated once on the first iteration
            buf = nextBuf;
        }
        combiner.finish(promise);
    } catch (Throwable cause) {
        promise.tryFailure(cause);
    } finally {
        if (endOfStream) {
            cleanup(stream, channel);
        }
    }
    return promise;
}
 
Example 12
Source File: ActionHandler.java    From nomulus with Apache License 2.0 4 votes vote down vote up
/** Initializes {@link ChannelPromise} */
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
  // Once handler is added to channel pipeline, initialize channel and future for this handler
  finished = ctx.newPromise();
}
 
Example 13
Source File: PoloniexWSSClientRouter.java    From poloniex-api-java with MIT License 4 votes vote down vote up
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
Example 14
Source File: WebSocketClientHandler.java    From msf4j with Apache License 2.0 4 votes vote down vote up
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
    this.ctx = ctx;
}
 
Example 15
Source File: WebSocketClientHandler.java    From tools-journey with Apache License 2.0 4 votes vote down vote up
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
Example 16
Source File: NettyWSClientHandler.java    From ari4java with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    handshakeFuture = ctx.newPromise();
}
 
Example 17
Source File: WebSocketClientHandler.java    From zheshiyigeniubidexiangmu with MIT License 4 votes vote down vote up
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
Example 18
Source File: WebSocketClientHandler.java    From karate with MIT License 4 votes vote down vote up
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
Example 19
Source File: TunnelClientSocketClientHandler.java    From arthas with Apache License 2.0 4 votes vote down vote up
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    registerPromise = ctx.newPromise();
}
 
Example 20
Source File: LocalFrameHandler.java    From arthas with Apache License 2.0 4 votes vote down vote up
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}