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:waazdoh.cp2p.network.TCPListener.java

License:Open Source License

public void start() {
    if (!isClosed()) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override//w  w w.  j a  va 2  s  .c  o  m
                    protected void initChannel(SocketChannel ch) throws Exception {
                        if (!isClosed()) {
                            log.info("initChannel " + ch);
                            ChannelPipeline pipe = ch.pipeline();
                            pipe.addLast("zipencoder", new JZlibEncoder());
                            pipe.addLast("zipdecoder", new JZlibDecoder());
                            pipe.addLast("messageencoder", new MessageEncoder());
                            pipe.addLast("messagedecoder", new MessageDecoder());
                            pipe.addLast("server", new MServerHandler());
                            //
                            List<MMessage> mlist = new LinkedList<MMessage>();
                            mlist.add(messenger.getMessage("hello"));
                            ch.writeAndFlush(mlist);
                        } else {
                            log.info("InitChannel on closed listener. Closing channel.");
                            ch.close();
                        }
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        //
        port = preferences.getInteger(WPreferences.NETWORK_SERVER_PORT, DEFAULT_PORT);
        //
        try {
            while (!isClosed() && bind == null && port < 65000) {
                startListening(bootstrap);
                if (bind == null) {
                    port++;
                }
            }
        } catch (InterruptedException e) {
            log.error(e);
        }

        log.info("listening " + port + " messager:" + this.messenger);
        //
        MMessage b = this.messenger.getMessage("newnode");
        b.addAttribute("port", port);
        //
        this.messenger.broadcastMessage(b);
    }
}

From source file:xyz.kvantum.server.implementation.HTTPThread.java

License:Apache License

HTTPThread(final ServerSocketFactory serverSocketFactory, final NioClassResolver classResolver)
        throws KvantumInitializationException {
    super("http");
    this.setPriority(Thread.MAX_PRIORITY);

    this.workerGroup = classResolver.getClassProvider()
            .getEventLoopGroup(CoreConfig.Pools.httpWorkerGroupThreads);
    this.bossGroup = classResolver.getClassProvider().getEventLoopGroup(CoreConfig.Pools.httpBossGroupThreads);

    if (!serverSocketFactory.createServerSocket()) {
        throw new KvantumInitializationException("Failed to start server...");
    }/* www .ja  v  a2  s .co m*/

    this.port = serverSocketFactory.getServerSocketPort();

    this.serverBootstrap = new ServerBootstrap();
    this.serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024);
    serverBootstrap.group(bossGroup, workerGroup)
            .channel(classResolver.getClassProvider().getServerSocketChannelClass())
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(final SocketChannel ch) {
                    ch.pipeline().addLast(new KvantumReadTimeoutHandler()).addLast(new ByteArrayEncoder())
                            .addLast(new KvantumServerHandler(ProtocolType.HTTP));
                }
            });
}