1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Protocol; |
6
|
|
|
|
7
|
|
|
use LogicException; |
8
|
|
|
use OutOfRangeException; |
9
|
|
|
use unreal4u\MQTT\Internals\ClientInterface; |
10
|
|
|
use unreal4u\MQTT\Internals\PacketIdentifierFunctionality; |
11
|
|
|
use unreal4u\MQTT\Internals\ProtocolBase; |
12
|
|
|
use unreal4u\MQTT\Internals\ReadableContent; |
13
|
|
|
use unreal4u\MQTT\Internals\ReadableContentInterface; |
14
|
|
|
use unreal4u\MQTT\Internals\WritableContentInterface; |
15
|
|
|
|
16
|
|
|
use function ord; |
17
|
|
|
use function strlen; |
18
|
|
|
use function substr; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A SUBACK Packet is sent by the Server to the Client to confirm receipt and processing of a SUBSCRIBE Packet. |
22
|
|
|
*/ |
23
|
|
|
final class SubAck extends ProtocolBase implements ReadableContentInterface |
24
|
|
|
{ |
25
|
1 |
|
use ReadableContent; |
26
|
1 |
|
use PacketIdentifierFunctionality; |
27
|
|
|
|
28
|
|
|
private const CONTROL_PACKET_VALUE = 9; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Information about each topic |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $payload = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $rawMQTTHeaders |
38
|
|
|
* @param ClientInterface $client |
39
|
|
|
* @return ReadableContentInterface |
40
|
|
|
* @throws OutOfRangeException |
41
|
|
|
*/ |
42
|
2 |
|
public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface |
43
|
|
|
{ |
44
|
|
|
// Read out the remaining length bytes should only 1 byte have come in until now |
45
|
2 |
|
if (strlen($rawMQTTHeaders) === 1) { |
46
|
2 |
|
$rawMQTTHeaders .= $client->readBrokerData(1); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
$remainingLength = ord($rawMQTTHeaders[1]); |
50
|
|
|
// Check if we have a complete message |
51
|
2 |
|
if ($remainingLength + 2 !== strlen($rawMQTTHeaders)) { |
52
|
2 |
|
$rawMQTTHeaders .= $client->readBrokerData($remainingLength + 2 - strlen($rawMQTTHeaders)); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$this->setPacketIdentifierFromRawHeaders($rawMQTTHeaders); |
56
|
|
|
// TODO Check which QoS corresponds to each topic we are subscribed to |
57
|
2 |
|
$this->payload = substr($rawMQTTHeaders, 4); |
58
|
2 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
* @throws LogicException |
64
|
|
|
*/ |
65
|
1 |
|
public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool |
66
|
|
|
{ |
67
|
1 |
|
$this->controlPacketIdentifiers($originalRequest); |
68
|
1 |
|
$client->updateLastCommunication(); |
69
|
1 |
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
1 |
|
public function getOriginControlPacket(): int |
76
|
|
|
{ |
77
|
1 |
|
return Subscribe::getControlPacketValue(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|