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);
}
}
Aggregations