PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
56 $targetsByCompressor = [];
57 foreach($recipients as $recipient){
58 //TODO: different compressors might be compatible, it might not be necessary to split them up by object
59 $compressor = $recipient->getCompressor();
60 $compressors[spl_object_id($compressor)] = $compressor;
61
62 $targetsByCompressor[spl_object_id($compressor)][] = $recipient;
63 }
64
65 $totalLength = 0;
66 $packetBuffers = [];
67 foreach($packets as $packet){
68 $buffer = NetworkSession::encodePacketTimed(PacketSerializer::encoder(), $packet);
69 //varint length prefix + packet buffer
70 $totalLength += (((int) log(strlen($buffer), 128)) + 1) + strlen($buffer);
71 $packetBuffers[] = $buffer;
72 }
73
74 foreach($targetsByCompressor as $compressorId => $compressorTargets){
75 $compressor = $compressors[$compressorId];
76
77 $threshold = $compressor->getCompressionThreshold();
78 if(count($compressorTargets) > 1 && $threshold !== null && $totalLength >= $threshold){
79 //do not prepare shared batch unless we're sure it will be compressed
80 $stream = new BinaryStream();
81 PacketBatch::encodeRaw($stream, $packetBuffers);
82 $batchBuffer = $stream->getBuffer();
83
84 $batch = $this->server->prepareBatch($batchBuffer, $compressor, timings: Timings::$playerNetworkSendCompressBroadcast);
85 foreach($compressorTargets as $target){
86 $target->queueCompressed($batch);
87 }
88 }else{
89 foreach($compressorTargets as $target){
90 foreach($packetBuffers as $packetBuffer){
91 $target->addToSendBuffer($packetBuffer);
92 }
93 }
94 }
95 }
96 }
97}