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