mirror of
https://github.com/ViaVersion/ViaFabric.git
synced 2024-12-18 15:47:46 +01:00
Hold messages for auto protocol detector, fixes #65, 0.2.14
This commit is contained in:
parent
748085f8aa
commit
754bdd855d
@ -94,8 +94,8 @@ Adding [ViaBackwards](https://viaversion.com/backwards) (and optionally [ViaRewi
|
|||||||
**How to use protocol auto detector?:**
|
**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
|
- 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.
|
- 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.
|
- It may hold your handshake for up to 10 seconds.
|
||||||
- The versions are cached for 100 seconds.
|
- The results are cached for 100 seconds.
|
||||||
|
|
||||||
|
|
||||||
**How can I set the version for specific servers?:**
|
**How can I set the version for specific servers?:**
|
||||||
|
@ -20,7 +20,7 @@ val branch = if (!travisBranch.isNullOrBlank()) travisBranch else try {
|
|||||||
"unknown"
|
"unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
version = "0.2.13-SNAPSHOT+" + try {
|
version = "0.2.14-SNAPSHOT+" + try {
|
||||||
gitVersion() + "-" + branch
|
gitVersion() + "-" + branch
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
"unknown"
|
"unknown"
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -27,11 +27,11 @@ package com.github.creeper123123321.viafabric.mixin.client;
|
|||||||
|
|
||||||
import com.github.creeper123123321.viafabric.ViaFabric;
|
import com.github.creeper123123321.viafabric.ViaFabric;
|
||||||
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
|
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.VRDecodeHandler;
|
||||||
import com.github.creeper123123321.viafabric.handler.clientside.VREncodeHandler;
|
import com.github.creeper123123321.viafabric.handler.clientside.VREncodeHandler;
|
||||||
import com.github.creeper123123321.viafabric.platform.VRClientSideUserConnection;
|
import com.github.creeper123123321.viafabric.platform.VRClientSideUserConnection;
|
||||||
import com.github.creeper123123321.viafabric.protocol.ViaFabricHostnameProtocol;
|
import com.github.creeper123123321.viafabric.protocol.ViaFabricHostnameProtocol;
|
||||||
import com.github.creeper123123321.viafabric.service.ProtocolAutoDetector;
|
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
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.data.UserConnection;
|
||||||
import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
|
import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
@Mixin(targets = "net.minecraft.network.ClientConnection$1")
|
@Mixin(targets = "net.minecraft.network.ClientConnection$1")
|
||||||
public class MixinClientConnectionChInit {
|
public class MixinClientConnectionChInit {
|
||||||
@Inject(method = "initChannel", at = @At(value = "TAIL"), remap = false)
|
@Inject(method = "initChannel", at = @At(value = "TAIL"), remap = false)
|
||||||
@ -54,13 +52,8 @@ public class MixinClientConnectionChInit {
|
|||||||
channel.pipeline()
|
channel.pipeline()
|
||||||
.addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new VREncodeHandler(user))
|
.addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new VREncodeHandler(user))
|
||||||
.addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new VRDecodeHandler(user));
|
.addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new VRDecodeHandler(user));
|
||||||
|
if (ViaFabric.config.isClientSideEnabled()) {
|
||||||
if (channel.remoteAddress() != null) {
|
channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler());
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,6 @@ import java.lang.reflect.Method;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class VRVersionProvider extends VersionProvider {
|
public class VRVersionProvider extends VersionProvider {
|
||||||
@ -102,8 +101,12 @@ public class VRVersionProvider extends VersionProvider {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (serverVer == -2) {
|
if (serverVer == -2) {
|
||||||
// sadly we'll need to block netty thread, we'll need to be fast
|
// Hope protocol was autodetected
|
||||||
serverVer = ProtocolAutoDetector.SERVER_VER.get((InetSocketAddress) addr).get(1, TimeUnit.SECONDS).getId();
|
ProtocolVersion autoVer =
|
||||||
|
ProtocolAutoDetector.SERVER_VER.get((InetSocketAddress) addr).getNow(null);
|
||||||
|
if (autoVer != null) {
|
||||||
|
serverVer = autoVer.getId();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ViaFabric.JLOGGER.warning("Couldn't auto detect: " + e);
|
ViaFabric.JLOGGER.warning("Couldn't auto detect: " + e);
|
||||||
|
Loading…
Reference in New Issue
Block a user