1
0
Fork 0

Made ProtocolStreamWriter async

pull/112/head
Stephan Unverwerth 2016-10-24 23:08:59 +02:00
parent 5ecab683eb
commit 45ac0c8b5f
1 changed files with 14 additions and 8 deletions

View File

@ -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;
}
}