3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-10-03 08:41:05 +02:00

Replace runnables with method in packet send methods

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-05-16 21:50:09 +02:00
Ursprung 91f31b578f
Commit 5a1a22a007
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
2 geänderte Dateien mit 49 neuen und 51 gelöschten Zeilen

Datei anzeigen

@ -23,12 +23,12 @@ import com.viaversion.viaversion.api.connection.ProtocolInfo;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.entity.EntityTracker;
import com.viaversion.viaversion.api.platform.ViaInjector;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketTracker;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.exception.CancelException;
import com.viaversion.viaversion.exception.InformativeException;
@ -39,6 +39,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.CodecException;
import java.util.Collection;
import java.util.Collections;
@ -162,20 +163,12 @@ public class UserConnectionImpl implements UserConnection {
sendRawPacket(packet, false);
}
private void sendRawPacket(final ByteBuf packet, boolean currentThread) {
Runnable act;
if (clientSide) {
// We'll just assume that Via decoder isn't wrapping the original decoder
act = () -> getChannel().pipeline()
.context(Via.getManager().getInjector().getDecoderName()).fireChannelRead(packet);
} else {
act = () -> channel.pipeline().context(Via.getManager().getInjector().getEncoderName()).writeAndFlush(packet);
}
private void sendRawPacket(final ByteBuf packet, final boolean currentThread) {
if (currentThread) {
act.run();
sendRawPacketNow(packet);
} else {
try {
channel.eventLoop().submit(act);
channel.eventLoop().submit(() -> sendRawPacketNow(packet));
} catch (Throwable e) {
packet.release(); // Couldn't schedule
e.printStackTrace();
@ -183,6 +176,17 @@ public class UserConnectionImpl implements UserConnection {
}
}
private void sendRawPacketNow(final ByteBuf buf) {
final ChannelPipeline pipeline = getChannel().pipeline();
final ViaInjector injector = Via.getManager().getInjector();
if (clientSide) {
// We'll just assume that Via decoder isn't wrapping the original decoder
pipeline.context(injector.getDecoderName()).fireChannelRead(buf);
} else {
pipeline.context(injector.getEncoderName()).writeAndFlush(buf);
}
}
@Override
public ChannelFuture sendRawPacketFuture(final ByteBuf packet) {
if (clientSide) {
@ -229,7 +233,7 @@ public class UserConnectionImpl implements UserConnection {
}
}
private void sendRawPacketToServerServerSide(final ByteBuf packet, boolean currentThread) {
private void sendRawPacketToServerServerSide(final ByteBuf packet, final boolean currentThread) {
final ByteBuf buf = packet.alloc().buffer();
try {
// We'll use passing through because there are some encoder wrappers
@ -243,18 +247,11 @@ public class UserConnectionImpl implements UserConnection {
}
buf.writeBytes(packet);
Runnable act = () -> {
if (context != null) {
context.fireChannelRead(buf);
} else {
channel.pipeline().fireChannelRead(buf);
}
};
if (currentThread) {
act.run();
fireChannelRead(context, buf);
} else {
try {
channel.eventLoop().submit(act);
channel.eventLoop().submit(() -> fireChannelRead(context, buf));
} catch (Throwable t) {
// Couldn't schedule
buf.release();
@ -266,14 +263,20 @@ public class UserConnectionImpl implements UserConnection {
}
}
private void sendRawPacketToServerClientSide(final ByteBuf packet, boolean currentThread) {
Runnable act = () -> getChannel().pipeline()
.context(Via.getManager().getInjector().getEncoderName()).writeAndFlush(packet);
private void fireChannelRead(@Nullable final ChannelHandlerContext context, final ByteBuf buf) {
if (context != null) {
context.fireChannelRead(buf);
} else {
channel.pipeline().fireChannelRead(buf);
}
}
private void sendRawPacketToServerClientSide(final ByteBuf packet, final boolean currentThread) {
if (currentThread) {
act.run();
writeAndFlush(packet);
} else {
try {
getChannel().eventLoop().submit(act);
getChannel().eventLoop().submit(() -> writeAndFlush(packet));
} catch (Throwable e) {
e.printStackTrace();
packet.release(); // Couldn't schedule
@ -281,6 +284,10 @@ public class UserConnectionImpl implements UserConnection {
}
}
private void writeAndFlush(final ByteBuf buf) {
getChannel().pipeline().context(Via.getManager().getInjector().getEncoderName()).writeAndFlush(buf);
}
@Override
public boolean checkServerboundPacket() {
if (pendingDisconnect) {

Datei anzeigen

@ -265,33 +265,24 @@ public class PacketWrapperImpl implements PacketWrapper {
final UserConnection connection = user();
if (currentThread) {
try {
final ByteBuf output = constructPacket(protocol, skipCurrentPipeline, Direction.CLIENTBOUND);
connection.sendRawPacket(output);
} catch (InformativeException e) {
throw e;
} catch (CancelException ignored) {
} catch (Exception e) {
if (!PipelineUtil.containsCause(e, CancelException.class)) {
throw new InformativeException(e);
}
}
return;
sendNow(protocol, skipCurrentPipeline);
} else {
connection.getChannel().eventLoop().submit(() -> sendNow(protocol, skipCurrentPipeline));
}
}
connection.getChannel().eventLoop().submit(() -> {
try {
final ByteBuf output = constructPacket(protocol, skipCurrentPipeline, Direction.CLIENTBOUND);
connection.sendRawPacket(output);
} catch (InformativeException e) {
throw e;
} catch (CancelException ignored) {
} catch (Exception e) {
if (!PipelineUtil.containsCause(e, CancelException.class)) {
throw new InformativeException(e);
}
private void sendNow(final Class<? extends Protocol> protocol, final boolean skipCurrentPipeline) throws InformativeException {
try {
final ByteBuf output = constructPacket(protocol, skipCurrentPipeline, Direction.CLIENTBOUND);
user().sendRawPacket(output);
} catch (InformativeException e) {
throw e;
} catch (CancelException ignored) {
} catch (Exception e) {
if (!PipelineUtil.containsCause(e, CancelException.class)) {
throw new InformativeException(e);
}
});
}
}
/**