@@ -97,61 +97,36 @@ public function tryFindMessageInBuffer(string $buffer, int $bufferLength, string
97
97
*/
98
98
public function buildConnectMessage (ConnectionSettings $ connectionSettings , bool $ useCleanSession = false ): string
99
99
{
100
- $ i = 0 ;
101
- $ buffer = '' ;
102
-
103
100
// The protocol name and version.
104
- $ buffer .= chr (0x00 ); // length of protocol name 1
105
- $ buffer .= chr (0x06 ); // length of protocol name 2
106
- $ buffer .= chr (0x4d ); // protocol name: M
107
- $ buffer .= chr (0x51 ); // protocol name: Q
108
- $ buffer .= chr (0x49 ); // protocol name: I
109
- $ buffer .= chr (0x73 ); // protocol name: s
110
- $ buffer .= chr (0x64 ); // protocol name: d
111
- $ buffer .= chr (0x70 ); // protocol name: p
101
+ $ buffer = $ this ->buildLengthPrefixedString ('MQIsdp ' );
112
102
$ buffer .= chr (0x03 ); // protocol version (3)
113
- $ i += 9 ;
114
103
115
104
// Build connection flags based on the connection settings.
116
- $ flags = $ this ->buildConnectionFlags ($ connectionSettings , $ useCleanSession );
117
- $ buffer .= chr ($ flags );
118
- $ i ++;
105
+ $ buffer .= chr ($ this ->buildConnectionFlags ($ connectionSettings , $ useCleanSession ));
119
106
120
107
// Encode and add the keep alive interval.
121
108
$ buffer .= chr ($ connectionSettings ->getKeepAliveInterval () >> 8 );
122
109
$ buffer .= chr ($ connectionSettings ->getKeepAliveInterval () & 0xff );
123
- $ i += 2 ;
124
110
125
111
// Encode and add the client identifier.
126
- $ clientIdPart = $ this ->buildLengthPrefixedString ($ this ->clientId );
127
- $ buffer .= $ clientIdPart ;
128
- $ i += strlen ($ clientIdPart );
112
+ $ buffer .= $ this ->buildLengthPrefixedString ($ this ->clientId );
129
113
130
114
// Encode and add the last will topic and message, if configured.
131
115
if ($ connectionSettings ->hasLastWill ()) {
132
- $ topicPart = $ this ->buildLengthPrefixedString ($ connectionSettings ->getLastWillTopic ());
133
- $ buffer .= $ topicPart ;
134
- $ i += strlen ($ topicPart );
135
-
136
- $ messagePart = $ this ->buildLengthPrefixedString ($ connectionSettings ->getLastWillMessage ());
137
- $ buffer .= $ messagePart ;
138
- $ i += strlen ($ messagePart );
116
+ $ buffer .= $ this ->buildLengthPrefixedString ($ connectionSettings ->getLastWillTopic ());
117
+ $ buffer .= $ this ->buildLengthPrefixedString ($ connectionSettings ->getLastWillMessage ());
139
118
}
140
119
141
120
// Encode and add the credentials, if configured.
142
121
if ($ connectionSettings ->getUsername () !== null ) {
143
- $ usernamePart = $ this ->buildLengthPrefixedString ($ connectionSettings ->getUsername ());
144
- $ buffer .= $ usernamePart ;
145
- $ i += strlen ($ usernamePart );
122
+ $ buffer .= $ this ->buildLengthPrefixedString ($ connectionSettings ->getUsername ());
146
123
}
147
124
if ($ connectionSettings ->getPassword () !== null ) {
148
- $ passwordPart = $ this ->buildLengthPrefixedString ($ connectionSettings ->getPassword ());
149
- $ buffer .= $ passwordPart ;
150
- $ i += strlen ($ passwordPart );
125
+ $ buffer .= $ this ->buildLengthPrefixedString ($ connectionSettings ->getPassword ());
151
126
}
152
127
153
128
// The header consists of the message type 0x10 and the length.
154
- $ header = chr (0x10 ) . chr ( $ i );
129
+ $ header = chr (0x10 ) . $ this -> encodeMessageLength ( strlen ( $ buffer ) );
155
130
156
131
return $ header . $ buffer ;
157
132
}
@@ -287,6 +262,7 @@ public function handleConnectAcknowledgement(string $message): void
287
262
*/
288
263
public function buildPingMessage (): string
289
264
{
265
+ // The message consists of the command 0xc0 and the length 0.
290
266
return chr (0xc0 ) . chr (0x00 );
291
267
}
292
268
@@ -297,6 +273,7 @@ public function buildPingMessage(): string
297
273
*/
298
274
public function buildDisconnectMessage (): string
299
275
{
276
+ // The message consists of the command 0xe0 and the length 0.
300
277
return chr (0xe0 ) . chr (0x00 );
301
278
}
302
279
@@ -307,19 +284,15 @@ public function buildSubscribeMessage(int $messageId, string $topic, int $qualit
307
284
{
308
285
// Encode the message id, it always consists of two bytes.
309
286
$ buffer = $ this ->encodeMessageId ($ messageId );
310
- $ i = 2 ;
311
287
312
288
// Encode the topic as length prefixed string.
313
- $ topicPart = $ this ->buildLengthPrefixedString ($ topic );
314
- $ buffer .= $ topicPart ;
315
- $ i += strlen ($ topicPart );
289
+ $ buffer .= $ this ->buildLengthPrefixedString ($ topic );
316
290
317
291
// Encode the quality of service level.
318
292
$ buffer .= chr ($ qualityOfService );
319
- $ i ++;
320
293
321
294
// The header consists of the message type 0x82 and the length.
322
- $ header = chr (0x82 ) . chr ( $ i );
295
+ $ header = chr (0x82 ) . $ this -> encodeMessageLength ( strlen ( $ buffer ) );
323
296
324
297
return $ header . $ buffer ;
325
298
}
@@ -331,17 +304,14 @@ public function buildUnsubscribeMessage(int $messageId, string $topic, bool $isD
331
304
{
332
305
// Encode the message id, it always consists of two bytes.
333
306
$ buffer = $ this ->encodeMessageId ($ messageId );
334
- $ i = 2 ;
335
307
336
308
// Encode the topic as length prefixed string.
337
- $ topicPart = $ this ->buildLengthPrefixedString ($ topic );
338
- $ buffer .= $ topicPart ;
339
- $ i += strlen ($ topicPart );
309
+ $ buffer .= $ this ->buildLengthPrefixedString ($ topic );
340
310
341
311
// The header consists of the message type 0xa2 and the length.
342
312
// Additionally, the first byte may contain the duplicate flag.
343
313
$ command = 0xa2 | ($ isDuplicate ? 1 << 3 : 0 );
344
- $ header = chr ($ command ) . chr ( $ i );
314
+ $ header = chr ($ command ) . $ this -> encodeMessageLength ( strlen ( $ buffer ) );
345
315
346
316
return $ header . $ buffer ;
347
317
}
@@ -359,20 +329,16 @@ public function buildPublishMessage(
359
329
): string
360
330
{
361
331
// Encode the topic as length prefixed string.
362
- $ topicPart = $ this ->buildLengthPrefixedString ($ topic );
363
- $ buffer = $ topicPart ;
364
- $ i = strlen ($ topicPart );
332
+ $ buffer = $ this ->buildLengthPrefixedString ($ topic );
365
333
366
334
// Encode the message id, if given. It always consists of two bytes.
367
335
if ($ messageId !== null )
368
336
{
369
337
$ buffer .= $ this ->encodeMessageId ($ messageId );
370
- $ i += 2 ;
371
338
}
372
339
373
340
// Add the message without encoding.
374
341
$ buffer .= $ message ;
375
- $ i += strlen ($ message );
376
342
377
343
// Encode the command with supported flags.
378
344
$ command = 0x30 ;
@@ -387,7 +353,7 @@ public function buildPublishMessage(
387
353
}
388
354
389
355
// Build the header from the command and the encoded message length.
390
- $ header = chr ($ command ) . $ this ->encodeMessageLength ($ i );
356
+ $ header = chr ($ command ) . $ this ->encodeMessageLength (strlen ( $ buffer ) );
391
357
392
358
return $ header . $ buffer ;
393
359
}
0 commit comments