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:net.mcsproject.master.network.DaemonServer.java

License:Open Source License

public DaemonServer(int port) {
    this.thread = new Thread(() -> {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {//from w w w . j  a  va 2s.  co  m
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    socketChannel.pipeline().addLast(new PacketDecoder()).addLast(new PacketEncoder())
                            .addLast(new PacketMessageHandler());
                }
            });
            bootstrap.option(ChannelOption.SO_BACKLOG, 50);
            bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture future = bootstrap.bind(port).sync();
            log.info("Server started!");
            future.channel().closeFuture().sync();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    });

    thread.setName("DaemonServer Thread");
    thread.start();
}

From source file:net.oebs.jalos.netty.HttpServer.java

License:Open Source License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  www  .j  av  a2  s  .c  o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 8192);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpInitializer());
        Channel ch = b.bind(host, port).sync().channel();
        log.info("Listening at http://{}:{}/", host, port);
        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:net.openhft.performance.tests.third.party.frameworks.netty.NettyEchoServer.java

License:Apache License

public void run() throws InterruptedException {
    @NotNull//ww w . jav a 2s. com
    EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
    @NotNull
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        @NotNull
        ServerBootstrap b = new ServerBootstrap(); // (2)
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3)
                .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                    @Override
                    public void initChannel(@NotNull SocketChannel ch) {
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                            // echo server
                            @Override
                            public void channelRead(@NotNull ChannelHandlerContext ctx, Object msg) { // (2)
                                ctx.write(msg); // (1)
                                ctx.flush(); // (2)
                            }

                            @Override
                            public void exceptionCaught(@NotNull ChannelHandlerContext ctx,
                                    @NotNull Throwable cause) { // (4)
                                // Close the connection when an exception is raised.
                                cause.printStackTrace();
                                ctx.close();
                            }
                        });
                    }
                }).option(ChannelOption.SO_BACKLOG, 128) // (5)
                .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)

        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind(port).sync(); // (7)

        // 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.
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:net.petercashel.nettyCore.server.serverCore.java

License:Apache License

/**
 * Initializes the Server listerning socket
 *
 * @param port/*from   w  w  w .  j av a  2s  .co  m*/
 *            - Int port to bind to
 * @throws Exception
 */
public static void initializeServer(int port) throws Exception {
    clientConnectionMap = new HashMap<SocketAddress, ChannelUserHolder>();
    AuthTmpUserMap = new HashMap<String, String>();
    PacketRegistry.setupRegistry();
    PacketRegistry.Side = side;
    if (UseSSL)
        SSLContextProvider.SetupSSL();

    bossGroup = new NioEventLoopGroup(); // (1)
    workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap(); // (2)
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3)
                .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast("readTimeoutHandler", new ReadTimeoutHandler(300));
                        if (UseSSL && !SSLContextProvider.selfSigned)
                            p.addLast("ssl", getSSLHandler());
                        if (UseSSL && SSLContextProvider.selfSigned)
                            p.addLast("ssl", SSLContextProvider.getSelfServer().newHandler(ch.alloc()));
                        p.addLast("InboundOutboundServerHandler", new ServerConnectionHandler());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128) // (5)
                .childOption(ChannelOption.TCP_NODELAY, true); // (6)

        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind("0.0.0.0", port).sync(); // (7)
        System.out.println("Server Core Initalised!");
        // 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.
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}

From source file:net.qing.sms.simulator.NettySmsSimulatorServer.java

License:Apache License

protected void applyConnectionOptions(ServerBootstrap bootstrap) {
    SocketConfig config = configuration.getSocketConfig();
    bootstrap.childOption(ChannelOption.TCP_NODELAY, config.isTcpNoDelay());
    if (config.getTcpSendBufferSize() != -1) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize());
    }// w ww .jav a  2  s.c om
    if (config.getTcpReceiveBufferSize() != -1) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, config.getTcpReceiveBufferSize());
    }
    // bootstrap.option(ChannelOption.ALLOCATOR,
    // PooledByteBufAllocator.DEFAULT);
    bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    // bootstrap.childOption(ChannelOption.ALLOCATOR,
    // PooledByteBufAllocator.DEFAULT);
    bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    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:net.smert.frameworkgl.Network.java

License:Apache License

private void createServer(int port, Supplier<ChannelHandler> channelHandlerSupplier) {

    // Are we already running?
    if (serverRunning) {
        return;/* w w w  .j  a  v  a2s.c  om*/
    }

    serverPort = port;

    // Create event loops
    serverAcceptGroup = new NioEventLoopGroup(1);
    serverWorkerGroup = new NioEventLoopGroup();

    // Create channel initializer
    ChannelInitializer<SocketChannel> channelInit = new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            if (debug) {
                p.addLast(new LoggingHandler(logLevel));
            }
            p.addLast(channelHandlerSupplier.get());
        }

    };

    // Bootstrap the server
    server = new ServerBootstrap();
    if (debug) {
        server.handler(new LoggingHandler(logLevel));
    }
    server.group(serverAcceptGroup, serverWorkerGroup).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, backlog).childHandler(channelInit)
            .childOption(ChannelOption.SO_KEEPALIVE, keepAlive)
            .childOption(ChannelOption.TCP_NODELAY, tcpNoDelay);

    // Start listening on the port
    server.bind(port);

    // The server is now running
    serverRunning = true;
}

From source file:netty.codec.marshalling.SubReqServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO//from w w w  . j ava2  s  .  co  m
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024).localAddress(new InetSocketAddress(port))
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) {
                        ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
                        ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
                        ch.pipeline().addLast("serverHandler", new SubReqServerHandler());
                    }
                });

        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:netty.codec.protobuf.SubReqServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO// ww  w .j  a v  a2s.c  o m
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    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) {
                        // ch.pipeline().addLast(
                        // new ProtobufVarint32FrameDecoder());
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(
                                new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance()));
                        pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
                        pipeline.addLast(new ProtobufEncoder());
                        pipeline.addLast(new SubReqServerHandler());
                    }
                });

        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:netty.demo1.DiscardServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from   w  ww .ja  va 2s.  c o  m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new DiscardServerHandler());
                    }
                }).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:netty.echo.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//from  w w  w .  j  a  v  a2s.  co  m
        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(new LoggingHandler(LogLevel.INFO));
                        //?handler
                        p.addLast(new EchoServerHandler());
                    }
                });

        // Start the server.
        //bindNioServerSocketChannelServerSocket
        ChannelFuture f = b.bind(PORT).sync();
        //ChannelFuture.result, ?resultSignal SUCCESS = Signal.valueOf(DefaultPromise.class, "SUCCESS");,
        // sync()?,?

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