Skip to content

Commit 1643830

Browse files
committed
Send only one packet per tick
1 parent fbad8ca commit 1643830

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/ReactMqttClient.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class ReactMqttClient extends EventEmitter
7272
private $publishQos2 = [];
7373
/** @var PublishRequestPacket[] */
7474
private $incomingQos2 = [];
75+
/** @var Packet[] */
76+
private $packetQueue = [];
7577

7678
/**
7779
* Constructs an instance of this class.
@@ -171,8 +173,7 @@ public function disconnect()
171173

172174
$packet = new DisconnectRequestPacket();
173175

174-
$this->stream->write($packet);
175-
$this->stream->close();
176+
$this->stream->end($packet);
176177
$this->stream = null;
177178

178179
$this->isConnected = false;
@@ -192,7 +193,7 @@ public function subscribe($topic, $qosLevel = 0)
192193
$packet->setTopic($topic);
193194
$packet->setQosLevel($qosLevel);
194195

195-
$this->stream->write($packet);
196+
$this->writePacket($packet);
196197

197198
$id = $packet->getIdentifier();
198199
$this->deferred['subscribe'][$id] = new Deferred();
@@ -213,7 +214,7 @@ public function unsubscribe($topic)
213214
$packet = new UnsubscribeRequestPacket();
214215
$packet->setTopic($topic);
215216

216-
$this->stream->write($packet);
217+
$this->writePacket($packet);
217218

218219
$id = $packet->getIdentifier();
219220
$this->deferred['unsubscribe'][$id] = new Deferred();
@@ -241,7 +242,7 @@ public function publish($topic, $message, $qosLevel = 0, $retain = false)
241242
$packet->setRetained($retain);
242243
$packet->setDuplicate(false);
243244

244-
$this->stream->write($packet);
245+
$this->writePacket($packet);
245246

246247
if ($qosLevel == 0) {
247248
return new FulfilledPromise($message);
@@ -305,6 +306,13 @@ private function connectStream(Stream $stream, array $settings, $timeout, $keepA
305306
$this->handleData($data);
306307
});
307308

309+
$this->stream->getBuffer()->on('full-drain', function () {
310+
if (count($this->packetQueue) > 0) {
311+
$packet = array_shift($this->packetQueue);
312+
$this->stream->write($packet);
313+
}
314+
});
315+
308316
$this->stream->on('error', function (\Exception $e) {
309317
$this->emitError($e);
310318
});
@@ -321,7 +329,7 @@ private function connectStream(Stream $stream, array $settings, $timeout, $keepA
321329
$this->timer[] = $this->loop->addPeriodicTimer(
322330
floor($keepAlive * 0.75),
323331
function () {
324-
$this->stream->write(new PingRequestPacket());
332+
$this->writePacket(new PingRequestPacket());
325333
}
326334
);
327335

@@ -349,7 +357,7 @@ function () use ($timeout) {
349357
$packet->setWill($will['topic'], $will['message'], $will['qos'], $will['retain']);
350358
}
351359

352-
$this->stream->write($packet);
360+
$this->writePacket($packet);
353361
}
354362

355363
/**
@@ -460,6 +468,20 @@ private function sanitizeOptions(array $options)
460468
return array_merge($defaults, $options);
461469
}
462470

471+
/**
472+
* Writes a packet to the stream or buffers it if the stream is busy.
473+
*
474+
* @param Packet $packet
475+
*/
476+
private function writePacket(Packet $packet)
477+
{
478+
if ($this->stream->getBuffer()->listening) {
479+
$this->packetQueue[] = $packet;
480+
} else {
481+
$this->stream->write($packet);
482+
}
483+
}
484+
463485
/**
464486
* Handles a CONNACK packet.
465487
*
@@ -563,7 +585,7 @@ private function handlePublishRequest(PublishRequestPacket $packet)
563585

564586
if ($response !== null) {
565587
$response->setIdentifier($packet->getIdentifier());
566-
$this->stream->write($response);
588+
$this->writePacket($response);
567589
}
568590

569591
if ($emit) {
@@ -606,7 +628,7 @@ private function handlePublishReceived(PublishReceivedPacket $packet)
606628

607629
$response = new PublishReleasePacket();
608630
$response->setIdentifier($id);
609-
$this->stream->write($response);
631+
$this->writePacket($response);
610632
}
611633

612634
/**
@@ -620,7 +642,7 @@ private function handlePublishRelease(PublishReleasePacket $packet)
620642

621643
$response = new PublishCompletePacket();
622644
$response->setIdentifier($id);
623-
$this->stream->write($response);
645+
$this->writePacket($response);
624646

625647
if (!isset($this->incomingQos2[$id])) {
626648
$this->emitWarning(new \LogicException(sprintf('PUBREL: Packet identifier %d not found.', $id)));

0 commit comments

Comments
 (0)