mirror of
https://github.com/ViaVersion/ViaFabric.git
synced 2025-01-25 21:51:23 +01:00
trying to implement send() refactor
This commit is contained in:
parent
201e627adf
commit
caf6ab22ce
@ -37,13 +37,14 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo;
|
||||
import us.myles.ViaVersion.util.PipelineUtil;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class VRInHandler extends ByteToMessageDecoder {
|
||||
public class VRDecodeHandler extends ByteToMessageDecoder {
|
||||
private UserConnection user;
|
||||
private ByteToMessageDecoder minecraftDecoder;
|
||||
|
||||
public VRInHandler(UserConnection user, ByteToMessageDecoder minecraftDecoder) {
|
||||
public VRDecodeHandler(UserConnection user, ByteToMessageDecoder minecraftDecoder) {
|
||||
this.user = user;
|
||||
this.minecraftDecoder = minecraftDecoder;
|
||||
}
|
||||
@ -94,9 +95,29 @@ public class VRInHandler extends ByteToMessageDecoder {
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
if (PipelineUtil.containsCause(cause, CancelException.class)) return;
|
||||
if (user.isActive()) {
|
||||
Iterator<Runnable> iterator = user.getPostProcessingTasks().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
iterator.next().run();
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
super.channelRead(ctx, msg);
|
||||
if (user.isActive()) {
|
||||
Iterator<Runnable> iterator = user.getPostProcessingTasks().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
iterator.next().run();
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
// todo implement to other platforms
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
ProtocolInfo info = user.get(ProtocolInfo.class);
|
@ -26,6 +26,7 @@ package com.github.creeper123123321.viarift.handler;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import us.myles.ViaVersion.api.PacketWrapper;
|
||||
import us.myles.ViaVersion.api.data.UserConnection;
|
||||
@ -36,12 +37,13 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo;
|
||||
import us.myles.ViaVersion.util.PipelineUtil;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class VROutHandler extends MessageToByteEncoder {
|
||||
public class VREncodeHandler extends MessageToByteEncoder {
|
||||
private UserConnection user;
|
||||
private MessageToByteEncoder minecraftEncoder;
|
||||
|
||||
public VROutHandler(UserConnection user, MessageToByteEncoder minecraftEncoder) {
|
||||
public VREncodeHandler(UserConnection user, MessageToByteEncoder minecraftEncoder) {
|
||||
this.user = user;
|
||||
this.minecraftEncoder = minecraftEncoder;
|
||||
}
|
||||
@ -100,10 +102,28 @@ public class VROutHandler extends MessageToByteEncoder {
|
||||
pre.release();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
if (PipelineUtil.containsCause(cause, CancelException.class)) return;
|
||||
if (user.isActive()) {
|
||||
Iterator<Runnable> iterator = user.getPostProcessingTasks().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
iterator.next().run();
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
super.write(ctx, msg, promise);
|
||||
if (user.isActive()) {
|
||||
Iterator<Runnable> iterator = user.getPostProcessingTasks().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
iterator.next().run();
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -24,8 +24,8 @@
|
||||
|
||||
package com.github.creeper123123321.viarift.mixin;
|
||||
|
||||
import com.github.creeper123123321.viarift.handler.VRInHandler;
|
||||
import com.github.creeper123123321.viarift.handler.VROutHandler;
|
||||
import com.github.creeper123123321.viarift.handler.VRDecodeHandler;
|
||||
import com.github.creeper123123321.viarift.handler.VREncodeHandler;
|
||||
import com.github.creeper123123321.viarift.platform.VRUserConnection;
|
||||
import com.github.creeper123123321.viarift.protocol.Interceptor;
|
||||
import io.netty.channel.Channel;
|
||||
@ -50,8 +50,8 @@ public class MixinNetworkManagerClientChInit {
|
||||
MessageToByteEncoder oldEncoder = (MessageToByteEncoder) channel.pipeline().get("encoder");
|
||||
ByteToMessageDecoder oldDecoder = (ByteToMessageDecoder) channel.pipeline().get("decoder");
|
||||
|
||||
channel.pipeline().replace("encoder", "encoder", new VROutHandler(user, oldEncoder));
|
||||
channel.pipeline().replace("decoder", "decoder", new VRInHandler(user, oldDecoder));
|
||||
channel.pipeline().replace("encoder", "encoder", new VREncodeHandler(user, oldEncoder));
|
||||
channel.pipeline().replace("decoder", "decoder", new VRDecodeHandler(user, oldDecoder));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@
|
||||
package com.github.creeper123123321.viarift.platform;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import us.myles.ViaVersion.api.PacketWrapper;
|
||||
@ -84,4 +85,28 @@ public class VRUserConnection extends UserConnection {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendRawPacketAfterProcessing(ByteBuf packet) {
|
||||
ByteBuf copy = packet.alloc().buffer();
|
||||
try {
|
||||
Type.VAR_INT.write(copy, PacketWrapper.PASSTHROUGH_ID);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
copy.writeBytes(packet);
|
||||
packet.release();
|
||||
final Channel channel = this.getChannel();
|
||||
getPostProcessingTasks().add(() ->
|
||||
PipelineUtil.getPreviousContext("decoder", channel.pipeline()).fireChannelRead(copy));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendRawPacketToServerAfterProcessing(ByteBuf packet) {
|
||||
final ByteBuf buf = Unpooled.buffer();
|
||||
buf.writeBytes(packet);
|
||||
packet.release();
|
||||
this.getPostProcessingTasks().add(() ->
|
||||
getChannel().pipeline().context("encoder").writeAndFlush(buf));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user