Search in sources :

Example 1 with NioDatagramChannel

use of io.netty.channel.socket.nio.NioDatagramChannel 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;
}
Also used : NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator)

Aggregations

FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)1 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)1