PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
StandardPacketBroadcaster.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe;
25
26use pmmp\encoding\ByteBufferWriter;
31use function count;
32use function log;
33use function spl_object_id;
34use function strlen;
35
37 public function __construct(
38 private Server $server
39 ){}
40
41 public function broadcastPackets(array $recipients, array $packets) : void{
42 //TODO: this shouldn't really be called here, since the broadcaster might be replaced by an alternative
43 //implementation that doesn't fire events
44 if(DataPacketSendEvent::hasHandlers()){
45 $ev = new DataPacketSendEvent($recipients, $packets);
46 $ev->call();
47 if($ev->isCancelled()){
48 return;
49 }
50 $packets = $ev->getPackets();
51 }
52
53 $compressors = [];
54
55 $targetsByCompressor = [];
56 foreach($recipients as $recipient){
57 //TODO: different compressors might be compatible, it might not be necessary to split them up by object
58 $compressor = $recipient->getCompressor();
59 $compressors[spl_object_id($compressor)] = $compressor;
60
61 $targetsByCompressor[spl_object_id($compressor)][] = $recipient;
62 }
63
64 $totalLength = 0;
65 $packetBuffers = [];
66 $writer = new ByteBufferWriter();
67 foreach($packets as $packet){
68 $writer->clear(); //memory reuse let's gooooo
69 $buffer = NetworkSession::encodePacketTimed($writer, $packet);
70 //varint length prefix + packet buffer
71 $totalLength += (((int) log(strlen($buffer), 128)) + 1) + strlen($buffer);
72 $packetBuffers[] = $buffer;
73 }
74
75 foreach($targetsByCompressor as $compressorId => $compressorTargets){
76 $compressor = $compressors[$compressorId];
77
78 $threshold = $compressor->getCompressionThreshold();
79 if(count($compressorTargets) > 1 && $threshold !== null && $totalLength >= $threshold){
80 //do not prepare shared batch unless we're sure it will be compressed
81 $stream = new ByteBufferWriter();
82 PacketBatch::encodeRaw($stream, $packetBuffers);
83 $batchBuffer = $stream->getData();
84
85 $batch = $this->server->prepareBatch($batchBuffer, $compressor, timings: Timings::$playerNetworkSendCompressBroadcast);
86 foreach($compressorTargets as $target){
87 $target->queueCompressed($batch);
88 }
89 }else{
90 foreach($compressorTargets as $target){
91 foreach($packetBuffers as $packetBuffer){
92 $target->addToSendBuffer($packetBuffer);
93 }
94 }
95 }
96 }
97 }
98}