PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
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
32use function count;
33use function log;
34use function spl_object_id;
35use function strlen;
36
38 public function __construct(
39 private Server $server
40 ){}
41
42 public function broadcastPackets(array $recipients, array $packets) : void{
43 //TODO: this shouldn't really be called here, since the broadcaster might be replaced by an alternative
44 //implementation that doesn't fire events
45 if(DataPacketSendEvent::hasHandlers()){
46 $ev = new DataPacketSendEvent($recipients, $packets);
47 $ev->call();
48 if($ev->isCancelled()){
49 return;
50 }
51 $packets = $ev->getPackets();
52 }
53
54 $compressors = [];
55
57 $targetsByCompressor = [];
58 foreach($recipients as $recipient){
59 //TODO: different compressors might be compatible, it might not be necessary to split them up by object
60 $compressor = $recipient->getCompressor();
61 $compressors[spl_object_id($compressor)] = $compressor;
62
63 $targetsByCompressor[spl_object_id($compressor)][] = $recipient;
64 }
65
66 $totalLength = 0;
67 $packetBuffers = [];
68 foreach($packets as $packet){
69 $buffer = NetworkSession::encodePacketTimed(PacketSerializer::encoder(), $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 BinaryStream();
82 PacketBatch::encodeRaw($stream, $packetBuffers);
83 $batchBuffer = $stream->getBuffer();
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}