diff --git a/src/Property/PackProperty.php b/src/Property/PackProperty.php index 01cc1f5..0092677 100644 --- a/src/Property/PackProperty.php +++ b/src/Property/PackProperty.php @@ -19,7 +19,6 @@ class PackProperty { public static function connect(array $data): string { - $length = 0; $tmpBody = ''; $connect = array_flip(PacketMap::$connect); foreach ($data as $key => $item) { @@ -28,43 +27,34 @@ public static function connect(array $data): string $tmpBody .= chr($property); switch ($property) { case Property::SESSION_EXPIRY_INTERVAL: - $length += 5; $tmpBody .= PackTool::longInt($item); break; case Property::AUTHENTICATION_METHOD: case Property::AUTHENTICATION_DATA: - $length += 3; - $length += strlen($item); $tmpBody .= PackTool::string($item); break; case Property::REQUEST_PROBLEM_INFORMATION: case Property::REQUEST_RESPONSE_INFORMATION: - $length += 2; $tmpBody .= chr((int) $item); break; case Property::RECEIVE_MAXIMUM: case Property::TOPIC_ALIAS_MAXIMUM: case Property::MAXIMUM_PACKET_SIZE: - $length += 3; $tmpBody .= PackTool::shortInt($item); break; } } else { // Property::USER_PROPERTY - $length += 5; - $length += strlen((string) $key); - $length += strlen((string) $item); $tmpBody .= chr($connect['user_property']); $tmpBody .= PackTool::stringPair((string) $key, (string) $item); } } - return chr($length) . $tmpBody; + return PackTool::genProperties($tmpBody); } public static function willProperties(array $data): string { - $length = 0; $tmpBody = ''; $willProperties = array_flip(PacketMap::$willProperties); foreach ($data as $key => $item) { @@ -74,37 +64,29 @@ public static function willProperties(array $data): string switch ($property) { case Property::MESSAGE_EXPIRY_INTERVAL: case Property::WILL_DELAY_INTERVAL: - $length += 5; $tmpBody .= PackTool::longInt($item); break; case Property::CONTENT_TYPE: case Property::RESPONSE_TOPIC: case Property::CORRELATION_DATA: - $length += 3; - $length += strlen($item); $tmpBody .= PackTool::string($item); break; case Property::PAYLOAD_FORMAT_INDICATOR: - $length += 2; $tmpBody .= chr((int) $item); break; } } else { // Property::USER_PROPERTY - $length += 5; - $length += strlen((string) $key); - $length += strlen((string) $item); $tmpBody .= chr($willProperties['user_property']); $tmpBody .= PackTool::stringPair((string) $key, (string) $item); } } - return chr($length) . $tmpBody; + return PackTool::genProperties($tmpBody); } public static function connAck(array $data): string { - $length = 0; $tmpBody = ''; $connAck = array_flip(PacketMap::$connAck); foreach ($data as $key => $item) { @@ -114,13 +96,11 @@ public static function connAck(array $data): string switch ($property) { case Property::SESSION_EXPIRY_INTERVAL: case Property::MAXIMUM_PACKET_SIZE: - $length += 5; $tmpBody .= PackTool::longInt($item); break; case Property::SERVER_KEEP_ALIVE: case Property::RECEIVE_MAXIMUM: case Property::TOPIC_ALIAS_MAXIMUM: - $length += 3; $tmpBody .= PackTool::shortInt($item); break; case Property::ASSIGNED_CLIENT_IDENTIFIER: @@ -129,8 +109,6 @@ public static function connAck(array $data): string case Property::RESPONSE_INFORMATION: case Property::SERVER_REFERENCE: case Property::REASON_STRING: - $length += 3; - $length += strlen($item); $tmpBody .= PackTool::string($item); break; case Property::MAXIMUM_QOS: @@ -138,26 +116,21 @@ public static function connAck(array $data): string case Property::WILDCARD_SUBSCRIPTION_AVAILABLE: case Property::SUBSCRIPTION_IDENTIFIER_AVAILABLE: case Property::SHARED_SUBSCRIPTION_AVAILABLE: - $length += 2; $tmpBody .= chr((int) $item); break; } } else { // Property::USER_PROPERTY - $length += 5; - $length += strlen((string) $key); - $length += strlen((string) $item); $tmpBody .= chr($connAck['user_property']); $tmpBody .= PackTool::stringPair((string) $key, (string) $item); } } - return chr($length) . $tmpBody; + return PackTool::genProperties($tmpBody); } public static function publish(array $data): string { - $length = 0; $tmpBody = ''; $publish = array_flip(PacketMap::$publish); foreach ($data as $key => $item) { @@ -166,47 +139,35 @@ public static function publish(array $data): string $tmpBody .= chr($property); switch ($property) { case Property::MESSAGE_EXPIRY_INTERVAL: - $length += 5; $tmpBody .= PackTool::longInt($item); break; case Property::TOPIC_ALIAS: - $length += 3; $tmpBody .= PackTool::shortInt($item); break; case Property::CONTENT_TYPE: case Property::RESPONSE_TOPIC: case Property::CORRELATION_DATA: - $length += 3; - $length += strlen($item); $tmpBody .= PackTool::string($item); break; case Property::PAYLOAD_FORMAT_INDICATOR: - $length += 2; $tmpBody .= chr((int) $item); break; case Property::SUBSCRIPTION_IDENTIFIER: - $length += 1; - $value = PackTool::varInt((int) $item); - $length += strlen($value); - $tmpBody .= $value; + $tmpBody .= PackTool::varInt((int) $item); break; } } else { // Property::USER_PROPERTY - $length += 5; - $length += strlen((string) $key); - $length += strlen((string) $item); $tmpBody .= chr($publish['user_property']); $tmpBody .= PackTool::stringPair((string) $key, (string) $item); } } - return chr($length) . $tmpBody; + return PackTool::genProperties($tmpBody); } public static function pubAndSub(array $data): string { - $length = 0; $tmpBody = ''; $pubAndSub = array_flip(PacketMap::$pubAndSub); foreach ($data as $key => $item) { @@ -215,27 +176,21 @@ public static function pubAndSub(array $data): string $tmpBody .= chr($property); switch ($property) { case Property::REASON_STRING: - $length += 3; - $length += strlen($item); $tmpBody .= PackTool::string($item); break; } } else { // Property::USER_PROPERTY - $length += 5; - $length += strlen((string) $key); - $length += strlen((string) $item); $tmpBody .= chr($pubAndSub['user_property']); $tmpBody .= PackTool::stringPair((string) $key, (string) $item); } } - return chr($length) . $tmpBody; + return PackTool::genProperties($tmpBody); } public static function subscribe(array $data): string { - $length = 0; $tmpBody = ''; $subscribe = array_flip(PacketMap::$subscribe); foreach ($data as $key => $item) { @@ -244,45 +199,34 @@ public static function subscribe(array $data): string $tmpBody .= chr($property); switch ($property) { case Property::SUBSCRIPTION_IDENTIFIER: - $length += 1; - $value = PackTool::varInt((int) $item); - $length += strlen($value); - $tmpBody .= $value; + $tmpBody .= PackTool::varInt((int) $item); break; } } else { // Property::USER_PROPERTY - $length += 5; - $length += strlen((string) $key); - $length += strlen((string) $item); $tmpBody .= chr($subscribe['user_property']); $tmpBody .= PackTool::stringPair((string) $key, (string) $item); } } - return chr($length) . $tmpBody; + return PackTool::genProperties($tmpBody); } public static function unSubscribe(array $data): string { - $length = 0; $tmpBody = ''; $unSubscribe = array_flip(PacketMap::$unSubscribe); foreach ($data as $key => $item) { // Property::USER_PROPERTY - $length += 5; - $length += strlen((string) $key); - $length += strlen((string) $item); $tmpBody .= chr($unSubscribe['user_property']); $tmpBody .= PackTool::stringPair((string) $key, (string) $item); } - return chr($length) . $tmpBody; + return PackTool::genProperties($tmpBody); } public static function disConnect(array $data): string { - $length = 0; $tmpBody = ''; $disConnect = array_flip(PacketMap::$disConnect); foreach ($data as $key => $item) { @@ -291,32 +235,25 @@ public static function disConnect(array $data): string $tmpBody .= chr($property); switch ($property) { case Property::SESSION_EXPIRY_INTERVAL: - $length += 5; $tmpBody .= PackTool::longInt($item); break; case Property::SERVER_REFERENCE: case Property::REASON_STRING: - $length += 3; - $length += strlen($item); $tmpBody .= PackTool::string($item); break; } } else { // Property::USER_PROPERTY - $length += 5; - $length += strlen((string) $key); - $length += strlen((string) $item); $tmpBody .= chr($disConnect['user_property']); $tmpBody .= PackTool::stringPair((string) $key, (string) $item); } } - return chr($length) . $tmpBody; + return PackTool::genProperties($tmpBody); } public static function auth(array $data): string { - $length = 0; $tmpBody = ''; $auth = array_flip(PacketMap::$auth); foreach ($data as $key => $item) { @@ -327,21 +264,16 @@ public static function auth(array $data): string case Property::AUTHENTICATION_METHOD: case Property::AUTHENTICATION_DATA: case Property::REASON_STRING: - $length += 3; - $length += strlen($item); $tmpBody .= PackTool::string($item); break; } } else { // Property::USER_PROPERTY - $length += 5; - $length += strlen((string) $key); - $length += strlen((string) $item); $tmpBody .= chr($auth['user_property']); $tmpBody .= PackTool::stringPair((string) $key, (string) $item); } } - return chr($length) . $tmpBody; + return PackTool::genProperties($tmpBody); } } diff --git a/src/Tools/PackTool.php b/src/Tools/PackTool.php index 133c989..1ec2ddd 100644 --- a/src/Tools/PackTool.php +++ b/src/Tools/PackTool.php @@ -57,6 +57,11 @@ public static function packHeader(int $type, int $bodyLength, int $dup = 0, int return chr($type) . static::packRemainingLength($bodyLength); } + public static function genProperties(string $body): string + { + return static::packRemainingLength(strlen($body)) . $body; + } + protected static function packRemainingLength(int $bodyLength): string { $string = ''; diff --git a/tests/Unit/ClientTest.php b/tests/Unit/ClientTest.php index 9a02555..7debe61 100644 --- a/tests/Unit/ClientTest.php +++ b/tests/Unit/ClientTest.php @@ -40,12 +40,14 @@ public function testBase64() { $topic = 'simps-mqtt/test/base64'; $base64 = base64_encode(file_get_contents(TESTS_DIR . '/files/wechat.jpg')); - $client = new MQTTClient(SIMPS_MQTT_REMOTE_HOST, SIMPS_MQTT_PORT, getTestConnectConfig()); + $config = getTestConnectConfig(); + $config->setSwooleConfig(['read_timeout' => 10.0]); + $client = new MQTTClient('test.mosquitto.org', SIMPS_MQTT_PORT, $config); $client->connect(false); $client->subscribe([$topic => 0]); Coroutine::create(function () use ($topic, $base64) { - $client = new MQTTClient(SIMPS_MQTT_REMOTE_HOST, SIMPS_MQTT_PORT, getTestConnectConfig()); + $client = new MQTTClient('test.mosquitto.org', SIMPS_MQTT_PORT, getTestConnectConfig()); $client->connect(); $client->publish($topic, $base64); });