1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
--- a/net/minecraft/network/Connection.java
+++ b/net/minecraft/network/Connection.java
@@ -50,7 +50,7 @@
import net.minecraft.network.protocol.login.ClientLoginPacketListener;
import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket;
import net.minecraft.network.protocol.status.ClientStatusPacketListener;
-import net.minecraft.server.RunningOnDifferentThreadException;
+import net.minecraft.server.CancelledPacketHandleException;
import net.minecraft.util.Mth;
import net.minecraft.util.SampleLogger;
import org.apache.commons.lang3.Validate;
@@ -72,8 +72,8 @@
public static final Marker PACKET_SENT_MARKER = (Marker) Util.make(MarkerFactory.getMarker("PACKET_SENT"), (marker) -> {
marker.add(Connection.PACKET_MARKER);
});
- public static final AttributeKey<ConnectionProtocol.CodecData<?>> ATTRIBUTE_SERVERBOUND_PROTOCOL = AttributeKey.valueOf("serverbound_protocol");
- public static final AttributeKey<ConnectionProtocol.CodecData<?>> ATTRIBUTE_CLIENTBOUND_PROTOCOL = AttributeKey.valueOf("clientbound_protocol");
+ public static final AttributeKey<ConnectionProtocol.a<?>> ATTRIBUTE_SERVERBOUND_PROTOCOL = AttributeKey.valueOf("serverbound_protocol");
+ public static final AttributeKey<ConnectionProtocol.a<?>> ATTRIBUTE_CLIENTBOUND_PROTOCOL = AttributeKey.valueOf("clientbound_protocol");
public static final Supplier<NioEventLoopGroup> NETWORK_WORKER_GROUP = Suppliers.memoize(() -> {
return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Client IO #%d").setDaemon(true).build());
});
@@ -85,8 +85,8 @@
});
private final PacketFlow receiving;
private final Queue<Consumer<Connection>> pendingActions = Queues.newConcurrentLinkedQueue();
- private Channel channel;
- private SocketAddress address;
+ public Channel channel;
+ public SocketAddress address;
@Nullable
private volatile PacketListener disconnectListener;
@Nullable
@@ -105,12 +105,12 @@
private volatile Component delayedDisconnect;
@Nullable
BandwidthDebugMonitor bandwidthDebugMonitor;
+ public String hostname = ""; // CraftBukkit - add field
- public Connection(PacketFlow packetflow) {
- this.receiving = packetflow;
+ public Connection(PacketFlow receiving) {
+ this.receiving = receiving;
}
- @Override
public void channelActive(ChannelHandlerContext channelhandlercontext) throws Exception {
super.channelActive(channelhandlercontext);
this.channel = channelhandlercontext.channel();
@@ -126,12 +126,10 @@
channel.attr(Connection.ATTRIBUTE_CLIENTBOUND_PROTOCOL).set(ConnectionProtocol.HANDSHAKING.codec(PacketFlow.CLIENTBOUND));
}
- @Override
public void channelInactive(ChannelHandlerContext channelhandlercontext) {
this.disconnect(Component.translatable("disconnect.endOfStream"));
}
- @Override
public void exceptionCaught(ChannelHandlerContext channelhandlercontext, Throwable throwable) {
if (throwable instanceof SkipPacketException) {
Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause());
@@ -144,25 +142,25 @@
Connection.LOGGER.debug("Timeout", throwable);
this.disconnect(Component.translatable("disconnect.timeout"));
} else {
- MutableComponent mutablecomponent = Component.translatable("disconnect.genericReason", "Internal Exception: " + throwable);
+ MutableComponent ichatmutablecomponent = Component.translatable("disconnect.genericReason", "Internal Exception: " + throwable);
if (flag) {
Connection.LOGGER.debug("Failed to sent packet", throwable);
if (this.getSending() == PacketFlow.CLIENTBOUND) {
- ConnectionProtocol connectionprotocol = ((ConnectionProtocol.CodecData) this.channel.attr(Connection.ATTRIBUTE_CLIENTBOUND_PROTOCOL).get()).protocol();
- Packet<?> packet = connectionprotocol == ConnectionProtocol.LOGIN ? new ClientboundLoginDisconnectPacket(mutablecomponent) : new ClientboundDisconnectPacket(mutablecomponent);
+ ConnectionProtocol enumprotocol = ((ConnectionProtocol.a) this.channel.attr(Connection.ATTRIBUTE_CLIENTBOUND_PROTOCOL).get()).protocol();
+ Packet<?> packet = enumprotocol == ConnectionProtocol.LOGIN ? new ClientboundLoginDisconnectPacket(ichatmutablecomponent) : new ClientboundDisconnectPacket(ichatmutablecomponent);
this.send((Packet) packet, PacketSendListener.thenRun(() -> {
- this.disconnect(mutablecomponent);
+ this.disconnect(ichatmutablecomponent);
}));
} else {
- this.disconnect(mutablecomponent);
+ this.disconnect(ichatmutablecomponent);
}
this.setReadOnly();
} else {
Connection.LOGGER.debug("Double fault", throwable);
- this.disconnect(mutablecomponent);
+ this.disconnect(ichatmutablecomponent);
}
}
@@ -170,8 +168,7 @@
}
}
- @Override
- protected void channelRead0(ChannelHandlerContext channelhandlercontext, Packet<?> packet) {
+ protected void channelRead0(ChannelHandlerContext context, Packet<?> packet) {
if (this.channel.isOpen()) {
PacketListener packetlistener = this.packetListener;
@@ -181,7 +178,7 @@
if (packetlistener.shouldHandleMessage(packet)) {
try {
genericsFtw(packet, packetlistener);
- } catch (RunningOnDifferentThreadException runningondifferentthreadexception) {
+ } catch (CancelledPacketHandleException cancelledpackethandleexception) {
;
} catch (RejectedExecutionException rejectedexecutionexception) {
this.disconnect(Component.translatable("multiplayer.disconnect.server_shutdown"));
@@ -197,8 +194,8 @@
}
}
- private static <T extends PacketListener> void genericsFtw(Packet<T> packet, PacketListener packetlistener) {
- packet.handle(packetlistener);
+ private static <T extends PacketListener> void genericsFtw(Packet<T> packet, PacketListener listener) {
+ packet.handle((T) listener); // CraftBukkit - decompile error
}
public void suspendInboundAfterProtocolChange() {
@@ -209,20 +206,20 @@
this.channel.config().setAutoRead(true);
}
- public void setListener(PacketListener packetlistener) {
- Validate.notNull(packetlistener, "packetListener", new Object[0]);
- PacketFlow packetflow = packetlistener.flow();
+ public void setListener(PacketListener handler) {
+ Validate.notNull(handler, "packetListener", new Object[0]);
+ PacketFlow enumprotocoldirection = handler.flow();
- if (packetflow != this.receiving) {
- throw new IllegalStateException("Trying to set listener for wrong side: connection is " + this.receiving + ", but listener is " + packetflow);
+ if (enumprotocoldirection != this.receiving) {
+ throw new IllegalStateException("Trying to set listener for wrong side: connection is " + this.receiving + ", but listener is " + enumprotocoldirection);
} else {
- ConnectionProtocol connectionprotocol = packetlistener.protocol();
- ConnectionProtocol connectionprotocol1 = ((ConnectionProtocol.CodecData) this.channel.attr(getProtocolKey(packetflow)).get()).protocol();
+ ConnectionProtocol enumprotocol = handler.protocol();
+ ConnectionProtocol enumprotocol1 = ((ConnectionProtocol.a) this.channel.attr(getProtocolKey(enumprotocoldirection)).get()).protocol();
- if (connectionprotocol1 != connectionprotocol) {
- throw new IllegalStateException("Trying to set listener for protocol " + connectionprotocol.id() + ", but current " + packetflow + " protocol is " + connectionprotocol1.id());
+ if (enumprotocol1 != enumprotocol) {
+ throw new IllegalStateException("Trying to set listener for protocol " + enumprotocol.id() + ", but current " + enumprotocoldirection + " protocol is " + enumprotocol1.id());
} else {
- this.packetListener = packetlistener;
+ this.packetListener = handler;
this.disconnectListener = null;
}
}
@@ -238,20 +235,20 @@
}
}
- public void initiateServerboundStatusConnection(String s, int i, ClientStatusPacketListener clientstatuspacketlistener) {
- this.initiateServerboundConnection(s, i, clientstatuspacketlistener, ClientIntent.STATUS);
+ public void initiateServerboundStatusConnection(String s, int i, ClientStatusPacketListener packetstatusoutlistener) {
+ this.initiateServerboundConnection(s, i, packetstatusoutlistener, ClientIntent.STATUS);
}
- public void initiateServerboundPlayConnection(String s, int i, ClientLoginPacketListener clientloginpacketlistener) {
- this.initiateServerboundConnection(s, i, clientloginpacketlistener, ClientIntent.LOGIN);
+ public void initiateServerboundPlayConnection(String s, int i, ClientLoginPacketListener packetloginoutlistener) {
+ this.initiateServerboundConnection(s, i, packetloginoutlistener, ClientIntent.LOGIN);
}
private void initiateServerboundConnection(String s, int i, PacketListener packetlistener, ClientIntent clientintent) {
this.disconnectListener = packetlistener;
- this.runOnceConnected((connection) -> {
- connection.setClientboundProtocolAfterHandshake(clientintent);
+ this.runOnceConnected((networkmanager) -> {
+ networkmanager.setClientboundProtocolAfterHandshake(clientintent);
this.setListener(packetlistener);
- connection.sendPacket(new ClientIntentionPacket(SharedConstants.getCurrentVersion().getProtocolVersion(), s, i, clientintent), (PacketSendListener) null, true);
+ networkmanager.sendPacket(new ClientIntentionPacket(SharedConstants.getCurrentVersion().getProtocolVersion(), s, i, clientintent), (PacketSendListener) null, true);
});
}
@@ -263,8 +260,8 @@
this.send(packet, (PacketSendListener) null);
}
- public void send(Packet<?> packet, @Nullable PacketSendListener packetsendlistener) {
- this.send(packet, packetsendlistener, true);
+ public void send(Packet<?> packet, @Nullable PacketSendListener sendListener) {
+ this.send(packet, sendListener, true);
}
public void send(Packet<?> packet, @Nullable PacketSendListener packetsendlistener, boolean flag) {
@@ -272,8 +269,8 @@
this.flushQueue();
this.sendPacket(packet, packetsendlistener, flag);
} else {
- this.pendingActions.add((connection) -> {
- connection.sendPacket(packet, packetsendlistener, flag);
+ this.pendingActions.add((networkmanager) -> {
+ networkmanager.sendPacket(packet, packetsendlistener, flag);
});
}
@@ -344,10 +341,10 @@
}
- private static AttributeKey<ConnectionProtocol.CodecData<?>> getProtocolKey(PacketFlow packetflow) {
+ private static AttributeKey<ConnectionProtocol.a<?>> getProtocolKey(PacketFlow enumprotocoldirection) {
AttributeKey attributekey;
- switch (packetflow) {
+ switch (enumprotocoldirection) {
case CLIENTBOUND:
attributekey = Connection.ATTRIBUTE_CLIENTBOUND_PROTOCOL;
break;
@@ -419,14 +416,14 @@
return this.address == null ? "local" : (flag ? this.address.toString() : "IP hidden");
}
- public void disconnect(Component component) {
+ public void disconnect(Component message) {
if (this.channel == null) {
- this.delayedDisconnect = component;
+ this.delayedDisconnect = message;
}
if (this.isConnected()) {
- this.channel.close().awaitUninterruptibly();
- this.disconnectedReason = component;
+ this.channel.close(); // We can't wait as this may be called from an event loop.
+ this.disconnectedReason = message;
}
}
@@ -444,23 +441,23 @@
}
public static Connection connectToServer(InetSocketAddress inetsocketaddress, boolean flag, @Nullable SampleLogger samplelogger) {
- Connection connection = new Connection(PacketFlow.CLIENTBOUND);
+ Connection networkmanager = new Connection(PacketFlow.CLIENTBOUND);
if (samplelogger != null) {
- connection.setBandwidthLogger(samplelogger);
+ networkmanager.setBandwidthLogger(samplelogger);
}
- ChannelFuture channelfuture = connect(inetsocketaddress, flag, connection);
+ ChannelFuture channelfuture = connect(inetsocketaddress, flag, networkmanager);
channelfuture.syncUninterruptibly();
- return connection;
+ return networkmanager;
}
- public static ChannelFuture connect(InetSocketAddress inetsocketaddress, boolean flag, final Connection connection) {
+ public static ChannelFuture connect(InetSocketAddress address, boolean useEpollIfAvailable, final Connection connection) {
Class oclass;
EventLoopGroup eventloopgroup;
- if (Epoll.isAvailable() && flag) {
+ if (Epoll.isAvailable() && useEpollIfAvailable) {
oclass = EpollSocketChannel.class;
eventloopgroup = (EventLoopGroup) Connection.NETWORK_EPOLL_WORKER_GROUP.get();
} else {
@@ -469,7 +466,6 @@
}
return ((Bootstrap) ((Bootstrap) ((Bootstrap) (new Bootstrap()).group(eventloopgroup)).handler(new ChannelInitializer<Channel>() {
- @Override
protected void initChannel(Channel channel) {
Connection.setInitialProtocolAttributes(channel);
@@ -484,13 +480,13 @@
Connection.configureSerialization(channelpipeline, PacketFlow.CLIENTBOUND, connection.bandwidthDebugMonitor);
connection.configurePacketHandler(channelpipeline);
}
- })).channel(oclass)).connect(inetsocketaddress.getAddress(), inetsocketaddress.getPort());
+ })).channel(oclass)).connect(address.getAddress(), address.getPort());
}
- public static void configureSerialization(ChannelPipeline channelpipeline, PacketFlow packetflow, @Nullable BandwidthDebugMonitor bandwidthdebugmonitor) {
- PacketFlow packetflow1 = packetflow.getOpposite();
- AttributeKey<ConnectionProtocol.CodecData<?>> attributekey = getProtocolKey(packetflow);
- AttributeKey<ConnectionProtocol.CodecData<?>> attributekey1 = getProtocolKey(packetflow1);
+ public static void configureSerialization(ChannelPipeline channelpipeline, PacketFlow enumprotocoldirection, @Nullable BandwidthDebugMonitor bandwidthdebugmonitor) {
+ PacketFlow enumprotocoldirection1 = enumprotocoldirection.getOpposite();
+ AttributeKey<ConnectionProtocol.a<?>> attributekey = getProtocolKey(enumprotocoldirection);
+ AttributeKey<ConnectionProtocol.a<?>> attributekey1 = getProtocolKey(enumprotocoldirection1);
channelpipeline.addLast("splitter", new Varint21FrameDecoder(bandwidthdebugmonitor)).addLast("decoder", new PacketDecoder(attributekey)).addLast("prepender", new Varint21LengthFieldPrepender()).addLast("encoder", new PacketEncoder(attributekey1)).addLast("unbundler", new PacketBundleUnpacker(attributekey1)).addLast("bundler", new PacketBundlePacker(attributekey));
}
@@ -499,38 +495,37 @@
channelpipeline.addLast(new ChannelHandler[]{new FlowControlHandler()}).addLast("packet_handler", this);
}
- private static void configureInMemoryPacketValidation(ChannelPipeline channelpipeline, PacketFlow packetflow) {
- PacketFlow packetflow1 = packetflow.getOpposite();
- AttributeKey<ConnectionProtocol.CodecData<?>> attributekey = getProtocolKey(packetflow);
- AttributeKey<ConnectionProtocol.CodecData<?>> attributekey1 = getProtocolKey(packetflow1);
+ private static void configureInMemoryPacketValidation(ChannelPipeline channelpipeline, PacketFlow enumprotocoldirection) {
+ PacketFlow enumprotocoldirection1 = enumprotocoldirection.getOpposite();
+ AttributeKey<ConnectionProtocol.a<?>> attributekey = getProtocolKey(enumprotocoldirection);
+ AttributeKey<ConnectionProtocol.a<?>> attributekey1 = getProtocolKey(enumprotocoldirection1);
channelpipeline.addLast("validator", new PacketFlowValidator(attributekey, attributekey1));
}
- public static void configureInMemoryPipeline(ChannelPipeline channelpipeline, PacketFlow packetflow) {
- configureInMemoryPacketValidation(channelpipeline, packetflow);
+ public static void configureInMemoryPipeline(ChannelPipeline channelpipeline, PacketFlow enumprotocoldirection) {
+ configureInMemoryPacketValidation(channelpipeline, enumprotocoldirection);
}
- public static Connection connectToLocalServer(SocketAddress socketaddress) {
- final Connection connection = new Connection(PacketFlow.CLIENTBOUND);
+ public static Connection connectToLocalServer(SocketAddress address) {
+ final Connection networkmanager = new Connection(PacketFlow.CLIENTBOUND);
((Bootstrap) ((Bootstrap) ((Bootstrap) (new Bootstrap()).group((EventLoopGroup) Connection.LOCAL_WORKER_GROUP.get())).handler(new ChannelInitializer<Channel>() {
- @Override
protected void initChannel(Channel channel) {
Connection.setInitialProtocolAttributes(channel);
ChannelPipeline channelpipeline = channel.pipeline();
Connection.configureInMemoryPipeline(channelpipeline, PacketFlow.CLIENTBOUND);
- connection.configurePacketHandler(channelpipeline);
+ networkmanager.configurePacketHandler(channelpipeline);
}
- })).channel(LocalChannel.class)).connect(socketaddress).syncUninterruptibly();
- return connection;
+ })).channel(LocalChannel.class)).connect(address).syncUninterruptibly();
+ return networkmanager;
}
- public void setEncryptionKey(Cipher cipher, Cipher cipher1) {
+ public void setEncryptionKey(Cipher decryptingCipher, Cipher encryptingCipher) {
this.encrypted = true;
- this.channel.pipeline().addBefore("splitter", "decrypt", new CipherDecoder(cipher));
- this.channel.pipeline().addBefore("prepender", "encrypt", new CipherEncoder(cipher1));
+ this.channel.pipeline().addBefore("splitter", "decrypt", new CipherDecoder(decryptingCipher));
+ this.channel.pipeline().addBefore("prepender", "encrypt", new CipherEncoder(encryptingCipher));
}
public boolean isEncrypted() {
@@ -562,18 +557,18 @@
}
- public void setupCompression(int i, boolean flag) {
- if (i >= 0) {
+ public void setupCompression(int threshold, boolean validateDecompressed) {
+ if (threshold >= 0) {
if (this.channel.pipeline().get("decompress") instanceof CompressionDecoder) {
- ((CompressionDecoder) this.channel.pipeline().get("decompress")).setThreshold(i, flag);
+ ((CompressionDecoder) this.channel.pipeline().get("decompress")).setThreshold(threshold, validateDecompressed);
} else {
- this.channel.pipeline().addBefore("decoder", "decompress", new CompressionDecoder(i, flag));
+ this.channel.pipeline().addBefore("decoder", "decompress", new CompressionDecoder(threshold, validateDecompressed));
}
if (this.channel.pipeline().get("compress") instanceof CompressionEncoder) {
- ((CompressionEncoder) this.channel.pipeline().get("compress")).setThreshold(i);
+ ((CompressionEncoder) this.channel.pipeline().get("compress")).setThreshold(threshold);
} else {
- this.channel.pipeline().addBefore("encoder", "compress", new CompressionEncoder(i));
+ this.channel.pipeline().addBefore("encoder", "compress", new CompressionEncoder(threshold));
}
} else {
if (this.channel.pipeline().get("decompress") instanceof CompressionDecoder) {
@@ -597,11 +592,11 @@
PacketListener packetlistener1 = packetlistener != null ? packetlistener : this.disconnectListener;
if (packetlistener1 != null) {
- Component component = (Component) Objects.requireNonNullElseGet(this.getDisconnectedReason(), () -> {
+ Component ichatbasecomponent = (Component) Objects.requireNonNullElseGet(this.getDisconnectedReason(), () -> {
return Component.translatable("multiplayer.disconnect.generic");
});
- packetlistener1.onDisconnect(component);
+ packetlistener1.onDisconnect(ichatbasecomponent);
}
}
|