Skip to content

Commit 2eb1e23

Browse files
Properly detect tcp fast open support
1 parent c5b101b commit 2eb1e23

File tree

5 files changed

+47
-46
lines changed

5 files changed

+47
-46
lines changed

src/main/java/net/pistonmaster/soulfire/server/SoulFireServer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ public class SoulFireServer {
101101
.mapper(TranslatableComponent.class, TranslationMapper.INSTANCE)
102102
.build();
103103
public static final PlainTextComponentSerializer PLAIN_MESSAGE_SERIALIZER =
104-
PlainTextComponentSerializer.builder()
105-
.flattener(FLATTENER)
106-
.build();
104+
PlainTextComponentSerializer.builder().flattener(FLATTENER).build();
107105

108106
private final Injector injector =
109107
new InjectorBuilder().addDefaultHandlers("net.pistonmaster.soulfire").create();

src/main/java/net/pistonmaster/soulfire/server/plugins/ChatMessageLogger.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public static void onMessage(ChatMessageReceiveEvent event) {
5656
return;
5757
}
5858

59-
var sender = Optional.ofNullable(event.sender())
60-
.map(ChatMessageReceiveEvent.ChatMessageSender::senderName)
61-
.orElse("Server");
59+
var sender =
60+
Optional.ofNullable(event.sender())
61+
.map(ChatMessageReceiveEvent.ChatMessageSender::senderName)
62+
.orElse("Server");
6263
var message = Component.text("<" + sender + "> ").append(event.message());
6364

6465
var ansiMessage = ANSI_MESSAGE_SERIALIZER.serialize(message);

src/main/java/net/pistonmaster/soulfire/server/protocol/netty/SFNettyHelper.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import io.netty.channel.Channel;
2222
import io.netty.channel.ChannelPipeline;
2323
import io.netty.channel.EventLoopGroup;
24+
import io.netty.channel.epoll.Epoll;
2425
import io.netty.channel.epoll.EpollDatagramChannel;
2526
import io.netty.channel.epoll.EpollEventLoopGroup;
2627
import io.netty.channel.epoll.EpollSocketChannel;
28+
import io.netty.channel.kqueue.KQueue;
2729
import io.netty.channel.kqueue.KQueueDatagramChannel;
2830
import io.netty.channel.kqueue.KQueueEventLoopGroup;
2931
import io.netty.channel.kqueue.KQueueSocketChannel;
@@ -39,49 +41,43 @@
3941
import io.netty.incubator.channel.uring.IOUringEventLoopGroup;
4042
import io.netty.incubator.channel.uring.IOUringSocketChannel;
4143
import java.util.concurrent.ThreadFactory;
44+
import java.util.function.BiFunction;
4245
import net.pistonmaster.soulfire.proxy.SWProxy;
4346

4447
public class SFNettyHelper {
45-
public static final boolean SUPPORTS_TPC_FAST_OPEN_CONNECT =
46-
IOUring.isTcpFastOpenClientSideAvailable();
47-
public static final Class<? extends Channel> CHANNEL_CLASS;
48-
public static final Class<? extends DatagramChannel> DATAGRAM_CHANNEL_CLASS;
49-
50-
static {
51-
var transportMethod = TransportHelper.determineTransportMethod();
52-
switch (transportMethod) {
53-
case IO_URING -> {
54-
CHANNEL_CLASS = IOUringSocketChannel.class;
55-
DATAGRAM_CHANNEL_CLASS = IOUringDatagramChannel.class;
56-
}
57-
case EPOLL -> {
58-
CHANNEL_CLASS = EpollSocketChannel.class;
59-
DATAGRAM_CHANNEL_CLASS = EpollDatagramChannel.class;
60-
}
61-
case KQUEUE -> {
62-
CHANNEL_CLASS = KQueueSocketChannel.class;
63-
DATAGRAM_CHANNEL_CLASS = KQueueDatagramChannel.class;
64-
}
65-
case NIO -> {
66-
CHANNEL_CLASS = NioSocketChannel.class;
67-
DATAGRAM_CHANNEL_CLASS = NioDatagramChannel.class;
68-
}
69-
default -> throw new IllegalStateException("Unexpected value: " + transportMethod);
70-
}
71-
}
48+
public static final TransportMethod TRANSPORT_METHOD =
49+
switch (TransportHelper.determineTransportMethod()) {
50+
case IO_URING ->
51+
new TransportMethod(
52+
IOUring.isTcpFastOpenClientSideAvailable(),
53+
IOUringSocketChannel.class,
54+
IOUringDatagramChannel.class,
55+
IOUringEventLoopGroup::new);
56+
case EPOLL ->
57+
new TransportMethod(
58+
Epoll.isTcpFastOpenClientSideAvailable(),
59+
EpollSocketChannel.class,
60+
EpollDatagramChannel.class,
61+
EpollEventLoopGroup::new);
62+
case KQUEUE ->
63+
new TransportMethod(
64+
KQueue.isTcpFastOpenClientSideAvailable(),
65+
KQueueSocketChannel.class,
66+
KQueueDatagramChannel.class,
67+
KQueueEventLoopGroup::new);
68+
case NIO ->
69+
new TransportMethod(
70+
false, NioSocketChannel.class, NioDatagramChannel.class, NioEventLoopGroup::new);
71+
};
7272

7373
private SFNettyHelper() {}
7474

7575
public static EventLoopGroup createEventLoopGroup(int threads, String name) {
76-
ThreadFactory threadFactory =
77-
r -> Thread.ofPlatform().name(name).daemon().priority(Thread.MAX_PRIORITY).unstarted(r);
78-
EventLoopGroup group =
79-
switch (TransportHelper.determineTransportMethod()) {
80-
case IO_URING -> new IOUringEventLoopGroup(threads, threadFactory);
81-
case EPOLL -> new EpollEventLoopGroup(threads, threadFactory);
82-
case KQUEUE -> new KQueueEventLoopGroup(threads, threadFactory);
83-
case NIO -> new NioEventLoopGroup(threads, threadFactory);
84-
};
76+
var group =
77+
TRANSPORT_METHOD.eventLoopFactory.apply(
78+
threads,
79+
r ->
80+
Thread.ofPlatform().name(name).daemon().priority(Thread.MAX_PRIORITY).unstarted(r));
8581

8682
Runtime.getRuntime().addShutdownHook(new Thread(group::shutdownGracefully));
8783

@@ -106,4 +102,10 @@ public static void addProxy(ChannelPipeline pipeline, SWProxy proxy) {
106102
default -> throw new UnsupportedOperationException("Unsupported proxy type: " + proxy.type());
107103
}
108104
}
105+
106+
public record TransportMethod(
107+
boolean tcpFastOpenClientSideAvailable,
108+
Class<? extends Channel> channelClass,
109+
Class<? extends DatagramChannel> datagramChannelClass,
110+
BiFunction<Integer, ThreadFactory, EventLoopGroup> eventLoopFactory) {}
109111
}

src/main/java/net/pistonmaster/soulfire/server/protocol/netty/ViaClientSession.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ public void connect(boolean wait) {
123123
throw new IllegalStateException("Proxy must support UDP! (Only SOCKS5 is supported)");
124124
}
125125

126-
bootstrap.channelFactory(RakChannelFactory.client(SFNettyHelper.DATAGRAM_CHANNEL_CLASS));
126+
bootstrap.channelFactory(
127+
RakChannelFactory.client(SFNettyHelper.TRANSPORT_METHOD.datagramChannelClass()));
127128
} else {
128-
bootstrap.channel(SFNettyHelper.CHANNEL_CLASS);
129+
bootstrap.channel(SFNettyHelper.TRANSPORT_METHOD.channelClass());
129130
}
130131

131132
bootstrap
@@ -143,7 +144,7 @@ public void connect(boolean wait) {
143144
} else {
144145
bootstrap.option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true);
145146

146-
if (SFNettyHelper.SUPPORTS_TPC_FAST_OPEN_CONNECT) {
147+
if (SFNettyHelper.TRANSPORT_METHOD.tcpFastOpenClientSideAvailable()) {
147148
bootstrap.option(ChannelOption.TCP_FASTOPEN_CONNECT, true);
148149
}
149150
}

src/main/java/net/pistonmaster/soulfire/server/util/ExpiringSet.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
package net.pistonmaster.soulfire.server.util;
1919

20-
2120
import com.github.benmanes.caffeine.cache.Cache;
2221
import com.github.benmanes.caffeine.cache.Caffeine;
2322
import java.util.concurrent.TimeUnit;

0 commit comments

Comments
 (0)