Update codec handlers

This commit is contained in:
KennyTV 2020-06-10 09:18:25 +02:00
parent 14ccf796d5
commit 3051b32d07
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
5 changed files with 60 additions and 120 deletions

View File

@ -24,62 +24,7 @@
package com.github.creeper123123321.viafabric.handler; package com.github.creeper123123321.viafabric.handler;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import java.util.function.Function;
// TODO delete this when https://github.com/ViaVersion/ViaVersion/pull/1505 is merged
public class CommonTransformer { public class CommonTransformer {
public static final String HANDLER_DECODER_NAME = "via-decoder"; public static final String HANDLER_DECODER_NAME = "via-decoder";
public static final String HANDLER_ENCODER_NAME = "via-encoder"; public static final String HANDLER_ENCODER_NAME = "via-encoder";
public static boolean preServerboundCheck(UserConnection user) {
// Ignore if pending disconnect
if (user.isPendingDisconnect()) return true;
// Increment received + Check PPS
return user.incrementReceived() && user.handlePPS();
}
public static void preClientbound(UserConnection user) {
user.incrementSent();
}
public static boolean willTransformPacket(UserConnection user) {
return user.isActive();
}
public static void transformClientbound(ByteBuf draft, UserConnection user, Function<Throwable, Exception> cancelSupplier) throws Exception {
if (!draft.isReadable()) return;
transform(draft, user, Direction.OUTGOING, cancelSupplier);
}
public static void transformServerbound(ByteBuf draft, UserConnection user, Function<Throwable, Exception> cancelSupplier) throws Exception {
if (!draft.isReadable()) return;
transform(draft, user, Direction.INCOMING, cancelSupplier);
}
private static void transform(ByteBuf draft, UserConnection user, Direction direction, Function<Throwable, Exception> cancelSupplier) throws Exception {
int id = Type.VAR_INT.read(draft);
if (id == PacketWrapper.PASSTHROUGH_ID) return;
PacketWrapper wrapper = new PacketWrapper(id, draft, user);
ProtocolInfo protInfo = user.get(ProtocolInfo.class);
try {
protInfo.getPipeline().transform(direction, protInfo.getState(), wrapper);
} catch (CancelException ex) {
throw cancelSupplier.apply(ex);
}
ByteBuf transformed = draft.alloc().buffer();
try {
wrapper.writeToBuffer(transformed);
draft.clear().writeBytes(transformed);
} finally {
transformed.release();
}
}
} }

View File

@ -24,43 +24,42 @@
package com.github.creeper123123321.viafabric.handler.clientside; package com.github.creeper123123321.viafabric.handler.clientside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.MessageToMessageDecoder;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelCodecException;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.exception.CancelDecoderException;
import java.util.List; import java.util.List;
public class VRDecodeHandler extends MessageToMessageDecoder<ByteBuf> { public class VRDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
private final UserConnection user; private final UserConnection info;
public VRDecodeHandler(UserConnection user) { public VRDecodeHandler(UserConnection info) {
this.user = user; this.info = info;
} }
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
CommonTransformer.preClientbound(user); info.checkOutgoingPacket();
if (!CommonTransformer.willTransformPacket(user)) { if (!info.shouldTransformPacket()) {
out.add(msg.retain()); out.add(bytebuf.retain());
return; return;
} }
ByteBuf draft = msg.alloc().buffer().writeBytes(msg);
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
try { try {
CommonTransformer.transformClientbound(draft, user, ignored -> CancelException.CACHED); info.transformOutgoing(transformedBuf, CancelDecoderException::generate);
out.add(draft.retain()); out.add(transformedBuf.retain());
} finally { } finally {
draft.release(); transformedBuf.release();
} }
} }
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (PipelineUtil.containsCause(cause, CancelException.class)) return; if (cause instanceof CancelCodecException) return;
super.exceptionCaught(ctx, cause); super.exceptionCaught(ctx, cause);
} }
} }

View File

@ -24,44 +24,42 @@
package com.github.creeper123123321.viafabric.handler.clientside; package com.github.creeper123123321.viafabric.handler.clientside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder; import io.netty.handler.codec.MessageToMessageEncoder;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelCodecException;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.exception.CancelEncoderException;
import java.util.List; import java.util.List;
public class VREncodeHandler extends MessageToMessageEncoder<ByteBuf> { public class VREncodeHandler extends MessageToMessageEncoder<ByteBuf> {
private final UserConnection user; private final UserConnection info;
public VREncodeHandler(UserConnection user) { public VREncodeHandler(UserConnection info) {
this.user = user; this.info = info;
} }
@Override @Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { protected void encode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
if (CommonTransformer.preServerboundCheck(user)) { if (!info.checkIncomingPacket()) throw CancelEncoderException.generate(null);
throw CancelException.CACHED; // M2ME expects at least one message if (!info.shouldTransformPacket()) {
} out.add(bytebuf.retain());
if (!CommonTransformer.willTransformPacket(user)) {
out.add(msg.retain());
return; return;
} }
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
try { try {
CommonTransformer.transformServerbound(draft, user, ignored -> CancelException.CACHED); info.transformIncoming(transformedBuf, CancelEncoderException::generate);
out.add(draft.retain()); out.add(transformedBuf.retain());
} finally { } finally {
draft.release(); transformedBuf.release();
} }
} }
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (PipelineUtil.containsCause(cause, CancelException.class)) return; if (cause instanceof CancelCodecException) return;
super.exceptionCaught(ctx, cause); super.exceptionCaught(ctx, cause);
} }
} }

View File

@ -24,46 +24,43 @@
package com.github.creeper123123321.viafabric.handler.serverside; package com.github.creeper123123321.viafabric.handler.serverside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.MessageToMessageDecoder;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelCodecException;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.exception.CancelDecoderException;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
import java.util.List; import java.util.List;
public class FabricDecodeHandler extends MessageToMessageDecoder<ByteBuf> { public class FabricDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
private final UserConnection user; private final UserConnection info;
public FabricDecodeHandler(UserConnection user) { public FabricDecodeHandler(UserConnection info) {
this.user = user; this.info = info;
} }
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
if (CommonTransformer.preServerboundCheck(user)) { if (!info.checkIncomingPacket()) throw CancelDecoderException.generate(null);
throw CancelException.CACHED; if (!info.shouldTransformPacket()) {
} out.add(bytebuf.retain());
if (!CommonTransformer.willTransformPacket(user)) {
out.add(msg.retain());
return; return;
} }
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
try { try {
CommonTransformer.transformServerbound(draft, user, ignored -> CancelException.CACHED); info.transformIncoming(transformedBuf, CancelDecoderException::generate);
out.add(draft.retain()); out.add(transformedBuf.retain());
} finally { } finally {
draft.release(); transformedBuf.release();
} }
} }
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (PipelineUtil.containsCause(cause, CancelException.class)) return; if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return;
super.exceptionCaught(ctx, cause); super.exceptionCaught(ctx, cause);
} }
} }

View File

@ -24,42 +24,43 @@
package com.github.creeper123123321.viafabric.handler.serverside; package com.github.creeper123123321.viafabric.handler.serverside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder; import io.netty.handler.codec.MessageToMessageEncoder;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelCodecException;
import us.myles.ViaVersion.exception.CancelEncoderException;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
import java.util.List; import java.util.List;
public class FabricEncodeHandler extends MessageToMessageEncoder<ByteBuf> { public class FabricEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
private final UserConnection user; private final UserConnection info;
public FabricEncodeHandler(UserConnection user) { public FabricEncodeHandler(UserConnection info) {
this.user = user; this.info = info;
} }
@Override @Override
protected void encode(final ChannelHandlerContext ctx, ByteBuf msg, final List<Object> out) throws Exception { protected void encode(final ChannelHandlerContext ctx, ByteBuf bytebuf, final List<Object> out) throws Exception {
CommonTransformer.preClientbound(user); info.checkOutgoingPacket();
if (!CommonTransformer.willTransformPacket(user)) { if (!info.shouldTransformPacket()) {
out.add(msg.readRetainedSlice(msg.readableBytes())); out.add(bytebuf.retain());
return; return;
} }
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
try { try {
CommonTransformer.transformClientbound(draft, user, ignored -> CancelException.CACHED); info.transformOutgoing(transformedBuf, CancelEncoderException::generate);
out.add(draft.retain()); out.add(transformedBuf.retain());
} finally { } finally {
draft.release(); transformedBuf.release();
} }
} }
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (PipelineUtil.containsCause(cause, CancelException.class)) return; if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return;
super.exceptionCaught(ctx, cause); super.exceptionCaught(ctx, cause);
} }
} }