Example usage for io.netty.channel.socket.nio NioDatagramChannel NioDatagramChannel

List of usage examples for io.netty.channel.socket.nio NioDatagramChannel NioDatagramChannel

Introduction

In this page you can find the example usage for io.netty.channel.socket.nio NioDatagramChannel NioDatagramChannel.

Prototype

public NioDatagramChannel() 

Source Link

Document

Create a new instance which will use the Operation Systems default InternetProtocolFamily .

Usage

From source file:io.advantageous.conekt.datagram.impl.DatagramSocketImpl.java

License:Open Source License

private static NioDatagramChannel createChannel(
        io.advantageous.conekt.datagram.impl.InternetProtocolFamily family, DatagramSocketOptions options) {
    NioDatagramChannel channel;/*  w w  w . j a va2  s  .  c o  m*/
    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;
}

From source file:io.jsync.datagram.impl.DefaultDatagramSocket.java

License:Open Source License

private static NioDatagramChannel createChannel(io.jsync.datagram.InternetProtocolFamily family) {
    if (family == null) {
        return new NioDatagramChannel();
    }/*from  w w w  .  j av  a2 s.  c  o m*/
    switch (family) {
    case IPv4:
        return new NioDatagramChannel(InternetProtocolFamily.IPv4);
    case IPv6:
        return new NioDatagramChannel(InternetProtocolFamily.IPv6);
    default:
        return new NioDatagramChannel();
    }
}

From source file:io.vertx.core.datagram.impl.DatagramSocketImpl.java

License:Open Source License

private static NioDatagramChannel createChannel(io.vertx.core.datagram.impl.InternetProtocolFamily family,
        DatagramSocketOptions options) {
    NioDatagramChannel channel;//from  w w w  .j  a v  a  2 s  .c om
    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;
}

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

License:Open Source License

/**
 * @return a new datagram channel
 */
public DatagramChannel datagramChannel() {
    return new NioDatagramChannel();
}

From source file:org.graylog2.inputs.transports.netty.DatagramChannelFactory.java

License:Open Source License

@Override
public DatagramChannel newChannel() {
    switch (transportType) {
    case EPOLL:/*from  w  w  w .j  a  va 2  s  .c o m*/
        return new EpollDatagramChannel();
    case KQUEUE:
        return new KQueueDatagramChannel();
    case NIO:
        return new NioDatagramChannel();
    default:
        throw new IllegalArgumentException("Invalid or unknown Netty transport type " + transportType);
    }
}

From source file:org.vertx.java.core.datagram.impl.DefaultDatagramSocket.java

License:Open Source License

private static NioDatagramChannel createChannel(org.vertx.java.core.datagram.InternetProtocolFamily family) {
    if (family == null) {
        return new NioDatagramChannel();
    }/* w w w  .j a v  a  2 s . c o m*/
    switch (family) {
    case IPv4:
        return new NioDatagramChannel(InternetProtocolFamily.IPv4);
    case IPv6:
        return new NioDatagramChannel(InternetProtocolFamily.IPv6);
    default:
        return new NioDatagramChannel();
    }
}

From source file:reactor.io.net.netty.udp.NettyDatagramServer.java

License:Apache License

public NettyDatagramServer(@Nonnull Environment env, @Nonnull EventBus reactor,
        @Nullable InetSocketAddress listenAddress, @Nullable final NetworkInterface multicastInterface,
        @Nonnull final ServerSocketOptions options, @Nullable Codec<Buffer, IN, OUT> codec,
        Collection<Consumer<NetChannel<IN, OUT>>> consumers) {
    super(env, reactor, listenAddress, multicastInterface, options, codec, consumers);

    if (options instanceof NettyServerSocketOptions) {
        this.nettyOptions = (NettyServerSocketOptions) options;
    } else {//from   w  w w  . j a v a2  s. c o  m
        this.nettyOptions = null;
    }

    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        int ioThreadCount = env.getProperty("reactor.udp.ioThreadCount", Integer.class, Environment.PROCESSORS);
        this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-udp-io"));
    }

    final NettyNetChannelInboundHandler inboundHandler = new NettyNetChannelInboundHandler() {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            super.channelRead(ctx, ((DatagramPacket) msg).content());
        }
    };

    this.bootstrap = new Bootstrap().group(ioGroup).option(ChannelOption.SO_RCVBUF, options.rcvbuf())
            .option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_REUSEADDR, options.reuseAddr())
            .channelFactory(new ChannelFactory<Channel>() {
                @Override
                public Channel newChannel() {
                    final NioDatagramChannel ch = new NioDatagramChannel();
                    DatagramChannelConfig config = ch.config();
                    config.setReceiveBufferSize(options.rcvbuf());
                    config.setSendBufferSize(options.sndbuf());
                    config.setReuseAddress(options.reuseAddr());

                    if (null != multicastInterface) {
                        config.setNetworkInterface(multicastInterface);
                    }

                    if (null != nettyOptions && null != nettyOptions.pipelineConfigurer()) {
                        nettyOptions.pipelineConfigurer().accept(ch.pipeline());
                    }

                    ch.closeFuture().addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if (log.isInfoEnabled()) {
                                log.info("CLOSE {}", ch);
                            }
                            close(ch);
                        }
                    });

                    netChannel = (NettyNetChannel<IN, OUT>) select(ch);
                    inboundHandler.setNetChannel(netChannel);

                    ch.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
                        @Override
                        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
                                throws Exception {
                            super.write(ctx, msg, promise);
                        }
                    });

                    return ch;
                }
            }).handler(inboundHandler);

    if (null != listenAddress) {
        bootstrap.localAddress(listenAddress);
    } else {
        bootstrap.localAddress(NetUtil.LOCALHOST, 3000);
    }
    if (null != multicastInterface) {
        bootstrap.option(ChannelOption.IP_MULTICAST_IF, multicastInterface);
    }
}