Hold messages for auto protocol detector, fixes #65, 0.2.14

This commit is contained in:
creeper123123321 2020-10-17 16:10:51 -03:00
parent a1d621f351
commit a754427707
5 changed files with 115 additions and 16 deletions

View File

@ -94,8 +94,8 @@ Adding [ViaBackwards](https://viaversion.com/backwards) (and optionally [ViaRewi
**How to use protocol auto detector?:**
- For using globally, set the protocol to AUTO or -2. For using in a specific server: ddns.example.com._v-2.viafabric
- The protocol auto detector will try to ping with the client native protocol version so if you have ViaVersion or similar in the server it will use the translator, differently than multiconnect which uses -1 version, which may get the native server version.
- It may block the network thread up to 1 second.
- The versions are cached for 100 seconds.
- It may hold your handshake for up to 10 seconds.
- The results are cached for 100 seconds.
**How can I set the version for specific servers?:**

View File

@ -21,7 +21,7 @@ val branch = if (!travisBranch.isNullOrBlank()) travisBranch else try {
"unknown"
}
version = "0.2.13-SNAPSHOT+" + try {
version = "0.2.14-SNAPSHOT+" + try {
gitVersion() + "-" + branch
} catch (e: Exception) {
"unknown"

View File

@ -0,0 +1,103 @@
/*
* MIT License
*
* Copyright (c) 2018- creeper123123321 <https://creeper123123321.keybase.pub/>
* Copyright (c) 2019- contributors <https://github.com/ViaVersion/ViaFabric/graphs/contributors>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.creeper123123321.viafabric.handler.clientside;
import com.github.creeper123123321.viafabric.ViaFabric;
import com.github.creeper123123321.viafabric.service.ProtocolAutoDetector;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import us.myles.ViaVersion.api.Pair;
import java.net.InetSocketAddress;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.ExecutionException;
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) {
try {
ScheduledFuture<?> timeoutRun = ctx.executor().schedule(() -> {
ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in "
+ ctx.channel().remoteAddress() + " server");
hold = false;
ctx.pipeline().remove(this);
}, 10, TimeUnit.SECONDS);
ProtocolAutoDetector.SERVER_VER.get(((InetSocketAddress) ctx.channel().remoteAddress()))
.whenComplete((obj, ex) -> {
hold = false;
ctx.pipeline().remove(this);
timeoutRun.cancel(false);
});
// Let's cache it before we need it
} catch (ExecutionException e) {
ViaFabric.JLOGGER.warning("Protocol auto detector error: " + e);
}
}
}
@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;
}
}
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);
}
}

View File

@ -27,11 +27,11 @@ package com.github.creeper123123321.viafabric.mixin.client;
import com.github.creeper123123321.viafabric.ViaFabric;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import com.github.creeper123123321.viafabric.handler.clientside.ProtocolDetectionHandler;
import com.github.creeper123123321.viafabric.handler.clientside.VRDecodeHandler;
import com.github.creeper123123321.viafabric.handler.clientside.VREncodeHandler;
import com.github.creeper123123321.viafabric.platform.VRClientSideUserConnection;
import com.github.creeper123123321.viafabric.protocol.ViaFabricHostnameProtocol;
import com.github.creeper123123321.viafabric.service.ProtocolAutoDetector;
import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;
import org.spongepowered.asm.mixin.Mixin;
@ -41,8 +41,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
import java.util.concurrent.ExecutionException;
@Mixin(targets = "net.minecraft.network.ClientConnection$5")
public class MixinClientConnectionChInit {
@Inject(method = "initChannel", at = @At(value = "TAIL"), remap = false)
@ -54,13 +52,8 @@ public class MixinClientConnectionChInit {
channel.pipeline()
.addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new VREncodeHandler(user))
.addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new VRDecodeHandler(user));
if (channel.remoteAddress() != null) {
try {
ProtocolAutoDetector.SERVER_VER.get(((SocketChannel) channel).remoteAddress()); // Let's cache it before we need it, and hope we'll not block netty thread
} catch (ExecutionException e) {
ViaFabric.JLOGGER.warning("Protocol auto detector error: " + e);
}
if (ViaFabric.config.isClientSideEnabled()) {
channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler());
}
}
}

View File

@ -49,7 +49,6 @@ import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
public class VRVersionProvider extends VersionProvider {
@ -102,8 +101,12 @@ public class VRVersionProvider extends VersionProvider {
try {
if (serverVer == -2) {
// sadly we'll need to block netty thread, we'll need to be fast
serverVer = ProtocolAutoDetector.SERVER_VER.get((InetSocketAddress) addr).get(1, TimeUnit.SECONDS).getId();
// Hope protocol was autodetected
ProtocolVersion autoVer =
ProtocolAutoDetector.SERVER_VER.get((InetSocketAddress) addr).getNow(null);
if (autoVer != null) {
serverVer = autoVer.getId();
}
}
} catch (Exception e) {
ViaFabric.JLOGGER.warning("Couldn't auto detect: " + e);