21
21
import io .netty .channel .Channel ;
22
22
import io .netty .channel .ChannelPipeline ;
23
23
import io .netty .channel .EventLoopGroup ;
24
+ import io .netty .channel .epoll .Epoll ;
24
25
import io .netty .channel .epoll .EpollDatagramChannel ;
25
26
import io .netty .channel .epoll .EpollEventLoopGroup ;
26
27
import io .netty .channel .epoll .EpollSocketChannel ;
28
+ import io .netty .channel .kqueue .KQueue ;
27
29
import io .netty .channel .kqueue .KQueueDatagramChannel ;
28
30
import io .netty .channel .kqueue .KQueueEventLoopGroup ;
29
31
import io .netty .channel .kqueue .KQueueSocketChannel ;
39
41
import io .netty .incubator .channel .uring .IOUringEventLoopGroup ;
40
42
import io .netty .incubator .channel .uring .IOUringSocketChannel ;
41
43
import java .util .concurrent .ThreadFactory ;
44
+ import java .util .function .BiFunction ;
42
45
import net .pistonmaster .soulfire .proxy .SWProxy ;
43
46
44
47
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
+ };
72
72
73
73
private SFNettyHelper () {}
74
74
75
75
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 ));
85
81
86
82
Runtime .getRuntime ().addShutdownHook (new Thread (group ::shutdownGracefully ));
87
83
@@ -106,4 +102,10 @@ public static void addProxy(ChannelPipeline pipeline, SWProxy proxy) {
106
102
default -> throw new UnsupportedOperationException ("Unsupported proxy type: " + proxy .type ());
107
103
}
108
104
}
105
+
106
+ public record TransportMethod (
107
+ boolean tcpFastOpenClientSideAvailable ,
108
+ Class <? extends Channel > channelClass ,
109
+ Class <? extends DatagramChannel > datagramChannelClass ,
110
+ BiFunction <Integer , ThreadFactory , EventLoopGroup > eventLoopFactory ) {}
109
111
}
0 commit comments