| 1 | package net.minecraft.server.network;
|
| 2 |
|
| 3 | import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
| 4 | import io.netty.channel.Channel;
|
| 5 | import io.netty.channel.EventLoopGroup;
|
| 6 | import io.netty.channel.IoHandlerFactory;
|
| 7 | import io.netty.channel.MultiThreadIoEventLoopGroup;
|
| 8 | import io.netty.channel.ServerChannel;
|
| 9 | import io.netty.channel.epoll.Epoll;
|
| 10 | import io.netty.channel.epoll.EpollIoHandler;
|
| 11 | import io.netty.channel.epoll.EpollServerSocketChannel;
|
| 12 | import io.netty.channel.epoll.EpollSocketChannel;
|
| 13 | import io.netty.channel.kqueue.KQueue;
|
| 14 | import io.netty.channel.kqueue.KQueueIoHandler;
|
| 15 | import io.netty.channel.kqueue.KQueueServerSocketChannel;
|
| 16 | import io.netty.channel.kqueue.KQueueSocketChannel;
|
| 17 | import io.netty.channel.local.LocalChannel;
|
| 18 | import io.netty.channel.local.LocalIoHandler;
|
| 19 | import io.netty.channel.local.LocalServerChannel;
|
| 20 | import io.netty.channel.nio.NioIoHandler;
|
| 21 | import io.netty.channel.socket.nio.NioServerSocketChannel;
|
| 22 | import io.netty.channel.socket.nio.NioSocketChannel;
|
| 23 | import java.util.concurrent.ThreadFactory;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public abstract class EventLoopGroupHolder {
|
| 27 | private static final EventLoopGroupHolder NIO = new EventLoopGroupHolder("NIO", NioSocketChannel.class, NioServerSocketChannel.class) {
|
| 28 | @Override
|
| 29 | protected IoHandlerFactory ioHandlerFactory() {
|
| 30 | return NioIoHandler.newFactory();
|
| 31 | }
|
| 32 | };
|
| 33 | private static final EventLoopGroupHolder EPOLL = new EventLoopGroupHolder("Epoll", EpollSocketChannel.class, EpollServerSocketChannel.class) {
|
| 34 | @Override
|
| 35 | protected IoHandlerFactory ioHandlerFactory() {
|
| 36 | return EpollIoHandler.newFactory();
|
| 37 | }
|
| 38 | };
|
| 39 | private static final EventLoopGroupHolder KQUEUE = new EventLoopGroupHolder("Kqueue", KQueueSocketChannel.class, KQueueServerSocketChannel.class) {
|
| 40 | @Override
|
| 41 | protected IoHandlerFactory ioHandlerFactory() {
|
| 42 | return KQueueIoHandler.newFactory();
|
| 43 | }
|
| 44 | };
|
| 45 | private static final EventLoopGroupHolder LOCAL = new EventLoopGroupHolder("Local", LocalChannel.class, LocalServerChannel.class) {
|
| 46 | @Override
|
| 47 | protected IoHandlerFactory ioHandlerFactory() {
|
| 48 | return LocalIoHandler.newFactory();
|
| 49 | }
|
| 50 | };
|
| 51 | private final String type;
|
| 52 | private final Class<? extends Channel> channelCls;
|
| 53 | private final Class<? extends ServerChannel> serverChannelCls;
|
| 54 | private volatile @Nullable EventLoopGroup group;
|
| 55 |
|
| 56 | public static EventLoopGroupHolder remote(final boolean allowNativeTransport) {
|
| 57 | if (allowNativeTransport) {
|
| 58 | if (KQueue.isAvailable()) {
|
| 59 | return KQUEUE;
|
| 60 | }
|
| 61 |
|
| 62 | if (Epoll.isAvailable()) {
|
| 63 | return EPOLL;
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | return NIO;
|
| 68 | }
|
| 69 |
|
| 70 | public static EventLoopGroupHolder local() {
|
| 71 | return LOCAL;
|
| 72 | }
|
| 73 |
|
| 74 | private EventLoopGroupHolder(final String type, final Class<? extends Channel> channelCls, final Class<? extends ServerChannel> serverChannelCls) {
|
| 75 | this.type = type;
|
| 76 | this.channelCls = channelCls;
|
| 77 | this.serverChannelCls = serverChannelCls;
|
| 78 | }
|
| 79 |
|
| 80 | private ThreadFactory createThreadFactory() {
|
| 81 | return new ThreadFactoryBuilder().setNameFormat("Netty " + this.type + " IO #%d").setDaemon(true).build();
|
| 82 | }
|
| 83 |
|
| 84 | protected abstract IoHandlerFactory ioHandlerFactory();
|
| 85 |
|
| 86 | private EventLoopGroup createEventLoopGroup() {
|
| 87 | return new MultiThreadIoEventLoopGroup(this.createThreadFactory(), this.ioHandlerFactory());
|
| 88 | }
|
| 89 |
|
| 90 | public EventLoopGroup eventLoopGroup() {
|
| 91 | EventLoopGroup result = this.group;
|
| 92 | if (result == null) {
|
| 93 | synchronized (this) {
|
| 94 | result = this.group;
|
| 95 | if (result == null) {
|
| 96 | result = this.createEventLoopGroup();
|
| 97 | this.group = result;
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | return result;
|
| 103 | }
|
| 104 |
|
| 105 | public Class<? extends Channel> channelCls() {
|
| 106 | return this.channelCls;
|
| 107 | }
|
| 108 |
|
| 109 | public Class<? extends ServerChannel> serverChannelCls() {
|
| 110 | return this.serverChannelCls;
|
| 111 | }
|
| 112 | }
|
| 113 |
|