Example usage for io.netty.channel ChannelFutureListener CLOSE

List of usage examples for io.netty.channel ChannelFutureListener CLOSE

Introduction

In this page you can find the example usage for io.netty.channel ChannelFutureListener CLOSE.

Prototype

ChannelFutureListener CLOSE

To view the source code for io.netty.channel ChannelFutureListener CLOSE.

Click Source Link

Document

A ChannelFutureListener that closes the Channel which is associated with the specified ChannelFuture .

Usage

From source file:com.topsec.bdc.platform.api.test.http.websocketx.benchmarkserver.WebSocketServerHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {

    // Generate an error page if response getStatus code is not OK (200).
    if (res.getStatus().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);/* w  w  w .  j av  a  2s .  c  om*/
        buf.release();
        HttpHeaders.setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.ttpod.rpc.netty.telnet.TelnetServerHandler.java

License:Apache License

public void messageReceived(ChannelHandlerContext ctx, String request) throws Exception {

    request = request.trim();/*from www  .j a  v  a  2 s  . c  om*/
    int blank = request.indexOf(' ');
    String key = request;
    String args = null;
    if (blank > 0) {
        key = request.substring(0, blank);
        args = request.substring(++blank).trim();
    }
    String response;
    ServerProcessor proc = processors.get(key);
    boolean close = false;
    if (null != proc) {
        RequestBean<String> req = new RequestBean<>();
        req.setData(args);
        response = String.valueOf(proc.handle(req).getData()) + "\r\n";
    } else if ("bye".equals(request.toLowerCase())) {
        response = "Have a good day!\r\n";
        close = true;
    } else {
        response = usage(processors);
    }

    // We do not need to write a ChannelBuffer here.
    // We know the encoder inserted at TelnetPipelineFactory will do the conversion.
    ChannelFuture future = ctx.write(response);

    // Close the connection after sending 'Have a good day!'
    // if the client has sent 'bye'.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.twitter.http2.HttpConnectionHandler.java

License:Apache License

private void issueConnectionError(HttpErrorCode status) {
    ChannelFuture future = sendGoAwayFrame(status);
    future.addListener(ChannelFutureListener.CLOSE);
}

From source file:com.ura.proxy.HexDumpProxyInboundHandler.java

License:Apache License

/**
 * Closes the specified channel after all queued write requests are flushed.
 *//*from ww w. j a v a2 s . c  o  m*/
static void closeOnFlush(Channel ch) {
    if (ch.isConnected()) {
        ch.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.vmware.dcp.common.http.netty.NettyHttpClientRequestHandler.java

License:Open Source License

private void writeResponse(ChannelHandlerContext ctx, Operation request, FullHttpResponse response) {
    boolean isClose = !request.isKeepAlive() || response == null;
    Object rsp = Unpooled.EMPTY_BUFFER;
    if (response != null) {
        AsciiString v = isClose ? HttpHeaderValues.CLOSE : HttpHeaderValues.KEEP_ALIVE;
        response.headers().set(HttpHeaderNames.CONNECTION, v);
        rsp = response;/* ww w .ja  va 2 s  . c o m*/
    }

    ctx.channel().attr(NettyChannelContext.OPERATION_KEY).remove();

    ChannelFuture future = ctx.writeAndFlush(rsp);
    if (isClose) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.waitme.game.net.netty.HttpServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {

        BlockTest bolck = Bootstrap.context.getBean("blockTest", BlockTest.class);
        bolck.init();/*w  w w  . j a v a2 s .c o m*/

        HttpRequest req = (HttpRequest) msg;

        if (HttpUtil.is100ContinueExpected(req)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }
        boolean keepAlive = HttpUtil.isKeepAlive(req);
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT));
        response.headers().set(CONTENT_TYPE, "text/plain");
        response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());

        if (!keepAlive) {
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        } else {
            response.headers().set(CONNECTION, KEEP_ALIVE);
            ctx.write(response);
        }
    }
}

From source file:com.wx3.galacdecks.networking.HttpHandler.java

License:Open Source License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    if (res.getStatus().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);//from   ww  w.j  a v  a 2  s  .  co m
        buf.release();
        HttpHeaders.setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.xx_dev.apn.proxy.ApnProxyRelayHandler.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    LoggerUtil.debug(logger, ctx.channel().attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), tag,
            "channel inactive");
    if (relayChannel != null && relayChannel.isActive()) {
        relayChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }//from w w w  .  j  a v a 2 s  .c  o  m
    ctx.fireChannelInactive();
}

From source file:com.xx_dev.apn.proxy.ApnProxyUserAgentTunnelHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext uaChannelCtx, Object msg) throws Exception {

    if (msg instanceof HttpRequest) {
        final HttpRequest httpRequest = (HttpRequest) msg;

        //Channel uaChannel = uaChannelCtx.channel();

        // connect remote
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(uaChannelCtx.channel().eventLoop()).channel(NioSocketChannel.class)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .option(ChannelOption.AUTO_READ, false)
                .handler(new ApnProxyTunnelChannelInitializer(uaChannelCtx.channel()));

        final ApnProxyRemote apnProxyRemote = uaChannelCtx.channel()
                .attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get().getRemote();

        // set local address
        if (StringUtils.isNotBlank(ApnProxyLocalAddressChooser.choose(apnProxyRemote.getRemoteHost()))) {
            bootstrap.localAddress(new InetSocketAddress(
                    (ApnProxyLocalAddressChooser.choose(apnProxyRemote.getRemoteHost())), 0));
        }/*ww  w.j  a  v  a2 s  .c o m*/

        bootstrap.connect(apnProxyRemote.getRemoteHost(), apnProxyRemote.getRemotePort())
                .addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(final ChannelFuture future1) throws Exception {
                        if (future1.isSuccess()) {
                            if (apnProxyRemote.isAppleyRemoteRule()) {
                                uaChannelCtx.pipeline().remove("codec");
                                uaChannelCtx.pipeline().remove(ApnProxyPreHandler.HANDLER_NAME);
                                uaChannelCtx.pipeline().remove(ApnProxyUserAgentTunnelHandler.HANDLER_NAME);

                                // add relay handler
                                uaChannelCtx.pipeline()
                                        .addLast(new ApnProxyRelayHandler("UA --> Remote", future1.channel()));

                                future1.channel()
                                        .writeAndFlush(Unpooled.copiedBuffer(
                                                constructConnectRequestForProxy(httpRequest, apnProxyRemote),
                                                CharsetUtil.UTF_8))
                                        .addListener(new ChannelFutureListener() {
                                            @Override
                                            public void operationComplete(ChannelFuture future2)
                                                    throws Exception {
                                                if (!future2.channel().config()
                                                        .getOption(ChannelOption.AUTO_READ)) {
                                                    future2.channel().read();
                                                }
                                            }
                                        });

                            } else {
                                HttpResponse proxyConnectSuccessResponse = new DefaultFullHttpResponse(
                                        HttpVersion.HTTP_1_1,
                                        new HttpResponseStatus(200, "Connection established"));
                                uaChannelCtx.writeAndFlush(proxyConnectSuccessResponse)
                                        .addListener(new ChannelFutureListener() {
                                            @Override
                                            public void operationComplete(ChannelFuture future2)
                                                    throws Exception {
                                                // remove handlers
                                                uaChannelCtx.pipeline().remove("codec");
                                                uaChannelCtx.pipeline().remove(ApnProxyPreHandler.HANDLER_NAME);
                                                uaChannelCtx.pipeline()
                                                        .remove(ApnProxyUserAgentTunnelHandler.HANDLER_NAME);

                                                // add relay handler
                                                uaChannelCtx.pipeline()
                                                        .addLast(new ApnProxyRelayHandler(
                                                                "UA --> " + apnProxyRemote.getRemoteAddr(),
                                                                future1.channel()));
                                            }

                                        });
                            }

                        } else {
                            if (uaChannelCtx.channel().isActive()) {
                                uaChannelCtx.channel().writeAndFlush(Unpooled.EMPTY_BUFFER)
                                        .addListener(ChannelFutureListener.CLOSE);
                            }
                        }
                    }
                });

    }
    ReferenceCountUtil.release(msg);
}

From source file:com.yahoo.pulsar.broker.service.Consumer.java

License:Apache License

public void sendError(ByteBuf error) {
    cnx.ctx().writeAndFlush(error).addListener(ChannelFutureListener.CLOSE);
}