@Override
public void initChannel(NioSocketChannel ch) throws Exception {
try {
if (!peerDiscoveryMode) {
logger.debug("Open {} connection, channel: {}", isInbound() ? "inbound" : "outbound", ch.toString());
}
if (notEligibleForIncomingConnection(ch)) {
ch.disconnect();
return;
}
final Channel channel = ctx.getBean(Channel.class);
channel.setInetSocketAddress(ch.remoteAddress());
channel.init(ch.pipeline(), remoteId, peerDiscoveryMode, channelManager);
if(!peerDiscoveryMode) {
channelManager.add(channel);
}
ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(256 * 1024));
ch.config().setOption(ChannelOption.SO_RCVBUF, 256 * 1024);
ch.config().setOption(ChannelOption.SO_BACKLOG, 1024);
ch.closeFuture().addListener((ChannelFutureListener) future -> {
if (!peerDiscoveryMode) {
channelManager.notifyDisconnect(channel);
}
});
} catch (Exception e) {
logger.error("Unexpected error: ", e);
}
}