Exemple #1
0
 /**
  * Decode Packet Header and returns payload position.
  *
  * @param string & $packet_data
  * @param int    $remaining_length
  * @param int    & $payload_pos
  * @throws \sskaje\mqtt\Exception
  */
 public final function decode(&$packet_data, $remaining_length, &$payload_pos)
 {
     $cmd = Utility::ParseCommand(ord($packet_data[0]));
     $message_type = $cmd['message_type'];
     if ($this->message->getMessageType() != $message_type) {
         throw new Exception('Unexpected Control Packet Type');
     }
     $flags = $cmd['flags'];
     $this->setFlags($flags);
     $pos = 1;
     $rl_len = strlen($this->remaining_length_bytes = Utility::EncodeLength($remaining_length));
     if (strpos($packet_data, $this->remaining_length_bytes) !== $pos) {
         throw new Exception('Remaining Length mismatch.');
     }
     $pos += $rl_len;
     $this->remaining_length = $remaining_length;
     $this->decodeVariableHeader($packet_data, $pos);
     $payload_pos = $pos;
 }
Exemple #2
0
 /**
  * Read Message And Create Message Object
  *
  * @return \sskaje\mqtt\Message\Base
  * @throws \sskaje\mqtt\Exception
  */
 protected function message_read()
 {
     if ($this->socket->eof()) {
         if (++$this->count_eof > 5) {
             usleep(pow(2, $this->count_eof));
         }
         Debug::Log(Debug::NOTICE, 'message_read(): EOF ' . $this->count_eof);
         if ($this->count_eof > $this->max_eof) {
             throw new Exception\NetworkError();
         }
         return false;
     }
     # Reset EOF counter
     $this->count_eof = 0;
     # read 2 bytes
     $read_fh_bytes = 2;
     $read_more_length_bytes = 3;
     $read_bytes = 0;
     $read_message = $this->socket->read($read_fh_bytes);
     if (empty($read_message)) {
         throw new Exception('WTFFFFFF!!!! ');
     }
     $read_bytes += $read_fh_bytes;
     $cmd = Utility::ParseCommand(ord($read_message[0]));
     $message_type = $cmd['message_type'];
     $flags = $cmd['flags'];
     Debug::Log(Debug::DEBUG, "message_read(): message_type=" . Message::$name[$message_type] . ", flags={$flags}");
     if (ord($read_message[1]) > 0x7f) {
         # read 3 more bytes
         $read_message .= $this->socket->read($read_more_length_bytes);
         $read_bytes += $read_more_length_bytes;
     }
     $pos = 1;
     $remaining_length = Utility::DecodeLength($read_message, $pos);
     $to_read = 0;
     if ($remaining_length) {
         $to_read = $remaining_length - ($read_bytes - $pos);
     }
     Debug::Log(Debug::DEBUG, 'message_read(): remaining length=' . $remaining_length . ', data to read=' . $to_read);
     if ($to_read) {
         $read_message .= $this->socket->read($to_read);
     }
     Debug::Log(Debug::DEBUG, 'message_read(): Dump', $read_message);
     $message_object = $this->getMessageObject($message_type);
     $message_object->decode($read_message, $remaining_length);
     return $message_object;
 }