Skip to content

Commit 5c5505a

Browse files
authored
Add getLevel (#55)
* Add getLevel * Add getLevel example
1 parent 6b1b136 commit 5c5505a

File tree

4 files changed

+122
-13
lines changed

4 files changed

+122
-13
lines changed

docs/zh-cn/README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,13 @@ composer require simps/mqtt
2020

2121
参考 [examples](https://github.com/simps/mqtt/tree/master/examples) 目录
2222

23-
## 捐献及赞助 :id=donate
23+
## 关注
2424

25-
### 一次性赞助
25+
![](https://cdn.jsdelivr.net/gh/sy-records/staticfile/images/202012/wechat_white.png)
2626

27-
如果您是个人开发者并且 PHPMQTT 给您解决燃眉之急或带来些许明朗,您可以打赏一杯咖啡或一杯香茗 :)
27+
## 赞赏 :id=donate
2828

29-
我们通过以下方式接受赞助:
29+
如果给您解决燃眉之急或带来些许明朗,您可以打赏一杯咖啡或一杯香茗 :)
3030

31-
![alipay](https://cdn.jsdelivr.net/gh/sy-records/staticfile/images/alipay.jpg ':size=362x562')
32-
![wechat](https://cdn.jsdelivr.net/gh/sy-records/staticfile/images/wechatpay.png ':size=autox562')
31+
[donate](https://donate.qq52o.me/ ':include :type=iframe')
3332

34-
### 周期性赞助
35-
36-
如果您是企业经营者并且将 PHPMQTT 用在商业产品中,那么赞助 PHPMQTT 有商业上的益处:可以让您的产品所依赖的类库保持健康并得到积极的升级维护,也能获得一些技术支持。
37-
38-
周期性赞助可以获得额外的回报,比如您的名字或组织/公司 Logo 及链接会出现在 PHPMQTT 的 GitHub 仓库中。
39-
40-
> 如您希望为 PHPMQTT 提供周期性的赞助,可邮件至 [email protected]

examples/getLevel.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* This file is part of Simps
4+
*
5+
* @link https://github.com/simps/mqtt
6+
* @contact Lu Fei <[email protected]>
7+
*
8+
* For the full copyright and license information,
9+
* please view the LICENSE file that was distributed with this source code
10+
*/
11+
12+
include __DIR__ . '/bootstrap.php';
13+
14+
use Simps\MQTT\Protocol\Types;
15+
use Simps\MQTT\Protocol\V3;
16+
use Simps\MQTT\Protocol\V5;
17+
use Simps\MQTT\Tools\UnPackTool;
18+
use Simps\MQTT\Protocol\ProtocolInterface;
19+
20+
$server = new Swoole\Server('127.0.0.1', 1883, SWOOLE_BASE);
21+
22+
$server->set(
23+
[
24+
'open_mqtt_protocol' => true,
25+
'worker_num' => 1,
26+
'package_max_length' => 2 * 1024 * 1024,
27+
]
28+
);
29+
30+
$server->on('connect', function ($server, $fd) {
31+
echo "Client #{$fd}: Connect.\n";
32+
});
33+
34+
$server->on('receive', function (Swoole\Server $server, $fd, $from_id, $data) {
35+
$type = UnPackTool::getType($data);
36+
if ($type === Types::CONNECT) {
37+
$level = UnPackTool::getLevel($data);
38+
$class = $level === ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0 ? V5::class : V3::class;
39+
$server->fds[$fd] = ['level' => $level, 'class' => $class];
40+
}
41+
/** @var ProtocolInterface $unpack */
42+
$unpack = $server->fds[$fd]['class'];
43+
var_dump($unpack::unpack($data));
44+
});
45+
46+
$server->on('close', function ($server, $fd) {
47+
unset($server->fds[$fd]);
48+
echo "Client #{$fd}: Close.\n";
49+
});
50+
51+
$server->start();

src/Tools/UnPackTool.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace Simps\MQTT\Tools;
1515

16+
use Simps\MQTT\Exception\InvalidArgumentException;
1617
use Simps\MQTT\Exception\LengthException;
18+
use Simps\MQTT\Protocol\Types;
1719

1820
class UnPackTool extends Common
1921
{
@@ -97,4 +99,21 @@ public static function getRemaining(string $data): string
9799

98100
return substr($data, $headBytes, $remainingLength);
99101
}
102+
103+
/**
104+
* Get the MQTT protocol level.
105+
*/
106+
public static function getLevel(string $data): int
107+
{
108+
$type = static::getType($data);
109+
110+
if ($type !== Types::CONNECT) {
111+
throw new InvalidArgumentException(sprintf('packet must be of type connect, %s given', Types::getType($type)));
112+
}
113+
114+
$remaining = static::getRemaining($data);
115+
$length = unpack('n', $remaining)[1];
116+
117+
return ord($remaining[$length + 2]);
118+
}
100119
}

tests/Unit/ToolsTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* This file is part of Simps
4+
*
5+
* @link https://github.com/simps/mqtt
6+
* @contact Lu Fei <[email protected]>
7+
*
8+
* For the full copyright and license information,
9+
* please view the LICENSE file that was distributed with this source code
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace SimpsTest\MQTT\Unit;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Simps\MQTT\Exception\InvalidArgumentException;
18+
use Simps\MQTT\Protocol\ProtocolInterface;
19+
use Simps\MQTT\Tools\UnPackTool;
20+
21+
/**
22+
* @internal
23+
* @coversNothing
24+
*/
25+
class ToolsTest extends TestCase
26+
{
27+
public function testGetLevel()
28+
{
29+
$connect_31 = '104400064d51497364700306000a001353696d70735f36313362316164323236626334001973696d70732d6d7174742f757365723030312f64656c6574650006627965627965';
30+
$connect_311 = '104200044d5154540406000a001353696d70735f36313362313830323035636633001973696d70732d6d7174742f757365723030312f64656c6574650006627965627965';
31+
$connect_50 = '106200044d515454050e000a0b110000003c21ffff22ffff001353696d70735f3631336231613330313962663213180000003c020000003c030004746573740101001973696d70732d6d7174742f757365723030312f64656c6574650006627965627965';
32+
$this->assertSame(UnPackTool::getLevel(hex2bin($connect_31)), ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1);
33+
$this->assertSame(UnPackTool::getLevel(hex2bin($connect_311)), ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1_1);
34+
$this->assertSame(UnPackTool::getLevel(hex2bin($connect_50)), ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0);
35+
}
36+
37+
public function testInvalidGetLevel()
38+
{
39+
try {
40+
$connAck = '200200';
41+
UnPackTool::getLevel(hex2bin($connAck));
42+
} catch (\Throwable $ex) {
43+
$this->assertInstanceOf(InvalidArgumentException::class, $ex);
44+
$this->assertSame($ex->getMessage(), 'packet must be of type connect, connack given');
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)