aboutsummaryrefslogtreecommitdiff
path: root/mod/src/main/java/moe/ymc/acron/mixin/ServerNetworkIoMixin.java
blob: f49914ecb8b9602d0811d7cdfd8a07f84efff0d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package moe.ymc.acron.mixin;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.MultithreadEventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import moe.ymc.acron.config.Config;
import moe.ymc.acron.net.AcronInitializer;
import net.minecraft.server.ServerNetworkIo;
import net.minecraft.util.Lazy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;

import static net.minecraft.server.ServerNetworkIo.EPOLL_CHANNEL;

@Mixin(ServerNetworkIo.class)
public class ServerNetworkIoMixin {
    private static final Logger AC_LOGGER = LogManager.getLogger();

    @Shadow
    @Final
    private List<ChannelFuture> channels;

    @Shadow
    @Final
    public static Lazy<NioEventLoopGroup> DEFAULT_CHANNEL;

    @Inject(at = @At("RETURN"), method = "<init>")
    private void init(CallbackInfo info) {
        AC_LOGGER.debug("Adding Acron channel.");
        Lazy<? extends MultithreadEventLoopGroup> group;
        Class<? extends ServerChannel> channel;
        if (Epoll.isAvailable() && Config.getGlobalConfig().useNativeTransport()) {
            channel = EpollServerSocketChannel.class;
            group = EPOLL_CHANNEL;
            AC_LOGGER.info("Using native transport.");
        } else {
            channel = NioServerSocketChannel.class;
            group = DEFAULT_CHANNEL;
            AC_LOGGER.info("Not using native transport due to " +
                    "it is either disabled in acron.json or not available.");
        }
        channels.add(new ServerBootstrap()
                .channel(channel)
                .childHandler(new AcronInitializer())
                .group(group.get())
                .localAddress(Config.getGlobalConfig().address(),
                        Config.getGlobalConfig().port())
                .bind()
                .syncUninterruptibly());

    }
}