1
0
Fork 0

Try writing all pending messages.

pull/112/head
Stephan Unverwerth 2016-10-25 22:19:32 +02:00
parent 55913eba79
commit b8b23f04bf
1 changed files with 23 additions and 26 deletions

View File

@ -59,36 +59,33 @@ class ProtocolStreamWriter implements ProtocolWriter
*/ */
public function writeData() public function writeData()
{ {
$message = $this->messages[0]['message']; $keepWriting = true;
$promise = $this->messages[0]['promise']; while ($keepWriting) {
$message = $this->messages[0]['message'];
$promise = $this->messages[0]['promise'];
error_clear_last(); error_clear_last();
$bytesWritten = @fwrite($this->output, $message); $bytesWritten = @fwrite($this->output, $message);
if ($bytesWritten === false) { if ($bytesWritten > 0) {
$error = error_get_last(); $message = substr($message, $bytesWritten);
$promise->reject($error); }
if ($error !== null) {
throw new RuntimeException('Could not write message: ' . error_get_last()['message']); // Determine if this message was completely sent
if (strlen($message) === 0) {
array_shift($this->messages);
// This was the last message in the queue, remove the write handler.
if (count($this->messages) === 0) {
Loop\removeWriteStream($this->output);
$keepWriting = false;
}
$promise->fulfill();
} else { } else {
throw new RuntimeException('Could not write message'); $this->messages[0]['message'] = $message;
$keepWriting = false;
} }
} else if ($bytesWritten > 0) {
$message = substr($message, $bytesWritten);
}
// Determine if this message was completely sent
if (strlen($message) === 0) {
array_shift($this->messages);
// This was the last message in the queue, remove the write handler.
if (count($this->messages) === 0) {
Loop\removeWriteStream($this->output);
}
$promise->fulfill();
} else {
$this->messages[0]['message'] = $message;
} }
} }
} }