From cd3bf18fe292e751dca0b634a1f4c567c500f775 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Mon, 24 Oct 2016 23:20:15 +0200 Subject: [PATCH] Revert "Handle closed input or output stream (#110)" This reverts commit 83afa0c1b8f779d339d2034e4e6fdc154762fdb3. --- src/ProtocolStreamReader.php | 6 +----- src/ProtocolStreamWriter.php | 12 +++++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/ProtocolStreamReader.php b/src/ProtocolStreamReader.php index 699fb3c..2d0e351 100644 --- a/src/ProtocolStreamReader.php +++ b/src/ProtocolStreamReader.php @@ -6,7 +6,6 @@ namespace LanguageServer; use LanguageServer\Protocol\Message; use AdvancedJsonRpc\Message as MessageBody; use Sabre\Event\Loop; -use RuntimeException; class ProtocolStreamReader implements ProtocolReader { @@ -26,11 +25,8 @@ class ProtocolStreamReader implements ProtocolReader public function __construct($input) { $this->input = $input; - Loop\addReadStream($this->input, function () { - if (feof($this->input)) { - throw new RuntimeException('Stream is closed'); - } + Loop\addReadStream($this->input, function () { while (($c = fgetc($this->input)) !== false && $c !== '') { $this->buffer .= $c; switch ($this->parsingMode) { diff --git a/src/ProtocolStreamWriter.php b/src/ProtocolStreamWriter.php index cee9b60..2ac3579 100644 --- a/src/ProtocolStreamWriter.php +++ b/src/ProtocolStreamWriter.php @@ -4,6 +4,7 @@ declare(strict_types = 1); namespace LanguageServer; use LanguageServer\Protocol\Message; +use RuntimeException; class ProtocolStreamWriter implements ProtocolWriter { @@ -30,7 +31,16 @@ class ProtocolStreamWriter implements ProtocolWriter $totalBytesWritten = 0; while ($totalBytesWritten < $msgSize) { - $bytesWritten = fwrite($this->output, substr($data, $totalBytesWritten)); + error_clear_last(); + $bytesWritten = @fwrite($this->output, substr($data, $totalBytesWritten)); + if ($bytesWritten === false) { + $error = error_get_last(); + if ($error !== null) { + throw new RuntimeException('Could not write message: ' . error_get_last()['message']); + } else { + throw new RuntimeException('Could not write message'); + } + } $totalBytesWritten += $bytesWritten; } }