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:com.couchbase.client.core.endpoint.AbstractEndpoint.java

License:Apache License

/**
 * Create a new {@link AbstractEndpoint}.
 *
 * @param hostname the hostname/ipaddr of the remote channel.
 * @param bucket the name of the bucket.
 * @param password the password of the bucket.
 * @param port the port of the remote channel.
 * @param environment the environment of the core.
 * @param responseBuffer the response buffer for passing responses up the stack.
 *///from w ww .  j ava  2 s  .com
protected AbstractEndpoint(final String hostname, final String bucket, final String password, final int port,
        final CoreEnvironment environment, final RingBuffer<ResponseEvent> responseBuffer,
        boolean isTransient) {
    super(LifecycleState.DISCONNECTED);
    this.bucket = bucket;
    this.password = password;
    this.responseBuffer = responseBuffer;
    this.env = environment;
    this.isTransient = isTransient;
    if (environment.sslEnabled()) {
        this.sslEngineFactory = new SSLEngineFactory(environment);
    }

    Class<? extends Channel> channelClass = NioSocketChannel.class;
    if (environment.ioPool() instanceof EpollEventLoopGroup) {
        channelClass = EpollSocketChannel.class;
    } else if (environment.ioPool() instanceof OioEventLoopGroup) {
        channelClass = OioSocketChannel.class;
    }

    ByteBufAllocator allocator = env.bufferPoolingEnabled() ? PooledByteBufAllocator.DEFAULT
            : UnpooledByteBufAllocator.DEFAULT;

    boolean tcpNodelay = environment().tcpNodelayEnabled();
    bootstrap = new BootstrapAdapter(
            new Bootstrap().remoteAddress(hostname, port).group(environment.ioPool()).channel(channelClass)
                    .option(ChannelOption.ALLOCATOR, allocator).option(ChannelOption.TCP_NODELAY, tcpNodelay)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, env.socketConnectTimeout())
                    .handler(new ChannelInitializer<Channel>() {
                        @Override
                        protected void initChannel(Channel channel) throws Exception {
                            ChannelPipeline pipeline = channel.pipeline();
                            if (environment.sslEnabled()) {
                                pipeline.addLast(new SslHandler(sslEngineFactory.get()));
                            }
                            if (LOGGER.isTraceEnabled()) {
                                pipeline.addLast(LOGGING_HANDLER_INSTANCE);
                            }
                            customEndpointHandlers(pipeline);
                        }
                    }));
}

From source file:com.couchbase.client.core.io.endpoint.AbstractEndpoint.java

License:Open Source License

/**
 * Create a new {@link AbstractEndpoint} and supply essential params.
 *
 * @param addr the socket address to connect to.
 * @param env the environment to attach to.
 * @param group the {@link EventLoopGroup} to use.
 *//*www . j av  a  2  s . c  om*/
protected AbstractEndpoint(final InetSocketAddress addr, final Environment env, final EventLoopGroup group) {
    this.env = env;
    endpointStateDeferred = Streams.defer(env, defaultPromiseEnv);
    endpointStateStream = endpointStateDeferred.compose();

    connectionBootstrap = new BootstrapAdapter(new Bootstrap().group(group).channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(final SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    if (LOGGER.isTraceEnabled()) {
                        pipeline.addLast(new LoggingHandler(LogLevel.TRACE));
                    }

                    customEndpointHandlers(pipeline);
                    pipeline.addLast(new GenericEndpointHandler<REQ, RES>());
                }
            }).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.TCP_NODELAY, false).option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 1500)
            .remoteAddress(addr));
}

From source file:com.datastax.driver.core.NettyOptions.java

License:Apache License

/**
 * Hook invoked each time the driver creates a new {@link Connection}
 * and configures a new instance of {@link Bootstrap} for it.
 * <p/>//from ww w .jav a2  s .co  m
 * This hook is guaranteed to be called <em>after</em> the driver has applied all
 * {@link SocketOptions}s.
 * <p/>
 * This is a good place to add extra {@link io.netty.channel.ChannelHandler ChannelOption}s to the boostrap; e.g.
 * plug a custom {@link io.netty.buffer.ByteBufAllocator ByteBufAllocator} implementation:
 * <p/>
 * <pre>
 * ByteBufAllocator myCustomByteBufAllocator = ...
 *
 * public void afterBootstrapInitialized(Bootstrap bootstrap) {
 *     bootstrap.option(ChannelOption.ALLOCATOR, myCustomByteBufAllocator);
 * }
 * </pre>
 * <p/>
 * Note that the default implementation of this method configures a pooled {@code ByteBufAllocator} (Netty 4.0
 * defaults to unpooled). If you override this method to set unrelated options, make sure you call
 * {@code super.afterBootstrapInitialized(bootstrap)}.
 *
 * @param bootstrap the {@link Bootstrap} being initialized.
 */
public void afterBootstrapInitialized(Bootstrap bootstrap) {
    // In Netty 4.1.x, pooled will be the default, so this won't be necessary anymore
    bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 *  4/*from  www .j av a2s. c om*/
 *
 * @param ID          ??
 * @param messageLite
 * @return
 */
public static ByteBuf wrappedBufferInt(int ID, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 8 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x4);//writeIndex? //4
    buffer.writeByte(ID); // 4
    buffer.writeBytes(messageLite.toByteArray());
    buffer.setInt(0, buffer.writerIndex() - 0x4);
    messageLite = null;
    return buffer;
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * ??       </br>//from w ww . j  a  v a  2 s.  c  o m
 * Encoder buffer          </br>
 * ?4    </br>
 * proto buffer     </br>
 *          </br>
 *
 * @param arg1
 * @param arg2
 * @param messageLite
 * @return + ID+protoBufferData
 */
public static ByteBuf wrappedBufferInt(int arg1, int arg2, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 12 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x4);//writeIndex? //4
    buffer.writeByte(arg1); // 4
    buffer.writeByte(arg2); // 4
    buffer.writeBytes(messageLite.toByteArray());
    buffer.setInt(0, buffer.writerIndex() - 0x4);
    messageLite = null;
    return buffer;
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * ??     </br>//w  w  w. j  a  va 2 s  .  co m
 * Encoder buffer       </br>
 * ?2         </br>
 * proto buffer         </br>
 *             </br>
 *
 * @param arg1
 * @param arg2
 * @param messageLite
 * @return + ID+protoBufferData
 */
public static ByteBuf wrappedBufferShort(int arg1, int arg2, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 10 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x2);//writeIndex?
    buffer.writeByte(arg1);
    buffer.writeByte(arg2);
    buffer.writeBytes(messageLite.toByteArray());
    /**?2*/
    buffer.setShort(0, buffer.writerIndex() - 0x2);
    messageLite = null;
    return buffer;
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * Encoder buffer            </br>
 * 2            </br>//  w ww. j a v a  2 s . c  o  m
 *
 * @param ID          ??
 * @param messageLite byte[]
 * @return
 */
public static ByteBuf wrappedBufferShort(int ID, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 6 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x2);//writeIndex?     2
    buffer.writeInt(ID); //?                4
    buffer.writeBytes(src);
    buffer.setShort(0, buffer.writerIndex() - 0x2); //  short
    messageLite = null; //set null ,collection by GC
    return buffer;
}

From source file:com.dianping.cat.message.spi.codec.NativeMessageCodec.java

License:Open Source License

@Override
public ByteBuf encode(MessageTree tree) {
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(4 * 1024);

    try {//  w  ww.jav  a2  s  . c  o m
        Context ctx = new Context(tree);

        buf.writeInt(0); // place-holder

        Codec.HEADER.encode(ctx, buf, null);

        Message msg = tree.getMessage();

        if (msg != null) {
            encodeMessage(ctx, buf, msg);
        }
        int readableBytes = buf.readableBytes();

        buf.setInt(0, readableBytes - 4); // reset the message size

        return buf;
    } catch (RuntimeException e) {
        buf.release();

        throw e;
    }
}

From source file:com.difference.historybook.proxy.littleproxy.LittleProxyResponseTest.java

License:Apache License

@Test
public void testGetContentWithNoBackingArray() {
    testGetContent("This is a test", PooledByteBufAllocator.DEFAULT.compositeBuffer());
}

From source file:com.digitalpetri.modbus.examples.slave.SlaveExample.java

License:Apache License

public void start() throws ExecutionException, InterruptedException {
    slave.setRequestHandler(new ServiceRequestHandler() {
        @Override/*from   w w  w.j  a  v  a 2 s.  c  o  m*/
        public void onReadHoldingRegisters(
                ServiceRequest<ReadHoldingRegistersRequest, ReadHoldingRegistersResponse> service) {
            ReadHoldingRegistersRequest request = service.getRequest();

            ByteBuf registers = PooledByteBufAllocator.DEFAULT.buffer(request.getQuantity());

            for (int i = 0; i < request.getQuantity(); i++) {
                registers.writeShort(i);
            }

            service.sendResponse(new ReadHoldingRegistersResponse(registers));

            ReferenceCountUtil.release(request);
        }
    });

    slave.bind("localhost", 50200).get();
}