Skip to content

Commit f53145e

Browse files
committed
Handle ping pong on thread
1 parent f9125b5 commit f53145e

3 files changed

Lines changed: 212 additions & 134 deletions

File tree

ProxyListener.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

ProxyNetworkInterface.php

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace libproxy;
77

88
use Error;
9-
use ErrorException;
109
use Exception;
1110
use libproxy\data\LatencyData;
1211
use libproxy\data\TickSyncPacket;
@@ -18,16 +17,12 @@
1817
use libproxy\protocol\ProxyPacketSerializer;
1918
use pmmp\thread\Thread as NativeThread;
2019
use pmmp\thread\ThreadSafeArray;
21-
use pocketmine\network\mcpe\compression\DecompressionException;
2220
use pocketmine\network\mcpe\compression\ZlibCompressor;
2321
use pocketmine\network\mcpe\convert\TypeConverter;
2422
use pocketmine\network\mcpe\EntityEventBroadcaster;
2523
use pocketmine\network\mcpe\NetworkSession;
2624
use pocketmine\network\mcpe\PacketBroadcaster;
27-
use pocketmine\network\mcpe\protocol\PacketDecodeException;
2825
use pocketmine\network\mcpe\protocol\PacketPool;
29-
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
30-
use pocketmine\network\mcpe\protocol\types\CompressionAlgorithm;
3126
use pocketmine\network\mcpe\raklib\PthreadsChannelReader;
3227
use pocketmine\network\mcpe\raklib\PthreadsChannelWriter;
3328
use pocketmine\network\NetworkInterface;
@@ -37,16 +32,13 @@
3732
use pocketmine\Server;
3833
use pocketmine\snooze\SleeperHandlerEntry;
3934
use pocketmine\thread\ThreadCrashException;
40-
use pocketmine\timings\Timings;
4135
use pocketmine\utils\Binary;
4236
use pocketmine\utils\BinaryDataException;
43-
use pocketmine\utils\BinaryStream;
4437
use Socket;
4538
use ThreadedArray;
4639
use WeakMap;
4740
use function base64_encode;
4841
use function bin2hex;
49-
use function ord;
5042
use function socket_close;
5143
use function socket_create_pair;
5244
use function socket_last_error;
@@ -55,7 +47,6 @@
5547
use function strlen;
5648
use function substr;
5749
use function trim;
58-
use function zstd_uncompress;
5950
use const AF_INET;
6051
use const AF_UNIX;
6152
use const SOCK_STREAM;
@@ -148,8 +139,6 @@ public function __construct(PluginBase $plugin, int $port, ?string $composerPath
148139
$this->sendBytes = 0;
149140
$this->receiveBytes = 0;
150141
}), 20, 20);
151-
152-
$server->getPluginManager()->registerEvents(new ProxyListener(), $plugin);
153142
}
154143

155144
public static function handleRawLatency(NetworkSession $session, int $upstream, int $downstream): void
@@ -217,82 +206,28 @@ private function onPacketReceive(string $buffer): void
217206
break;
218207
case ForwardPacket::NETWORK_ID:
219208
/** @var ForwardPacket $pk */
220-
if (($session = $this->getSession($socketId)) === null) {
209+
if (($session = $this->getSession($socketId)) === null || !(fn() => $this->connected)->call($session)) {
221210
break; // might be data arriving from the client after the server has closed the connection
222211
}
223212

224-
$this->handleEncoded($session, $pk->payload);
225-
$this->receiveBytes += strlen($pk->payload);
226-
break;
227-
}
228-
} catch (PacketHandlingException|BinaryDataException $exception) {
229-
$this->close($socketId, 'Error handling a Packet (Server)');
230-
231-
$this->server->getLogger()->logException($exception);
232-
}
233-
}
234-
235-
/**
236-
* @throws PacketHandlingException
237-
*/
238-
public function handleEncoded(NetworkSession $session, string $payload): void
239-
{
240-
if (!(fn() => $this->connected)->call($session)) {
241-
return;
242-
}
243-
244-
Timings::$playerNetworkReceive->startTiming();
245-
try {
246-
(fn() => $this->packetBatchLimiter->decrement())->call($session);
247-
248-
if (strlen($payload) < 1) {
249-
throw new PacketHandlingException("No bytes in payload");
250-
}
251-
252-
Timings::$playerNetworkReceiveDecompress->startTiming();
253-
$compressionType = ord($payload[0]);
254-
$compressed = substr($payload, 1);
255-
256-
try {
257-
$decompressed = match ($compressionType) {
258-
CompressionAlgorithm::NONE => $compressed,
259-
CompressionAlgorithm::ZLIB => $session->getCompressor()->decompress($compressed),
260-
CompressionAlgorithm::NONE - 1 => ($d = zstd_uncompress($compressed)) === false ? throw new DecompressionException("Failed to decompress packet") : $d,
261-
default => throw new PacketHandlingException("Packet compressed with unexpected compression type $compressionType")
262-
};
263-
} catch (ErrorException|DecompressionException $e) {
264-
$session->getLogger()->debug("Failed to decompress packet: " . base64_encode($compressed));
265-
throw PacketHandlingException::wrap($e, "Compressed packet batch decode error");
266-
} finally {
267-
Timings::$playerNetworkReceiveDecompress->stopTiming();
268-
}
269-
270-
try {
271-
$stream = new BinaryStream($decompressed);
272-
$count = 0;
273-
foreach (PacketBatch::decodeRaw($stream) as $buffer) {
274-
(fn() => $this->gamePacketLimiter->decrement())->call($session);
275-
if (++$count > 100) {
276-
throw new PacketHandlingException("Too many packets in batch");
277-
}
278-
$packet = PacketPool::getInstance()->getPacket($buffer);
213+
$packet = PacketPool::getInstance()->getPacket($pk->payload);
279214
if ($packet === null) {
280-
$session->getLogger()->debug("Unknown packet: " . base64_encode($buffer));
215+
$session->getLogger()->debug("Unknown packet: " . base64_encode($pk->payload));
281216
throw new PacketHandlingException("Unknown packet received");
282217
}
283218
try {
284-
$session->handleDataPacket($packet, $buffer);
219+
$session->handleDataPacket($packet, $pk->payload);
285220
} catch (PacketHandlingException $e) {
286-
$session->getLogger()->debug($packet->getName() . ": " . base64_encode($buffer));
221+
$session->getLogger()->debug($packet->getName() . ": " . base64_encode($pk->payload));
287222
throw PacketHandlingException::wrap($e, "Error processing " . $packet->getName());
288223
}
289-
}
290-
} catch (PacketDecodeException|BinaryDataException $e) {
291-
$session->getLogger()->logException($e);
292-
throw PacketHandlingException::wrap($e, "Packet batch decode error");
224+
$this->receiveBytes += strlen($pk->payload);
225+
break;
293226
}
294-
} finally {
295-
Timings::$playerNetworkReceive->stopTiming();
227+
} catch (PacketHandlingException|BinaryDataException $exception) {
228+
$this->close($socketId, 'Error handling a Packet (Server)');
229+
230+
$this->server->getLogger()->logException($exception);
296231
}
297232
}
298233

0 commit comments

Comments
 (0)