From 45ac0c8b5fef71bde6e81fb63694160dfaa046be Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Mon, 24 Oct 2016 23:08:59 +0200 Subject: [PATCH] Made ProtocolStreamWriter async --- src/ProtocolStreamWriter.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/ProtocolStreamWriter.php b/src/ProtocolStreamWriter.php index cee9b60..4c6b424 100644 --- a/src/ProtocolStreamWriter.php +++ b/src/ProtocolStreamWriter.php @@ -4,17 +4,30 @@ declare(strict_types = 1); namespace LanguageServer; use LanguageServer\Protocol\Message; +use Sabre\Event\Loop; class ProtocolStreamWriter implements ProtocolWriter { private $output; + /** + * @var string $buffer + */ + private $buffer; + /** * @param resource $output */ public function __construct($output) { $this->output = $output; + Loop\addWriteStream($this->output, function() { + $msgSize = strlen($this->buffer); + $bytesWritten = @fwrite($this->output, $this->buffer); + if ($bytesWritten > 0) { + $this->buffer = substr($this->buffer, $bytesWritten); + } + }); } /** @@ -25,13 +38,6 @@ class ProtocolStreamWriter implements ProtocolWriter */ public function write(Message $msg) { - $data = (string)$msg; - $msgSize = strlen($data); - $totalBytesWritten = 0; - - while ($totalBytesWritten < $msgSize) { - $bytesWritten = fwrite($this->output, substr($data, $totalBytesWritten)); - $totalBytesWritten += $bytesWritten; - } + $this->buffer .= $msg; } }