Search in sources :

Example 16 with FixedRecvByteBufAllocator

use of io.netty.channel.FixedRecvByteBufAllocator in project activemq-artemis by apache.

the class NettyTcpTransport method configureNetty.

private void configureNetty(Bootstrap bootstrap, NettyTransportOptions options) {
    bootstrap.option(ChannelOption.TCP_NODELAY, options.isTcpNoDelay());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeout());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, options.isTcpKeepAlive());
    bootstrap.option(ChannelOption.SO_LINGER, options.getSoLinger());
    if (options.getSendBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, options.getSendBufferSize());
    }
    if (options.getReceiveBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, options.getReceiveBufferSize());
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }
    if (options.getTrafficClass() != -1) {
        bootstrap.option(ChannelOption.IP_TOS, options.getTrafficClass());
    }
}
Also used : FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator)

Example 17 with FixedRecvByteBufAllocator

use of io.netty.channel.FixedRecvByteBufAllocator in project java-tron by tronprotocol.

the class TronChannelInitializer method initChannel.

@Override
public void initChannel(NioSocketChannel ch) throws Exception {
    try {
        if (isInbound() && channelManager.isRecentlyDisconnected(ch.remoteAddress().getAddress())) {
            // avoid too frequent connection attempts
            logger.info("Drop connection - the same IP was disconnected recently, channel: {}", ch.toString());
            ch.disconnect();
            return;
        }
        final Channel channel = ctx.getBean(PeerConnection.class);
        channel.init(ch.pipeline(), remoteId, peerDiscoveryMode, channelManager, p2pNode);
        if (!peerDiscoveryMode) {
            channelManager.add(channel);
        }
        // limit the size of receiving buffer to 1024
        ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(256 * 1024));
        ch.config().setOption(ChannelOption.SO_RCVBUF, 256 * 1024);
        ch.config().setOption(ChannelOption.SO_BACKLOG, 1024);
        // be aware of channel closing
        ch.closeFuture().addListener((ChannelFutureListener) future -> {
            logger.info("Close channel:" + channel.getNode());
            if (!peerDiscoveryMode) {
                channelManager.notifyDisconnect(channel);
            }
        });
    } catch (Exception e) {
        logger.error("Unexpected error: ", e);
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Logger(org.slf4j.Logger) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelOption(io.netty.channel.ChannelOption) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApplicationContext(org.springframework.context.ApplicationContext) Scope(org.springframework.context.annotation.Scope) PeerConnection(org.tron.core.net.peer.PeerConnection) Component(org.springframework.stereotype.Component) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) NodeImpl(org.tron.core.net.node.NodeImpl) ChannelFutureListener(io.netty.channel.ChannelFutureListener) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator)

Example 18 with FixedRecvByteBufAllocator

use of io.netty.channel.FixedRecvByteBufAllocator in project graylog2-server by Graylog2.

the class UdpTransportTest method receiveBufferSizePredictorIsUsingDefaultSize.

@Test
public void receiveBufferSizePredictorIsUsingDefaultSize() {
    FixedRecvByteBufAllocator recvByteBufAllocator = (FixedRecvByteBufAllocator) udpTransport.getBootstrap(mock(MessageInput.class)).config().options().get(ChannelOption.RCVBUF_ALLOCATOR);
    assertThat(recvByteBufAllocator.newHandle().guess()).isEqualTo(RECV_BUFFER_SIZE);
}
Also used : FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) MessageInput(org.graylog2.plugin.inputs.MessageInput) Test(org.junit.Test)

Example 19 with FixedRecvByteBufAllocator

use of io.netty.channel.FixedRecvByteBufAllocator in project netty by netty.

the class DetectPeerCloseWithoutReadTest method serverCloseWithoutClientReadIsDetected0.

private void serverCloseWithoutClientReadIsDetected0(final boolean extraReadRequested) throws InterruptedException {
    EventLoopGroup serverGroup = null;
    EventLoopGroup clientGroup = null;
    Channel serverChannel = null;
    Channel clientChannel = null;
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicInteger bytesRead = new AtomicInteger();
        final int expectedBytes = 100;
        serverGroup = newGroup();
        clientGroup = newGroup();
        ServerBootstrap sb = new ServerBootstrap();
        sb.group(serverGroup);
        sb.channel(serverChannel());
        sb.childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) {
                        ByteBuf buf = ctx.alloc().buffer(expectedBytes);
                        buf.writerIndex(buf.writerIndex() + expectedBytes);
                        ctx.writeAndFlush(buf).addListener(ChannelFutureListener.CLOSE);
                        ctx.fireChannelActive();
                    }
                });
            }
        });
        serverChannel = sb.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        Bootstrap cb = new Bootstrap();
        cb.group(serverGroup);
        cb.channel(clientChannel());
        // Ensure we read only one message per read() call and that we need multiple read()
        // calls to consume everything.
        cb.option(ChannelOption.AUTO_READ, false);
        cb.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
        cb.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(expectedBytes / 10));
        cb.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new TestHandler(bytesRead, extraReadRequested, latch));
            }
        });
        clientChannel = cb.connect(serverChannel.localAddress()).syncUninterruptibly().channel();
        latch.await();
        assertEquals(expectedBytes, bytesRead.get());
    } finally {
        if (serverChannel != null) {
            serverChannel.close().syncUninterruptibly();
        }
        if (clientChannel != null) {
            clientChannel.close().syncUninterruptibly();
        }
        if (serverGroup != null) {
            serverGroup.shutdownGracefully();
        }
        if (clientGroup != null) {
            clientGroup.shutdownGracefully();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 20 with FixedRecvByteBufAllocator

use of io.netty.channel.FixedRecvByteBufAllocator in project netty by netty.

the class EpollDatagramUnicastTest method testSegmentedDatagramPacket.

private void testSegmentedDatagramPacket(Bootstrap sb, Bootstrap cb, boolean composite, boolean gro) throws Throwable {
    if (!(cb.group() instanceof EpollEventLoopGroup)) {
        // Only supported for the native epoll transport.
        return;
    }
    if (gro && !(sb.group() instanceof EpollEventLoopGroup)) {
        // Only supported for the native epoll transport.
        return;
    }
    assumeTrue(EpollDatagramChannel.isSegmentedDatagramPacketSupported());
    Channel sc = null;
    Channel cc = null;
    try {
        cb.handler(new SimpleChannelInboundHandler<Object>() {

            @Override
            public void channelRead0(ChannelHandlerContext ctx, Object msgs) {
            // Nothing will be sent.
            }
        });
        cc = cb.bind(newSocketAddress()).sync().channel();
        final int numBuffers = 16;
        final int segmentSize = 512;
        int bufferCapacity = numBuffers * segmentSize;
        final CountDownLatch latch = new CountDownLatch(numBuffers);
        AtomicReference<Throwable> errorRef = new AtomicReference<Throwable>();
        if (gro) {
            // Enable GRO and also ensure we can read everything with one read as otherwise
            // we will drop things on the floor.
            sb.option(EpollChannelOption.UDP_GRO, true);
            sb.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(bufferCapacity));
        }
        sc = sb.handler(new SimpleChannelInboundHandler<DatagramPacket>() {

            @Override
            public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) {
                if (packet.content().readableBytes() == segmentSize) {
                    latch.countDown();
                }
            }
        }).bind(newSocketAddress()).sync().channel();
        if (sc instanceof EpollDatagramChannel) {
            assertEquals(gro, sc.config().getOption(EpollChannelOption.UDP_GRO));
        }
        InetSocketAddress addr = sendToAddress((InetSocketAddress) sc.localAddress());
        final ByteBuf buffer;
        if (composite) {
            CompositeByteBuf compositeBuffer = Unpooled.compositeBuffer();
            for (int i = 0; i < numBuffers; i++) {
                compositeBuffer.addComponent(true, Unpooled.directBuffer(segmentSize).writeZero(segmentSize));
            }
            buffer = compositeBuffer;
        } else {
            buffer = Unpooled.directBuffer(bufferCapacity).writeZero(bufferCapacity);
        }
        cc.writeAndFlush(new io.netty.channel.unix.SegmentedDatagramPacket(buffer, segmentSize, addr)).sync();
        if (!latch.await(10, TimeUnit.SECONDS)) {
            Throwable error = errorRef.get();
            if (error != null) {
                throw error;
            }
            fail();
        }
    } finally {
        if (cc != null) {
            cc.close().sync();
        }
        if (sc != null) {
            sc.close().sync();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) DatagramPacket(io.netty.channel.socket.DatagramPacket) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator)

Aggregations

FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)20 Bootstrap (io.netty.bootstrap.Bootstrap)6 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 InetSocketAddress (java.net.InetSocketAddress)4 ByteBuf (io.netty.buffer.ByteBuf)3 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 EventLoopGroup (io.netty.channel.EventLoopGroup)3 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ChannelHandler (io.netty.channel.ChannelHandler)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 ServerChannel (io.netty.channel.ServerChannel)2 DatagramChannel (io.netty.channel.socket.DatagramChannel)2 DatagramPacket (io.netty.channel.socket.DatagramPacket)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1