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:james.learn.netty.echo2.EchoClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO/*from ww  w  .  ja v  a2s .  co m*/
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        /* ByteBuf delimiter = Unpooled.copiedBuffer("$_"
                            .getBytes());
                         ch.pipeline().addLast(
                            new DelimiterBasedFrameDecoder(1024,
                               delimiter));*/
                        ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024 * 1024, 0, 4));
                        ch.pipeline().addLast(new LengthFieldPrepender(4, false));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });

        // ??
        ChannelFuture f = b.connect(host, port).sync();

        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}

From source file:jj.http.server.HttpServer.java

License:Apache License

private void makeServerBootstrap(int bindingCount) {
    serverBootstrap = new ServerBootstrap()
            .group(new NioEventLoopGroup(bindingCount, threadFactory), ioEventLoopGroup)
            .channel(NioServerSocketChannel.class).childHandler(initializer)
            .option(ChannelOption.SO_KEEPALIVE, configuration.keepAlive())
            .option(ChannelOption.SO_REUSEADDR, configuration.reuseAddress())
            .option(ChannelOption.TCP_NODELAY, configuration.tcpNoDelay())
            .option(ChannelOption.SO_TIMEOUT, configuration.timeout())
            .option(ChannelOption.SO_BACKLOG, configuration.backlog())
            .option(ChannelOption.SO_RCVBUF, configuration.receiveBufferSize())
            .option(ChannelOption.SO_SNDBUF, configuration.sendBufferSize());
}

From source file:lunarion.node.LunarDBServerStandAlone.java

License:Open Source License

public void bind(int port) throws InterruptedException {

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.RCVBUF_ALLOCATOR,
                    new AdaptiveRecvByteBufAllocator(64, 1024, 65536 * 512))
            .option(ChannelOption.SO_BACKLOG, 1024).option(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.SO_KEEPALIVE, true)
            .childHandler(new LunarDBServerChannelInitializer(node_tc));

    Channel ch = bootstrap.bind(port).sync().channel();
    System.err.println(Timer.currentTime() + " DB node server channel binded at port: " + port + '.');
    logger.info(Timer.currentTime() + " [NODE INFO]:  DB node server channel binded at port: " + port + '.');
    ch.closeFuture().sync();//from   w ww . j  a  v a 2  s  .  c  o  m
    /*
     *  Bind and start to accept incoming connections.
     */
    // ChannelFuture future = bootstrap.bind(port).sync();
    /*
     *  Wait until the server socket is closed.
     *  In this example, this does not happen, but you can do that to gracefully 
     *  shut down your server.
     */

    // future.channel().closeFuture().sync();

}

From source file:majordodo.network.netty.NettyConnector.java

License:Apache License

public NettyChannel connect() throws Exception {
    boolean useOpenSSL = NetworkUtils.isOpenSslAvailable();
    if (ssl) {// w  w  w  .ja v  a  2  s.com
        if (sslUnsecure) {
            this.sslCtx = SslContextBuilder.forClient()
                    .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK)
                    .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        } else {
            this.sslCtx = SslContextBuilder.forClient()
                    .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK).build();
        }
    }
    if (NetworkUtils.isEnableEpollNative()) {
        group = new EpollEventLoopGroup();
    } else {
        group = new NioEventLoopGroup();
    }
    LOG.log(Level.INFO, "Trying to connect to broker at " + host + ":" + port + " ssl:" + ssl + ", sslUnsecure:"
            + sslUnsecure + " openSsl:" + useOpenSSL);

    Bootstrap b = new Bootstrap();
    b.group(group)
            .channel(NetworkUtils.isEnableEpollNative() ? EpollSocketChannel.class : NioSocketChannel.class)
            .option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    channel = new NettyChannel(host + ":" + port, ch, callbackExecutor, NettyConnector.this);
                    channel.setMessagesReceiver(receiver);
                    channel.setRemoteHost(host);
                    if (ssl) {
                        ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), host, port));
                    }
                    ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4));
                    ch.pipeline().addLast("lengthbaseddecoder",
                            new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                    //
                    ch.pipeline().addLast("messageencoder", new DodoMessageEncoder());
                    ch.pipeline().addLast("messagedecoder", new DodoMessageDecoder());
                    ch.pipeline().addLast(new InboundMessageHandler(channel));
                }
            });

    ChannelFuture f = b.connect(host, port).sync();
    socketchannel = f.channel();
    return channel;

}

From source file:me.calvinliu.netty.echo.EchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.git
    final SslContext sslCtx;
    if (SSL) {/*from  w  w w.  ja  v a2s .c  o m*/
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                        }
                        //                     p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoClientHandler());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:me.ferrybig.javacoding.teamspeakconnector.TeamspeakApi.java

License:Open Source License

private ChannelFuture openChannel(SocketAddress addr, ChannelInitializer<? extends SocketChannel> ch,
        int timneout) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.remoteAddress(addr);/*from w  w  w  . java 2s  .  co  m*/
    bootstrap.handler(ch);
    bootstrap.group(group);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
    bootstrap.option(ChannelOption.TCP_NODELAY, true); // We send relatively small packets, we benefit more from delayed ack
    return bootstrap.connect();
}

From source file:me.jtalk.socketconnector.io.TCPManager.java

License:Open Source License

private ServerBootstrap instantiateServer(TCPActivationSpec spec) throws ResourceException {

    ServerBootstrap newServer = new ServerBootstrap();
    newServer.group(this.listeners, this.workers);
    newServer.channel(NioServerSocketChannel.class);
    newServer.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override//  w  w w .  j a va 2 s  . c  om
        protected void initChannel(SocketChannel c) throws Exception {
            c.pipeline().addLast(new Receiver(TCPManager.this, TCPManager.this.ids.incrementAndGet()))
                    .addLast(new Sender());
        }
    });
    newServer.option(ChannelOption.SO_KEEPALIVE, true);
    newServer.option(ChannelOption.SO_BACKLOG, spec.getBacklog());
    newServer.option(ChannelOption.TCP_NODELAY, true);

    return newServer;
}

From source file:mmo.client.connection.ServerConnection.java

License:Apache License

/**
 * Opens connection to server. This method must be called explicitly.
 *///  w w w  . j  ava  2  s .  c  o m
public void open() {
    new Bootstrap().group(notificationGroup).channel(NioSocketChannel.class)
            .handler(new NotificationInitializer())
            .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(16384))
            .option(ChannelOption.TCP_NODELAY, true).connect(this.host, this.port);
    new Bootstrap().group(dataGroup).channel(NioSocketChannel.class).handler(new DataInitializer())
            .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(16384))
            .option(ChannelOption.TCP_NODELAY, true).connect(this.host, this.port);
}

From source file:mmo.server.Server.java

License:Open Source License

public void run(String host, int port) {
    parentGroup = new NioEventLoopGroup();
    childGroup = new NioEventLoopGroup();

    new ServerBootstrap().group(parentGroup, childGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new HttpServerCodec(), //
                            new HttpObjectAggregator(65536), //
                            routeHandlerProvider.get());

                }//from w  w  w  .  j  a v  a2s . co m
            }).option(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(16384)).bind(host, port)
            .addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        L.error("Error setting up server channel: {}", future.cause(), null);
                        new Thread(() -> { // TODO shutdown program
                            // gracefuller
                            try {
                                shutdown();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } finally {
                                System.exit(1);
                            }
                        }).start();
                    }
                }
            });
}

From source file:nenea.client.echo.EchoClient.java

License:Apache License

public void start() throws Exception {
    // Configure SSL.git
    final SslContext sslCtx;
    if (SSL) {/*  w w w.  j av a2s  .  c om*/
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                        }
                        // p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoClientHandler());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}