Search in sources :

Example 11 with FixedRecvByteBufAllocator

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

the class EthereumChannelInitializer method initChannel.

@Override
public void initChannel(NioSocketChannel ch) {
    try {
        logger.info("Open {} connection, channel: {}", isInbound() ? "inbound" : "outbound", ch);
        if (isInbound()) {
            InetAddress address = ch.remoteAddress().getAddress();
            if (channelManager.isRecentlyDisconnected(address)) {
                // avoid too frequent connection attempts
                logger.info("Drop connection - the same IP was disconnected recently, channel: {}", ch);
                ch.disconnect();
                return;
            } else if (!channelManager.isAddressBlockAvailable(address)) {
                // avoid too many connection from same block address
                logger.info("IP range is full, IP {} is not accepted for new connection", address);
                ch.disconnect();
                return;
            } else if (peerScoringManager.isAddressBanned(address)) {
                // avoid connections to banned addresses
                logger.info("Drop connection - the address is banned, channel: {}", address);
                ch.disconnect();
                return;
            }
        }
        MessageQueue messageQueue = new MessageQueue();
        P2pHandler p2pHandler = new P2pHandler(ethereumListener, messageQueue, config.getPeerP2PPingInterval());
        MessageCodec messageCodec = new MessageCodec(ethereumListener, config);
        HandshakeHandler handshakeHandler = new HandshakeHandler(config, peerScoringManager, p2pHandler, messageCodec, configCapabilities);
        Channel channel = new Channel(messageQueue, messageCodec, nodeManager, rskWireProtocolFactory, eth62MessageFactory, staticMessages, remoteId);
        ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(config.peerChannelReadTimeout(), TimeUnit.SECONDS));
        ch.pipeline().addLast("handshakeHandler", handshakeHandler);
        handshakeHandler.setRemoteId(remoteId, channel);
        messageCodec.setChannel(channel);
        messageQueue.setChannel(channel);
        messageCodec.setP2pMessageFactory(new P2pMessageFactory());
        channelManager.add(channel);
        // limit the size of receiving buffer to 1024
        SocketChannelConfig channelConfig = ch.config();
        channelConfig.setRecvByteBufAllocator(new FixedRecvByteBufAllocator(16_777_216));
        channelConfig.setOption(ChannelOption.SO_RCVBUF, 16_777_216);
        channelConfig.setOption(ChannelOption.SO_BACKLOG, 1024);
        // be aware of channel closing
        ch.closeFuture().addListener(future -> channelManager.notifyDisconnect(channel));
    } catch (Exception e) {
        logger.error("Unexpected error: ", e);
    }
}
Also used : HandshakeHandler(org.ethereum.net.rlpx.HandshakeHandler) MessageQueue(org.ethereum.net.MessageQueue) P2pHandler(org.ethereum.net.p2p.P2pHandler) SocketChannelConfig(io.netty.channel.socket.SocketChannelConfig) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) P2pMessageFactory(org.ethereum.net.p2p.P2pMessageFactory) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) MessageCodec(org.ethereum.net.rlpx.MessageCodec) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) InetAddress(java.net.InetAddress)

Example 12 with FixedRecvByteBufAllocator

use of io.netty.channel.FixedRecvByteBufAllocator in project vert.x by eclipse.

the class DatagramSocketImpl method createChannel.

private static NioDatagramChannel createChannel(io.vertx.core.datagram.impl.InternetProtocolFamily family, DatagramSocketOptions options) {
    NioDatagramChannel channel;
    if (family == null) {
        channel = new NioDatagramChannel();
    } else {
        switch(family) {
            case IPv4:
                channel = new NioDatagramChannel(InternetProtocolFamily.IPv4);
                break;
            case IPv6:
                channel = new NioDatagramChannel(InternetProtocolFamily.IPv6);
                break;
            default:
                channel = new NioDatagramChannel();
        }
    }
    if (options.getSendBufferSize() != -1) {
        channel.config().setSendBufferSize(options.getSendBufferSize());
    }
    if (options.getReceiveBufferSize() != -1) {
        channel.config().setReceiveBufferSize(options.getReceiveBufferSize());
        channel.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }
    channel.config().setReuseAddress(options.isReuseAddress());
    if (options.getTrafficClass() != -1) {
        channel.config().setTrafficClass(options.getTrafficClass());
    }
    channel.config().setBroadcast(options.isBroadcast());
    channel.config().setLoopbackModeDisabled(options.isLoopbackModeDisabled());
    if (options.getMulticastTimeToLive() != -1) {
        channel.config().setTimeToLive(options.getMulticastTimeToLive());
    }
    if (options.getMulticastNetworkInterface() != null) {
        try {
            channel.config().setNetworkInterface(NetworkInterface.getByName(options.getMulticastNetworkInterface()));
        } catch (SocketException e) {
            throw new IllegalArgumentException("Could not find network interface with name " + options.getMulticastNetworkInterface());
        }
    }
    return channel;
}
Also used : NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator)

Example 13 with FixedRecvByteBufAllocator

use of io.netty.channel.FixedRecvByteBufAllocator in project vert.x by eclipse.

the class NetClientImpl method applyConnectionOptions.

private void applyConnectionOptions(Bootstrap bootstrap) {
    if (options.getLocalAddress() != null) {
        bootstrap.localAddress(options.getLocalAddress(), 0);
    }
    bootstrap.option(ChannelOption.TCP_NODELAY, options.isTcpNoDelay());
    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.getSoLinger() != -1) {
        bootstrap.option(ChannelOption.SO_LINGER, options.getSoLinger());
    }
    if (options.getTrafficClass() != -1) {
        bootstrap.option(ChannelOption.IP_TOS, options.getTrafficClass());
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeout());
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, options.isTcpKeepAlive());
}
Also used : FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator)

Example 14 with FixedRecvByteBufAllocator

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

the class UdpListener method start.

public void start() throws InterruptedException {
    bossGroup = new NioEventLoopGroup();
    final Bootstrap b = new Bootstrap().group(bossGroup).channel(NioDatagramChannel.class).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_RCVBUF, Integer.MAX_VALUE).option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(maxPacketSize)).handler(new ChannelInitializer<DatagramChannel>() {

        @Override
        protected void initChannel(DatagramChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new MessageToMessageDecoder<DatagramPacket>() {

                @Override
                protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
                    // Wrap the contents of the packet in a ByteBuffer, referencing
                    // the underlying byte array if possible
                    final ByteBuffer buffer = wrapContentsWithNioByteBuffer(packet);
                    // Build the message to dispatch via the Sink API
                    final TelemetryMessage msg = new TelemetryMessage(packet.sender(), buffer);
                    // Dispatch and retain a reference to the packet
                    // in the case that we are sharing the underlying byte array
                    final CompletableFuture<TelemetryMessage> future = dispatcher.send(msg);
                    packet.retain();
                    future.whenComplete((res, ex) -> packet.release());
                }
            });
        }
    });
    future = b.bind(host, port).await();
}
Also used : DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuffer(java.nio.ByteBuffer) ChannelPipeline(io.netty.channel.ChannelPipeline) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage) MessageToMessageDecoder(io.netty.handler.codec.MessageToMessageDecoder) DatagramPacket(io.netty.channel.socket.DatagramPacket) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) Bootstrap(io.netty.bootstrap.Bootstrap) List(java.util.List) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 15 with FixedRecvByteBufAllocator

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

the class BGPDispatcherImpl method createClientBootStrap.

private synchronized Bootstrap createClientBootStrap(final KeyMapping keys, final boolean reuseAddress) {
    final Bootstrap bootstrap = new Bootstrap();
    if (Epoll.isAvailable()) {
        bootstrap.channel(EpollSocketChannel.class);
        bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    } else {
        bootstrap.channel(NioSocketChannel.class);
    }
    if (keys != null && !keys.isEmpty()) {
        if (Epoll.isAvailable()) {
            bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys);
        } else {
            throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
        }
    }
    // Make sure we are doing round-robin processing
    bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(FIX_BUFFER_SIZE));
    bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
    bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
    bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
    if (bootstrap.config().group() == null) {
        bootstrap.group(this.workerGroup);
    }
    return bootstrap;
}
Also used : FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

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