Skip to content

Commit 8c3561c

Browse files
committed
Optimisation duplicate codes
1 parent c4a2813 commit 8c3561c

File tree

9 files changed

+280
-325
lines changed

9 files changed

+280
-325
lines changed

src/Packet/Pack.php

+16-53
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
namespace Simps\MQTT\Packet;
1515

16+
use Simps\MQTT\Tools\PackTool;
1617
use Simps\MQTT\Types;
1718

1819
class Pack
1920
{
2021
public static function connect(array $array): string
2122
{
22-
$body = static::string($array['protocol_name']) . chr($array['protocol_level']);
23+
$body = PackTool::string($array['protocol_name']) . chr($array['protocol_level']);
2324
$connect_flags = 0;
2425
if (!empty($array['clean_session'])) {
2526
$connect_flags |= 1 << 1;
@@ -42,18 +43,18 @@ public static function connect(array $array): string
4243
$keepAlive = !empty($array['keep_alive']) && (int) $array['keep_alive'] >= 0 ? (int) $array['keep_alive'] : 0;
4344
$body .= pack('n', $keepAlive);
4445

45-
$body .= static::string($array['client_id']);
46+
$body .= PackTool::string($array['client_id']);
4647
if (!empty($array['will'])) {
47-
$body .= static::string($array['will']['topic']);
48-
$body .= static::string($array['will']['message']);
48+
$body .= PackTool::string($array['will']['topic']);
49+
$body .= PackTool::string($array['will']['message']);
4950
}
5051
if (!empty($array['user_name'])) {
51-
$body .= static::string($array['user_name']);
52+
$body .= PackTool::string($array['user_name']);
5253
}
5354
if (!empty($array['password'])) {
54-
$body .= static::string($array['password']);
55+
$body .= PackTool::string($array['password']);
5556
}
56-
$head = static::packHeader(Types::CONNECT, strlen($body));
57+
$head = PackTool::packHeader(Types::CONNECT, strlen($body));
5758

5859
return $head . $body;
5960
}
@@ -63,22 +64,22 @@ public static function connAck(array $array): string
6364
$body = !empty($array['session_present']) ? chr(1) : chr(0);
6465
$code = !empty($array['code']) ? $array['code'] : 0;
6566
$body .= chr($code);
66-
$head = static::packHeader(Types::CONNACK, strlen($body));
67+
$head = PackTool::packHeader(Types::CONNACK, strlen($body));
6768

6869
return $head . $body;
6970
}
7071

7172
public static function publish(array $array): string
7273
{
73-
$body = static::string($array['topic']);
74+
$body = PackTool::string($array['topic']);
7475
$qos = $array['qos'] ?? 0;
7576
if ($qos) {
7677
$body .= pack('n', $array['message_id']);
7778
}
7879
$body .= $array['message'];
7980
$dup = $array['dup'] ?? 0;
8081
$retain = $array['retain'] ?? 0;
81-
$head = static::packHeader(Types::PUBLISH, strlen($body), $dup, $qos, $retain);
82+
$head = PackTool::packHeader(Types::PUBLISH, strlen($body), $dup, $qos, $retain);
8283

8384
return $head . $body;
8485
}
@@ -88,10 +89,10 @@ public static function subscribe(array $array): string
8889
$id = $array['message_id'];
8990
$body = pack('n', $id);
9091
foreach ($array['topics'] as $topic => $qos) {
91-
$body .= static::string($topic);
92+
$body .= PackTool::string($topic);
9293
$body .= chr($qos);
9394
}
94-
$head = static::packHeader(Types::SUBSCRIBE, strlen($body), 0, 1);
95+
$head = PackTool::packHeader(Types::SUBSCRIBE, strlen($body), 0, 1);
9596

9697
return $head . $body;
9798
}
@@ -103,7 +104,7 @@ public static function subAck(array $array): string
103104
'pack',
104105
array_merge(['C*'], $payload)
105106
);
106-
$head = static::packHeader(Types::SUBACK, strlen($body));
107+
$head = PackTool::packHeader(Types::SUBACK, strlen($body));
107108

108109
return $head . $body;
109110
}
@@ -112,48 +113,10 @@ public static function unSubscribe(array $array): string
112113
{
113114
$body = pack('n', $array['message_id']);
114115
foreach ($array['topics'] as $topic) {
115-
$body .= static::string($topic);
116+
$body .= PackTool::string($topic);
116117
}
117-
$head = static::packHeader(Types::UNSUBSCRIBE, strlen($body), 0, 1);
118+
$head = PackTool::packHeader(Types::UNSUBSCRIBE, strlen($body), 0, 1);
118119

119120
return $head . $body;
120121
}
121-
122-
private static function string(string $str)
123-
{
124-
$len = strlen($str);
125-
126-
return pack('n', $len) . $str;
127-
}
128-
129-
public static function packHeader(int $type, int $bodyLength, int $dup = 0, int $qos = 0, int $retain = 0): string
130-
{
131-
$type = $type << 4;
132-
if ($dup) {
133-
$type |= 1 << 3;
134-
}
135-
if ($qos) {
136-
$type |= $qos << 1;
137-
}
138-
if ($retain) {
139-
$type |= 1;
140-
}
141-
142-
return chr($type) . static::packRemainingLength($bodyLength);
143-
}
144-
145-
private static function packRemainingLength(int $bodyLength)
146-
{
147-
$string = '';
148-
do {
149-
$digit = $bodyLength % 128;
150-
$bodyLength = $bodyLength >> 7;
151-
if ($bodyLength > 0) {
152-
$digit = ($digit | 0x80);
153-
}
154-
$string .= chr($digit);
155-
} while ($bodyLength > 0);
156-
157-
return $string;
158-
}
159122
}

0 commit comments

Comments
 (0)