Ejemplo n.º 1
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.º 2
0
 /**
  * Reads the remaining length from the given stream.
  *
  * @param PacketStream $stream
  *
  * @throws MalformedPacketException
  */
 private function readRemainingLength(PacketStream $stream)
 {
     $this->remainingPacketLength = 0;
     $multiplier = 1;
     do {
         $encodedByte = $stream->readByte();
         $this->remainingPacketLength += ($encodedByte & 127) * $multiplier;
         $multiplier *= 128;
         if ($multiplier > 128 * 128 * 128 * 128) {
             throw new MalformedPacketException('Malformed remaining length.');
         }
     } while (($encodedByte & 128) !== 0);
 }