1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to use Async and dataPacket together?

Discussion in 'Development' started by asyven, Apr 2, 2017.

  1. asyven

    asyven Spider

    Messages:
    14
    Guys, i wanna load images in maps Asynchronously
    but core only dumped
    PHP:
    class AsyncUpdate extends AsyncTask{
        private 
    $player;
        private 
    $mapId;
        private 
    $color;
        private 
    $pk;
        
        function 
    __construct(Player $playerint $mapId$color){
            
    $this->player $player;
            
    $this->mapId $mapId;
            
    $this->color $color;
        }

        function 
    onRun( ){
            
    $this->pk = new ClientboundMapItemDataPacket();
            
    $this->pk->mapId $this->mapId;
            
    $this->pk->scale 0;
            
    $this->pk->width 128;
            
    $this->pk->height 128;
            
    $this->pk->colors $this->color;
        }
        
        function 
    onCompletion(Server $server){
            
    //$this->player->dataPacket($this->pk); - Segmentation fault (core dumped)
            //$server->broadcastPacket([$this->player],$this->pk); - Segmentation fault (core dumped)
            
    unset($this->pk);
            unset(
    $this->player);
            unset(
    $this->mapId);
            unset(
    $this->color);
        }
    }

    //func in main
     
    public function sendMap(Player $playerint $mapId$color) {
            
    $this->getServer()->getScheduler()->scheduleAsyncTask(new AsyncUpdate($player$mapId$color));
        }

     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    PHP:
    /**
     * @param $player string
     */
    function __construct(string $playerint $mapId$color){
            
    $this->player $player;
            
    $this->mapId $mapId;
            
    $this->color $color;
    }

    public function 
    onRun( ) {
            
    $pk = new ClientboundMapItemDataPacket();
            
    $pk->mapId $this->mapId;
            
    $pk->scale 0;
            
    $pk->width 128;
            
    $pk->height 128;
            
    $pk->colors $this->color;
            
    $this->setResult($pk);
    }

    public function 
    onCompletion(Server $server) {
           if((
    $player $server->getPlayer($this->player))) {
                  
    $player->dataPacket($this->getResult());
           }
    }
    Don't pass objects through threads. Except ones that can be serialized
     
    Muqsit and asyven like this.
  3. asyven

    asyven Spider

    Messages:
    14
    Code:
    Error: pthreads detected an attempt to connect to an object which has already been destroyed
    File: /src/pocketmine/scheduler/AsyncTask
    Line: 85
    Type: notice
    
    Code:
    [76]
    [77]     public function isCrashed(){
    [78]         return $this->crashed;
    [79]     }
    [80]
    [81]     /**
    [82]      * @return mixed
    [83]      */
    [84]     public function getResult(){
    [85]         return $this->serialized ? unserialize($this->result) : $this->result;
    [86]     }
     
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    Oh yeah, didn't notice. By doing Player::dataPacket($this->getResult()) the packet (object) is passed through threads. Easiest way to resolve this is to, break packet data into primitives and build the packet back together in the main thread.
     
    asyven likes this.
  5. asyven

    asyven Spider

    Messages:
    14
    Tell me more about this, please.
     
  6. Primus

    Primus Zombie Pigman

    Messages:
    749
    Pass all object properties individually to main thread
    PHP:
    $server->getPluginManager()->getPlugin("MyPlugin")->constructAndSendMapPacket(
        
    $pk->scale$pk->width$pk->height$pk->colors$pk->mapId$this->playerName
    );
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The whole task is worthless. The laggy part either comes from encoding data for sending, or in the process of sending. You are only setting fields in the task, which does not give any improvement at all.
     
    Muqsit likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.