Skip to content

Commit a5a0ef4

Browse files
committed
Support netty 4.2.1, add system property for opt-in io_uring
1 parent 30e2bad commit a5a0ef4

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ cloudburstnbt = "3.0.0.Final"
77
slf4j = "2.0.9"
88
math = "2.0"
99
fastutil-maps = "8.5.3"
10-
netty = "4.1.103.Final"
11-
netty-io_uring = "0.0.24.Final"
10+
netty = "4.2.1.Final"
1211
gson = "2.11.0"
1312
minecraftauth = "4.1.1"
1413
checkerframework = "3.42.0"
@@ -37,7 +36,6 @@ fastutil-int2object-maps = { module = "com.nukkitx.fastutil:fastutil-int-object-
3736
fastutil-int2int-maps = { module = "com.nukkitx.fastutil:fastutil-int-int-maps", version.ref = "fastutil-maps" }
3837

3938
netty-all = { module = "io.netty:netty-all", version.ref = "netty" }
40-
netty-incubator-transport-native-io_uring = { module = "io.netty.incubator:netty-incubator-transport-native-io_uring", version.ref = "netty-io_uring" }
4139

4240
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
4341

@@ -57,4 +55,3 @@ lombok = { module = "io.freefair.gradle:lombok-plugin", version.ref = "lombok-pl
5755
adventure = ["adventure-text-serializer-gson", "adventure-text-serializer-json-legacy-impl"]
5856
math = ["math-api", "math-immutable"]
5957
fastutil = ["fastutil-object2int-maps", "fastutil-int2object-maps", "fastutil-int2int-maps"]
60-
netty = ["netty-all", "netty-incubator-transport-native-io_uring"]

protocol/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies {
2929
api(libs.bundles.fastutil)
3030

3131
// Netty
32-
api(libs.bundles.netty)
32+
api(libs.netty.all)
3333

3434
// Checker Framework
3535
api(libs.checkerframework.qual)

protocol/src/main/java/org/geysermc/mcprotocollib/network/helper/TransportHelper.java

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,41 @@
22

33
import io.netty.channel.ChannelFactory;
44
import io.netty.channel.EventLoopGroup;
5+
import io.netty.channel.IoHandlerFactory;
6+
import io.netty.channel.MultiThreadIoEventLoopGroup;
57
import io.netty.channel.epoll.Epoll;
68
import io.netty.channel.epoll.EpollDatagramChannel;
79
import io.netty.channel.epoll.EpollEventLoopGroup;
10+
import io.netty.channel.epoll.EpollIoHandler;
811
import io.netty.channel.epoll.EpollServerSocketChannel;
912
import io.netty.channel.epoll.EpollSocketChannel;
1013
import io.netty.channel.kqueue.KQueue;
1114
import io.netty.channel.kqueue.KQueueDatagramChannel;
1215
import io.netty.channel.kqueue.KQueueEventLoopGroup;
16+
import io.netty.channel.kqueue.KQueueIoHandler;
1317
import io.netty.channel.kqueue.KQueueServerSocketChannel;
1418
import io.netty.channel.kqueue.KQueueSocketChannel;
1519
import io.netty.channel.nio.NioEventLoopGroup;
20+
import io.netty.channel.nio.NioIoHandler;
1621
import io.netty.channel.socket.DatagramChannel;
1722
import io.netty.channel.socket.ServerSocketChannel;
1823
import io.netty.channel.socket.SocketChannel;
1924
import io.netty.channel.socket.nio.NioDatagramChannel;
2025
import io.netty.channel.socket.nio.NioServerSocketChannel;
2126
import io.netty.channel.socket.nio.NioSocketChannel;
22-
import io.netty.incubator.channel.uring.IOUring;
23-
import io.netty.incubator.channel.uring.IOUringDatagramChannel;
24-
import io.netty.incubator.channel.uring.IOUringEventLoopGroup;
25-
import io.netty.incubator.channel.uring.IOUringServerSocketChannel;
26-
import io.netty.incubator.channel.uring.IOUringSocketChannel;
27+
import io.netty.channel.uring.IoUring;
28+
import io.netty.channel.uring.IoUringDatagramChannel;
29+
import io.netty.channel.uring.IoUringIoHandler;
30+
import io.netty.channel.uring.IoUringServerSocketChannel;
31+
import io.netty.channel.uring.IoUringSocketChannel;
32+
import org.checkerframework.checker.nullness.qual.Nullable;
2733

2834
import java.util.concurrent.ThreadFactory;
29-
import java.util.function.Function;
35+
import java.util.function.BiFunction;
3036

3137
public class TransportHelper {
3238
public static final TransportHelper.TransportType TRANSPORT_TYPE = TransportHelper.determineTransportMethod();
39+
public static final boolean NEW_NETTY = isClassAvailable("io.netty.channel.local.LocalIoHandle");
3340

3441
public enum TransportMethod {
3542
NIO, EPOLL, KQUEUE, IO_URING
@@ -42,24 +49,27 @@ public record TransportType(TransportMethod method,
4249
ChannelFactory<? extends SocketChannel> socketChannelFactory,
4350
Class<? extends DatagramChannel> datagramChannelClass,
4451
ChannelFactory<? extends DatagramChannel> datagramChannelFactory,
45-
Function<ThreadFactory, EventLoopGroup> eventLoopGroupFactory,
52+
@Nullable BiFunction<Integer, ThreadFactory, EventLoopGroup> eventLoopGroupFactory,
4653
boolean supportsTcpFastOpenServer,
4754
boolean supportsTcpFastOpenClient) {
4855
}
4956

5057
private static TransportType determineTransportMethod() {
51-
if (isClassAvailable("io.netty.incubator.channel.uring.IOUring") && IOUring.isAvailable()) {
58+
if (isClassAvailable("io.netty.channel.uring.IoUring")
59+
&& IoUring.isAvailable()
60+
&& Boolean.parseBoolean(System.getProperty("Mcpl.io_uring"))
61+
) {
5262
return new TransportType(
5363
TransportMethod.IO_URING,
54-
IOUringServerSocketChannel.class,
55-
IOUringServerSocketChannel::new,
56-
IOUringSocketChannel.class,
57-
IOUringSocketChannel::new,
58-
IOUringDatagramChannel.class,
59-
IOUringDatagramChannel::new,
60-
factory -> new IOUringEventLoopGroup(0, factory),
61-
IOUring.isTcpFastOpenServerSideAvailable(),
62-
IOUring.isTcpFastOpenClientSideAvailable()
64+
IoUringServerSocketChannel.class,
65+
IoUringServerSocketChannel::new,
66+
IoUringSocketChannel.class,
67+
IoUringSocketChannel::new,
68+
IoUringDatagramChannel.class,
69+
IoUringDatagramChannel::new,
70+
(threads, factory) -> null,
71+
IoUring.isTcpFastOpenServerSideAvailable(),
72+
IoUring.isTcpFastOpenClientSideAvailable()
6373
);
6474
}
6575

@@ -72,7 +82,7 @@ private static TransportType determineTransportMethod() {
7282
EpollSocketChannel::new,
7383
EpollDatagramChannel.class,
7484
EpollDatagramChannel::new,
75-
factory -> new EpollEventLoopGroup(0, factory),
85+
NEW_NETTY ? null : EpollEventLoopGroup::new,
7686
Epoll.isTcpFastOpenServerSideAvailable(),
7787
Epoll.isTcpFastOpenClientSideAvailable()
7888
);
@@ -87,7 +97,7 @@ private static TransportType determineTransportMethod() {
8797
KQueueSocketChannel::new,
8898
KQueueDatagramChannel.class,
8999
KQueueDatagramChannel::new,
90-
factory -> new KQueueEventLoopGroup(0, factory),
100+
NEW_NETTY ? null : KQueueEventLoopGroup::new,
91101
KQueue.isTcpFastOpenServerSideAvailable(),
92102
KQueue.isTcpFastOpenClientSideAvailable()
93103
);
@@ -101,12 +111,30 @@ private static TransportType determineTransportMethod() {
101111
NioSocketChannel::new,
102112
NioDatagramChannel.class,
103113
NioDatagramChannel::new,
104-
factory -> new NioEventLoopGroup(0, factory),
114+
NEW_NETTY ? null : NioEventLoopGroup::new,
105115
false,
106116
false
107117
);
108118
}
109119

120+
public static EventLoopGroup createEventLoopGroup(ThreadFactory threadFactory) {
121+
return createEventLoopGroup(threadFactory, 0);
122+
}
123+
124+
public static EventLoopGroup createEventLoopGroup(ThreadFactory threadFactory, int threads) {
125+
if (TRANSPORT_TYPE.eventLoopGroupFactory() == null) {
126+
IoHandlerFactory handler = switch (TRANSPORT_TYPE.method()) {
127+
case NIO -> NioIoHandler.newFactory();
128+
case EPOLL -> EpollIoHandler.newFactory();
129+
case KQUEUE -> KQueueIoHandler.newFactory();
130+
case IO_URING -> IoUringIoHandler.newFactory();
131+
};
132+
return new MultiThreadIoEventLoopGroup(threads, threadFactory, handler);
133+
} else {
134+
return TRANSPORT_TYPE.eventLoopGroupFactory().apply(threads, threadFactory);
135+
}
136+
}
137+
110138
/**
111139
* Used so implementations can opt to remove these dependencies if so desired
112140
*/

protocol/src/main/java/org/geysermc/mcprotocollib/network/server/NetworkServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ protected ChannelFactory<? extends ServerChannel> getChannelFactory() {
8787
}
8888

8989
protected EventLoopGroup createBossEventLoopGroup() {
90-
return TransportHelper.TRANSPORT_TYPE.eventLoopGroupFactory().apply(null);
90+
return TransportHelper.createEventLoopGroup(null);
9191
}
9292

9393
protected EventLoopGroup createWorkerEventLoopGroup() {
94-
return TransportHelper.TRANSPORT_TYPE.eventLoopGroupFactory().apply(null);
94+
return TransportHelper.createEventLoopGroup(null);
9595
}
9696

9797
protected void setOptions(ServerBootstrap bootstrap) {

protocol/src/main/java/org/geysermc/mcprotocollib/network/session/ClientNetworkSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private static void createEventLoopGroup() {
127127
return;
128128
}
129129

130-
EVENT_LOOP_GROUP = TransportHelper.TRANSPORT_TYPE.eventLoopGroupFactory().apply(newThreadFactory());
130+
EVENT_LOOP_GROUP = TransportHelper.createEventLoopGroup(newThreadFactory());
131131

132132
Runtime.getRuntime().addShutdownHook(new Thread(
133133
() -> EVENT_LOOP_GROUP.shutdownGracefully(SHUTDOWN_QUIET_PERIOD_MS, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS)));

0 commit comments

Comments
 (0)