1
0
Fork 0

Shutdown when the socket is closed (#191)

pull/165/head^2
Felix Becker 2016-11-30 21:10:05 +01:00 committed by GitHub
parent 5077b1a87a
commit f56b14438b
4 changed files with 22 additions and 4 deletions

View File

@ -68,10 +68,12 @@ if (!empty($options['tcp'])) {
exit(1);
} else if ($pid === 0) {
// Child process
$ls = new LanguageServer(
new ProtocolStreamReader($socket),
new ProtocolStreamWriter($socket)
);
$reader = new ProtocolStreamReader($socket);
$writer = new ProtocolStreamWriter($socket);
$reader->on('close', function () {
fwrite(STDOUT, "Connection closed\n");
});
$ls = new LanguageServer($reader, $writer);
Loop\run();
// Just for safety
exit(0);

View File

@ -65,6 +65,10 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
{
parent::__construct($this, '/');
$this->protocolReader = $reader;
$this->protocolReader->on('close', function () {
$this->shutdown();
$this->exit();
});
$this->protocolReader->on('message', function (Message $msg) {
coroutine(function () use ($msg) {
// Ignore responses, this is the handler for requests and notifications

View File

@ -8,6 +8,8 @@ use Sabre\Event\EmitterInterface;
/**
* Must emit a "message" event with a Protocol\Message object as parameter
* when a message comes in
*
* Must emit a "close" event when the stream closes
*/
interface ProtocolReader extends EmitterInterface
{

View File

@ -25,7 +25,17 @@ class ProtocolStreamReader extends Emitter implements ProtocolReader
{
$this->input = $input;
$this->on('close', function () {
Loop\removeReadStream($this->input);
});
Loop\addReadStream($this->input, function () {
if (feof($this->input)) {
// If stream_select reported a status change for this stream,
// but the stream is EOF, it means it was closed.
$this->emit('close');
return;
}
while (($c = fgetc($this->input)) !== false && $c !== '') {
$this->buffer .= $c;
switch ($this->parsingMode) {