Example usage for io.netty.channel ChannelOption SO_BACKLOG

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

Introduction

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

Prototype

ChannelOption SO_BACKLOG

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

Click Source Link

Usage

From source file:com.phei.netty.codec.msgpack.EchoServerV2.java

License:Apache License

public void run() throws Exception {
    // Configure the server.
    EventLoopGroup acceptorGroup = new NioEventLoopGroup();
    EventLoopGroup IOGroup = new NioEventLoopGroup();
    try {// w w  w .j a v  a2s  . co  m
        ServerBootstrap b = new ServerBootstrap();
        b.group(acceptorGroup, IOGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("frameDecoder",
                                new LengthFieldBasedFrameDecoder(65535, 0, 2, 0, 2));
                        ch.pipeline().addLast("msgpack decoder", new MsgpackDecoder());
                        ch.pipeline().addLast("frameEncoder", new LengthFieldPrepender(2));
                        ch.pipeline().addLast("msgpack encoder", new MsgpackEncoder());
                        ch.pipeline().addLast(new EchoServerHandler());
                    }
                });

        // Start the server.
        ChannelFuture f = b.bind(port).sync();

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        acceptorGroup.shutdownGracefully();
        IOGroup.shutdownGracefully();
    }
}

From source file:com.phei.netty.protocol.netty.server.NettyServer.java

License:Apache License

public void bind() throws Exception {
    // ??NIO//  w  w  w . j  av a  2s. c  o m
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100)
            //abstractBootstraphandlerNioServerSocketChannel
            .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws IOException {
                    //?ServerBootstraphandlersocketchannel
                    ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4));
                    ch.pipeline().addLast(new NettyMessageEncoder());
                    ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50));
                    ch.pipeline().addLast(new LoginAuthRespHandler());
                    ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler());
                }
            });

    // ???
    b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync();
    System.out.println("Netty server start ok : " + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT));
}

From source file:com.qc.you.socket.server.Application.java

License:Apache License

@Bean(name = "tcpChannelOptions")
public Map<ChannelOption<?>, Object> tcpChannelOptions() {
    Map<ChannelOption<?>, Object> options = new HashMap<ChannelOption<?>, Object>();
    options.put(ChannelOption.SO_KEEPALIVE, keepAlive);
    options.put(ChannelOption.SO_BACKLOG, backlog);
    return options;
}

From source file:com.robert.NettyProject.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/* w w w . j a v  a 2s.com*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc()));
                        }
                        p.addLast("encode", new StringEncoder());
                        p.addLast("decode", new StringDecoder());
                        // p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoServerHandler());
                    }
                });

        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.rr.echoserver.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    System.out.println("Writing on separate threads: " + EchoServer.THREADED);

    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from   w  w w .ja  va 2  s. c  o m*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } else {
        sslCtx = null;
    }

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc()));
                        }
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoServerHandler());
                    }
                });

        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.rs3e.Main.java

License:Open Source License

/**
 * Initates a new gaming enviroment, here we are going to setup everything
 * needed for the server to be able to bind.
 *//*  w  w  w  . jav a  2 s . c  o m*/
private void initate() {
    bootstrap = new ServerBootstrap();
    bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup());
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.option(ChannelOption.SO_BACKLOG, 100);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.handler(new LoggingHandler(LogLevel.INFO));
    bootstrap.childHandler(new ChannelChildHandler(this));
    try {
        bootstrap.localAddress(Constants.ServerPort).bind().sync();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.sample.netty.socket.server.Server.java

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  www.j  av  a2s  .  c om*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new MessageDecoder(), new ServerHandlerInbound());
                        ch.pipeline().addLast(new MessageEncoder(), new ServerHandlerOutbound());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.sangupta.swift.netty.spdy.SpdyStaticFileServer.java

License:Apache License

public SpdyStaticFileServer(SwiftServer server) {
    if (server.isSpdyEnabled() && !server.isSslEnabled()) {
        throw new IllegalStateException("SPDY can only be enabled along with SSL");
    }/* w  ww  . j  a  va 2 s .c  om*/

    if (server.isSslEnabled()) {
        if (server.isSelfSignedSSL()) {
            SelfSignedCertificate ssc;

            if (server.isSpdyEnabled()) {
                try {
                    ssc = new SelfSignedCertificate();
                    this.sslContext = SslContext.newServerContext(ssc.certificate(), ssc.privateKey(), null,
                            null, Arrays.asList(SelectedProtocol.SPDY_3_1.protocolName(),
                                    SelectedProtocol.HTTP_1_1.protocolName()),
                            0, 0);
                } catch (CertificateException e) {
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                } catch (SSLException e) {
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                }
            } else {
                // basic self signed cert
                try {
                    ssc = new SelfSignedCertificate();
                    this.sslContext = SslContext.newServerContext(SslProvider.JDK, ssc.certificate(),
                            ssc.privateKey());
                } catch (CertificateException e) {
                    e.printStackTrace();
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                } catch (SSLException e) {
                    e.printStackTrace();
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                }
            }
        }
    } else {
        this.sslContext = null;
    }

    this.bossGroup = new NioEventLoopGroup(1);
    this.workerGroup = new NioEventLoopGroup();

    try {
        this.serverBootstrap = new ServerBootstrap();
        if (server.isSpdyEnabled()) {
            this.serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024);
        }

        SpdyStaticFileServerHandler fileServerHandler = new SpdyStaticFileServerHandler(server);

        this.serverBootstrap.group(this.bossGroup, this.workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new SpdyStaticFileServerInitializer(this.sslContext, fileServerHandler));

        if (AssertUtils.isNotEmpty(server.getServerName())) {
            this.channel = this.serverBootstrap.bind(server.getServerName(), server.getListenPort()).sync()
                    .channel();
        } else {
            this.channel = this.serverBootstrap.bind(server.getListenPort()).sync().channel();
        }

        System.out.println("Listening on port " + server.getListenPort());

        this.channel.closeFuture().sync();
    } catch (InterruptedException e) {
        // TODO: think what we can do with this
    } finally {
        this.shutdownGracefully();
    }
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.udt.UdtTransportProvider.java

License:Open Source License

public void doInit() throws InterruptedException {

    this.port = this.service.getServiceConfiguration().getPort();

    final ThreadFactory acceptFactory = new NioThreadFactory("UdtAccept");

    final ThreadFactory connectFactory = new NioThreadFactory("UdtConnect");

    bossGroup = new NioEventLoopGroup(10, acceptFactory, NioUdtProvider.MESSAGE_PROVIDER);

    workerGroup = new NioEventLoopGroup(10, connectFactory, NioUdtProvider.MESSAGE_PROVIDER);

    msChannelInitializer = new UdtChannelInitializer(this.service);

    bootstrap = new ServerBootstrap();

    bootstrap.group(bossGroup, workerGroup).channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR)
            .option(ChannelOption.SO_BACKLOG, 1000).option(ChannelOption.SO_REUSEADDR, true)
            .childHandler(msChannelInitializer);

    logger.info("Kinetic udt service binding on port =" + port);

    channelFuture = bootstrap.bind(port).sync();

    logger.info("Kinetic udt service bound on port =" + port);
}

From source file:com.seed.nettyechoserver.EchoServer.java

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(1);
    try {// w w  w . ja v a2  s  . c  om
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoServerHandler());
                    }
                });

        ChannelFuture f = b.bind(PORT).sync();
        f.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}