|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Internals; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use unreal4u\MQTT\Exceptions\MessageTooBig; |
|
9
|
|
|
use unreal4u\MQTT\Utilities; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trait WritableContent |
|
13
|
|
|
* @package unreal4u\MQTT\Internals |
|
14
|
|
|
*/ |
|
15
|
|
|
trait WritableContent |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var LoggerInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $logger; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Any special flags that are set on runtime |
|
24
|
|
|
* |
|
25
|
|
|
* PUBLISH for example needs to know QoS, the retain bit and duplicate delivery settings |
|
26
|
|
|
* PUBREL, SUBSCRIBE and UNSUBSCRIBE has always bit 1 set to true |
|
27
|
|
|
* |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $specialFlags = 0; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The protocol version we are talking with. Currently only v3.1.1 is supported |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
public $protocolLevel = '3.1.1'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns the fixed header part needed for all methods |
|
40
|
|
|
* |
|
41
|
|
|
* This takes into account the basic control packet value, any special flags and, in the second byte, the variable |
|
42
|
|
|
* header length |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $variableHeaderLength |
|
45
|
|
|
* @return string |
|
46
|
|
|
* @throws \unreal4u\MQTT\Exceptions\MessageTooBig |
|
47
|
|
|
*/ |
|
48
|
2 |
|
final public function createFixedHeader(int $variableHeaderLength): string |
|
49
|
|
|
{ |
|
50
|
2 |
|
$this->logger->debug('Creating fixed header with values', [ |
|
51
|
2 |
|
'controlPacketValue' => static::CONTROL_PACKET_VALUE, |
|
|
|
|
|
|
52
|
2 |
|
'specialFlags' => $this->specialFlags, |
|
53
|
2 |
|
'variableHeaderLength' => $variableHeaderLength, |
|
54
|
2 |
|
'composed' => decbin(\chr((static::CONTROL_PACKET_VALUE << 4) | $this->specialFlags)), |
|
|
|
|
|
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
// Binary OR is safe to do because the first 4 bits are always 0 after shifting |
|
58
|
|
|
return |
|
59
|
2 |
|
\chr((static::CONTROL_PACKET_VALUE << 4) | $this->specialFlags) . |
|
60
|
2 |
|
$this->getRemainingLength($variableHeaderLength); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns the correct format for the length in bytes of the remaining bytes |
|
65
|
|
|
* |
|
66
|
|
|
* @param int $lengthInBytes |
|
67
|
|
|
* @return string |
|
68
|
|
|
* @throws \unreal4u\MQTT\Exceptions\MessageTooBig |
|
69
|
|
|
*/ |
|
70
|
2 |
|
final public function getRemainingLength(int $lengthInBytes): string |
|
71
|
|
|
{ |
|
72
|
2 |
|
if ($lengthInBytes > 268435455) { |
|
73
|
|
|
throw new MessageTooBig('The message cannot exceed 268435455 bytes in length'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
$x = $lengthInBytes; |
|
77
|
2 |
|
$outputString = ''; |
|
78
|
|
|
do { |
|
79
|
2 |
|
$encodedByte = $x % 128; |
|
80
|
2 |
|
$x >>= 7; // Shift 7 bytes |
|
81
|
|
|
// if there are more data to encode, set the top bit of this byte |
|
82
|
2 |
|
if ($x > 0) { |
|
83
|
|
|
$encodedByte |= 128; |
|
84
|
|
|
} |
|
85
|
2 |
|
$outputString .= \chr($encodedByte); |
|
86
|
2 |
|
} while ($x > 0); |
|
87
|
|
|
|
|
88
|
2 |
|
return $outputString; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Creates the entire message |
|
93
|
|
|
* @return string |
|
94
|
|
|
* @throws \unreal4u\MQTT\Exceptions\MessageTooBig |
|
95
|
|
|
*/ |
|
96
|
1 |
|
final public function createSendableMessage(): string |
|
97
|
|
|
{ |
|
98
|
1 |
|
$variableHeader = $this->createVariableHeader(); |
|
|
|
|
|
|
99
|
1 |
|
$this->logger->debug('Creating variable header', ['variableHeader' => base64_encode($variableHeader)]); |
|
100
|
1 |
|
$payload = $this->createPayload(); |
|
|
|
|
|
|
101
|
1 |
|
$this->logger->debug('Creating payload', ['payload' => base64_encode($payload)]); |
|
102
|
1 |
|
$fixedHeader = $this->createFixedHeader(mb_strlen($variableHeader . $payload)); |
|
103
|
1 |
|
$this->logger->debug('Created fixed header', ['fixedHeader' => base64_encode($fixedHeader)]); |
|
104
|
|
|
|
|
105
|
1 |
|
return $fixedHeader . $variableHeader . $payload; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Gets the current protocol lvl bit |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
3 |
|
final public function getProtocolLevel(): string |
|
113
|
|
|
{ |
|
114
|
3 |
|
if ($this->protocolLevel === '3.1.1') { |
|
115
|
2 |
|
return \chr(4); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Return a default of 0, which will be invalid anyway (but data will be sent to the broker this way) |
|
119
|
1 |
|
return \chr(0); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Creates a UTF8 big-endian representation of the given string |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $data |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
8 |
|
final public function createUTF8String(string $data): string |
|
129
|
|
|
{ |
|
130
|
8 |
|
return Utilities::convertNumberToBinaryString(mb_strlen($data)) . $data; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|