Example usage for io.netty.channel FixedRecvByteBufAllocator FixedRecvByteBufAllocator

List of usage examples for io.netty.channel FixedRecvByteBufAllocator FixedRecvByteBufAllocator

Introduction

In this page you can find the example usage for io.netty.channel FixedRecvByteBufAllocator FixedRecvByteBufAllocator.

Prototype

public FixedRecvByteBufAllocator(int bufferSize) 

Source Link

Document

Creates a new predictor that always returns the same prediction of the specified buffer size.

Usage

From source file:io.vertx.core.net.impl.transport.Transport.java

License:Open Source License

public void configure(DatagramChannel channel, DatagramSocketOptions options) {
    channel.config().setAllocator(PartialPooledByteBufAllocator.INSTANCE);
    if (options.getSendBufferSize() != -1) {
        channel.config().setSendBufferSize(options.getSendBufferSize());
    }/*from   w ww .ja  va  2 s.c  o m*/
    if (options.getReceiveBufferSize() != -1) {
        channel.config().setReceiveBufferSize(options.getReceiveBufferSize());
        channel.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }
    channel.config().setOption(ChannelOption.SO_REUSEADDR, options.isReuseAddress());
    if (options.getTrafficClass() != -1) {
        channel.config().setTrafficClass(options.getTrafficClass());
    }
    channel.config().setBroadcast(options.isBroadcast());
    if (this == Transport.JDK) {
        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());
            }
        }
    }
}

From source file:io.vertx.core.net.impl.transport.Transport.java

License:Open Source License

public void configure(ClientOptionsBase options, Bootstrap bootstrap) {
    if (options.getLocalAddress() != null) {
        bootstrap.localAddress(options.getLocalAddress(), 0);
    }/*w w w  .  j  a  v a2s. c  o  m*/
    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());
    bootstrap.option(ChannelOption.SO_REUSEADDR, options.isReuseAddress());
}

From source file:io.vertx.core.net.impl.transport.Transport.java

License:Open Source License

public void configure(NetServerOptions options, ServerBootstrap bootstrap) {
    bootstrap.childOption(ChannelOption.TCP_NODELAY, options.isTcpNoDelay());
    if (options.getSendBufferSize() != -1) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, options.getSendBufferSize());
    }//www.ja  v a 2s  . co  m
    if (options.getReceiveBufferSize() != -1) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, options.getReceiveBufferSize());
        bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }
    if (options.getSoLinger() != -1) {
        bootstrap.childOption(ChannelOption.SO_LINGER, options.getSoLinger());
    }
    if (options.getTrafficClass() != -1) {
        bootstrap.childOption(ChannelOption.IP_TOS, options.getTrafficClass());
    }
    bootstrap.childOption(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, options.isTcpKeepAlive());
    bootstrap.option(ChannelOption.SO_REUSEADDR, options.isReuseAddress());
    if (options.getAcceptBacklog() != -1) {
        bootstrap.option(ChannelOption.SO_BACKLOG, options.getAcceptBacklog());
    }
}

From source file:mmo.client.connection.ServerConnection.java

License:Apache License

/**
 * Opens connection to server. This method must be called explicitly.
 *//*from  www.j a va 2  s . c o m*/
public void open() {
    new Bootstrap().group(notificationGroup).channel(NioSocketChannel.class)
            .handler(new NotificationInitializer())
            .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(16384))
            .option(ChannelOption.TCP_NODELAY, true).connect(this.host, this.port);
    new Bootstrap().group(dataGroup).channel(NioSocketChannel.class).handler(new DataInitializer())
            .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(16384))
            .option(ChannelOption.TCP_NODELAY, true).connect(this.host, this.port);
}

From source file:mmo.server.Server.java

License:Open Source License

public void run(String host, int port) {
    parentGroup = new NioEventLoopGroup();
    childGroup = new NioEventLoopGroup();

    new ServerBootstrap().group(parentGroup, childGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new HttpServerCodec(), //
                            new HttpObjectAggregator(65536), //
                            routeHandlerProvider.get());

                }//from   w w  w  .  j a  v a  2s. c o  m
            }).option(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(16384)).bind(host, port)
            .addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        L.error("Error setting up server channel: {}", future.cause(), null);
                        new Thread(() -> { // TODO shutdown program
                            // gracefuller
                            try {
                                shutdown();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } finally {
                                System.exit(1);
                            }
                        }).start();
                    }
                }
            });
}

From source file:net.tomp2p.connection.ChannelCreator.java

License:Apache License

/**
 * Creates a "channel" to the given address. This won't send any message
 * unlike TCP.//  ww w.ja v  a  2  s  . co  m
 * 
 * @param broadcast
 *            Sets this channel to be able to broadcast
 * @param channelHandlers
 *            The handlers to set
 * @param futureResponse
 *            The futureResponse
 * @return The channel future object or null if we are shut down
 */
public ChannelFuture createUDP(final boolean broadcast,
        final Map<String, Pair<EventExecutorGroup, ChannelHandler>> channelHandlers,
        FutureResponse futureResponse) {
    readUDP.lock();
    try {
        if (shutdownUDP) {
            return null;
        }
        if (!semaphoreUPD.tryAcquire()) {
            LOG.error("Tried to acquire more resources (UDP) than announced! Announced {}", maxPermitsUDP);
            throw new RuntimeException("Tried to acquire more resources (UDP) than announced!");
        }
        final Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioDatagramChannel.class);
        b.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(ConnectionBean.UDP_LIMIT));
        if (broadcast) {
            b.option(ChannelOption.SO_BROADCAST, true);
        }
        Map<String, Pair<EventExecutorGroup, ChannelHandler>> channelHandlers2 = channelClientConfiguration
                .pipelineFilter().filter(channelHandlers, false, true);
        addHandlers(b, channelHandlers2);
        // Here we need to bind, as opposed to the TCP, were we connect if
        // we do a connect, we cannot receive
        // broadcast messages
        final ChannelFuture channelFuture;
        channelFuture = b.bind(new InetSocketAddress(channelClientConfiguration.senderUDP(), 0));
        recipients.add(channelFuture.channel());
        setupCloseListener(channelFuture, semaphoreUPD, futureResponse);
        return channelFuture;
    } finally {
        readUDP.unlock();
    }
}

From source file:net.tomp2p.connection.ChannelServer.java

License:Apache License

/**
 * Start to listen on a UPD port.//w w w .  j a  va  2  s  .  c o  m
 * 
 * @param listenAddresses
 *            The address to listen to
 * @param config
 *            Can create handlers to be attached to this port
 * @param broadcastFlag 
 * @return True if startup was successful
 */
boolean startupUDP(final InetSocketAddress listenAddresses, final ChannelServerConfiguration config,
        boolean broadcastFlag) {
    Bootstrap b = new Bootstrap();
    b.group(workerGroup);
    b.channel(NioDatagramChannel.class);
    //option broadcast only required as we not listen to the broadcast address directly
    if (broadcastFlag) {
        b.option(ChannelOption.SO_BROADCAST, true);
    }
    b.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(ConnectionBean.UDP_LIMIT));

    b.handler(new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ch.config().setAllocator(channelServerConfiguration.byteBufAllocator());
            for (Map.Entry<String, Pair<EventExecutorGroup, ChannelHandler>> entry : handlers(false)
                    .entrySet()) {
                if (!entry.getValue().isEmpty()) {
                    ch.pipeline().addLast(entry.getValue().element0(), entry.getKey(),
                            entry.getValue().element1());
                } else if (entry.getValue().element1() != null) {
                    ch.pipeline().addLast(entry.getKey(), entry.getValue().element1());
                }
            }
        }
    });

    ChannelFuture future = b.bind(listenAddresses);
    channelsUDP.put(listenAddresses.getAddress(), future.channel());
    return handleFuture(future);
}

From source file:net.tomp2p.connection2.ChannelCreator.java

License:Apache License

/**
 * Creates a "channel" to the given address. This won't send any message unlike TCP.
 * /*from  ww  w.j  a  va2s.co  m*/
 * @param recipient
 *            The recipient of the a message
 * 
 * @param broadcast
 *            Sets this channel to be able to broadcast
 * @param channelHandlers
 *            The handlers to set
 * @return The channel future object or null if we are shut down
 */
public ChannelFuture createUDP(final SocketAddress recipient, final boolean broadcast,
        final Map<String, ChannelHandler> channelHandlers) {
    readUDP.lock();
    try {
        if (shutdownUDP) {
            return null;
        }
        if (!semaphoreUPD.tryAcquire()) {
            LOG.error("Tried to acquire more resources (UDP) than announced!");
            throw new RuntimeException("Tried to acquire more resources (UDP) than announced!");
        }
        final Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioDatagramChannel.class);
        b.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(ConnectionBean.UDP_LIMIT));
        if (broadcast) {
            b.option(ChannelOption.SO_BROADCAST, true);
        }
        channelClientConfiguration.pipelineFilter().filter(channelHandlers, false, true);
        addHandlers(b, channelHandlers);
        // Here we need to bind, as opposed to the TCP, were we connect if we do a connect, we cannot receive
        // broadcast messages
        final ChannelFuture channelFuture;
        if (broadcast) {
            channelFuture = b.bind(new InetSocketAddress(0));
        } else {
            channelFuture = b.connect(recipient);
        }

        setupCloseListener(channelFuture, semaphoreUPD);
        CREATED_UDP_CONNECTIONS.incrementAndGet();
        return channelFuture;
    } finally {
        readUDP.unlock();
    }
}

From source file:net.tomp2p.connection2.ChannelServer.java

License:Apache License

/**
 * Start to listen on a UPD port.//from w w w  .  j  a  v a 2 s .co  m
 * 
 * @param listenAddresses
 *            The address to listen to
 * @param config
 *            Can create handlers to be attached to this port
 * @return True if startup was successful
 */
boolean startupUDP(final InetSocketAddress listenAddresses, final ChannelServerConficuration config) {
    Bootstrap b = new Bootstrap();
    b.group(workerGroup);
    b.channel(NioDatagramChannel.class);
    b.option(ChannelOption.SO_BROADCAST, true);
    b.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(ConnectionBean.UDP_LIMIT));

    b.handler(new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(final Channel ch) throws Exception {
            for (Map.Entry<String, ChannelHandler> entry : handlers(false).entrySet()) {
                ch.pipeline().addLast(entry.getKey(), entry.getValue());
            }
        }
    });

    ChannelFuture future = b.bind(listenAddresses);
    channelUDP = future.channel();
    return handleFuture(future);
}

From source file:org.apache.activemq.transport.amqp.client.transport.NettyTcpTransport.java

License:Apache License

private void configureNetty(Bootstrap bootstrap, NettyTransportOptions options) {
    bootstrap.option(ChannelOption.TCP_NODELAY, options.isTcpNoDelay());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeout());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, options.isTcpKeepAlive());
    bootstrap.option(ChannelOption.SO_LINGER, options.getSoLinger());
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);

    if (options.getSendBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, options.getSendBufferSize());
    }//from   w ww  .  j  a  v  a 2  s.  c o  m

    if (options.getReceiveBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, options.getReceiveBufferSize());
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }

    if (options.getTrafficClass() != -1) {
        bootstrap.option(ChannelOption.IP_TOS, options.getTrafficClass());
    }
}