From dbfb7b391286aebcf96dded29815d86df452329a Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Thu, 29 Sep 2016 20:34:45 +0200 Subject: [PATCH] Fixed ProtocolStreamWriter for nonblocking connection. --- src/ProtocolStreamWriter.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ProtocolStreamWriter.php b/src/ProtocolStreamWriter.php index f44daeb..643e940 100644 --- a/src/ProtocolStreamWriter.php +++ b/src/ProtocolStreamWriter.php @@ -25,6 +25,16 @@ class ProtocolStreamWriter implements ProtocolWriter */ public function write(Message $msg) { - fwrite($this->output, (string)$msg); + $data = (string)$msg; + $msgSize = strlen($data); + $totalBytesWritten = 0; + + while ($totalBytesWritten < $msgSize) { + $bytesWritten = fwrite($this->output, substr($data, $totalBytesWritten)); + if ($bytesWritten === false) { + throw new Error('Could not write message.'); + } + $totalBytesWritten += $bytesWritten; + } } }