Made ProtocolStreamWriter async
parent
5ecab683eb
commit
45ac0c8b5f
|
@ -4,17 +4,30 @@ declare(strict_types = 1);
|
||||||
namespace LanguageServer;
|
namespace LanguageServer;
|
||||||
|
|
||||||
use LanguageServer\Protocol\Message;
|
use LanguageServer\Protocol\Message;
|
||||||
|
use Sabre\Event\Loop;
|
||||||
|
|
||||||
class ProtocolStreamWriter implements ProtocolWriter
|
class ProtocolStreamWriter implements ProtocolWriter
|
||||||
{
|
{
|
||||||
private $output;
|
private $output;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string $buffer
|
||||||
|
*/
|
||||||
|
private $buffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param resource $output
|
* @param resource $output
|
||||||
*/
|
*/
|
||||||
public function __construct($output)
|
public function __construct($output)
|
||||||
{
|
{
|
||||||
$this->output = $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)
|
public function write(Message $msg)
|
||||||
{
|
{
|
||||||
$data = (string)$msg;
|
$this->buffer .= $msg;
|
||||||
$msgSize = strlen($data);
|
|
||||||
$totalBytesWritten = 0;
|
|
||||||
|
|
||||||
while ($totalBytesWritten < $msgSize) {
|
|
||||||
$bytesWritten = fwrite($this->output, substr($data, $totalBytesWritten));
|
|
||||||
$totalBytesWritten += $bytesWritten;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue