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

Unable to encode/decode packet (nemisys)

Discussion in 'Development' started by Kyd, Aug 31, 2018.

  1. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    I'm trying to implement SynapseAPI for PocketMine-MP, but whenever I encode and decode ConnectPacket the data aren't correct
    PHP:
    <?php

    namespace SynapseAPI\network\protocol;

    class 
    ConnectPacket extends DataPacket {
       const 
    NETWORK_ID Info::CONNECT_PACKET;

       public 
    $protocol Info::CURRENT_PROTOCOL;
       public 
    $maxPlayers;
       public 
    $isMainServer;
       public 
    $isLobbyServer true;
       public 
    $transferShutdown true;
       public 
    $description;
       public 
    $password;

       public function 
    encode() {
          
    $this->reset();
          
    $this->putInt($this->protocol);
          
    $this->putInt($this->maxPlayers);
          
    $this->putBool($this->isMainServer);
          
    $this->putBool($this->isLobbyServer);
          
    $this->putBool($this->transferShutdown);
          
    $this->putString($this->description);
          
    $this->putString($this->password);
       }

       public function 
    decode() {
          
    $this->protocol $this->getInt();
          
    $this->maxPlayers $this->getInt();
          
    $this->isMainServer $this->getBool();
          
    $this->isLobbyServer $this->getBool();
          
    $this->transferShutdown $this->getBool();
          
    $this->description $this->getString();
          
    $this->password $this->getString();
       }
    }
    PHP:
    /**
    * @return string
    */
    public function getString() : string{
        return 
    $this->get($this->getUnsignedVarInt());
    }

    /**
    * @param string $v
    */
    public function putString(string $v) : void{
        
    $this->putUnsignedVarInt(strlen($v));
        
    $this->put($v);
    }
    Other functions like getBool(), putInt() etc are from BinaryStream

    Now I encode & decode the packet
    PHP:
    $pk = new ConnectPacket();
    $pk->password "test123456789";
    $pk->isMainServer true;
    $pk->isLobbyServer true;
    $pk->description "test";
    $pk->maxPlayers 20;
    $pk->protocol 10;
    $pk->encode();
    $pk->decode();
    Result:
    Code:
    object(SynapseAPI\network\protocol\ConnectPacket)#181 (9) {
      ["protocol"]=>
      int(33554432)
      ["maxPlayers"]=>
      int(167772160)
      ["isMainServer"]=>
      bool(true)
      ["isLobbyServer"]=>
      bool(true)
      ["transferShutdown"]=>
      bool(true)
      ["description"]=>
      string(1) ""
      ["password"]=>
    test123456789"est
      ["offset"]=>
      int(130)
      ["buffer"]=>
      string(31) "
    test123456789"
    }
    
    not sure what is causing 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.