Skip to content

Commit c07f1b4

Browse files
Tim203dmulloy2
authored andcommitted
Preserve the pipeline order when async sending/receiving packets
1 parent 2c0d632 commit c07f1b4

File tree

2 files changed

+5
-75
lines changed

2 files changed

+5
-75
lines changed

src/main/java/com/comphenix/protocol/injector/netty/channel/NettyChannelInjector.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void sendClientboundPacket(Object packet, NetworkMarker marker, boolean f
271271
}
272272

273273
try {
274-
this.listenerInvoker.send(packet);
274+
this.channel.pipeline().context(WIRE_PACKET_ENCODER_NAME).writeAndFlush(packet);
275275
} catch (Exception exception) {
276276
this.errorReporter.reportWarning(this, Report.newBuilder(REPORT_CANNOT_SEND_PACKET)
277277
.messageParam(packet, this.playerName)
@@ -289,8 +289,7 @@ public void readServerboundPacket(Object packet) {
289289

290290
this.ensureInEventLoop(() -> {
291291
try {
292-
// try to invoke the method, this should normally not fail
293-
this.listenerInvoker.read(packet);
292+
this.channel.pipeline().context(INBOUND_INTERCEPTOR_NAME).fireChannelRead(packet);
294293
} catch (Exception exception) {
295294
this.errorReporter.reportWarning(this, Report.newBuilder(REPORT_CANNOT_READ_PACKET)
296295
.messageParam(packet, this.playerName)

src/main/java/com/comphenix/protocol/injector/netty/channel/PacketListenerInvoker.java

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
import com.comphenix.protocol.utility.MinecraftReflection;
1515
import com.comphenix.protocol.wrappers.WrappedChatComponent;
1616

17-
import io.netty.channel.ChannelHandlerContext;
18-
1917
/**
2018
* This class facilitates the invocation of methods on the current packet listener.
21-
* It attempts to execute the <code>send</code>, <code>read</code>, and <code>disconnect</code>
22-
* methods and, upon failure (either due to the absence of the method or the packet
23-
* listener being of an incorrect type), it delegates the call to the network manager.
19+
* It attempts to execute the <code>disconnect</code> method, upon failure
20+
* (either due to the absence of the method or the packet listener being of an incorrect type),
21+
* it delegates the call to the network manager.
2422
*
2523
* <p>Supported packet listener types include CONFIGURATION and PLAY. If the packet
2624
* listener does not match these types, or if the required method is missing, the
@@ -38,36 +36,15 @@ public class PacketListenerInvoker {
3836
private static final Class<?> COMMON_PACKET_LISTENER_CLASS = MinecraftReflection.getNullableNMS("server.network.ServerCommonPacketListenerImpl");
3937
private static final Class<?> PREFERRED_PACKET_LISTENER_CLASS = COMMON_PACKET_LISTENER_CLASS != null ? COMMON_PACKET_LISTENER_CLASS : GAME_PACKET_LISTENER_CLASS;
4038

41-
private static final MethodAccessor PACKET_LISTENER_SEND = getPacketListenerSend();
4239
private static final MethodAccessor PACKET_LISTENER_DISCONNECT = getPacketListenerDisconnect();
4340
private static final boolean DOES_PACKET_LISTENER_DISCONNECT_USE_COMPONENT = doesPacketListenerDisconnectUseComponent();
4441

45-
private static final MethodAccessor NETWORK_MANAGER_SEND = getNetworkManagerSend();
46-
private static final MethodAccessor NETWORK_MANAGER_READ = getNetworkManagerRead();
4742
private static final MethodAccessor NETWORK_MANAGER_DISCONNECT = getNetworkManagerDisconnect();
4843
private static final MethodAccessor NETWORK_MANAGER_PACKET_LISTENER = getNetworkManagerPacketListener();
4944

5045
public static void ensureStaticInitializedWithoutError() {
5146
}
5247

53-
private static MethodAccessor getPacketListenerSend() {
54-
FuzzyReflection packetListener = FuzzyReflection.fromClass(PREFERRED_PACKET_LISTENER_CLASS);
55-
56-
List<Method> send = packetListener.getMethodList(FuzzyMethodContract.newBuilder()
57-
.banModifier(Modifier.STATIC)
58-
.returnTypeVoid()
59-
.parameterCount(1)
60-
.parameterExactType(MinecraftReflection.getPacketClass(), 0)
61-
.build());
62-
63-
if (send.isEmpty()) {
64-
ProtocolLogger.debug("Can't get packet listener send method");
65-
return null;
66-
}
67-
68-
return Accessors.getMethodAccessor(send.get(0));
69-
}
70-
7148
private static MethodAccessor getPacketListenerDisconnect() {
7249
FuzzyReflection packetListener = FuzzyReflection.fromClass(PREFERRED_PACKET_LISTENER_CLASS);
7350

@@ -104,28 +81,6 @@ private static boolean doesPacketListenerDisconnectUseComponent() {
10481
return false;
10582
}
10683

107-
private static MethodAccessor getNetworkManagerSend() {
108-
FuzzyReflection networkManager = FuzzyReflection.fromClass(MinecraftReflection.getNetworkManagerClass());
109-
110-
Method send = networkManager.getMethod(FuzzyMethodContract.newBuilder()
111-
.banModifier(Modifier.STATIC)
112-
.returnTypeVoid()
113-
.parameterCount(1)
114-
.parameterExactType(MinecraftReflection.getPacketClass(), 0)
115-
.build());
116-
117-
return Accessors.getMethodAccessor(send);
118-
}
119-
120-
private static MethodAccessor getNetworkManagerRead() {
121-
FuzzyReflection networkManager = FuzzyReflection.fromClass(MinecraftReflection.getNetworkManagerClass(), true);
122-
123-
Method read = networkManager
124-
.getMethodByParameters("read", ChannelHandlerContext.class, MinecraftReflection.getPacketClass());
125-
126-
return Accessors.getMethodAccessor(read);
127-
}
128-
12984
private static MethodAccessor getNetworkManagerDisconnect() {
13085
FuzzyReflection networkManager = FuzzyReflection.fromClass(MinecraftReflection.getNetworkManagerClass());
13186

@@ -189,30 +144,6 @@ private Object getPacketListener() {
189144
return this.packetListener.get();
190145
}
191146

192-
/**
193-
* Sends a packet using the current packet listener if available and valid; otherwise,
194-
* falls back to the network manager.
195-
*
196-
* @param packet The packet to be sent.
197-
*/
198-
public void send(Object packet) {
199-
Object packetListener = this.getPacketListener();
200-
if (PACKET_LISTENER_SEND != null && packetListener != null) {
201-
PACKET_LISTENER_SEND.invoke(packetListener, packet);
202-
} else {
203-
NETWORK_MANAGER_SEND.invoke(this.networkManager, packet);
204-
}
205-
}
206-
207-
/**
208-
* Reads a packet directly using the network manager.
209-
*
210-
* @param packet The packet to be read.
211-
*/
212-
public void read(Object packet) {
213-
NETWORK_MANAGER_READ.invoke(this.networkManager, null, packet);
214-
}
215-
216147
/**
217148
* Disconnects the player using the current packet listener if available and valid; otherwise,
218149
* falls back to the network manager.

0 commit comments

Comments
 (0)