Ejemplo n.º 1
0
 public function write(PacketStream $stream)
 {
     $data = new PacketStream();
     $data->writeWord($this->generateIdentifier());
     $data->writeString($this->topic);
     $this->remainingPacketLength = $data->length();
     parent::write($stream);
     $stream->write($data->getData());
 }
Ejemplo n.º 2
0
 public function write(PacketStream $stream)
 {
     $data = new PacketStream();
     $data->writeWord($this->generateIdentifier());
     foreach ($this->returnCodes as $returnCode) {
         $data->writeByte($returnCode);
     }
     $this->remainingPacketLength = $data->length();
     parent::write($stream);
     $stream->write($data->getData());
 }
Ejemplo n.º 3
0
 /**
  * Writes the remaining length to the given stream.
  *
  * @param PacketStream $stream
  */
 private function writeRemainingLength(PacketStream $stream)
 {
     $x = $this->remainingPacketLength;
     do {
         $encodedByte = $x % 128;
         $x = (int) ($x / 128);
         if ($x > 0) {
             $encodedByte |= 128;
         }
         $stream->writeByte($encodedByte);
     } while ($x > 0);
 }
Ejemplo n.º 4
0
 /**
  * Appends the given data to the internal buffer and parses it.
  *
  * @param string $data
  *
  * @return Packet[]
  */
 public function push($data)
 {
     $this->buffer->write($data);
     $result = [];
     while ($this->buffer->getRemainingBytes() > 0) {
         $type = $this->buffer->readByte() >> 4;
         try {
             $packet = $this->factory->build($type);
         } catch (UnknownPacketTypeException $e) {
             $this->handleError($e);
             continue;
         }
         $this->buffer->seek(-1);
         $position = $this->buffer->getPosition();
         try {
             $packet->read($this->buffer);
             $result[] = $packet;
             $this->buffer->cut();
         } catch (EndOfStreamException $e) {
             $this->buffer->setPosition($position);
             break;
         } catch (MalformedPacketException $e) {
             $this->handleError($e);
         }
     }
     return $result;
 }
Ejemplo n.º 5
0
 public function write(PacketStream $stream)
 {
     if ($this->clientID === '') {
         $this->clientID = 'BinSoul' . mt_rand(100000, 999999);
     }
     $data = new PacketStream();
     $data->writeString($this->protocolName);
     $data->writeByte($this->protocolLevel);
     $data->writeByte($this->flags);
     $data->writeWord($this->keepAlive);
     $data->writeString($this->clientID);
     if ($this->hasWill()) {
         $data->writeString($this->willTopic);
         $data->writeString($this->willMessage);
     }
     if ($this->hasUsername()) {
         $data->writeString($this->username);
     }
     if ($this->hasPassword()) {
         $data->writeString($this->password);
     }
     $this->remainingPacketLength = $data->length();
     parent::write($stream);
     $stream->write($data->getData());
 }