Fixed some logic issues with async compression

This commit is contained in:
Dylan K. Taylor 2017-05-19 11:30:23 +01:00
parent 905d3f1610
commit 36c95660e6
2 changed files with 21 additions and 20 deletions

View File

@ -1787,8 +1787,8 @@ class Server{
$pk->addPacket($p);
}
if(!$forceSync and $this->networkCompressionAsync){
$task = new CompressBatchedTask($pk, $targets, $this->networkCompressionLevel, $immediate);
if(!$forceSync and !$immediate and $this->networkCompressionAsync){
$task = new CompressBatchedTask($pk, $targets, $this->networkCompressionLevel);
$this->getScheduler()->scheduleAsyncTask($task);
}else{
$pk->compress($this->networkCompressionLevel);
@ -1799,9 +1799,11 @@ class Server{
Timings::$playerNetworkTimer->stopTiming();
}
public function broadcastPacketsCallback(BatchPacket $pk, array $identifiers, bool $immediate){
$pk->encode();
$pk->isEncoded = true;
public function broadcastPacketsCallback(BatchPacket $pk, array $identifiers, bool $immediate = false){
if(!$pk->isEncoded){
$pk->encode();
$pk->isEncoded = true;
}
if($immediate){
foreach($identifiers as $i){

View File

@ -29,30 +29,29 @@ class CompressBatchedTask extends AsyncTask{
public $level = 7;
public $data;
public $final;
public $targets;
public $immediate = false;
public function __construct(BatchPacket $data, array $targets, $level = 7, bool $sendImmediate = false){
$this->data = serialize($data);
$this->targets = $targets;
public function __construct(BatchPacket $batch, array $targets, $level = 7){
$this->data = $batch->payload;
$this->targets = serialize($targets);
$this->level = $level;
$this->immediate = $sendImmediate;
}
public function onRun(){
try{
/** @var BatchPacket $pk */
$pk = unserialize($this->data);
$pk->compress($this->level);
$this->final = serialize($pk);
$this->data = null;
}catch(\Throwable $e){
$batch = new BatchPacket();
$batch->payload = $this->data;
$this->data = null;
}
$batch->compress($this->level);
$batch->encode();
$this->setResult($batch->buffer, false);
}
public function onCompletion(Server $server){
$server->broadcastPacketsCallback(unserialize($this->final), (array) $this->targets, $this->immediate);
$pk = new BatchPacket($this->getResult());
$pk->isEncoded = true;
$pk->compressed = true;
$server->broadcastPacketsCallback($pk, unserialize($this->targets));
}
}