Skip to content

Commit 8dbfc50

Browse files
committed
Make stream non-blocking
A blocking stream causes connection abortions, and we don't want that.
1 parent 96ce1ce commit 8dbfc50

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/MqttClient.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ protected function establishSocketConnection(): void
264264
// from some unrelated code of the users application.
265265
error_clear_last();
266266

267+
$this->logger->debug('Enabling TLS on the existing socket connection.');
268+
267269
$enableEncryptionResult = @stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_ANY_CLIENT);
268270

269271
if ($enableEncryptionResult === false) {
@@ -291,9 +293,14 @@ protected function establishSocketConnection(): void
291293
$tlsErrorMessage
292294
);
293295
}
296+
297+
$this->logger->debug('TLS enabled successfully.');
294298
}
295299

296300
stream_set_timeout($socket, $this->settings->getSocketTimeout());
301+
stream_set_blocking($socket, false);
302+
303+
$this->logger->debug('Socket opened and ready to use.');
297304

298305
$this->socket = $socket;
299306
}
@@ -1103,11 +1110,8 @@ protected function disconnect(): void
11031110
*/
11041111
protected function writeToSocket(string $data, int $length = null): void
11051112
{
1106-
if ($length === null) {
1107-
$length = strlen($data);
1108-
}
1109-
1110-
$length = min($length, strlen($data));
1113+
$calculatedLength = strlen($data);
1114+
$length = min($length ?? $calculatedLength, $calculatedLength);
11111115

11121116
$result = @fwrite($this->socket, $data, $length);
11131117

0 commit comments

Comments
 (0)