Example usage for io.netty.channel ChannelOption TCP_NODELAY

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

Introduction

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

Prototype

ChannelOption TCP_NODELAY

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

Click Source Link

Usage

From source file:org.tinygroup.nettyremote.impl.ClientImpl.java

License:GNU General Public License

protected void init(Bootstrap b) {
    b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
            .handler(new ChannelInitializer<SocketChannel>() {
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                    ch.pipeline().addLast("MessageEncoder", new ObjectEncoder());
                    ch.pipeline().addLast(new ClientHandler());
                }//from  w ww  .  j  a va2  s.  co  m
            });

}

From source file:org.vertx.java.core.net.impl.TCPSSLHelper.java

License:Open Source License

public void applyConnectionOptions(ServerBootstrap bootstrap) {
    bootstrap.childOption(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpSendBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }/* ww  w .j a  v a 2s. c om*/
    if (tcpReceiveBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
        bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(tcpReceiveBufferSize));
    }

    bootstrap.option(ChannelOption.SO_LINGER, soLinger);
    if (trafficClass != -1) {
        bootstrap.childOption(ChannelOption.IP_TOS, trafficClass);
    }
    bootstrap.childOption(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);

    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
    bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
    bootstrap.option(ChannelOption.SO_BACKLOG, acceptBackLog);
}

From source file:org.vertx.java.core.net.impl.TCPSSLHelper.java

License:Open Source License

public void applyConnectionOptions(Bootstrap bootstrap) {
    bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpSendBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }/*from   www  .  j a  v a2 s  . c om*/
    if (tcpReceiveBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(tcpReceiveBufferSize));
    }
    bootstrap.option(ChannelOption.SO_LINGER, soLinger);
    if (trafficClass != -1) {
        bootstrap.option(ChannelOption.IP_TOS, trafficClass);
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
}

From source file:org.virtue.network.Network.java

License:Open Source License

/**
 * Binds a pipeline to a port//from  w  ww  . j  av  a2s .c o  m
 */
public void bindNetwork() {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<NioSocketChannel>() {

                @Override
                protected void initChannel(NioSocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast("decoder", new HandshakeDecoder());
                    pipeline.addLast("read-timeout", new ReadTimeoutHandler(15));
                    pipeline.addLast("channel-handler", new NetworkHandler());
                }

            }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.TCP_NODELAY, true);

    try {
        future = bootstrap.bind(Constants.SERVER_PORT).sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    logger.info("Bound Virtue on: " + future.channel().localAddress().toString());
}

From source file:org.waarp.common.utility.WaarpNettyUtil.java

License:Open Source License

/**
 * Add default configuration for client bootstrap
 * //w ww .  j a  va  2  s . c o  m
 * @param bootstrap
 * @param group
 * @param timeout
 */
public static void setBootstrap(Bootstrap bootstrap, EventLoopGroup group, int timeout) {
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.group(group);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
    bootstrap.option(ChannelOption.SO_RCVBUF, 1048576);
    bootstrap.option(ChannelOption.SO_SNDBUF, 1048576);
}

From source file:org.waarp.common.utility.WaarpNettyUtil.java

License:Open Source License

/**
 * Add default configuration for server bootstrap
 * /*from   w w w  . j  a  v a  2s  .  com*/
 * @param bootstrap
 * @param groupBoss
 * @param groupWorker
 * @param timeout
 */
public static void setServerBootstrap(ServerBootstrap bootstrap, EventLoopGroup groupBoss,
        EventLoopGroup groupWorker, int timeout) {
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.group(groupBoss, groupWorker);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
    bootstrap.childOption(ChannelOption.SO_RCVBUF, 1048576);
    bootstrap.childOption(ChannelOption.SO_SNDBUF, 1048576);
}

From source file:org.waarp.openr66.protocol.localhandler.LocalTransaction.java

License:Open Source License

/**
 * Constructor//from  www. j  a va2s. c  om
 */
public LocalTransaction() {
    serverBootstrap.channel(LocalServerChannel.class);
    serverBootstrap.group(Configuration.configuration.getLocalBossGroup(),
            Configuration.configuration.getLocalWorkerGroup());
    serverBootstrap.option(ChannelOption.TCP_NODELAY, true);
    serverBootstrap.option(ChannelOption.SO_REUSEADDR, true);
    serverBootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            (int) Configuration.configuration.getTIMEOUTCON());
    serverBootstrap.childHandler(new LocalServerInitializer());
    try {
        serverChannel = serverBootstrap.bind(socketLocalServerAddress).sync().channel();
    } catch (InterruptedException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
    localChannelGroup.add(serverChannel);

    clientBootstrap.channel(LocalChannel.class);
    // Same Group than Network final handler 
    clientBootstrap.group(Configuration.configuration.getLocalWorkerGroup());
    clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            (int) Configuration.configuration.getTIMEOUTCON());
    clientBootstrap.handler(new LocalClientInitializer());
}

From source file:org.wenxueliu.netty.client.ClientTest.java

License:Apache License

private void connectRetry(String ip, int port, ChannelFutureListener clientConnectionListener) {
    try {/* www.  ja va  2 s  .c o m*/
        bootstrap = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_SNDBUF, SEND_BUFFER_SIZE)
                .option(ChannelOption.SO_RCVBUF, SEND_BUFFER_SIZE)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT);

        pipelineFactory = new DefaultChannelInitializer(timer, this);
        bootstrap.handler(pipelineFactory);
        //.handler(new ChannelInitializer<SocketChannel>() {

        //    @Override
        //    protected void initChannel(SocketChannel channel) throws Exception {
        //        ChannelPipeline p = channel.pipeline();
        //        p.addLast(new MessageDecoder(),
        //                  new StringEncoder(CharsetUtil.UTF_8),
        //                  new IdleStateHandler(IDLE_TIMEOUT_SEC, 0, 0),
        //                  new BootstrapTimeoutHandler(timer, 10),
        //                  new ConnectionHandler());
        //    }
        //});
        bootstrap.remoteAddress(ip, port);
        ChannelFuture future = bootstrap.connect();
        future.awaitUninterruptibly();
        future.addListener(clientConnectionListener);
    } catch (Exception e) {
        log.warn("Connection to the server {}:{} failed", ip, port);
    }
}

From source file:org.wildfly.swarm.arquillian.daemon.server.Server.java

License:Apache License

public final void start() throws ServerLifecycleException, IllegalStateException {

    // Precondition checks
    if (this.isRunning()) {
        throw new IllegalStateException("Already running");
    }/*from w  w  w  .  j  a  v  a 2  s  .co  m*/

    // Set up Netty Boostrap
    final EventLoopGroup parentGroup = new NioEventLoopGroup();
    final EventLoopGroup childGroup = new NioEventLoopGroup();
    this.eventLoopGroups.add(parentGroup);
    this.eventLoopGroups.add(childGroup);
    final ServerBootstrap bootstrap = new ServerBootstrap().group(parentGroup, childGroup)
            .channel(NioServerSocketChannel.class).localAddress(this.getBindAddress())
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(final SocketChannel channel) throws Exception {
                    final ChannelPipeline pipeline = channel.pipeline();
                    setupPipeline(pipeline);
                }
            }).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true);

    // Start 'er up
    final ChannelFuture openChannel;
    try {
        openChannel = bootstrap.bind().sync();
    } catch (final InterruptedException ie) {
        Thread.interrupted();
        throw new ServerLifecycleException("Interrupted while awaiting server start", ie);
    } catch (final RuntimeException re) {
        // Exception xlate
        throw new ServerLifecycleException("Encountered error in binding; could not start server.", re);
    }
    // Set bound address
    final InetSocketAddress boundAddress = ((InetSocketAddress) openChannel.channel().localAddress());

    // Running
    running = true;
    // Create the shutdown service
    this.shutdownService = Executors.newSingleThreadExecutor();

    if (log.isLoggable(Level.INFO)) {
        log.info("Arquillian Daemon server started on " + boundAddress.getHostName() + ":"
                + boundAddress.getPort());
    }

}