/** * Build packet data * * @param int & $length * @return string * @throws Exception */ public final function build(&$length = 0) { if ($this->protocol_type == self::FIXED_ONLY) { $payload = $this->payload(); } else { if ($this->protocol_type == self::WITH_VARIABLE) { $payload = $this->payload(); } else { if ($this->protocol_type == self::WITH_PAYLOAD) { $payload = $this->payload(); } else { throw new Exception('Invalid protocol type'); } } } $length = strlen($payload); $this->header->setPayloadLength($length); $length = $this->header->getFullLength(); Debug::Log(Debug::DEBUG, 'Message Build: total length=' . $length); return $this->header->build() . $payload; }
/** * Build fixed Header packet * * @return string * @throws Exception */ public function build() { $flags = 0; if (!$this->getQos()) { if ($this->getDup()) { /* In the QoS 0 delivery protocol, the Sender MUST send a PUBLISH packet with QoS=0, DUP=0 [MQTT-4.3.1-1]. */ throw new Exception('DUP MUST BE 0 if QoS is 0'); } } /** * Flags for fixed Header * * This 4-bit number was defined as DUP,QoS 1,QoS 0,RETAIN in MQTT 3.1, * In 3.1.1, only PUBLISH has those names, for PUBREL, SUBSCRIBE, UNSUBSCRIBE: 0010; and others, 0000 * * The definition DUP, QoS, RETAIN works in 3.1.1, and literally, * it means the same for PUBREL, SUBSCRIBE and UNSCRIBE. */ $flags |= $this->dup << 3; $flags |= $this->qos << 1; $flags |= $this->retain; $this->reserved_flags = $flags; return parent::build(); }