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.tongbanjie.tarzan.rpc.netty.NettyRpcServer.java

License:Apache License

@Override
public void start() {
    this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(//
            nettyServerConfig.getServerWorkerThreads(), //
            new ThreadFactory() {

                private AtomicInteger threadIndex = new AtomicInteger(0);

                @Override/* w  ww  . j  a  va 2  s.c  om*/
                public Thread newThread(Runnable r) {
                    return new Thread(r, "NettyServerCodecThread_" + this.threadIndex.incrementAndGet());
                }
            });

    ServerBootstrap childHandler = //
            this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupSelector)
                    .channel(NioServerSocketChannel.class)
                    //
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    //
                    .option(ChannelOption.SO_REUSEADDR, true)
                    //
                    .option(ChannelOption.SO_KEEPALIVE, false)
                    //
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    //
                    .option(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize())
                    //
                    .option(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize())
                    //
                    .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort()))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    //
                                    defaultEventExecutorGroup, //
                                    new NettyEncoder(), //
                                    new NettyDecoder(), //
                                    new IdleStateHandler(0, 0,
                                            nettyServerConfig.getServerChannelMaxIdleTimeSeconds()), //
                                    new NettyConnectManageHandler(), //
                                    new NettyServerHandler());
                        }
                    });

    if (nettyServerConfig.isServerPooledByteBufAllocatorEnable()) {
        childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    }

    try {
        ChannelFuture sync = this.serverBootstrap.bind().sync();
        InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress();
        this.port = addr.getPort();
    } catch (InterruptedException e1) {
        throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1);
    }

    if (this.channelEventListener != null) {
        this.nettyEventExecutor.start();
    }

    this.timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            try {
                NettyRpcServer.this.scanResponseTable();
            } catch (Exception e) {
                LOGGER.error("scanResponseTable exception", e);
            }
        }
    }, 1000 * 3, 1000);

    LOGGER.info("Rpc server [port:{}] start success", port);
}

From source file:com.turn.ttorrent.client.io.PeerClient.java

License:Apache License

public void start() throws Exception {
    bootstrap = new Bootstrap();
    bootstrap.group(environment.getEventService());
    bootstrap.channel(environment.getEventLoopType().getClientChannelType());
    // SocketAddress address = environment.getLocalPeerListenAddress();
    // if (address != null) bootstrap.localAddress(address);
    bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    // bootstrap.option(ChannelOption.SO_TIMEOUT, (int) TimeUnit.MINUTES.toMillis(CLIENT_KEEP_ALIVE_MINUTES));
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) TimeUnit.SECONDS.toMillis(10));
    // TODO: Derive from PieceHandler.DEFAULT_BLOCK_SIZE
    // bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 1024 * 1024);
    // bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
}

From source file:com.turn.ttorrent.client.io.PeerServer.java

License:Apache License

/**
 * Create and start a new listening service for our torrents, reporting
 * with our peer ID on the given address.
 *
 * <p>/*from w  ww. j  a va2  s  .  c o  m*/
 * This binds to the first available port in the client port range
 * PORT_RANGE_START to PORT_RANGE_END.
 * </p>
 */
public void start() throws Exception {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(environment.getEventService());
    bootstrap.channel(environment.getEventLoopType().getServerChannelType());
    bootstrap.option(ChannelOption.SO_BACKLOG, 128);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.childHandler(new ChannelInitializer() {
        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(new PeerServerHandshakeHandler(torrents));
        }
    });
    bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    // bootstrap.childOption(ChannelOption.SO_TIMEOUT, (int) TimeUnit.MINUTES.toMillis(CLIENT_KEEP_ALIVE_MINUTES));
    // TODO: Derive from PieceHandler.DEFAULT_BLOCK_SIZE
    // bootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 1024 * 1024);
    // bootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
    SocketAddress address = environment.getLocalPeerListenAddress();
    if (address != null) {
        future = bootstrap.bind(address).sync();
    } else {
        BIND: {
            Exception x = new IOException("No available port for the BitTorrent client!");
            for (int i = PORT_RANGE_START; i <= PORT_RANGE_END; i++) {
                try {
                    future = bootstrap.bind(i).sync();
                    break BIND;
                } catch (InterruptedException e) {
                    throw e;
                } catch (Exception e) {
                    x = e;
                }
            }
            throw new IOException("Failed to find an address to bind in range [" + PORT_RANGE_START + ","
                    + PORT_RANGE_END + "]", x);
        }
    }
}

From source file:com.uber.tchannel.codecs.CancelCodecTest.java

License:Open Source License

@Test
public void testEncodeDecode() throws Exception {

    CancelFrame cancelFrame = new CancelFrame(Integer.MAX_VALUE, Integer.MAX_VALUE,
            new Trace(0, 1, 2, (byte) 0x03), "Whoopsies");

    CancelFrame newCancelFrame = (CancelFrame) MessageCodec.decode(
            CodecTestUtil.encodeDecode(MessageCodec.encode(PooledByteBufAllocator.DEFAULT, cancelFrame)));

    assertEquals(cancelFrame.getId(), newCancelFrame.getId());
    assertEquals(cancelFrame.getTTL(), newCancelFrame.getTTL());
    assertTrue(newCancelFrame.getTTL() > 0);
    assertEquals(cancelFrame.getTracing().traceId, newCancelFrame.getTracing().traceId);
    assertEquals(cancelFrame.getWhy(), newCancelFrame.getWhy());
}

From source file:com.uber.tchannel.codecs.MessageCodec.java

License:Open Source License

public static ByteBuf encode(TFrame tFrame) {
    return TFrameCodec.encode(PooledByteBufAllocator.DEFAULT, tFrame);
}

From source file:com.uber.tchannel.codecs.MessageCodec.java

License:Open Source License

public static TFrame encode(Frame msg) {
    return encode(PooledByteBufAllocator.DEFAULT, msg);
}

From source file:com.uber.tchannel.Fixtures.java

License:Open Source License

public static CallRequestFrame callRequest(long id, boolean moreFragments, Map<String, String> headers,
        ByteBuf payload) {//from  ww  w .ja v a 2 s. c o  m
    CallRequestFrame callRequestFrame = new CallRequestFrame(id, moreFragments ? (byte) 1 : (byte) 0, 0L,
            new Trace(0, 0, 0, (byte) 0x00), "service", headers, ChecksumType.NoChecksum, 0, null);

    callRequestFrame.setPayload(
            Unpooled.wrappedBuffer(callRequestFrame.encodeHeader(PooledByteBufAllocator.DEFAULT), payload));

    return callRequestFrame;
}

From source file:com.uber.tchannel.Fixtures.java

License:Open Source License

public static CallRequestContinueFrame callRequestContinue(long id, boolean moreFragments, ByteBuf payload) {
    CallRequestContinueFrame callRequestContinueFrame = new CallRequestContinueFrame(id,
            moreFragments ? (byte) 1 : (byte) 0, ChecksumType.NoChecksum, 0, null);

    callRequestContinueFrame.setPayload(Unpooled
            .wrappedBuffer(callRequestContinueFrame.encodeHeader(PooledByteBufAllocator.DEFAULT), payload));

    return callRequestContinueFrame;
}

From source file:com.uber.tchannel.Fixtures.java

License:Open Source License

public static CallResponseFrame callResponse(long id, boolean moreFragments, Map<String, String> headers,
        ByteBuf payload) {/*from w  w  w .jav  a  2s. c om*/
    CallResponseFrame callResponseFrame = new CallResponseFrame(id, moreFragments ? (byte) 1 : (byte) 0,
            ResponseCode.OK, new Trace(0, 0, 0, (byte) 0x00), headers, ChecksumType.NoChecksum, 0, null);

    callResponseFrame.setPayload(
            Unpooled.wrappedBuffer(callResponseFrame.encodeHeader(PooledByteBufAllocator.DEFAULT), payload));

    return callResponseFrame;
}

From source file:com.uber.tchannel.Fixtures.java

License:Open Source License

public static CallResponseContinueFrame callResponseContinue(long id, boolean moreFragments, ByteBuf payload) {
    CallResponseContinueFrame callResponseContinueFrame = new CallResponseContinueFrame(id,
            moreFragments ? (byte) 1 : (byte) 0, ChecksumType.NoChecksum, 0, payload);

    callResponseContinueFrame.setPayload(Unpooled
            .wrappedBuffer(callResponseContinueFrame.encodeHeader(PooledByteBufAllocator.DEFAULT), payload));

    return callResponseContinueFrame;
}