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:com.friz.owari.network.client.Client.java

License:Open Source License

@Override
public void initialize() throws NoSuchAlgorithmException {
    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<NioSocketChannel>() {

        @Override/*from  ww w.j  av a  2 s .  co  m*/
        protected void initChannel(NioSocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();

            pipeline.addLast(HandshakeEncoder.class.getName(), new HandshakeEncoder());
            pipeline.addLast(HandshakeDecoder.class.getName(), new HandshakeDecoder());
            pipeline.addLast(IdleStateHandler.class.getName(), new IdleStateHandler(15, 0, 0));
            pipeline.addLast(ClientChannelHandler.class.getName(), new ClientChannelHandler(Client.this));
        }

    }).option(ChannelOption.TCP_NODELAY, true);

    hub.listen(HandshakeEvent.class, new HandshakeListener());
    hub.listen(ExchangeRecieveEvent.class, new ExchangeListener());
    hub.listen(PatchInitEvent.class, new PatchInitListener());
    hub.listen(PatchEvent.class, new PatchListener());

    final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH");
    keyPairGenerator.initialize(1024);

    final KeyPair keyPair = keyPairGenerator.generateKeyPair();

    privateKey = keyPair.getPrivate();
    publicKey = keyPair.getPublic();
}

From source file:com.friz.owari.network.server.Server.java

License:Open Source License

@Override
public void initialize() throws NoSuchAlgorithmException {
    bootstrap = new ServerBootstrap();

    bootstrap.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
            .childHandler(new ChannelInitializer<NioSocketChannel>() {

                @Override/*from  w w w  . ja  v  a  2  s  .  co  m*/
                protected void initChannel(NioSocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();

                    pipeline.addLast(HandshakeDecoder.class.getName(), new HandshakeDecoder());
                    pipeline.addLast(HandshakeEncoder.class.getName(), new HandshakeEncoder());
                    pipeline.addLast(IdleStateHandler.class.getName(), new IdleStateHandler(15, 0, 0));
                    pipeline.addLast(ServerChannelHandler.class.getName(),
                            new ServerChannelHandler(Server.this));
                }

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

    hub.listen(HandshakeEvent.class, new HandshakeListener());
    hub.listen(ExchangeRecieveEvent.class, new ExchangeListener());

    final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH");
    keyPairGenerator.initialize(1024);

    final KeyPair keyPair = keyPairGenerator.generateKeyPair();

    privateKey = keyPair.getPrivate();
    publicKey = keyPair.getPublic();
}

From source file:com.friz.update.UpdateServer.java

License:Open Source License

@Override
public void initialize() {
    group = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors());
    bootstrap = new ServerBootstrap();

    UpdateServer s = this;

    bootstrap.group(group).channel(NioServerSocketChannel.class)
            //.handler(new LoggingHandler(LogLevel.INFO))
            .childHandler(new ChannelInitializer<NioSocketChannel>() {

                @Override//w  w w .  java 2 s  .c o  m
                protected void initChannel(NioSocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    p.addLast(UpdateInitEncoder.class.getName(), new UpdateInitEncoder());
                    p.addLast(UpdateInitDecoder.class.getName(), new UpdateInitDecoder());
                    p.addLast(IdleStateHandler.class.getName(), new IdleStateHandler(15, 0, 0));
                    p.addLast(UpdateChannelHandler.class.getName(), new UpdateChannelHandler(s));
                }

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

    hub.listen(UpdateRequestEvent.class, new UpdateRequestEventListener());
    hub.listen(XorRequestEvent.class, new XorRequestEventListener());
    hub.listen(FileRequestEvent.class, new FileRequestEventListener());

    service.startAsync();
}

From source file:com.gdut.Netty_testing.time_server.client.TimeClient.java

License:Apache License

/**
 * //  w w  w .j a va  2  s.c om
 * @Title: main
 * @Description: TODO
 * @param @param args
 * @param @throws Exception
 * @return void
 * @throws
 */
public static void main(String[] args) throws Exception {
    // Configure SSL.git
    final SslContext sslCtx;
    if (SSL) {
        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 {
                        ch.pipeline().addLast(new TimeDecoder(), new TimeClientHandler(),
                                new TimeOutBoundHandler(), new LoggingHandler(LogLevel.INFO));
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();
        // channel.connect(remoteAddress, promise);
        // ?connect()??OutboundHandler
        // ?InboundHandlerlogic???
        // ??
        // InboundHandler?writeAndFlush(Object
        // msg)??OutBoundHandler
        // ??  ctx.write(encoded, promise);
        // writeAndFlush(Object msg)
        // ?InboundHandler  decoder?
        // ?TimeClientHandler()Handler,
        // ctx.close();?OutBoundHandler ?

        Thread.sleep(9000);
        // 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:com.ghrum.common.protocol.CommonConnectionManager.java

License:Apache License

/**
 * Initialise the server connection//  w  w w .  ja  v a  2s  .  c o  m
 *
 * @param configuration the configuration of the connection
 */
public void initialise(Protocol configuration) {
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ProtocolChannelInitializer(protocol.getMessageService(), null)) // null -> handler
            .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true);
}

From source file:com.github.herong.rpc.netty.protobuf.demo1.ProtobufClient.java

License:Apache License

public void run() throws Exception {
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {//ww  w .  j  a  v a  2  s.c o  m
        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 {
                        ch.pipeline()

                                // 
                                .addLast("frameDecoder", new ProtobufVarint32FrameDecoder())
                                // 
                                .addLast("protobufDecoder",
                                        new ProtobufDecoder(AddressBookProtos.AddressBook.getDefaultInstance()))
                                // 
                                .addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender())
                                .addLast("protobufEncoder", new ProtobufEncoder())
                                // 
                                .addLast("handler", new ProtobufClientHandler());

                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(host, port).sync();

        sendMsg(f.channel());
        // 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:com.github.herong.rpc.netty.protobuf.demo2.Demo2ProtobufClient.java

License:Apache License

public void run() throws Exception {
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*from  ww w. jav a 2s.co m*/
        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 {
                        ch.pipeline()

                                // 
                                .addLast("frameDecoder", new ProtobufVarint32FrameDecoder())
                                // 
                                .addLast("protobufDecoder",
                                        new ProtobufDecoder(Message.DTO.getDefaultInstance()))
                                // 
                                .addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender())
                                .addLast("protobufEncoder", new ProtobufEncoder())
                                // 
                                .addLast("handler", new Demo2ProtobufClientHandler());

                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(host, port).sync();

        sendMsg(f.channel());
        // 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:com.github.lburgazzoli.quickfixj.transport.netty.NettySocketInitiator.java

License:Apache License

/**
 *
 *//*www  .java2  s.  c  om*/
@Override
public void connect() {
    try {
        m_boot = new Bootstrap();
        m_boot.group(new NioEventLoopGroup());
        m_boot.channel(NioSocketChannel.class);
        m_boot.option(ChannelOption.SO_KEEPALIVE, true);
        m_boot.option(ChannelOption.TCP_NODELAY, true);
        m_boot.option(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
        m_boot.handler(new NettyChannelInitializer(this, getHelper(), FIXSessionType.INITIATOR));

        getHelper().getSession().getSessionID();
        String host = getHelper().getSettings().getString("SocketConnectHost");
        int port = getHelper().getSettings().getInt("SocketConnectPort");

        m_boot.remoteAddress(new InetSocketAddress(host, port));

        if (!isRunning()) {
            setRunning(true);
            doConnect();
        }
    } catch (Exception e) {
        LOGGER.warn("Exception", e);
        setRunning(false);
    }
}

From source file:com.github.milenkovicm.kafka.connection.AbstractKafkaBroker.java

License:Apache License

public AbstractKafkaBroker(String hostname, int port, String topicName, EventLoopGroup workerGroup,
        ProducerProperties properties) {
    this.hostname = hostname;
    this.port = port;
    this.topicName = topicName;
    this.workerGroup = workerGroup;
    this.properties = properties;

    this.bootstrap = new Bootstrap();
    this.bootstrap.group(this.workerGroup);
    this.bootstrap.channel(NioSocketChannel.class);
    this.bootstrap.option(ChannelOption.TCP_NODELAY, true);
    this.bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK,
            properties.get(ProducerProperties.NETTY_HIGH_WATERMARK));
    this.bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK,
            properties.get(ProducerProperties.NETTY_LOW_WATERMARK));

    this.bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    if (properties.get(ProducerProperties.SO_TIMEOUT) > 0) {
        this.bootstrap.option(ChannelOption.SO_TIMEOUT, 0);
    }//from www.  j  a v a 2s.  c o  m

    if (properties.get(ProducerProperties.SO_RCVBUF) > 0) {
        this.bootstrap.option(ChannelOption.SO_RCVBUF, 0);
    }

    if (properties.get(ProducerProperties.SO_SNDBUF) > 0) {
        this.bootstrap.option(ChannelOption.SO_SNDBUF, 0);
    }

    this.bootstrap.handler(pipeline());
}

From source file:com.github.rmannibucau.featuredmock.http.DefaultFeaturedHttpServer.java

License:Apache License

@Override
public FeaturedHttpServer start() {
    workerGroup = new NioEventLoopGroup(threads, new FeaturedThreadFactory());

    try {//from w  ww . j a v a2s .c o m
        final ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_SNDBUF, 1024)
                .option(ChannelOption.TCP_NODELAY, true).group(workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new FeaturedChannelInitializer(mappers, engine)).bind(host, port)
                .addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(final ChannelFuture future) throws Exception {
                        if (!future.isSuccess()) {
                            LOGGER.severe("Can't start HTTP server");
                        } else {
                            LOGGER.info(String.format("Server started on http://%s:%s", host, port));
                        }
                    }
                }).sync();
    } catch (final InterruptedException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
    }

    return this;
}