package net.minecraft.network;
import com.google.common.collect.Queues;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import why.noctra.utils.client.ClientUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.local.LocalChannel;
import io.netty.channel.local.LocalServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.TimeoutException;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.util.Queue;
import java.util.logging.Level;
import javax.annotation.Nullable;
import javax.crypto.Cipher;
import net.minecraft.network.login.ServerLoginNetHandler;
import net.minecraft.network.play.ServerPlayNetHandler;
import net.minecraft.network.play.server.SDisconnectPacket;
import net.minecraft.network.play.server.SUpdateBossInfoPacket;
import net.minecraft.util.LazyValue;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import org.apache.commons.lang3.Validate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import why.noctra.Noctra;
import why.noctra.events.EventPacket;
public class NetworkManager extends SimpleChannelInboundHandler<IPacket<?>> {
private static final Logger LOGGER = LogManager.getLogger();
public static final Marker NETWORK_MARKER = MarkerManager.getMarker("NETWORK");
public static final Marker NETWORK_PACKETS_MARKER = MarkerManager.getMarker("NETWORK_PACKETS", NETWORK_MARKER);
public static final AttributeKey<ProtocolType> PROTOCOL_ATTRIBUTE_KEY = AttributeKey.valueOf("protocol");
public static final LazyValue<NioEventLoopGroup> CLIENT_NIO_EVENTLOOP = new LazyValue<>(() ->
new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Client IO #%d").setDaemon(true).build())
);
public static final LazyValue<EpollEventLoopGroup> CLIENT_EPOLL_EVENTLOOP = new LazyValue<>(() ->
new EpollEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Epoll Client IO #%d").setDaemon(true).build())
);
public static final LazyValue<DefaultEventLoopGroup> CLIENT_LOCAL_EVENTLOOP = new LazyValue<>(() ->
new DefaultEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Local Client IO #%d").setDaemon(true).build())
);
private final PacketDirection direction;
private final Queue<QueuedPacket> outboundPacketsQueue = Queues.newConcurrentLinkedQueue();
private Channel channel;
private SocketAddress socketAddress;
private INetHandler packetListener;
private ITextComponent terminationReason;
private boolean isEncrypted;
private boolean disconnected;
private int field_211394_q;
private int field_211395_r;
private float field_211396_s;
private float field_211397_t;
private int ticks;
private boolean field_211399_v;
public NetworkManager(PacketDirection packetDirection) {
this.direction = packetDirection;
}
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
this.channel = ctx.channel();
this.socketAddress = this.channel.remoteAddress();
try {
this.setConnectionState(ProtocolType.HANDSHAKING);
} catch (Throwable throwable) {
LOGGER.fatal(throwable);
}
}
public void sendPacketWithoutEvent(IPacket<?> packetIn) {
this.sendPacketWithoutEvent(packetIn, null);
}
public void sendPacketWithoutEvent(IPacket<?> packetIn, @Nullable GenericFutureListener<? extends Future<? super Void>> listener) {
if (this.isChannelOpen()) {
this.flushOutboundQueue();
this.dispatchPacket(packetIn, listener);
} else {
this.outboundPacketsQueue.add(new QueuedPacket(packetIn, listener));
}
}
public void setConnectionState(ProtocolType newState) {
this.channel.attr(PROTOCOL_ATTRIBUTE_KEY).set(newState);
this.channel.config().setAutoRead(true);
LOGGER.debug("Enabled auto read");
}
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
this.closeChannel(new TranslationTextComponent("disconnect.endOfStream"));
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable t) {
if (t instanceof SkipableEncoderException) {
LOGGER.debug("Skipping packet due to errors", t.getCause());
} else {
boolean flag = !this.field_211399_v;
this.field_211399_v = true;
if (this.channel.isOpen()) {
if (t instanceof TimeoutException) {
LOGGER.debug("Timeout", t);
this.closeChannel(new TranslationTextComponent("disconnect.timeout"));
} else {
ITextComponent msg = new TranslationTextComponent("disconnect.genericReason", "Internal Exception: " + t);
if (flag) {
LOGGER.debug("Failed to sent packet", t);
this.sendPacket(new SDisconnectPacket(msg), f -> this.closeChannel(msg));
this.disableAutoRead();
} else {
LOGGER.debug("Double fault", t);
this.closeChannel(msg);
}
}
}
}
}
EventPacket receive = new EventPacket(null, EventPacket.Type.RECEIVE);
protected void channelRead0(ChannelHandlerContext ctx, IPacket<?> packet) throws Exception {
if (packet instanceof SUpdateBossInfoPacket p) {
ClientUtil.updateBossInfo(p);
}
if (this.channel.isOpen()) {
receive.setPacket(packet);
Noctra.getInstance().getEventBus().post(receive);
if (receive.isCancel()) {
receive.open();
return;
}
try {
processPacket(receive.getPacket(), this.packetListener);
} catch (ThreadQuickExitException ignored) {}
++this.field_211394_q;
}
}
public static <T extends INetHandler> void processPacket(IPacket<T> packet, INetHandler handler) {
packet.processPacket((T) handler);
}
public void setNetHandler(INetHandler handler) {
Validate.notNull(handler, "packetListener");
this.packetListener = handler;
}
public void sendPacket(IPacket<?> packetIn) {
this.sendPacket(packetIn, null);
}
EventPacket send = new EventPacket(null, EventPacket.Type.SEND);
public void sendPacket(IPacket<?> packetIn, @Nullable GenericFutureListener<? extends Future<? super Void>> listener) {
send.setPacket(packetIn);
Noctra.getInstance().getEventBus().post(send);
if (send.isCancel()) {
send.open();
return;
}
if (this.isChannelOpen()) {
this.flushOutboundQueue();
this.dispatchPacket(send.getPacket(), listener);
} else {
this.outboundPacketsQueue.add(new QueuedPacket(send.getPacket(), listener));
}
}
private void dispatchPacket(IPacket<?> packet, @Nullable GenericFutureListener<? extends Future<? super Void>> listener) {
ProtocolType p1 = ProtocolType.getFromPacket(packet);
ProtocolType p2 = this.channel.attr(PROTOCOL_ATTRIBUTE_KEY).get();
++this.field_211395_r;
if (p2 != p1) {
this.channel.config().setAutoRead(false);
}
if (this.channel.eventLoop().inEventLoop()) {
if (p1 != p2) this.setConnectionState(p1);
ChannelFuture f = this.channel.writeAndFlush(packet);
if (listener != null) f.addListener(listener);
f.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
} else {
this.channel.eventLoop().execute(() -> {
if (p1 != p2) this.setConnectionState(p1);
ChannelFuture f = this.channel.writeAndFlush(packet);
if (listener != null) f.addListener(listener);
f.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
});
}
}
private void flushOutboundQueue() {
if (this.channel != null && this.channel.isOpen()) {
synchronized (this.outboundPacketsQueue) {
QueuedPacket qp;
while ((qp = this.outboundPacketsQueue.poll()) != null) {
this.dispatchPacket(qp.packet, qp.field_201049_b);
}
}
}
}
public void tick() {
this.flushOutboundQueue();
if (this.packetListener instanceof ServerLoginNetHandler)
((ServerLoginNetHandler) this.packetListener).tick();
if (this.packetListener instanceof ServerPlayNetHandler)
((ServerPlayNetHandler) this.packetListener).tick();
if (this.channel != null) this.channel.flush();
if (this.ticks++ % 20 == 0) this.func_241877_b();
}
protected void func_241877_b() {
this.field_211397_t = MathHelper.lerp(0.75F, (float)this.field_211395_r, this.field_211397_t);
this.field_211396_s = MathHelper.lerp(0.75F, (float)this.field_211394_q, this.field_211396_s);
this.field_211395_r = 0;
this.field_211394_q = 0;
}
public SocketAddress getRemoteAddress() {
return this.socketAddress;
}
public void closeChannel(ITextComponent message) {
if (this.channel.isOpen()) {
this.channel.close().awaitUninterruptibly();
this.terminationReason = message;
}
}
public boolean isLocalChannel() {
return this.channel instanceof LocalChannel || this.channel instanceof LocalServerChannel;
}
public static NetworkManager createNetworkManagerAndConnect(InetAddress address, int port, boolean useNativeTransport) {
final NetworkManager nm = new NetworkManager(PacketDirection.CLIENTBOUND);
Class<? extends SocketChannel> ch;
LazyValue<? extends EventLoopGroup> loop;
if (Epoll.isAvailable() && useNativeTransport) {
ch = EpollSocketChannel.class;
loop = CLIENT_EPOLL_EVENTLOOP;
} else {
ch = NioSocketChannel.class;
loop = CLIENT_NIO_EVENTLOOP;
}
new Bootstrap().group(loop.getValue()).handler(new ChannelInitializer<Channel>() {
protected void initChannel(Channel channel) {
try {
channel.config().setOption(ChannelOption.TCP_NODELAY, true);
} catch (ChannelException ignored) {}
channel.pipeline()
.addLast("timeout", new ReadTimeoutHandler(30))
.addLast("splitter", new NettyVarint21FrameDecoder())
.addLast("decoder", new NettyPacketDecoder(PacketDirection.CLIENTBOUND))
.addLast("prepender", new NettyVarint21FrameEncoder())
.addLast("encoder", new NettyPacketEncoder(PacketDirection.SERVERBOUND))
.addLast("packet_handler", nm);
}
}).channel(ch).connect(address, port).syncUninterruptibly();
return nm;
}
public static NetworkManager provideLocalClient(SocketAddress address) {
final NetworkManager nm = new NetworkManager(PacketDirection.CLIENTBOUND);
new Bootstrap().group(CLIENT_LOCAL_EVENTLOOP.getValue()).handler(new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) {
ch.pipeline().addLast("packet_handler", nm);
}
}).channel(LocalChannel.class).connect(address).syncUninterruptibly();
return nm;
}
public void func_244777_a(Cipher c1, Cipher c2) {
this.isEncrypted = true;
this.channel.pipeline().addBefore("splitter", "decrypt", new NettyEncryptingDecoder(c1));
this.channel.pipeline().addBefore("prepender", "encrypt", new NettyEncryptingEncoder(c2));
}
public boolean isEncrypted() {
return this.isEncrypted;
}
public boolean isChannelOpen() {
return this.channel != null && this.channel.isOpen();
}
public boolean hasNoChannel() {
return this.channel == null;
}
public INetHandler getNetHandler() {
return this.packetListener;
}
@Nullable
public ITextComponent getExitMessage() {
return this.terminationReason;
}
public void disableAutoRead() {
this.channel.config().setAutoRead(false);
}
public void setCompressionThreshold(int threshold) {
if (threshold >= 0) {
if (this.channel.pipeline().get("decompress") instanceof NettyCompressionDecoder)
((NettyCompressionDecoder)this.channel.pipeline().get("decompress")).setCompressionThreshold(threshold);
else
this.channel.pipeline().addBefore("decoder", "decompress", new NettyCompressionDecoder(threshold));
if (this.channel.pipeline().get("compress") instanceof NettyCompressionEncoder)
((NettyCompressionEncoder)this.channel.pipeline().get("compress")).setCompressionThreshold(threshold);
else
this.channel.pipeline().addBefore("encoder", "compress", new NettyCompressionEncoder(threshold));
} else {
if (this.channel.pipeline().get("decompress") instanceof NettyCompressionDecoder)
this.channel.pipeline().remove("decompress");
if (this.channel.pipeline().get("compress") instanceof NettyCompressionEncoder)
this.channel.pipeline().remove("compress");
}
}
public void handleDisconnection() {
if (this.channel != null && !this.channel.isOpen()) {
if (this.disconnected) {
LOGGER.warn("handleDisconnection() called twice");
} else {
this.disconnected = true;
if (this.getExitMessage() != null)
this.getNetHandler().onDisconnect(this.getExitMessage());
else if (this.getNetHandler() != null)
this.getNetHandler().onDisconnect(new TranslationTextComponent("multiplayer.disconnect.generic"));
}
}
}
public float getPacketsReceived() {
return this.field_211396_s;
}
public float getPacketsSent() {
return this.field_211397_t;
}
static class QueuedPacket {
private final IPacket<?> packet;
@Nullable
private final GenericFutureListener<? extends Future<? super Void>> field_201049_b;
public QueuedPacket(IPacket<?> p, @Nullable GenericFutureListener<? extends Future<? super Void>> l) {
this.packet = p;
this.field_201049_b = l;
}
}
}