From a368732c2856184d865c7c985a9175352a708bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88tz=20Gottwald?= Date: Mon, 9 Sep 2019 21:12:38 +0200 Subject: [PATCH 1/5] remove output --- src/MqttClient.php | 4 ---- src/packet/PublishComplete.php | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/MqttClient.php b/src/MqttClient.php index 58e1683..ee752e0 100644 --- a/src/MqttClient.php +++ b/src/MqttClient.php @@ -13,7 +13,6 @@ use oliverlorenz\reactphpmqtt\packet\ControlPacket; use oliverlorenz\reactphpmqtt\packet\Disconnect; use oliverlorenz\reactphpmqtt\packet\Factory; -use oliverlorenz\reactphpmqtt\packet\MessageHelper; use oliverlorenz\reactphpmqtt\packet\PingRequest; use oliverlorenz\reactphpmqtt\packet\Publish; use oliverlorenz\reactphpmqtt\packet\Subscribe; @@ -85,7 +84,6 @@ private function listenForPackets(Connection $stream) try { foreach (Factory::getNextPacket($this->version, $rawData) as $packet) { $stream->emit($packet::EVENT, [$packet]); - echo "received:\t" . get_class($packet) . PHP_EOL; } } catch (ProtocolViolation $e) { @@ -133,7 +131,6 @@ public function sendConnectPacket(Connection $stream, ConnectionOptions $options $options->keepAlive ); $message = $packet->get(); - echo MessageHelper::getReadableByRawString($message); $deferred = new Deferred(); $stream->on(ConnectionAck::EVENT, function($message) use ($stream, $deferred) { @@ -153,7 +150,6 @@ public function sendConnectPacket(Connection $stream, ConnectionOptions $options private function sendPacketToStream(Connection $stream, ControlPacket $controlPacket) { - echo "send:\t\t" . get_class($controlPacket) . "\n"; $message = $controlPacket->get(); return $stream->write($message); diff --git a/src/packet/PublishComplete.php b/src/packet/PublishComplete.php index ec7576b..52f9a5b 100644 --- a/src/packet/PublishComplete.php +++ b/src/packet/PublishComplete.php @@ -13,6 +13,8 @@ */ class PublishComplete extends ControlPacket { + const EVENT = 'PUBLISH_COMPLETE'; + public static function getControlPacketType() { return ControlPacketType::PUBCOMP; From c05300496a0bde8c2c5a45ffca3e6db156a404cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88tz=20Gottwald?= Date: Tue, 10 Sep 2019 20:43:16 +0200 Subject: [PATCH 2/5] get packet length of packets > 127 --- src/packet/Factory.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/packet/Factory.php b/src/packet/Factory.php index 4f138af..db2e76e 100644 --- a/src/packet/Factory.php +++ b/src/packet/Factory.php @@ -21,8 +21,14 @@ class Factory public static function getNextPacket(Version $version, $remainingData) { while(isset($remainingData{1})) { - $remainingLength = ord($remainingData{1}); - $packetLength = 2 + $remainingLength; + $byte = 1; + $packetLength = 0; + do { + $digit = ord($remainingData{$byte}); + $packetLength += $digit; + $byte++; + } while (($digit & 128) != 0); + $packetLength += 2; $nextPacketData = substr($remainingData, 0, $packetLength); $remainingData = substr($remainingData, $packetLength); From 1eb1b1372617a1a542b8c9e7519f23f789fcf2f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88tz=20Gottwald?= Date: Tue, 10 Sep 2019 21:04:36 +0200 Subject: [PATCH 3/5] get packet length of packets > 127 --- src/packet/ControlPacket.php | 5 +++-- src/packet/Factory.php | 6 +++--- src/packet/Publish.php | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/packet/ControlPacket.php b/src/packet/ControlPacket.php index 9f29c0d..39e91cc 100644 --- a/src/packet/ControlPacket.php +++ b/src/packet/ControlPacket.php @@ -25,10 +25,11 @@ public function __construct(Version $version) /** * @param Version $version - * @param string $rawInput + * @param string $rawInput + * @param int $topicStart * @return static */ - public static function parse(Version $version, $rawInput) + public static function parse(Version $version, $rawInput, $topicStart = 2) { static::checkRawInputValidControlPackageType($rawInput); diff --git a/src/packet/Factory.php b/src/packet/Factory.php index db2e76e..e807504 100644 --- a/src/packet/Factory.php +++ b/src/packet/Factory.php @@ -32,11 +32,11 @@ public static function getNextPacket(Version $version, $remainingData) $nextPacketData = substr($remainingData, 0, $packetLength); $remainingData = substr($remainingData, $packetLength); - yield self::getByMessage($version, $nextPacketData); + yield self::getByMessage($version, $nextPacketData, $byte); } } - private static function getByMessage(Version $version, $input) + private static function getByMessage(Version $version, $input, $topicStart = 2) { $controlPacketType = ord($input{0}) >> 4; @@ -51,7 +51,7 @@ private static function getByMessage(Version $version, $input) return SubscribeAck::parse($version, $input); case Publish::getControlPacketType(): - return Publish::parse($version, $input); + return Publish::parse($version, $input, $topicStart); case PublishComplete::getControlPacketType(): return PublishComplete::parse($version, $input); diff --git a/src/packet/Publish.php b/src/packet/Publish.php index 2d630ab..9d70200 100644 --- a/src/packet/Publish.php +++ b/src/packet/Publish.php @@ -32,13 +32,13 @@ public static function getControlPacketType() return ControlPacketType::PUBLISH; } - public static function parse(Version $version, $rawInput) + public static function parse(Version $version, $rawInput, $topicStart = 2) { /** @var Publish $packet */ $packet = parent::parse($version, $rawInput); //TODO 3.3.2.2 Packet Identifier not yet supported - $topic = static::getPayloadLengthPrefixFieldInRawInput(2, $rawInput); + $topic = static::getPayloadLengthPrefixFieldInRawInput($topicStart, $rawInput); $packet->setTopic($topic); $byte1 = $rawInput{0}; From fb03255dc4987bf0c0ed94ea8572569a7774200e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88tz=20Gottwald?= Date: Wed, 11 Sep 2019 07:23:06 +0200 Subject: [PATCH 4/5] calc topic length for packet length > 127 --- src/packet/Publish.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packet/Publish.php b/src/packet/Publish.php index 9d70200..20c7b69 100644 --- a/src/packet/Publish.php +++ b/src/packet/Publish.php @@ -53,7 +53,7 @@ public static function parse(Version $version, $rawInput, $topicStart = 2) } $packet->payload = substr( $rawInput, - 4 + strlen($topic) + $topicStart + 2 + strlen($topic) ); return $packet; From 624df20aa12973375b40231db656345f7332049e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88tz=20Gottwald?= Date: Tue, 1 Oct 2019 07:19:16 +0200 Subject: [PATCH 5/5] UPDATE reset debug --- src/MqttClient.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/MqttClient.php b/src/MqttClient.php index ee752e0..58e1683 100644 --- a/src/MqttClient.php +++ b/src/MqttClient.php @@ -13,6 +13,7 @@ use oliverlorenz\reactphpmqtt\packet\ControlPacket; use oliverlorenz\reactphpmqtt\packet\Disconnect; use oliverlorenz\reactphpmqtt\packet\Factory; +use oliverlorenz\reactphpmqtt\packet\MessageHelper; use oliverlorenz\reactphpmqtt\packet\PingRequest; use oliverlorenz\reactphpmqtt\packet\Publish; use oliverlorenz\reactphpmqtt\packet\Subscribe; @@ -84,6 +85,7 @@ private function listenForPackets(Connection $stream) try { foreach (Factory::getNextPacket($this->version, $rawData) as $packet) { $stream->emit($packet::EVENT, [$packet]); + echo "received:\t" . get_class($packet) . PHP_EOL; } } catch (ProtocolViolation $e) { @@ -131,6 +133,7 @@ public function sendConnectPacket(Connection $stream, ConnectionOptions $options $options->keepAlive ); $message = $packet->get(); + echo MessageHelper::getReadableByRawString($message); $deferred = new Deferred(); $stream->on(ConnectionAck::EVENT, function($message) use ($stream, $deferred) { @@ -150,6 +153,7 @@ public function sendConnectPacket(Connection $stream, ConnectionOptions $options private function sendPacketToStream(Connection $stream, ControlPacket $controlPacket) { + echo "send:\t\t" . get_class($controlPacket) . "\n"; $message = $controlPacket->get(); return $stream->write($message);