Example usage for io.netty.channel ChannelOption SO_KEEPALIVE

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

Introduction

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

Prototype

ChannelOption SO_KEEPALIVE

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

Click Source Link

Usage

From source file:com.codnos.dbgp.internal.impl.DBGpIdeImpl.java

License:Apache License

@Override
public void startListening() {
    registerInitHandler();//from   w w  w .  j  a v  a2 s  . c  o  m
    ServerBootstrap b = new ServerBootstrap();
    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(outboundConnectionHandler, new DBGpCommandEncoder(),
                            new DBGpResponseDecoder(), new DBGpResponseHandler(eventsHandler));
                }
            }).option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_REUSEADDR, true)
            .childOption(ChannelOption.SO_KEEPALIVE, true);
    bindPort(b);
}

From source file:com.comphenix.protocol.compat.netty.independent.NettySocketAdapter.java

License:Open Source License

@Override
public boolean getKeepAlive() throws SocketException {
    return ch.config().getOption(ChannelOption.SO_KEEPALIVE);
}

From source file:com.comphenix.protocol.compat.netty.independent.NettySocketAdapter.java

License:Open Source License

@Override
public void setKeepAlive(boolean on) throws SocketException {
    ch.config().setOption(ChannelOption.SO_KEEPALIVE, on);
}

From source file:com.corundumstudio.socketio.SocketIOServer.java

License:Apache License

protected void applyConnectionOptions(ServerBootstrap bootstrap) {
    SocketConfig config = configCopy.getSocketConfig();
    bootstrap.childOption(ChannelOption.TCP_NODELAY, config.isTcpNoDelay());
    if (config.getTcpSendBufferSize() != -1) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize());
    }//w ww  . ja  v a 2 s.c o m
    if (config.getTcpReceiveBufferSize() != -1) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, config.getTcpReceiveBufferSize());
        bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(config.getTcpReceiveBufferSize()));
    }
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive());

    bootstrap.option(ChannelOption.SO_LINGER, config.getSoLinger());
    bootstrap.option(ChannelOption.SO_REUSEADDR, config.isReuseAddress());
    bootstrap.option(ChannelOption.SO_BACKLOG, config.getAcceptBackLog());
}

From source file:com.dingwang.netty.client.DiscardClient.java

License:Open Source License

public void run() {
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    //BootStrapServerBootstrap,???channel?channel
    Bootstrap b = new Bootstrap();

    //?EventLoopGroup?bossworkder
    //??boss//from  ww  w  . j  av a2  s.  c  om
    b.group(workerGroup);

    //NioServerSocketChannelNioSocketChannel,channel
    b.channel(NioSocketChannel.class);

    //??ServerBootstrap?childOption()SocketChannelchannel
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new TimeDecoder(), new TimeClientHandler());
        }
    });

    ChannelFuture f;
    try {
        //connect()bind()
        f = b.connect(host, port).sync();
        f.channel().writeAndFlush("dddddd");
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
    }
}

From source file:com.dingwang.netty.client.PersonClient.java

License:Open Source License

public void run() {
    EventLoopGroup workerGroup = new NioEventLoopGroup(10);

    //BootStrapServerBootstrap,???channel?channel
    Bootstrap b = new Bootstrap();

    //?EventLoopGroup?bossworkder
    //??boss/*from   w  w w . j  av  a2s .  c  o  m*/
    b.group(workerGroup);

    //NioServerSocketChannelNioSocketChannel,channel
    b.channel(NioSocketChannel.class);

    //??ServerBootstrap?childOption()SocketChannelchannel
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new PersonEncoder(), new PersonDecoder(), new PersonInHandler());
        }
    });

    ChannelFuture f;
    try {
        //connect()bind()
        f = b.connect(host, port).sync();
        ChannelPool.setChannel(f.channel());
        //            Person p = new Person();
        //            p.setAge(10);
        //            p.setName("dw");
        //            f.channel().writeAndFlush(p);

        //            Thread.currentThread().sleep(1000);
        //
        //            p.setName("test");
        //            f.channel().writeAndFlush(p);
        //            f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        //            workerGroup.shutdownGracefully();
    }
}

From source file:com.dingwang.netty.server.DiscardServer.java

License:Open Source License

public void run() {
    //NioEventLoopGroup ??I/O?Netty????EventLoopGroup
    //????????2NioEventLoopGroup
    //???boss?????worker???
    //boss?worker???
    //Channels??EventLoopGroup???
    EventLoopGroup bossGroup = new NioEventLoopGroup(5);
    EventLoopGroup workerGroup = new NioEventLoopGroup(20);

    //ServerBootstrap ?NIO????Channel??
    //????//w w w . j a v a  2  s  . c o m
    ServerBootstrap b = new ServerBootstrap();

    //NioServerSocketChannel?Channel?
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            //?????ChannelChannelInitializer?
            //?Channel?DiscardServerHandle??
            //ChannelChannelPipeline???????
            //?pipline??????
            .childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new TimeEncoder(), new DiscardServerHandler());

                }
            })
            //????TCP/IP??socket?
            //tcpNoDelaykeepAlive?ChannelOptionChannelConfig??
            //ChannelOptions
            .option(ChannelOption.SO_BACKLOG, 128)
            //option()childOption()?option()??NioServerSocketChannel??
            //childOption()???ServerChannel?NioServerSocketChannel
            .childOption(ChannelOption.SO_KEEPALIVE, true);

    try {
        //?????8080?
        //?bind()(???)
        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}

From source file:com.dingwang.netty.server.PersonServer.java

License:Open Source License

public void run() {
    //NioEventLoopGroup ??I/O?Netty????EventLoopGroup
    //????????2NioEventLoopGroup
    //???boss?????worker???
    //boss?worker???
    //Channels??EventLoopGroup???
    EventLoopGroup bossGroup = new NioEventLoopGroup(5);
    EventLoopGroup workerGroup = new NioEventLoopGroup(20);

    //ServerBootstrap ?NIO????Channel??
    //????// w  w w .j a  v  a 2 s . co  m
    ServerBootstrap b = new ServerBootstrap();

    //NioServerSocketChannel?Channel?
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            //?????ChannelChannelInitializer?
            //?Channel?DiscardServerHandle??
            //ChannelChannelPipeline???????
            //?pipline??????
            .childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new PersonDecoder(), new PersonEncoder(), new PersonOutHandler());

                }
            })
            //????TCP/IP??socket?
            //tcpNoDelaykeepAlive?ChannelOptionChannelConfig??
            //ChannelOptions
            .option(ChannelOption.SO_BACKLOG, 128)
            //option()childOption()?option()??NioServerSocketChannel??
            //childOption()???ServerChannel?NioServerSocketChannel
            .childOption(ChannelOption.SO_KEEPALIVE, true);

    try {
        //?????8080?
        //?bind()(???)
        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}

From source file:com.dingwang.rpc.client.RpcClient.java

License:Open Source License

public RpcResponse send(RpcRequest request) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*  w  w  w . ja va  2s  .c  o m*/
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel channel) throws Exception {
                channel.pipeline().addLast(new RpcEncoder(RpcRequest.class)) //  RPC ???
                        .addLast(new RpcDecoder(RpcResponse.class)) //  RPC ????
                        .addLast(RpcClient.this); //  RpcClient ?? RPC 
            }
        }).option(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture future = bootstrap.connect(host, port).sync();
        future.channel().writeAndFlush(request).sync();

        synchronized (obj) {
            obj.wait(); // ?
        }

        if (response != null) {
            future.channel().closeFuture().sync();
        }
        return response;
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.dingwang.rpc.server.RpcServer.java

License:Open Source License

@Override
public void afterPropertiesSet() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  w ww.  j  a va  2  s  .  co m*/
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel channel) throws Exception {
                        channel.pipeline().addLast(new RpcDecoder(RpcRequest.class)) //  RPC ??
                                .addLast(new RpcEncoder(RpcResponse.class)) //  RPC ???
                                //                                    .addLast(new ProviderProxy()); // ? RPC 
                                .addLast(new RpcHandler(handlerMap));
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        String[] array = serverAddress.split(":");
        String host = array[0];
        int port = Integer.parseInt(array[1]);

        ChannelFuture future = bootstrap.bind(host, port).sync();
        LOGGER.debug("server started on port {}", port);

        if (serviceRegistry != null) {
            serviceRegistry.register(serverAddress); // ??
        }

        future.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}