Check is epoll available and use it if so

This commit is contained in:
TeksuSiK 2021-08-15 17:17:49 +02:00
parent 14e98718c1
commit e6ca9ecc8d
No known key found for this signature in database
GPG key ID: B62D34525FF2ACA9
2 changed files with 15 additions and 7 deletions

View file

@ -14,9 +14,13 @@ import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollDatagramChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.dns.DefaultDnsQuestion;
import io.netty.handler.codec.dns.DefaultDnsRawRecord;
import io.netty.handler.codec.dns.DefaultDnsRecordDecoder;
@ -47,6 +51,7 @@ public class TcpClientSession extends TcpSession {
private ProxyInfo proxy;
private EventLoopGroup group;
private final boolean epollAvailable = Epoll.isAvailable();
public TcpClientSession(String host, int port, PacketProtocol protocol) {
this(host, port, protocol, null);
@ -76,10 +81,10 @@ public class TcpClientSession extends TcpSession {
}
try {
this.group = new NioEventLoopGroup();
this.group = epollAvailable ? new EpollEventLoopGroup() : new NioEventLoopGroup();
final Bootstrap bootstrap = new Bootstrap();
bootstrap.channel(NioSocketChannel.class);
bootstrap.channel(epollAvailable ? EpollServerSocketChannel.class : NioServerSocketChannel.class);
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel channel) {
@ -147,7 +152,7 @@ public class TcpClientSession extends TcpSession {
AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = null;
try {
resolver = new DnsNameResolverBuilder(this.group.next())
.channelType(NioDatagramChannel.class)
.channelType(epollAvailable ? EpollDatagramChannel.class : NioDatagramChannel.class)
.build();
envelope = resolver.query(new DefaultDnsQuestion(name, DnsRecordType.SRV)).get();

View file

@ -2,7 +2,6 @@ package com.github.steveice10.packetlib.tcp;
import com.github.steveice10.packetlib.AbstractServer;
import com.github.steveice10.packetlib.BuiltinFlags;
import com.github.steveice10.packetlib.Server;
import com.github.steveice10.packetlib.packet.PacketProtocol;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
@ -12,6 +11,9 @@ import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.Future;
@ -38,8 +40,9 @@ public class TcpServer extends AbstractServer {
return;
}
this.group = new NioEventLoopGroup();
ChannelFuture future = new ServerBootstrap().channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {
final boolean epollAvailable = Epoll.isAvailable();
this.group = epollAvailable ? new EpollEventLoopGroup() : new NioEventLoopGroup();
ChannelFuture future = new ServerBootstrap().channel(epollAvailable ? EpollServerSocketChannel.class : NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel channel) throws Exception {
InetSocketAddress address = (InetSocketAddress) channel.remoteAddress();