Example usage for io.netty.channel ChannelOption ALLOCATOR

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

Introduction

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

Prototype

ChannelOption ALLOCATOR

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

Click Source Link

Usage

From source file:org.starnub.utilities.connectivity.server.TCPServer.java

License:Open Source License

public Channel start(int port, ChannelInitializer<SocketChannel> channelInitializer) {
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(connectionBossGroup, connectionWorkerGroup).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.SO_KEEPALIVE, true).childHandler(channelInitializer);
    return sb.bind(port).channel();

}

From source file:org.vertx.java.core.dns.impl.DefaultDnsClient.java

License:Open Source License

public DefaultDnsClient(VertxInternal vertx, InetSocketAddress... dnsServers) {
    if (dnsServers == null || dnsServers.length == 0) {
        throw new IllegalArgumentException("Need at least one default DNS Server");
    }/* www  .j  a va2  s  .  c  o m*/

    // use LinkedList as we will traverse it all the time
    this.dnsServers = new LinkedList<>(Arrays.asList(dnsServers));
    actualCtx = vertx.getOrCreateContext();
    this.vertx = vertx;
    bootstrap = new Bootstrap();
    bootstrap.group(actualCtx.getEventLoop());
    bootstrap.channel(NioDatagramChannel.class);
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.handler(new ChannelInitializer<DatagramChannel>() {
        @Override
        protected void initChannel(DatagramChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new DnsQueryEncoder());
            pipeline.addLast(new DnsResponseDecoder());
        }
    });
}

From source file:org.vertx.java.core.net.impl.TCPSSLHelper.java

License:Open Source License

public void applyConnectionOptions(ServerBootstrap bootstrap) {
    bootstrap.childOption(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpSendBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }/*from   w w w .j ava2  s  .co  m*/
    if (tcpReceiveBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
        bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(tcpReceiveBufferSize));
    }

    bootstrap.option(ChannelOption.SO_LINGER, soLinger);
    if (trafficClass != -1) {
        bootstrap.childOption(ChannelOption.IP_TOS, trafficClass);
    }
    bootstrap.childOption(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);

    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
    bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
    bootstrap.option(ChannelOption.SO_BACKLOG, acceptBackLog);
}

From source file:org.vertx.java.core.net.impl.TCPSSLHelper.java

License:Open Source License

public void applyConnectionOptions(Bootstrap bootstrap) {
    bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpSendBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }/*from   ww  w  .ja va  2 s  .  c o m*/
    if (tcpReceiveBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(tcpReceiveBufferSize));
    }
    bootstrap.option(ChannelOption.SO_LINGER, soLinger);
    if (trafficClass != -1) {
        bootstrap.option(ChannelOption.IP_TOS, trafficClass);
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
}

From source file:org.wenxueliu.netty.client.ClientTest.java

License:Apache License

/**
 * Accepts incoming connections./*from   ww  w  .j ava2 s  . c om*/
 */
private void startAcceptingConnections() throws InterruptedException {
    ServerBootstrap b = new ServerBootstrap();

    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new CommunicationChannelInitializer());
    b.option(ChannelOption.SO_BACKLOG, 128);
    b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
    b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
    b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.childOption(ChannelOption.SO_KEEPALIVE, true);
    b.bind(80).sync();
}

From source file:ratpack.server.internal.DefaultRatpackServer.java

License:Apache License

protected Channel buildChannel(final ServerConfig serverConfig, final ChannelHandler handlerAdapter)
        throws InterruptedException {

    SslContext sslContext = serverConfig.getNettySslContext();
    this.useSsl = sslContext != null;

    ServerBootstrap serverBootstrap = new ServerBootstrap();

    serverConfig.getConnectTimeoutMillis().ifPresent(i -> {
        serverBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, i);
        serverBootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, i);
    });/*www.j a v a2s.c om*/
    serverConfig.getMaxMessagesPerRead().ifPresent(i -> {
        FixedRecvByteBufAllocator allocator = new FixedRecvByteBufAllocator(i);
        serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, allocator);
        serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, allocator);
    });
    serverConfig.getReceiveBufferSize().ifPresent(i -> {
        serverBootstrap.option(ChannelOption.SO_RCVBUF, i);
        serverBootstrap.childOption(ChannelOption.SO_RCVBUF, i);
    });
    serverConfig.getWriteSpinCount().ifPresent(i -> {
        serverBootstrap.option(ChannelOption.WRITE_SPIN_COUNT, i);
        serverBootstrap.childOption(ChannelOption.WRITE_SPIN_COUNT, i);
    });
    serverConfig.getConnectQueueSize().ifPresent(i -> serverBootstrap.option(ChannelOption.SO_BACKLOG, i));

    return serverBootstrap.group(execController.getEventLoopGroup())
            .channel(ChannelImplDetector.getServerSocketChannelImpl())
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();

                    new ConnectionIdleTimeout(pipeline, serverConfig.getIdleTimeout());

                    if (sslContext != null) {
                        SSLEngine sslEngine = sslContext.newEngine(PooledByteBufAllocator.DEFAULT);
                        pipeline.addLast("ssl", new SslHandler(sslEngine));
                    }

                    pipeline.addLast("decoder", new HttpRequestDecoder(serverConfig.getMaxInitialLineLength(),
                            serverConfig.getMaxHeaderSize(), serverConfig.getMaxChunkSize(), false));
                    pipeline.addLast("encoder", new HttpResponseEncoder());
                    pipeline.addLast("deflater", new IgnorableHttpContentCompressor());
                    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
                    pipeline.addLast("adapter", handlerAdapter);

                    ch.config().setAutoRead(false);
                }
            }).bind(buildSocketAddress(serverConfig)).sync().channel();
}

From source file:ratpack.server.internal.NettyRatpackServer.java

License:Apache License

protected Channel buildChannel(final ServerConfig serverConfig, final ChannelHandler handlerAdapter)
        throws InterruptedException {

    SSLContext sslContext = serverConfig.getSSLContext();
    if (sslContext != null) {
        this.sslEngine = sslContext.createSSLEngine();
        sslEngine.setUseClientMode(false);
    }//from  w w  w .  j a v a2 s  .  c o m

    return new ServerBootstrap().group(execController.getEventLoopGroup())
            .channel(ChannelImplDetector.getServerSocketChannelImpl())
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    if (sslContext != null) {
                        pipeline.addLast("ssl", new SslHandler(sslEngine));
                    }

                    pipeline.addLast("decoder", new HttpRequestDecoder(4096, 8192, 8192, false));
                    pipeline.addLast("aggregator",
                            new HttpObjectAggregator(serverConfig.getMaxContentLength()));
                    pipeline.addLast("encoder", new HttpResponseEncoder());
                    pipeline.addLast("deflater", new SmartHttpContentCompressor());
                    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
                    pipeline.addLast("adapter", handlerAdapter);
                }
            }).bind(buildSocketAddress(serverConfig)).sync().channel();
}

From source file:ratpack.server.internal.NettyRatpackService.java

License:Apache License

@Override
protected void startUp() throws Exception {
    Stopper stopper = new Stopper() {
        @Override/*w w  w.  java  2s .  c om*/
        public void stop() {
            try {
                NettyRatpackService.this.stop();
            } catch (Exception e) {
                throw uncheck(e);
            }
        }
    };

    ServerBootstrap bootstrap = new ServerBootstrap();
    group = new NioEventLoopGroup(launchConfig.getMainThreads(),
            new DefaultThreadFactory("ratpack-group", Thread.MAX_PRIORITY));

    ChannelInitializer<SocketChannel> channelInitializer = channelInitializerTransformer.transform(stopper);

    bootstrap.group(group).channel(NioServerSocketChannel.class).childHandler(channelInitializer)
            .childOption(ChannelOption.ALLOCATOR, launchConfig.getBufferAllocator())
            .childOption(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.SO_BACKLOG, 1024)
            .option(ChannelOption.ALLOCATOR, launchConfig.getBufferAllocator());

    try {
        channel = bootstrap.bind(buildSocketAddress()).sync().channel();
    } catch (Exception e) {
        partialShutdown();
        throw e;
    }
    boundAddress = (InetSocketAddress) channel.localAddress();

    if (logger.isLoggable(Level.INFO)) {
        logger.info(String.format("Ratpack started for http://%s:%s", getBindHost(), getBindPort()));
    }
}

From source file:reactor.io.net.impl.netty.tcp.NettyTcpClient.java

License:Apache License

/**
 * Creates a new NettyTcpClient that will use the given {@code env} for configuration and the given {@code
 * reactor} to//from   www  . j  av a 2 s  .c o m
 * send events. The number of IO threads used by the client is configured by the environment's {@code
 * reactor.tcp.ioThreadCount} property. In its absence the number of IO threads will be equal to the {@link
 * Environment#PROCESSORS number of available processors}. </p> The client will connect to the given {@code
 * connectAddress}, configuring its socket using the given {@code opts}. The given {@code codec} will be used for
 * encoding and decoding of data.
 *
 * @param env            The configuration environment
 * @param dispatcher     The dispatcher used to send events
 * @param connectAddress The address the client will connect to
 * @param options        The configuration options for the client's socket
 * @param sslOptions     The SSL configuration options for the client's socket
 * @param codec          The codec used to encode and decode data
 */
public NettyTcpClient(Environment env, Dispatcher dispatcher, InetSocketAddress connectAddress,
        final ClientSocketOptions options, final SslOptions sslOptions, Codec<Buffer, IN, OUT> codec) {
    super(env, dispatcher, connectAddress, options, sslOptions, codec);
    this.connectAddress = connectAddress;

    if (options instanceof NettyClientSocketOptions) {
        this.nettyOptions = (NettyClientSocketOptions) options;
    } else {
        this.nettyOptions = null;

    }
    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        int ioThreadCount = env != null
                ? env.getIntProperty("reactor.tcp.ioThreadCount", Environment.PROCESSORS)
                : Environment.PROCESSORS;
        this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-tcp-io"));
    }

    this.bootstrap = new Bootstrap().group(ioGroup).channel(NioSocketChannel.class)
            .option(ChannelOption.SO_RCVBUF, options.rcvbuf()).option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_KEEPALIVE, options.keepAlive())
            .option(ChannelOption.SO_LINGER, options.linger())
            .option(ChannelOption.TCP_NODELAY, options.tcpNoDelay())
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.AUTO_READ, sslOptions != null)
    //.remoteAddress(this.connectAddress)
    ;

    this.connectionSupplier = new Supplier<ChannelFuture>() {
        @Override
        public ChannelFuture get() {
            if (started.get()) {
                return bootstrap.connect(getConnectAddress());
            } else {
                return null;
            }
        }
    };
}

From source file:reactor.io.net.impl.netty.tcp.NettyTcpServer.java

License:Apache License

protected NettyTcpServer(Environment env, Dispatcher dispatcher, InetSocketAddress listenAddress,
        final ServerSocketOptions options, final SslOptions sslOptions, Codec<Buffer, IN, OUT> codec) {
    super(env, dispatcher, listenAddress, options, sslOptions, codec);

    if (options instanceof NettyServerSocketOptions) {
        this.nettyOptions = (NettyServerSocketOptions) options;
    } else {/*from  w w  w. ja  v  a  2s  .  co  m*/
        this.nettyOptions = null;
    }

    int selectThreadCount = getDefaultEnvironment().getIntProperty("reactor.tcp.selectThreadCount",
            Environment.PROCESSORS / 2);
    int ioThreadCount = getDefaultEnvironment().getIntProperty("reactor.tcp.ioThreadCount",
            Environment.PROCESSORS);

    this.selectorGroup = new NioEventLoopGroup(selectThreadCount,
            new NamedDaemonThreadFactory("reactor-tcp-select"));

    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-tcp-io"));
    }

    this.bootstrap = new ServerBootstrap().group(selectorGroup, ioGroup).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, options.backlog())
            .option(ChannelOption.SO_RCVBUF, options.rcvbuf()).option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_REUSEADDR, options.reuseAddr())
            .localAddress((null == listenAddress ? new InetSocketAddress(0) : listenAddress))
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.AUTO_READ, sslOptions != null);
}