Implement send buffering and queuing for network sessions (#2358)

Async compression and broadcasts are now reliable and don't have race condition bugs.
This features improved performance and significantly reduced bandwidth wastage.

Reduce Level broadcast latency by ticking network after levels. This ensures that session buffers get flushed as soon as possible after level tick, if level broadcasts were done.
This commit is contained in:
Dylan K. Taylor
2018-08-13 14:37:18 +01:00
committed by GitHub
parent 22c8077bdf
commit 15bac8c58a
9 changed files with 225 additions and 87 deletions

View File

@@ -25,6 +25,7 @@ namespace pocketmine\inventory;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\network\mcpe\CompressBatchPromise;
use pocketmine\network\mcpe\NetworkCompression;
use pocketmine\network\mcpe\PacketStream;
use pocketmine\network\mcpe\protocol\CraftingDataPacket;
@@ -38,7 +39,7 @@ class CraftingManager{
/** @var FurnaceRecipe[] */
protected $furnaceRecipes = [];
/** @var string */
/** @var CompressBatchPromise */
private $craftingDataCache;
public function __construct(){
@@ -105,16 +106,18 @@ class CraftingManager{
$batch = new PacketStream();
$batch->putPacket($pk);
$this->craftingDataCache = NetworkCompression::compress($batch->buffer);
$this->craftingDataCache = new CompressBatchPromise();
$this->craftingDataCache->resolve(NetworkCompression::compress($batch->buffer));
Timings::$craftingDataCacheRebuildTimer->stopTiming();
}
/**
* Returns a pre-compressed CraftingDataPacket for sending to players. Rebuilds the cache if it is not found.
*
* @return string
* @return CompressBatchPromise
*/
public function getCraftingDataPacket() : string{
public function getCraftingDataPacket() : CompressBatchPromise{
if($this->craftingDataCache === null){
$this->buildCraftingDataCache();
}