Skip to content

Commit

Permalink
Replace runnables with method in packet send methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytv committed May 16, 2024
1 parent 91f31b5 commit 5a1a22a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
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;
Expand All @@ -39,6 +39,7 @@
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;
Expand Down Expand Up @@ -162,27 +163,30 @@ public void scheduleSendRawPacket(ByteBuf packet) {
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();
}
}
}

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) {
Expand Down Expand Up @@ -229,7 +233,7 @@ public void scheduleSendRawPacketToServer(ByteBuf packet) {
}
}

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
Expand All @@ -243,18 +247,11 @@ private void sendRawPacketToServerServerSide(final ByteBuf packet, boolean curre
}

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();
Expand All @@ -266,21 +263,31 @@ private void sendRawPacketToServerServerSide(final ByteBuf packet, boolean curre
}
}

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
}
}
}

private void writeAndFlush(final ByteBuf buf) {
getChannel().pipeline().context(Via.getManager().getInjector().getEncoderName()).writeAndFlush(buf);
}

@Override
public boolean checkServerboundPacket() {
if (pendingDisconnect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,33 +265,24 @@ private void send0(Class<? extends Protocol> protocol, boolean skipCurrentPipeli

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);
}
});
}
}

/**
Expand Down

0 comments on commit 5a1a22a

Please sign in to comment.