io.netty.channel.epoll.EpollDatagramChannel Java Examples

The following examples show how to use io.netty.channel.epoll.EpollDatagramChannel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VoiceWebsocket.java    From kyoko with MIT License 6 votes vote down vote up
private CompletableFuture<Bootstrap> setupNetty(InetSocketAddress address, ChannelHandler handler) {
    var future = new CompletableFuture<Bootstrap>();

    var bootstrap = new Bootstrap()
            .group(vertx.nettyEventLoopGroup());

    if (Epoll.isAvailable()) {
        logger.info("epoll support is available, using it for UDP connections.");
        bootstrap.channel(EpollDatagramChannel.class);
    } else {
        logger.info("epoll unavailable, falling back to NIO.");
        bootstrap.channel(NioDatagramChannel.class);
    }

    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.IP_TOS, 0x10 | 0x08); // IPTOS_LOWDELAY | IPTOS_THROUGHPUT
    bootstrap.handler(handler).connect(address).addListener(res -> {
        if (res.isSuccess()) {
            future.complete(bootstrap);
        } else {
            future.completeExceptionally(res.cause());
        }
    });

    return future;
}
 
Example #2
Source File: UDPServerSocket.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public UDPServerSocket(ThreadedLogger logger, int port, String interfaz) {
    this.logger = logger;
    try {
            bootstrap = new Bootstrap()
                    .channel(EPOLL ? EpollDatagramChannel.class : NioDatagramChannel.class)
                    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .handler(this)
                    .group(EPOLL ? new EpollEventLoopGroup() : new NioEventLoopGroup());
            this.logger.info("Epoll Status is " + EPOLL);
        channel = bootstrap.bind(interfaz, port).sync().channel();
    } catch (Exception e) {
        this.logger.critical("**** FAILED TO BIND TO " + interfaz + ":" + port + "!");
        this.logger.critical("Perhaps a server is already running on that port?");
        System.exit(1);
    }
}
 
Example #3
Source File: UdpServerChannel.java    From UdpServerSocketChannel with GNU Lesser General Public License v3.0 6 votes vote down vote up
public UdpServerChannel(int ioThreads) {
	if (ioThreads < 1) {
		throw new IllegalArgumentException("IO threads cound can't be less than 1");
	}
	boolean epollAvailabe = Epoll.isAvailable();
	if (!epollAvailabe) {
		ioThreads = 1;
	}
	group = epollAvailabe ? new EpollEventLoopGroup(ioThreads) : new NioEventLoopGroup(ioThreads);
	Class<? extends DatagramChannel> channel = epollAvailabe ? EpollDatagramChannel.class : NioDatagramChannel.class;
	ChannelInitializer<Channel> initializer = new ChannelInitializer<Channel>() {
		final ReadRouteChannelHandler ioReadRoute = new ReadRouteChannelHandler();
		@Override
		protected void initChannel(Channel ioChannel) throws Exception {
			ioChannel.pipeline().addLast(ioReadRoute);
		}
	};
	while (ioThreads-- > 0) {
		Bootstrap ioBootstrap = new Bootstrap().group(group).channel(channel).handler(initializer);
		if (epollAvailabe) {
			ioBootstrap.option(UnixChannelOption.SO_REUSEPORT, true);
		}
		ioBootstraps.add(ioBootstrap);
	}
}
 
Example #4
Source File: DefaultLoopEpoll.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <CHANNEL extends Channel> CHANNEL getChannel(Class<CHANNEL> channelClass) {
	if (channelClass.equals(SocketChannel.class)) {
		return (CHANNEL) new EpollSocketChannel();
	}
	if (channelClass.equals(ServerSocketChannel.class)) {
		return (CHANNEL) new EpollServerSocketChannel();
	}
	if (channelClass.equals(DatagramChannel.class)) {
		return (CHANNEL) new EpollDatagramChannel();
	}
	if (channelClass.equals(DomainSocketChannel.class)) {
		return (CHANNEL) new EpollDomainSocketChannel();
	}
	if (channelClass.equals(ServerDomainSocketChannel.class)) {
		return (CHANNEL) new EpollServerDomainSocketChannel();
	}
	throw new IllegalArgumentException("Unsupported channel type: " + channelClass.getSimpleName());
}
 
Example #5
Source File: UDPServerSocket.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public UDPServerSocket(ThreadedLogger logger, int port, String interfaz) {
    this.logger = logger;
    try {
        if (Epoll.isAvailable()) {
            bootstrap = new Bootstrap()
                    .channel(EpollDatagramChannel.class)
                    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .handler(this)
                    .group(new EpollEventLoopGroup());
            this.logger.info("Epoll is available. EpollEventLoop will be used.");
        } else {
            bootstrap = new Bootstrap()
                    .group(new NioEventLoopGroup())
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .handler(this);
            this.logger.info("Epoll is unavailable. Reverting to NioEventLoop.");
        }
        channel = bootstrap.bind(interfaz, port).sync().channel();
    } catch (Exception e) {
        this.logger.critical("**** FAILED TO BIND TO " + interfaz + ":" + port + "!");
        this.logger.critical("Perhaps a server is already running on that port?");
        System.exit(1);
    }
}
 
Example #6
Source File: BuilderUtils.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the correct {@link Class} to use with the given {@link EventLoopGroup}.
 *
 * @param group the {@link EventLoopGroup} for which the class is needed
 * @return the class that should be used for bootstrapping
 */
public static Class<? extends DatagramChannel> datagramChannel(EventLoopGroup group) {
    if (useEpoll(group)) {
        return EpollDatagramChannel.class;
    } else if (useKQueue(group)) {
        return KQueueDatagramChannel.class;
    } else {
        return NioDatagramChannel.class;
    }
}
 
Example #7
Source File: NettyUDPConnector.java    From mpush with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private void createEpollServer(Listener listener) {
    EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup(
            1, new DefaultThreadFactory(ThreadNames.T_GATEWAY_WORKER)
    );
    eventLoopGroup.setIoRatio(100);
    createServer(listener, eventLoopGroup, EpollDatagramChannel::new);
}
 
Example #8
Source File: DefaultLoopEpoll.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <CHANNEL extends Channel> Class<? extends CHANNEL> getChannelClass(Class<CHANNEL> channelClass) {
	if (channelClass.equals(SocketChannel.class)) {
		return (Class<? extends CHANNEL>) EpollSocketChannel.class;
	}
	if (channelClass.equals(ServerSocketChannel.class)) {
		return (Class<? extends CHANNEL>) EpollServerSocketChannel.class;
	}
	if (channelClass.equals(DatagramChannel.class)) {
		return (Class<? extends CHANNEL>) EpollDatagramChannel.class;
	}
	throw new IllegalArgumentException("Unsupported channel type: " + channelClass.getSimpleName());
}
 
Example #9
Source File: EventLoopUtil.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static Class<? extends DatagramChannel> getDatagramChannelClass(EventLoopGroup eventLoopGroup) {
    if (eventLoopGroup instanceof EpollEventLoopGroup) {
        return EpollDatagramChannel.class;
    } else {
        return NioDatagramChannel.class;
    }
}
 
Example #10
Source File: RedisClient.java    From redisson with Apache License 2.0 5 votes vote down vote up
private RedisClient(RedisClientConfig config) {
    RedisClientConfig copy = new RedisClientConfig(config);
    if (copy.getTimer() == null) {
        copy.setTimer(new HashedWheelTimer());
        hasOwnTimer = true;
    }
    if (copy.getGroup() == null) {
        copy.setGroup(new NioEventLoopGroup());
        hasOwnGroup = true;
    }
    if (copy.getExecutor() == null) {
        copy.setExecutor(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2));
        hasOwnExecutor = true;
    }
    if (copy.getResolverGroup() == null) {
        if (config.getSocketChannelClass() == EpollSocketChannel.class) {
            copy.setResolverGroup(new DnsAddressResolverGroup(EpollDatagramChannel.class, DnsServerAddressStreamProviders.platformDefault()));
        } else {
            copy.setResolverGroup(new DnsAddressResolverGroup(NioDatagramChannel.class, DnsServerAddressStreamProviders.platformDefault()));
        }
        hasOwnResolver = true;
    }

    this.config = copy;
    this.executor = copy.getExecutor();
    this.timer = copy.getTimer();
    
    uri = copy.getAddress();
    resolvedAddr = copy.getAddr();
    
    if (resolvedAddr != null) {
        resolvedAddrFuture.set(RedissonPromise.newSucceededFuture(resolvedAddr));
    }
    
    channels = new DefaultChannelGroup(copy.getGroup().next()); 
    bootstrap = createBootstrap(copy, Type.PLAIN);
    pubSubBootstrap = createBootstrap(copy, Type.PUBSUB);
    
    this.commandTimeout = copy.getCommandTimeout();
}
 
Example #11
Source File: LispControllerBootstrap.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes event loop group.
 */
private void initEventLoopGroup() {

    // try to use EpollEventLoopGroup if possible,
    // if OS does not support native Epoll, fallback to use netty NIO
    try {
        eventLoopGroup = new EpollEventLoopGroup();
        channelClass = EpollDatagramChannel.class;
    } catch (NoClassDefFoundError e) {
        log.debug("Failed to initialize native (epoll) transport. "
                    + "Reason: {}. Proceeding with NIO event group.", e);
    }
    eventLoopGroup = new NioEventLoopGroup();
    channelClass = NioDatagramChannel.class;
}
 
Example #12
Source File: MasterSlaveConnectionManager.java    From redisson with Apache License 2.0 4 votes vote down vote up
protected MasterSlaveConnectionManager(Config cfg, UUID id) {
    this.id = id.toString();
    Version.logVersion();

    if (cfg.getTransportMode() == TransportMode.EPOLL) {
        if (cfg.getEventLoopGroup() == null) {
            this.group = new EpollEventLoopGroup(cfg.getNettyThreads(), new DefaultThreadFactory("redisson-netty"));
        } else {
            this.group = cfg.getEventLoopGroup();
        }

        this.socketChannelClass = EpollSocketChannel.class;
        if (PlatformDependent.isAndroid()) {
            this.resolverGroup = DefaultAddressResolverGroup.INSTANCE;
        } else {
            this.resolverGroup = cfg.getAddressResolverGroupFactory().create(EpollDatagramChannel.class, DnsServerAddressStreamProviders.platformDefault());
        }
    } else if (cfg.getTransportMode() == TransportMode.KQUEUE) {
        if (cfg.getEventLoopGroup() == null) {
            this.group = new KQueueEventLoopGroup(cfg.getNettyThreads(), new DefaultThreadFactory("redisson-netty"));
        } else {
            this.group = cfg.getEventLoopGroup();
        }

        this.socketChannelClass = KQueueSocketChannel.class;
        if (PlatformDependent.isAndroid()) {
            this.resolverGroup = DefaultAddressResolverGroup.INSTANCE;
        } else {
            this.resolverGroup = cfg.getAddressResolverGroupFactory().create(KQueueDatagramChannel.class, DnsServerAddressStreamProviders.platformDefault());
        }
    } else {
        if (cfg.getEventLoopGroup() == null) {
            this.group = new NioEventLoopGroup(cfg.getNettyThreads(), new DefaultThreadFactory("redisson-netty"));
        } else {
            this.group = cfg.getEventLoopGroup();
        }

        this.socketChannelClass = NioSocketChannel.class;
        if (PlatformDependent.isAndroid()) {
            this.resolverGroup = DefaultAddressResolverGroup.INSTANCE;
        } else {
            this.resolverGroup = cfg.getAddressResolverGroupFactory().create(NioDatagramChannel.class, DnsServerAddressStreamProviders.platformDefault());
        }
    }
    
    if (cfg.getExecutor() == null) {
        int threads = Runtime.getRuntime().availableProcessors() * 2;
        if (cfg.getThreads() != 0) {
            threads = cfg.getThreads();
        }
        executor = Executors.newFixedThreadPool(threads, new DefaultThreadFactory("redisson"));
    } else {
        executor = cfg.getExecutor();
    }

    this.cfg = cfg;
    this.codec = cfg.getCodec();
    this.commandExecutor = new CommandSyncService(this);
}