mirror of
https://github.com/ViaVersion/ViaFabric.git
synced 2024-11-21 11:35:16 +01:00
remove ProtocolDetectionHandler
This commit is contained in:
parent
db97205169
commit
108268e8a6
@ -1,2 +1,2 @@
|
||||
# rip my ram
|
||||
org.gradle.jvmargs=-Xmx512M
|
||||
org.gradle.jvmargs=-Xms32M -Xmx512M
|
||||
|
@ -1,79 +0,0 @@
|
||||
package com.viaversion.fabric.mc114.handler.clientside;
|
||||
|
||||
import com.viaversion.fabric.mc114.ViaFabric;
|
||||
import com.viaversion.fabric.mc114.service.ProtocolAutoDetector;
|
||||
import com.viaversion.viaversion.util.Pair;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ProtocolDetectionHandler extends ChannelDuplexHandler {
|
||||
private final Queue<Pair<Object, ChannelPromise>> queuedMessages = new ArrayDeque<>();
|
||||
private boolean hold = true;
|
||||
private boolean pendentFlush;
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
if (ctx.channel().remoteAddress() instanceof InetSocketAddress) {
|
||||
ScheduledFuture<?> timeoutRun = ctx.executor().schedule(() -> {
|
||||
ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in "
|
||||
+ ctx.channel().remoteAddress() + " server");
|
||||
hold = false;
|
||||
drainQueue(ctx);
|
||||
ctx.pipeline().remove(this);
|
||||
}, 10, TimeUnit.SECONDS);
|
||||
ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress()))
|
||||
.whenComplete((obj, ex) -> {
|
||||
ctx.pipeline().remove(this);
|
||||
timeoutRun.cancel(false);
|
||||
});
|
||||
// Let's cache it before we need it
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.write(ctx, msg, promise);
|
||||
} else {
|
||||
queuedMessages.add(new Pair<>(msg, promise));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush(ChannelHandlerContext ctx) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.flush(ctx);
|
||||
} else {
|
||||
pendentFlush = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
private void drainQueue(ChannelHandlerContext ctx) {
|
||||
queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue()));
|
||||
queuedMessages.clear();
|
||||
if (pendentFlush) ctx.flush();
|
||||
pendentFlush = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.handlerRemoved(ctx);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.viaversion.fabric.mc114.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.mc114.ViaFabric;
|
||||
import com.viaversion.fabric.mc114.service.ProtocolAutoDetector;
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@Mixin(ClientConnection.class)
|
||||
public class MixinClientConnection {
|
||||
@Inject(method = "connect", at = @At("HEAD"))
|
||||
private static void onConnect(InetAddress address, int port, boolean shouldUseNativeTransport, CallbackInfoReturnable<ClientConnection> cir) {
|
||||
try {
|
||||
if (!ViaFabric.config.isClientSideEnabled()) return;
|
||||
ProtocolAutoDetector.detectVersion(new InetSocketAddress(address, port)).get(10, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,7 @@ package com.viaversion.fabric.mc114.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.common.handler.FabricDecodeHandler;
|
||||
import com.viaversion.fabric.common.handler.FabricEncodeHandler;
|
||||
import com.viaversion.fabric.mc114.ViaFabric;
|
||||
import com.viaversion.fabric.common.handler.CommonTransformer;
|
||||
import com.viaversion.fabric.mc114.handler.clientside.ProtocolDetectionHandler;
|
||||
import com.viaversion.fabric.common.protocol.HostnameParserProtocol;
|
||||
import com.viaversion.viaversion.connection.UserConnectionImpl;
|
||||
import com.viaversion.viaversion.protocol.ProtocolPipelineImpl;
|
||||
@ -27,9 +25,6 @@ public class MixinClientConnectionChInit {
|
||||
channel.pipeline()
|
||||
.addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user))
|
||||
.addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user));
|
||||
if (ViaFabric.config.isClientSideEnabled()) {
|
||||
channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
"MixinServerNetworkIoChInit"
|
||||
],
|
||||
"client": [
|
||||
"client.MixinClientConnection",
|
||||
"client.MixinClientConnectionChInit"
|
||||
],
|
||||
"injectors": {
|
||||
|
@ -1,79 +0,0 @@
|
||||
package com.viaversion.fabric.mc115.handler.clientside;
|
||||
|
||||
import com.viaversion.fabric.mc115.ViaFabric;
|
||||
import com.viaversion.fabric.mc115.service.ProtocolAutoDetector;
|
||||
import com.viaversion.viaversion.util.Pair;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ProtocolDetectionHandler extends ChannelDuplexHandler {
|
||||
private final Queue<Pair<Object, ChannelPromise>> queuedMessages = new ArrayDeque<>();
|
||||
private boolean hold = true;
|
||||
private boolean pendentFlush;
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
if (ctx.channel().remoteAddress() instanceof InetSocketAddress) {
|
||||
ScheduledFuture<?> timeoutRun = ctx.executor().schedule(() -> {
|
||||
ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in "
|
||||
+ ctx.channel().remoteAddress() + " server");
|
||||
hold = false;
|
||||
drainQueue(ctx);
|
||||
ctx.pipeline().remove(this);
|
||||
}, 10, TimeUnit.SECONDS);
|
||||
ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress()))
|
||||
.whenComplete((obj, ex) -> {
|
||||
ctx.pipeline().remove(this);
|
||||
timeoutRun.cancel(false);
|
||||
});
|
||||
// Let's cache it before we need it
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.write(ctx, msg, promise);
|
||||
} else {
|
||||
queuedMessages.add(new Pair<>(msg, promise));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush(ChannelHandlerContext ctx) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.flush(ctx);
|
||||
} else {
|
||||
pendentFlush = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
private void drainQueue(ChannelHandlerContext ctx) {
|
||||
queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue()));
|
||||
queuedMessages.clear();
|
||||
if (pendentFlush) ctx.flush();
|
||||
pendentFlush = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.handlerRemoved(ctx);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.viaversion.fabric.mc115.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.mc115.ViaFabric;
|
||||
import com.viaversion.fabric.mc115.service.ProtocolAutoDetector;
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@Mixin(ClientConnection.class)
|
||||
public class MixinClientConnection {
|
||||
@Inject(method = "connect", at = @At("HEAD"))
|
||||
private static void onConnect(InetAddress address, int port, boolean shouldUseNativeTransport, CallbackInfoReturnable<ClientConnection> cir) {
|
||||
try {
|
||||
if (!ViaFabric.config.isClientSideEnabled()) return;
|
||||
ProtocolAutoDetector.detectVersion(new InetSocketAddress(address, port)).get(10, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,7 @@ package com.viaversion.fabric.mc115.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.common.handler.FabricDecodeHandler;
|
||||
import com.viaversion.fabric.common.handler.FabricEncodeHandler;
|
||||
import com.viaversion.fabric.mc115.ViaFabric;
|
||||
import com.viaversion.fabric.common.handler.CommonTransformer;
|
||||
import com.viaversion.fabric.mc115.handler.clientside.ProtocolDetectionHandler;
|
||||
import com.viaversion.fabric.common.protocol.HostnameParserProtocol;
|
||||
import com.viaversion.viaversion.connection.UserConnectionImpl;
|
||||
import com.viaversion.viaversion.protocol.ProtocolPipelineImpl;
|
||||
@ -27,9 +25,6 @@ public class MixinClientConnectionChInit {
|
||||
channel.pipeline()
|
||||
.addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user))
|
||||
.addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user));
|
||||
if (ViaFabric.config.isClientSideEnabled()) {
|
||||
channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
"MixinServerNetworkIoChInit"
|
||||
],
|
||||
"client": [
|
||||
"client.MixinClientConnection",
|
||||
"client.MixinClientConnectionChInit"
|
||||
],
|
||||
"injectors": {
|
||||
|
@ -1,79 +0,0 @@
|
||||
package com.viaversion.fabric.mc116.handler.clientside;
|
||||
|
||||
import com.viaversion.fabric.mc116.ViaFabric;
|
||||
import com.viaversion.fabric.mc116.service.ProtocolAutoDetector;
|
||||
import com.viaversion.viaversion.util.Pair;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ProtocolDetectionHandler extends ChannelDuplexHandler {
|
||||
private final Queue<Pair<Object, ChannelPromise>> queuedMessages = new ArrayDeque<>();
|
||||
private boolean hold = true;
|
||||
private boolean pendentFlush;
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
if (ctx.channel().remoteAddress() instanceof InetSocketAddress) {
|
||||
ScheduledFuture<?> timeoutRun = ctx.executor().schedule(() -> {
|
||||
ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in "
|
||||
+ ctx.channel().remoteAddress() + " server");
|
||||
hold = false;
|
||||
drainQueue(ctx);
|
||||
ctx.pipeline().remove(this);
|
||||
}, 10, TimeUnit.SECONDS);
|
||||
ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress()))
|
||||
.whenComplete((obj, ex) -> {
|
||||
ctx.pipeline().remove(this);
|
||||
timeoutRun.cancel(false);
|
||||
});
|
||||
// Let's cache it before we need it
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.write(ctx, msg, promise);
|
||||
} else {
|
||||
queuedMessages.add(new Pair<>(msg, promise));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush(ChannelHandlerContext ctx) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.flush(ctx);
|
||||
} else {
|
||||
pendentFlush = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
private void drainQueue(ChannelHandlerContext ctx) {
|
||||
queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue()));
|
||||
queuedMessages.clear();
|
||||
if (pendentFlush) ctx.flush();
|
||||
pendentFlush = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.handlerRemoved(ctx);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.viaversion.fabric.mc116.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.mc116.ViaFabric;
|
||||
import com.viaversion.fabric.mc116.service.ProtocolAutoDetector;
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@Mixin(ClientConnection.class)
|
||||
public class MixinClientConnection {
|
||||
@Inject(method = "connect", at = @At("HEAD"))
|
||||
private static void onConnect(InetAddress address, int port, boolean shouldUseNativeTransport, CallbackInfoReturnable<ClientConnection> cir) {
|
||||
try {
|
||||
if (!ViaFabric.config.isClientSideEnabled()) return;
|
||||
ProtocolAutoDetector.detectVersion(new InetSocketAddress(address, port)).get(10, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,7 @@ package com.viaversion.fabric.mc116.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.common.handler.FabricDecodeHandler;
|
||||
import com.viaversion.fabric.common.handler.FabricEncodeHandler;
|
||||
import com.viaversion.fabric.mc116.ViaFabric;
|
||||
import com.viaversion.fabric.common.handler.CommonTransformer;
|
||||
import com.viaversion.fabric.mc116.handler.clientside.ProtocolDetectionHandler;
|
||||
import com.viaversion.fabric.common.protocol.HostnameParserProtocol;
|
||||
import com.viaversion.viaversion.connection.UserConnectionImpl;
|
||||
import com.viaversion.viaversion.protocol.ProtocolPipelineImpl;
|
||||
@ -27,9 +25,6 @@ public class MixinClientConnectionChInit {
|
||||
channel.pipeline()
|
||||
.addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user))
|
||||
.addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user));
|
||||
if (ViaFabric.config.isClientSideEnabled()) {
|
||||
channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
"MixinServerNetworkIoChInit"
|
||||
],
|
||||
"client": [
|
||||
"client.MixinClientConnection",
|
||||
"client.MixinClientConnectionChInit"
|
||||
],
|
||||
"injectors": {
|
||||
|
@ -1,79 +0,0 @@
|
||||
package com.viaversion.fabric.mc117.handler.clientside;
|
||||
|
||||
import com.viaversion.fabric.mc117.ViaFabric;
|
||||
import com.viaversion.fabric.mc117.service.ProtocolAutoDetector;
|
||||
import com.viaversion.viaversion.util.Pair;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ProtocolDetectionHandler extends ChannelDuplexHandler {
|
||||
private final Queue<Pair<Object, ChannelPromise>> queuedMessages = new ArrayDeque<>();
|
||||
private boolean hold = true;
|
||||
private boolean pendentFlush;
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
if (ctx.channel().remoteAddress() instanceof InetSocketAddress) {
|
||||
ScheduledFuture<?> timeoutRun = ctx.executor().schedule(() -> {
|
||||
ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in "
|
||||
+ ctx.channel().remoteAddress() + " server");
|
||||
hold = false;
|
||||
drainQueue(ctx);
|
||||
ctx.pipeline().remove(this);
|
||||
}, 10, TimeUnit.SECONDS);
|
||||
ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress()))
|
||||
.whenComplete((obj, ex) -> {
|
||||
ctx.pipeline().remove(this);
|
||||
timeoutRun.cancel(false);
|
||||
});
|
||||
// Let's cache it before we need it
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.write(ctx, msg, promise);
|
||||
} else {
|
||||
queuedMessages.add(new Pair<>(msg, promise));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush(ChannelHandlerContext ctx) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.flush(ctx);
|
||||
} else {
|
||||
pendentFlush = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
private void drainQueue(ChannelHandlerContext ctx) {
|
||||
queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue()));
|
||||
queuedMessages.clear();
|
||||
if (pendentFlush) ctx.flush();
|
||||
pendentFlush = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.handlerRemoved(ctx);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.viaversion.fabric.mc117.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.mc117.ViaFabric;
|
||||
import com.viaversion.fabric.mc117.service.ProtocolAutoDetector;
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@Mixin(ClientConnection.class)
|
||||
public class MixinClientConnection {
|
||||
@Inject(method = "connect", at = @At("HEAD"))
|
||||
private static void onConnect(InetSocketAddress address, boolean useEpoll, CallbackInfoReturnable<ClientConnection> cir) {
|
||||
try {
|
||||
if (!ViaFabric.config.isClientSideEnabled()) return;
|
||||
ProtocolAutoDetector.detectVersion(address).get(10, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,7 @@ package com.viaversion.fabric.mc117.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.common.handler.FabricDecodeHandler;
|
||||
import com.viaversion.fabric.common.handler.FabricEncodeHandler;
|
||||
import com.viaversion.fabric.mc117.ViaFabric;
|
||||
import com.viaversion.fabric.common.handler.CommonTransformer;
|
||||
import com.viaversion.fabric.mc117.handler.clientside.ProtocolDetectionHandler;
|
||||
import com.viaversion.fabric.common.protocol.HostnameParserProtocol;
|
||||
import com.viaversion.viaversion.connection.UserConnectionImpl;
|
||||
import com.viaversion.viaversion.protocol.ProtocolPipelineImpl;
|
||||
@ -27,9 +25,6 @@ public class MixinClientConnectionChInit {
|
||||
channel.pipeline()
|
||||
.addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user))
|
||||
.addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user));
|
||||
if (ViaFabric.config.isClientSideEnabled()) {
|
||||
channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
"MixinServerNetworkIoChInit"
|
||||
],
|
||||
"client": [
|
||||
"client.MixinClientConnection",
|
||||
"client.MixinClientConnectionChInit"
|
||||
],
|
||||
"injectors": {
|
||||
|
@ -1,79 +0,0 @@
|
||||
package com.viaversion.fabric.mc18.handler.clientside;
|
||||
|
||||
import com.viaversion.fabric.mc18.ViaFabric;
|
||||
import com.viaversion.fabric.mc18.service.ProtocolAutoDetector;
|
||||
import com.viaversion.viaversion.util.Pair;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ProtocolDetectionHandler extends ChannelDuplexHandler {
|
||||
private final Queue<Pair<Object, ChannelPromise>> queuedMessages = new ArrayDeque<>();
|
||||
private boolean hold = true;
|
||||
private boolean pendentFlush;
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
if (ctx.channel().remoteAddress() instanceof InetSocketAddress) {
|
||||
ScheduledFuture<?> timeoutRun = ctx.executor().schedule(() -> {
|
||||
ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in "
|
||||
+ ctx.channel().remoteAddress() + " server");
|
||||
hold = false;
|
||||
drainQueue(ctx);
|
||||
ctx.pipeline().remove(this);
|
||||
}, 10, TimeUnit.SECONDS);
|
||||
ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress()))
|
||||
.whenComplete((obj, ex) -> {
|
||||
ctx.pipeline().remove(this);
|
||||
timeoutRun.cancel(false);
|
||||
});
|
||||
// Let's cache it before we need it
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.write(ctx, msg, promise);
|
||||
} else {
|
||||
queuedMessages.add(new Pair<>(msg, promise));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush(ChannelHandlerContext ctx) throws Exception {
|
||||
if (!hold) {
|
||||
drainQueue(ctx);
|
||||
super.flush(ctx);
|
||||
} else {
|
||||
pendentFlush = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
private void drainQueue(ChannelHandlerContext ctx) {
|
||||
queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue()));
|
||||
queuedMessages.clear();
|
||||
if (pendentFlush) ctx.flush();
|
||||
pendentFlush = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
||||
drainQueue(ctx);
|
||||
super.handlerRemoved(ctx);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.viaversion.fabric.mc18.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.mc18.ViaFabric;
|
||||
import com.viaversion.fabric.mc18.service.ProtocolAutoDetector;
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@Mixin(ClientConnection.class)
|
||||
public class MixinClientConnection {
|
||||
@Inject(method = "connect", at = @At("HEAD"))
|
||||
private static void onConnect(InetAddress address, int port, boolean shouldUseNativeTransport, CallbackInfoReturnable<ClientConnection> cir) {
|
||||
try {
|
||||
if (!ViaFabric.config.isClientSideEnabled()) return;
|
||||
ProtocolAutoDetector.detectVersion(new InetSocketAddress(address, port)).get(10, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,7 @@ package com.viaversion.fabric.mc18.mixin.pipeline.client;
|
||||
|
||||
import com.viaversion.fabric.common.handler.FabricDecodeHandler;
|
||||
import com.viaversion.fabric.common.handler.FabricEncodeHandler;
|
||||
import com.viaversion.fabric.mc18.ViaFabric;
|
||||
import com.viaversion.fabric.common.handler.CommonTransformer;
|
||||
import com.viaversion.fabric.mc18.handler.clientside.ProtocolDetectionHandler;
|
||||
import com.viaversion.fabric.common.protocol.HostnameParserProtocol;
|
||||
import com.viaversion.viaversion.connection.UserConnectionImpl;
|
||||
import com.viaversion.viaversion.protocol.ProtocolPipelineImpl;
|
||||
@ -27,9 +25,6 @@ public class MixinClientConnectionChInit {
|
||||
channel.pipeline()
|
||||
.addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user))
|
||||
.addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user));
|
||||
if (ViaFabric.config.isClientSideEnabled()) {
|
||||
channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
"MixinServerNetworkIoChInit"
|
||||
],
|
||||
"client": [
|
||||
"client.MixinClientConnection",
|
||||
"client.MixinClientConnectionChInit"
|
||||
],
|
||||
"injectors": {
|
||||
|
Loading…
Reference in New Issue
Block a user