io.netty.channel.ChannelHandlerContext#newPromise ( )源码实例Demo

下面列出了io.netty.channel.ChannelHandlerContext#newPromise ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: netty-4.1.22   文件: JdkZlibEncoder.java
@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;
    }
}
 
源代码2 项目: netty4.0.27Learn   文件: SslHandler.java
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);
}
 
源代码3 项目: crate   文件: MainAndStaticFileHandler.java
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);
}
 
源代码4 项目: grpc-java   文件: NettyServerHandler.java
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);
  }
}
 
源代码5 项目: arcusplatform   文件: GatewayHandler.java
@Override
public void handlerAdded(@Nullable ChannelHandlerContext ctx) throws Exception {
   Preconditions.checkNotNull(ctx);

   this.ctx = ctx;
   this.handshakeFuture = ctx.newPromise();
}
 
源代码6 项目: netty-4.1.22   文件: SpdySessionHandler.java
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);
    }
}
 
@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);
}
 
@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);
}
 
源代码9 项目: netty-4.1.22   文件: SslHandler.java
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)));
    }
}
 
源代码10 项目: karate   文件: WebSocketClientHandler.java
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
源代码11 项目: tools-journey   文件: WebSocketClientHandler.java
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
源代码12 项目: netty-4.1.22   文件: WebSocketClientHandler.java
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
@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;
}
 
源代码14 项目: nomulus   文件: ActionHandler.java
/** 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();
}
 
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
源代码16 项目: msf4j   文件: WebSocketClientHandler.java
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
    this.ctx = ctx;
}
 
源代码17 项目: ari4java   文件: NettyWSClientHandler.java
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    handshakeFuture = ctx.newPromise();
}
 
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}
 
源代码19 项目: arthas   文件: TunnelClientSocketClientHandler.java
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    registerPromise = ctx.newPromise();
}
 
源代码20 项目: arthas   文件: LocalFrameHandler.java
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();
}