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