Skip to content

Commit

Permalink
Fix error in calculating properties length
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Aug 1, 2022
1 parent bd9f57e commit 7c4c248
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 81 deletions.
90 changes: 11 additions & 79 deletions src/Property/PackProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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:
Expand All @@ -129,35 +109,28 @@ 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:
case Property::RETAIN_AVAILABLE:
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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
}
5 changes: 5 additions & 0 deletions src/Tools/PackTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 7c4c248

Please sign in to comment.