Example usage for io.netty.buffer PooledByteBufAllocator DEFAULT

List of usage examples for io.netty.buffer PooledByteBufAllocator DEFAULT

Introduction

In this page you can find the example usage for io.netty.buffer PooledByteBufAllocator DEFAULT.

Prototype

PooledByteBufAllocator DEFAULT

To view the source code for io.netty.buffer PooledByteBufAllocator DEFAULT.

Click Source Link

Usage

From source file:org.starnub.utilities.connectivity.client.TCPClient.java

License:Open Source License

public ChannelFuture connect(String ipAddress, int port, ChannelInitializer<SocketChannel> channelInitializer) {
    Bootstrap b = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.SO_KEEPALIVE, true).handler(channelInitializer);
    return b.connect(ipAddress, port);
}

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.wenxueliu.netty.client.ClientTest.java

License:Apache License

/**
 * Accepts incoming connections.// w  ww . j  a  v  a 2 s. c o  m
 */
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);
    });//  w w  w  .j  a  va2s . co  m
    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 av a  2  s . com

    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.ServerRegistry.java

License:Apache License

private static Registry buildBaseRegistry(RatpackServer ratpackServer, ExecController execController,
        ServerConfig serverConfig) {// w  w w  .ja va  2 s.  c o m
    ErrorHandler errorHandler = serverConfig.isDevelopment() ? new DefaultDevelopmentErrorHandler()
            : new DefaultProductionErrorHandler();

    RegistryBuilder baseRegistryBuilder;
    try {
        baseRegistryBuilder = Registries.registry().add(ServerConfig.class, serverConfig)
                .add(CompressionConfig.class, CompressionConfig.of().build())
                .add(ByteBufAllocator.class, PooledByteBufAllocator.DEFAULT)
                .add(ExecController.class, execController).add(ExecControl.class, execController.getControl())
                .add(MimeTypes.class, new ActivationBackedMimeTypes())
                .add(PublicAddress.class,
                        new DefaultPublicAddress(serverConfig.getPublicAddress(),
                                serverConfig.getSSLContext() == null ? HTTP_SCHEME : HTTPS_SCHEME))
                .add(Redirector.class, new DefaultRedirector()).add(ClientErrorHandler.class, errorHandler)
                .add(ServerErrorHandler.class, errorHandler).with(new DefaultFileRenderer().register())
                .with(new PromiseRenderer().register()).with(new PublisherRenderer().register())
                .with(new RenderableRenderer().register()).with(new CharSequenceRenderer().register())
                .add(FormParser.class, FormParser.multiPart()).add(FormParser.class, FormParser.urlEncoded())
                .add(FormNoOptParser.class, FormNoOptParser.multiPart())
                .add(FormNoOptParser.class, FormNoOptParser.urlEncoded())
                .add(RatpackServer.class, ratpackServer)
                // TODO remove Stopper, and just use RatpackServer instead (will need to update perf and gradle tests)
                .add(Stopper.class, () -> uncheck(() -> {
                    ratpackServer.stop();
                    return null;
                }))
                .add(HttpClient.class,
                        HttpClient.httpClient(execController, PooledByteBufAllocator.DEFAULT,
                                serverConfig.getMaxContentLength()))
                .add(ServerSentEventStreamClient.class, ServerSentEventStreamClient
                        .sseStreamClient(execController, PooledByteBufAllocator.DEFAULT));
    } catch (Exception e) {
        // Uncheck because it really shouldn't happen
        throw uncheck(e);
    }

    if (serverConfig.isHasBaseDir()) {
        baseRegistryBuilder.add(FileSystemBinding.class, serverConfig.getBaseDir());
    }

    return baseRegistryBuilder.build();
}

From source file:ratpack.stream.bytebuf.ByteBufStreams.java

License:Apache License

/**
 * Buffers and composes byte bufs together into composites before emitting.
 * <p>//from   w w w.  j  a va2 s . c  om
 * Calls {@link #buffer(Publisher, long, int, ByteBufAllocator)} with {@link PooledByteBufAllocator#DEFAULT}
 *
 * @param publisher the publisher of byte bufs to buffer
 * @param sizeWatermark the watermark size for a composite
 * @param maxNum the maximum number of composite components
 * @return a byte buf composing publisher
 */
public static TransformablePublisher<CompositeByteBuf> buffer(Publisher<? extends ByteBuf> publisher,
        long sizeWatermark, int maxNum) {
    return buffer(publisher, sizeWatermark, maxNum, PooledByteBufAllocator.DEFAULT);
}

From source file:ratpack.stream.bytebuf.ByteBufStreams.java

License:Apache License

/**
 * Reduces the stream to a single composite byte buf.
 * <p>//from w w w  .j  a va2s.c  om
 * Calls {@link #compose(Publisher, ByteBufAllocator)} with {@link PooledByteBufAllocator#DEFAULT}.
 *
 * @param publisher the stream
 * @return the reduced composite buffer
 */
public static Promise<CompositeByteBuf> compose(Publisher<? extends ByteBuf> publisher) {
    return compose(publisher, PooledByteBufAllocator.DEFAULT);
}

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   w w  w . j a  v 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 {//  w  ww  . ja  v a 2 s .  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);
}