PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
PacketBatch.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol\serializer;
16
22use function strlen;
23
25
26 private function __construct(){
27 //NOOP
28 }
29
34 final public static function decodeRaw(BinaryStream $stream) : \Generator{
35 $c = 0;
36 while(!$stream->feof()){
37 try{
38 $length = $stream->getUnsignedVarInt();
39 $buffer = $stream->get($length);
40 }catch(BinaryDataException $e){
41 throw new PacketDecodeException("Error decoding packet $c in batch: " . $e->getMessage(), 0, $e);
42 }
43 yield $buffer;
44 $c++;
45 }
46 }
47
52 final public static function encodeRaw(BinaryStream $stream, array $packets) : void{
53 foreach($packets as $packet){
54 $stream->putUnsignedVarInt(strlen($packet));
55 $stream->put($packet);
56 }
57 }
58
63 final public static function decodePackets(BinaryStream $stream, PacketPool $packetPool) : \Generator{
64 $c = 0;
65 foreach(self::decodeRaw($stream) as $packetBuffer){
66 $packet = $packetPool->getPacket($packetBuffer);
67 if($packet !== null){
68 try{
69 $packet->decode(PacketSerializer::decoder($packetBuffer, 0));
70 }catch(PacketDecodeException $e){
71 throw new PacketDecodeException("Error decoding packet $c in batch: " . $e->getMessage(), 0, $e);
72 }
73 yield $packet;
74 }else{
75 throw new PacketDecodeException("Unknown packet $c in batch");
76 }
77 $c++;
78 }
79 }
80
85 final public static function encodePackets(BinaryStream $stream, array $packets) : void{
86 foreach($packets as $packet){
87 $serializer = PacketSerializer::encoder();
88 $packet->encode($serializer);
89 $stream->putUnsignedVarInt(strlen($serializer->getBuffer()));
90 $stream->put($serializer->getBuffer());
91 }
92 }
93}
static encodePackets(BinaryStream $stream, array $packets)
Definition: PacketBatch.php:85
static decodePackets(BinaryStream $stream, PacketPool $packetPool)
Definition: PacketBatch.php:63
static encodeRaw(BinaryStream $stream, array $packets)
Definition: PacketBatch.php:52