Example usage for io.netty.channel ChannelOption ALLOCATOR

List of usage examples for io.netty.channel ChannelOption ALLOCATOR

Introduction

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

Prototype

ChannelOption ALLOCATOR

To view the source code for io.netty.channel ChannelOption ALLOCATOR.

Click Source Link

Usage

From source file:uk.co.thinkofdeath.prismarine.network.NetworkManager.java

License:Apache License

/**
 * Start listening on the specified address & port. The initial handler for
 * each connection will be created from the supplier
 *
 * @param address// w  ww  .j  a v  a 2  s  . c  o  m
 *         the address of the server
 * @param port
 *         the port of the server
 * @param initialHandler
 *         the supplier which creates the initial packet handler
 */
public void listen(String address, int port, Supplier<PacketHandler> initialHandler) {
    // Listening == Server
    incomingPacketType = ProtocolDirection.SERVERBOUND;
    if (isOnlineMode()) {
        // Encryption is only enabled in online mode
        logger.info("Generating encryption keys");
        try {
            KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
            generator.initialize(1024);
            networkKeyPair = generator.generateKeyPair();
        } catch (NoSuchAlgorithmException e) {
            logger.info("Failed to generate encryption keys");
            throw new RuntimeException(e);
        }
    }

    logger.log(Level.INFO, "Starting on {0}:{1,number,#}", new Object[] { address, port });

    final EventLoopGroup group = new NioEventLoopGroup();
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(group).channel(NioServerSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childHandler(new ConnectionInitializer(this, initialHandler))
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.TCP_NODELAY, true);

    channel = bootstrap.bind(address, port).channel();
    channel.closeFuture().addListener(future -> group.shutdownGracefully());

}