Example usage for io.netty.channel ChannelOption ALLOW_HALF_CLOSURE

List of usage examples for io.netty.channel ChannelOption ALLOW_HALF_CLOSURE

Introduction

In this page you can find the example usage for io.netty.channel ChannelOption ALLOW_HALF_CLOSURE.

Prototype

ChannelOption ALLOW_HALF_CLOSURE

To view the source code for io.netty.channel ChannelOption ALLOW_HALF_CLOSURE.

Click Source Link

Usage

From source file:diskCacheV111.doors.NettyLineBasedDoor.java

License:Open Source License

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    try (CDC ignored = CDC.reset(getNucleus().getThisAddress())) {
        Transfer.initSession(false, true);
        cdc = new CDC();
    }//from  w w w  .j  a va2  s.co m

    channel = ctx.channel();
    channel.config().setOption(ChannelOption.ALLOW_HALF_CLOSURE, true);
    channel.config().setOption(ChannelOption.TCP_NODELAY, true);
    channel.config().setOption(ChannelOption.SO_KEEPALIVE, true);
}

From source file:org.fusesource.hawtdispatch.netty.HawtSocketChannel.java

License:Apache License

private void onReadReady() {
    final ChannelPipeline pipeline = pipeline();
    final ByteBuf byteBuf = pipeline.inboundByteBuffer();
    boolean closed = false;
    boolean read = false;
    boolean firedInboundBufferSuspended = false;
    try {//w  w w.j a  v  a2 s. c o m
        expandReadBuffer(byteBuf);
        loop: for (;;) {

            int localReadAmount = byteBuf.writeBytes(javaChannel(), byteBuf.writableBytes());
            if (localReadAmount > 0) {
                read = true;
            } else if (localReadAmount < 0) {
                closed = true;
                break;
            }

            switch (expandReadBuffer(byteBuf)) {
            case 0:
                // Read all - stop reading.
                break loop;
            case 1:
                // Keep reading until everything is read.
                break;
            case 2:
                // Let the inbound handler drain the buffer and continue reading.
                if (read) {
                    read = false;
                    pipeline.fireInboundBufferUpdated();
                    if (!byteBuf.isWritable()) {
                        throw new IllegalStateException(
                                "an inbound handler whose buffer is full must consume at " + "least one byte.");
                    }
                }
            }
        }
    } catch (Throwable t) {
        if (read) {
            read = false;
            pipeline.fireInboundBufferUpdated();
        }

        if (t instanceof IOException) {
            closed = true;
        } else if (!closed) {
            firedInboundBufferSuspended = true;
            pipeline.fireChannelReadSuspended();
        }
        pipeline().fireExceptionCaught(t);
    } finally {
        if (read) {
            pipeline.fireInboundBufferUpdated();
        }

        if (closed) {
            setInputShutdown();
            if (isOpen()) {
                if (Boolean.TRUE.equals(config().getOption(ChannelOption.ALLOW_HALF_CLOSURE))) {
                    pipeline.fireUserEventTriggered(ChannelInputShutdownEvent.INSTANCE);
                } else {
                    close(newPromise());
                }
            }
        } else if (!firedInboundBufferSuspended) {
            pipeline.fireChannelReadSuspended();
        }

        if (!config().isAutoRead()) {
            readSource.suspend();
        }
    }
}

From source file:org.jupiter.transport.netty.NettyTcpAcceptor.java

License:Apache License

@Override
protected void setOptions() {
    super.setOptions();

    ServerBootstrap boot = bootstrap();/* ww w .jav  a  2 s .  c  om*/

    // parent options
    NettyConfig.NettyTcpConfigGroup.ParentConfig parent = configGroup.parent();
    boot.option(ChannelOption.SO_BACKLOG, parent.getBacklog());
    boot.option(ChannelOption.SO_REUSEADDR, parent.isReuseAddress());
    if (parent.getRcvBuf() > 0) {
        boot.option(ChannelOption.SO_RCVBUF, parent.getRcvBuf());
    }

    // child options
    NettyConfig.NettyTcpConfigGroup.ChildConfig child = configGroup.child();
    boot.childOption(ChannelOption.SO_REUSEADDR, child.isReuseAddress())
            .childOption(ChannelOption.SO_KEEPALIVE, child.isKeepAlive())
            .childOption(ChannelOption.TCP_NODELAY, child.isTcpNoDelay())
            .childOption(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure());
    if (child.getRcvBuf() > 0) {
        boot.childOption(ChannelOption.SO_RCVBUF, child.getRcvBuf());
    }
    if (child.getSndBuf() > 0) {
        boot.childOption(ChannelOption.SO_SNDBUF, child.getSndBuf());
    }
    if (child.getLinger() > 0) {
        boot.childOption(ChannelOption.SO_LINGER, child.getLinger());
    }
    if (child.getIpTos() > 0) {
        boot.childOption(ChannelOption.IP_TOS, child.getIpTos());
    }
    int bufLowWaterMark = child.getWriteBufferLowWaterMark();
    int bufHighWaterMark = child.getWriteBufferHighWaterMark();
    if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) {
        WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark);
        boot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark);
    }
}

From source file:org.jupiter.transport.netty.NettyTcpConnector.java

License:Apache License

@Override
protected void setOptions() {
    super.setOptions();

    Bootstrap boot = bootstrap();//from   w w  w. jav a  2  s  .  com

    NettyConfig.NettyTcpConfigGroup.ChildConfig child = childConfig;

    // child options
    boot.option(ChannelOption.SO_REUSEADDR, child.isReuseAddress())
            .option(ChannelOption.SO_KEEPALIVE, child.isKeepAlive())
            .option(ChannelOption.TCP_NODELAY, child.isTcpNoDelay())
            .option(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure());
    if (child.getRcvBuf() > 0) {
        boot.option(ChannelOption.SO_RCVBUF, child.getRcvBuf());
    }
    if (child.getSndBuf() > 0) {
        boot.option(ChannelOption.SO_SNDBUF, child.getSndBuf());
    }
    if (child.getLinger() > 0) {
        boot.option(ChannelOption.SO_LINGER, child.getLinger());
    }
    if (child.getIpTos() > 0) {
        boot.option(ChannelOption.IP_TOS, child.getIpTos());
    }
    if (child.getConnectTimeoutMillis() > 0) {
        boot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, child.getConnectTimeoutMillis());
    }
    int bufLowWaterMark = child.getWriteBufferLowWaterMark();
    int bufHighWaterMark = child.getWriteBufferHighWaterMark();
    if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) {
        WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark);
        boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark);
    }
}

From source file:org.r358.poolnetty.test.simpleserver.SimpleServer.java

License:Open Source License

public SimpleServer(String host, int port, int backLog, final SimpleServerListener ssl) throws Exception {

    EventLoopGroup workers = new NioEventLoopGroup();
    EventLoopGroup bosses = new NioEventLoopGroup();

    bootstrap = new ServerBootstrap();

    bootstrap.group(bosses, workers);/*from  ww  w .  ja v  a  2s  .  co m*/
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline cpl = ch.pipeline();
            cpl.addLast("encode", new SimpleOutboundHandler(-1));
            cpl.addLast("decode", new SimpleInboundHandler());
            cpl.addLast("adapt", new ChannelInboundHandlerAdapter() {

                @Override
                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                    super.channelActive(ctx);
                    ssl.newConnection(ctx);
                }

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    ssl.newValue(ctx, msg.toString());
                }
            });

        }
    });

    bootstrap.localAddress(host, port);

    bootstrap.option(ChannelOption.ALLOW_HALF_CLOSURE, true);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.SO_BACKLOG, backLog);

}