Search in sources :

Example 1 with P2pHandler

use of org.ethereum.net.p2p.P2pHandler 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)

Aggregations

FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)1 SocketChannelConfig (io.netty.channel.socket.SocketChannelConfig)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)1 InetAddress (java.net.InetAddress)1 MessageQueue (org.ethereum.net.MessageQueue)1 P2pHandler (org.ethereum.net.p2p.P2pHandler)1 P2pMessageFactory (org.ethereum.net.p2p.P2pMessageFactory)1 HandshakeHandler (org.ethereum.net.rlpx.HandshakeHandler)1 MessageCodec (org.ethereum.net.rlpx.MessageCodec)1