Skip to content

Commit d281d5f

Browse files
authored
Merge pull request #12 from simps/Optimization
Optimisation duplicate codes
2 parents c4a2813 + 75aca0b commit d281d5f

File tree

11 files changed

+290
-332
lines changed

11 files changed

+290
-332
lines changed

examples/server.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Simps\MQTT\Protocol;
1515
use Simps\MQTT\Types;
16+
use Simps\MQTT\Tools\Common;
1617

1718
$server = new Swoole\Server('127.0.0.1', 1883, SWOOLE_BASE);
1819

@@ -30,7 +31,7 @@
3031

3132
$server->on('receive', function (Swoole\Server $server, $fd, $from_id, $data) {
3233
try {
33-
Protocol::printf($data);
34+
Common::printf($data);
3435
$data = Protocol::unpack($data);
3536
var_dump($data);
3637
if (is_array($data) && isset($data['type'])) {

examples/v5/server.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Simps\MQTT\ProtocolV5;
1515
use Simps\MQTT\Types;
16+
use Simps\MQTT\Tools\Common;
1617

1718
$server = new Swoole\Server('127.0.0.1', 1883, SWOOLE_BASE);
1819

@@ -30,8 +31,9 @@
3031

3132
$server->on('receive', function (Swoole\Server $server, $fd, $from_id, $data) {
3233
try {
33-
ProtocolV5::printf($data);
34+
Common::printf($data);
3435
$data = ProtocolV5::unpack($data);
36+
var_dump($data);
3537
if (is_array($data) && isset($data['type'])) {
3638
switch ($data['type']) {
3739
case Types::CONNECT:

src/Packet/Pack.php

+21-58
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;
@@ -40,20 +41,20 @@ public static function connect(array $array): string
4041
$body .= chr($connect_flags);
4142

4243
$keepAlive = !empty($array['keep_alive']) && (int) $array['keep_alive'] >= 0 ? (int) $array['keep_alive'] : 0;
43-
$body .= pack('n', $keepAlive);
44+
$body .= PackTool::shortInt($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,97 +64,59 @@ 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) {
76-
$body .= pack('n', $array['message_id']);
77+
$body .= PackTool::shortInt($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
}
8586

8687
public static function subscribe(array $array): string
8788
{
8889
$id = $array['message_id'];
89-
$body = pack('n', $id);
90+
$body = PackTool::shortInt($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
}
9899

99100
public static function subAck(array $array): string
100101
{
101102
$payload = $array['payload'];
102-
$body = pack('n', $array['message_id']) . call_user_func_array(
103+
$body = PackTool::shortInt($array['message_id']) . call_user_func_array(
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
}
110111

111112
public static function unSubscribe(array $array): string
112113
{
113-
$body = pack('n', $array['message_id']);
114+
$body = PackTool::shortInt($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)