Skip to content

Commit

Permalink
Properly detect tcp fast open support
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Feb 19, 2024
1 parent c5b101b commit 2eb1e23
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public class SoulFireServer {
.mapper(TranslatableComponent.class, TranslationMapper.INSTANCE)
.build();
public static final PlainTextComponentSerializer PLAIN_MESSAGE_SERIALIZER =
PlainTextComponentSerializer.builder()
.flattener(FLATTENER)
.build();
PlainTextComponentSerializer.builder().flattener(FLATTENER).build();

private final Injector injector =
new InjectorBuilder().addDefaultHandlers("net.pistonmaster.soulfire").create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ public static void onMessage(ChatMessageReceiveEvent event) {
return;
}

var sender = Optional.ofNullable(event.sender())
.map(ChatMessageReceiveEvent.ChatMessageSender::senderName)
.orElse("Server");
var sender =
Optional.ofNullable(event.sender())
.map(ChatMessageReceiveEvent.ChatMessageSender::senderName)
.orElse("Server");
var message = Component.text("<" + sender + "> ").append(event.message());

var ansiMessage = ANSI_MESSAGE_SERIALIZER.serialize(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import io.netty.channel.Channel;
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.EpollSocketChannel;
import io.netty.channel.kqueue.KQueue;
import io.netty.channel.kqueue.KQueueDatagramChannel;
import io.netty.channel.kqueue.KQueueEventLoopGroup;
import io.netty.channel.kqueue.KQueueSocketChannel;
Expand All @@ -39,49 +41,43 @@
import io.netty.incubator.channel.uring.IOUringEventLoopGroup;
import io.netty.incubator.channel.uring.IOUringSocketChannel;
import java.util.concurrent.ThreadFactory;
import java.util.function.BiFunction;
import net.pistonmaster.soulfire.proxy.SWProxy;

public class SFNettyHelper {
public static final boolean SUPPORTS_TPC_FAST_OPEN_CONNECT =
IOUring.isTcpFastOpenClientSideAvailable();
public static final Class<? extends Channel> CHANNEL_CLASS;
public static final Class<? extends DatagramChannel> DATAGRAM_CHANNEL_CLASS;

static {
var transportMethod = TransportHelper.determineTransportMethod();
switch (transportMethod) {
case IO_URING -> {
CHANNEL_CLASS = IOUringSocketChannel.class;
DATAGRAM_CHANNEL_CLASS = IOUringDatagramChannel.class;
}
case EPOLL -> {
CHANNEL_CLASS = EpollSocketChannel.class;
DATAGRAM_CHANNEL_CLASS = EpollDatagramChannel.class;
}
case KQUEUE -> {
CHANNEL_CLASS = KQueueSocketChannel.class;
DATAGRAM_CHANNEL_CLASS = KQueueDatagramChannel.class;
}
case NIO -> {
CHANNEL_CLASS = NioSocketChannel.class;
DATAGRAM_CHANNEL_CLASS = NioDatagramChannel.class;
}
default -> throw new IllegalStateException("Unexpected value: " + transportMethod);
}
}
public static final TransportMethod TRANSPORT_METHOD =
switch (TransportHelper.determineTransportMethod()) {
case IO_URING ->
new TransportMethod(
IOUring.isTcpFastOpenClientSideAvailable(),
IOUringSocketChannel.class,
IOUringDatagramChannel.class,
IOUringEventLoopGroup::new);
case EPOLL ->
new TransportMethod(
Epoll.isTcpFastOpenClientSideAvailable(),
EpollSocketChannel.class,
EpollDatagramChannel.class,
EpollEventLoopGroup::new);
case KQUEUE ->
new TransportMethod(
KQueue.isTcpFastOpenClientSideAvailable(),
KQueueSocketChannel.class,
KQueueDatagramChannel.class,
KQueueEventLoopGroup::new);
case NIO ->
new TransportMethod(
false, NioSocketChannel.class, NioDatagramChannel.class, NioEventLoopGroup::new);
};

private SFNettyHelper() {}

public static EventLoopGroup createEventLoopGroup(int threads, String name) {
ThreadFactory threadFactory =
r -> Thread.ofPlatform().name(name).daemon().priority(Thread.MAX_PRIORITY).unstarted(r);
EventLoopGroup group =
switch (TransportHelper.determineTransportMethod()) {
case IO_URING -> new IOUringEventLoopGroup(threads, threadFactory);
case EPOLL -> new EpollEventLoopGroup(threads, threadFactory);
case KQUEUE -> new KQueueEventLoopGroup(threads, threadFactory);
case NIO -> new NioEventLoopGroup(threads, threadFactory);
};
var group =
TRANSPORT_METHOD.eventLoopFactory.apply(
threads,
r ->
Thread.ofPlatform().name(name).daemon().priority(Thread.MAX_PRIORITY).unstarted(r));

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

Expand All @@ -106,4 +102,10 @@ public static void addProxy(ChannelPipeline pipeline, SWProxy proxy) {
default -> throw new UnsupportedOperationException("Unsupported proxy type: " + proxy.type());
}
}

public record TransportMethod(
boolean tcpFastOpenClientSideAvailable,
Class<? extends Channel> channelClass,
Class<? extends DatagramChannel> datagramChannelClass,
BiFunction<Integer, ThreadFactory, EventLoopGroup> eventLoopFactory) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ public void connect(boolean wait) {
throw new IllegalStateException("Proxy must support UDP! (Only SOCKS5 is supported)");
}

bootstrap.channelFactory(RakChannelFactory.client(SFNettyHelper.DATAGRAM_CHANNEL_CLASS));
bootstrap.channelFactory(
RakChannelFactory.client(SFNettyHelper.TRANSPORT_METHOD.datagramChannelClass()));
} else {
bootstrap.channel(SFNettyHelper.CHANNEL_CLASS);
bootstrap.channel(SFNettyHelper.TRANSPORT_METHOD.channelClass());
}

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

if (SFNettyHelper.SUPPORTS_TPC_FAST_OPEN_CONNECT) {
if (SFNettyHelper.TRANSPORT_METHOD.tcpFastOpenClientSideAvailable()) {
bootstrap.option(ChannelOption.TCP_FASTOPEN_CONNECT, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package net.pistonmaster.soulfire.server.util;


import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.TimeUnit;
Expand Down

0 comments on commit 2eb1e23

Please sign in to comment.