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

AsyncTask

Discussion in 'Development' started by LucGamesDE, Mar 12, 2017.

  1. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    How can I create a AsyncTask? Do I need to create a new class or like a repeating task? I found nothing
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
  3. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I tried the method in your plugin. But I don't get it how I can display the infos if I run the command /info ip port
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    AsyncTask is not a repeating task. It's just a task that's ran parallel to your Minecraft server (main thread). It's used to call heavy (time-consuming) functions.
     
  5. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    Oh ok.
     
  6. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I tried this. But nothing happens

    PHP:
    public function getPlayers($ip$port) {
    $this->getServer()->getScheduler()->scheduleAsyncTask(new SSSAsyncTask($this->doCheckServers$this->debug20));
    return 
    $this->asyncTaskPlayers;
    }
    //echo getPlayers() onCommand...

    //Task
    class SSSAsyncTask extends AsyncTask{
      private 
    $serverFINALdata;
      private 
    $doCheckServer;
     
      private 
    $startTick;
     
      public function 
    __construct(array $doCheckServersbool $debugint $startTick){
       
    $this->doCheckServer $doCheckServers;
       
    $this->debug $debug;
       
    $this->startTick $startTick;
      }
     
      private function 
    doQuery($ipint $port): array{
         if(
    $this->debug){
          echo(
    "doQuery:\n");
         }
          
    $sock = @fsockopen("udp://".$ip,$port);
          if(!
    $sock){return [-1NULL];}
          
    socket_set_timeout($sock0500000);
          if(!@
    fwrite($sock"\xFE\xFD\x09\x10\x20\x30\x40\xFF\xFF\xFF\x01")){return [0NULL];}
          
    $challenge fread($sock1400);
          if(!
    $challenge){return [0NULL];}
          
    $challenge substr(preg_replace("/[^0-9\-]/si"""$challenge ), 1);
          
    $query sprintf(
              
    "\xFE\xFD\x00\x10\x20\x30\x40%c%c%c%c\xFF\xFF\xFF\x01",
              (
    $challenge >> 24),
              (
    $challenge >> 16),
              (
    $challenge >> 8),
              (
    $challenge >> 0)
              );
          if(!@
    fwrite($sock$query)){return [0NULL];}
          
    $response = array();
          for(
    $x 0$x 2$x++){
              
    $response[] = @fread($sock,2048);
          }
       if(
    $this->debug){
           
    var_dump($response);
          }
          
    $response implode($response);
          
    $response substr($response,16);
          
    $response explode("\0",$response);
       if(
    $this->debug){
          
    var_dump($response);
       }
          
    array_pop($response);
          
    array_pop($response);
          
    array_pop($response);
          
    array_pop($response);
          
    $return = [];
          
    $type 0;
       if(
    $this->debug){
        
    var_dump($response);
          }
          foreach (
    $response as $key){
              if (
    $type == 0$val $key;
              if (
    $type == 1$return[$val] = $key;
              
    $type == $type $type 0;
       }
         return [
    1$return];
         if(
    $this->debug){
        echo(
    "DoQueryEnd\n");
         }
      }
     
      public function 
    onRun(){
         if(
    $this->debug){
        echo(
    "DoCheckServer:\n");
        
    var_dump($this->doCheckServer);
         }
       foreach(
    $this->doCheckServer as $server){
        
    $doCheck $server[1];
        if(
    $doCheck){
         
    $ip $server[0];
         
    $deParsedIP $ip[0];
         
    $port $ip[1];
         
    $return $this->doQuery($deParsedIP$port);
         
    $returnState $return[0];
         
    $queryResult $return[1];
         
    $serverData = [];
         if(
    $this->debug){
           echo(
    "returnState:\n");
          
    var_dump($returnState);
         }
         switch(
    $returnState){
          case -
    1;
           
    $serverData[2] = false;
            break;
          case 
    0:
             
    $serverData[2] = false;
          break;
          case 
    1:
              
    $serverData[0] = [$queryResult['numplayers'], $queryResult['maxplayers']];
              
    $serverData[1] = $queryResult['hostname'];
           
    $serverData[2] = true;
         }
         
    $serverFINALdata[$deParsedIP.$port] = $serverData;
         
    $this->setResult($serverFINALdata);
        }
       }
       if(
    $this->debug){
        echo(
    "\n");
       }
      }
      }
    }
     
  7. hoyinm14mc

    hoyinm14mc Silverfish

    Messages:
    22
    What is the use of $ip and $port parameter in the function getPlayers()?
     
  8. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I send the ip and port from the command. That's works. But I don't know how to finish it
     
  9. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    If I run this code every second, I get *this as result
    PHP:
    class SSSAsyncTask extends AsyncTask{
      private 
    $serverFINALdata;
      private 
    $doCheckServer;
     
      private 
    $startTick;
     
      public function 
    __construct(array $doCheckServersbool $debugint $startTick){
       
    $this->doCheckServer $doCheckServers;
       
    $this->debug $debug;
       
    $this->startTick $startTick;
      }
     
      private function 
    doQuery($ip "sg.lbsg.net"$port 19132): array{
         if(
    $this->debug){
          echo(
    "doQuery:\n");
         }
          
    $sock = @fsockopen("udp://".$ip,$port);
          if(!
    $sock){return [-1NULL];}
          
    socket_set_timeout($sock0500000);
          if(!@
    fwrite($sock"\xFE\xFD\x09\x10\x20\x30\x40\xFF\xFF\xFF\x01")){return [0NULL];}
          
    $challenge fread($sock1400);
          if(!
    $challenge){return [0NULL];}
          
    $challenge substr(preg_replace("/[^0-9\-]/si"""$challenge ), 1);
          
    $query sprintf(
              
    "\xFE\xFD\x00\x10\x20\x30\x40%c%c%c%c\xFF\xFF\xFF\x01",
              (
    $challenge >> 24),
              (
    $challenge >> 16),
              (
    $challenge >> 8),
              (
    $challenge >> 0)
              );
          if(!@
    fwrite($sock$query)){return [0NULL];}
          
    $response = array();
          for(
    $x 0$x 2$x++){
              
    $response[] = @fread($sock,2048);
          }
       if(
    $this->debug){
           
    var_dump($response);
          }
          
    $response implode($response);
          
    $response substr($response,16);
          
    $response explode("\0",$response);
       if(
    $this->debug){
          
    var_dump($response);
       }
          
    array_pop($response);
          
    array_pop($response);
          
    array_pop($response);
          
    array_pop($response);
          
    $return = [];
          
    $type 0;
       if(
    $this->debug){
        
    var_dump($response);
          }
          foreach (
    $response as $key){
              if (
    $type == 0$val $key;
              if (
    $type == 1$return[$val] = $key;
              
    $type == $type $type 0;
       }
         return [
    1$return];
         if(
    $this->debug){
        echo(
    "DoQueryEnd\n");
         }
      }
     
      public function 
    onRun(){
         if(
    $this->debug){
        echo(
    "DoCheckServer:\n");
        
    var_dump($this->doCheckServer);
         }
       foreach(
    $this->doCheckServer as $server){
        
    $doCheck $server[1];
        if(
    $doCheck){
         
    $ip $server[0];
         
    $deParsedIP $ip[0];
         
    $port $ip[1];
         
    $return $this->doQuery($deParsedIP$port);
         
    $returnState $return[0];
         
    $queryResult $return[1];
         
    $serverData = [];
         if(
    $this->debug){
           echo(
    "returnState:\n");
          
    var_dump($returnState);
         }
         switch(
    $returnState){
          case -
    1;
           
    $serverData[2] = false;
            break;
          case 
    0:
             
    $serverData[2] = false;
          break;
          case 
    1:
              
    $serverData[0] = [$queryResult['numplayers'], $queryResult['maxplayers']];
              
    $serverData[1] = $queryResult['hostname'];
           
    $serverData[2] = true;
         }
         
    $serverFINALdata[$deParsedIP.$port] = $serverData;
         
    $this->setResult($serverFINALdata);
        }
       }
       if(
    $this->debug){
        echo(
    "\n");
       }
      }
     
      public function 
    onCompletion(Server $server){
       
    $server->getPluginManager()->getPlugin("myPlugin")->asyncTaskCallBack($this->getResult(), $this->startTick);
      }
    }
    *This Result
    Code:
    DoCheckServer:                                                                      
    object(Volatile)#4314 (0) {
    }
     
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Add a debug line at the point exactly before you set the result to see what you are setting as result.
     
  11. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Also you might want to not simply copy the async task class out of my plugin. It won't do much if you don't pass correct data to it.
     
    jasonwynn10 likes this.
  12. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I tried something around and now I have these:
    PHP:
    class API extends AsyncTask
    {
    public function 
    getPlayers($ip$port) {
    //MY CODE
    }
    This crashes my server on load with this error.

    rror: Class myPlugin\API contains 1 abstract method and must therefore be de
    clared abstract or implement the remaining methods (pocketmine\scheduler\AsyncTask::
    onRun) in
     
  13. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    you are missing the onRun function
     
    Muqsit likes this.
  14. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    How can I implement it? I run this not in the main file
     
  15. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
  16. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
  17. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    What's your reason behind using an AsyncTask?
     
  18. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    so querying server wont result in blocking of the main thread?
     
  19. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It won't affect the main thread in any way as long as it's done asynchronously.
     
  20. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    thats why to use AsyncTask
     
  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.