Example usage for io.netty.channel ChannelHandlerContext read

List of usage examples for io.netty.channel ChannelHandlerContext read

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext read.

Prototype

@Override
    ChannelHandlerContext read();

Source Link

Usage

From source file:reactor.ipc.netty.http.client.HttpClientOperations.java

License:Open Source License

@Override
protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        setNettyResponse(response);//from www. j  a v a2s  .c o m
        if (response.decoderResult().isFailure()) {
            onOutboundError(response.decoderResult().cause());
            return;
        }

        if (log.isDebugEnabled()) {
            log.debug("Received response (auto-read:{}) : {}", channel().config().isAutoRead(),
                    responseHeaders().entries().toString());
        }

        if (checkResponseCode(response)) {
            prefetchMore(ctx);
            parentContext().fireContextActive(this);
        }
        if (msg instanceof FullHttpResponse) {
            super.onInboundNext(ctx, msg);
            onHandlerTerminate();
        }
        return;
    }
    if (msg instanceof LastHttpContent) {
        if (log.isDebugEnabled()) {
            log.debug("Received last HTTP packet");
        }
        if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
            super.onInboundNext(ctx, msg);
        }
        onHandlerTerminate();
        return;
    }

    super.onInboundNext(ctx, msg);
    if (isInboundCancelled()) {
        ctx.read();
    } else {
        prefetchMore(ctx);
    }

}

From source file:reactor.ipc.netty.http.client.HttpClientOperations.java

License:Open Source License

final void prefetchMore(ChannelHandlerContext ctx) {
    int inboundPrefetch = this.inboundPrefetch - 1;
    if (inboundPrefetch >= 0) {
        this.inboundPrefetch = inboundPrefetch;
        ctx.read();
    }//from  www . j a  v  a 2s.c  o  m
}

From source file:reactor.ipc.netty.http.client.HttpClientWSOperations.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void onInboundNext(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof FullHttpResponse) {
        channel().pipeline().remove(HttpObjectAggregator.class);
        FullHttpResponse response = (FullHttpResponse) msg;

        setNettyResponse(response);// ww  w  .ja  v  a2s  .  co m

        if (checkResponseCode(response)) {

            if (!handshaker.isHandshakeComplete()) {
                handshaker.finishHandshake(channel(), response);
            }

            parentContext().fireContextActive(this);
            handshakerResult.trySuccess();
        }
        return;
    }
    if (msg instanceof PingWebSocketFrame) {
        channel().writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) msg).content().retain()));
        ctx.read();
        return;
    }
    if (msg instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) msg).isFinalFragment()) {
        if (log.isDebugEnabled()) {
            log.debug("CloseWebSocketFrame detected. Closing Websocket");
        }

        CloseWebSocketFrame close = (CloseWebSocketFrame) msg;
        sendClose(new CloseWebSocketFrame(true, close.rsv(), close.content().retain()));
    } else {
        super.onInboundNext(ctx, msg);
    }
}

From source file:reactor.ipc.netty.http.server.HttpServerHandler.java

License:Open Source License

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    super.handlerAdded(ctx);
    this.ctx = ctx;
    if (HttpServerOperations.log.isDebugEnabled()) {
        HttpServerOperations.log.debug("New http connection, requesting read");
    }//from   w ww .  ja v a2 s .c om
    ctx.read();
}

From source file:reactor.ipc.netty.http.server.HttpServerHandler.java

License:Open Source License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt == NettyPipeline.handlerTerminatedEvent()) {
        if (mustRecycleEncoder) {
            mustRecycleEncoder = false;/*from  ww  w. j  a  v a  2s . c o m*/
            pendingResponses -= 1;

            ctx.pipeline().replace(NettyPipeline.HttpEncoder, NettyPipeline.HttpEncoder,
                    new HttpResponseEncoder());
        }

        if (pipelined != null && !pipelined.isEmpty()) {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug("draining next pipelined " + "request,"
                        + " pending response count: {}, queued: " + "{}", pendingResponses, pipelined.size());
            }
            ctx.executor().execute(this);
        } else {
            ctx.read();
        }
    }

    ctx.fireUserEventTriggered(evt);
}

From source file:reactor.ipc.netty.http.server.HttpServerWSOperations.java

License:Open Source License

@Override
public void onInboundNext(ChannelHandlerContext ctx, Object frame) {
    if (frame instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) frame).isFinalFragment()) {
        if (log.isDebugEnabled()) {
            log.debug("CloseWebSocketFrame detected. Closing Websocket");
        }/*from  ww w . j a v  a2 s  .com*/
        CloseWebSocketFrame close = (CloseWebSocketFrame) frame;
        sendClose(new CloseWebSocketFrame(true, close.rsv(), close.content().retain()),
                f -> onHandlerTerminate());
        return;
    }
    if (frame instanceof PingWebSocketFrame) {
        ctx.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) frame).content().retain()));
        ctx.read();
        return;
    }
    super.onInboundNext(ctx, frame);
}

From source file:rereverse.ReReverseHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    final Channel inboundChannel = ctx.channel();

    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass())
            .handler(new ChannelInboundHandlerAdapter() {
                @Override//from   www .ja  va2 s.  co m
                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                    ctx.read();
                    ctx.write(Unpooled.EMPTY_BUFFER);
                }

                @Override
                public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
                    inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if (future.isSuccess()) {
                                ctx.channel().read();
                            } else {
                                future.channel().close();
                            }
                        }
                    });
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                    closeChannel(inboundChannel);
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    cause.printStackTrace();
                    closeChannel(ctx.channel());
                }
            }).option(ChannelOption.AUTO_READ, false);

    //TODO map using url without port (?)
    ChannelFuture f = b.connect(application.remoteHost, application.remotePort);
    outbound = f.channel();
    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                inboundChannel.read();
            } else {
                // failed
                inboundChannel.close();
            }
        }
    });
}