Skip to content

Commit f56b144

Browse files
authored
Shutdown when the socket is closed (felixfbecker#191)
1 parent 5077b1a commit f56b144

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

bin/php-language-server.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@
6868
exit(1);
6969
} else if ($pid === 0) {
7070
// Child process
71-
$ls = new LanguageServer(
72-
new ProtocolStreamReader($socket),
73-
new ProtocolStreamWriter($socket)
74-
);
71+
$reader = new ProtocolStreamReader($socket);
72+
$writer = new ProtocolStreamWriter($socket);
73+
$reader->on('close', function () {
74+
fwrite(STDOUT, "Connection closed\n");
75+
});
76+
$ls = new LanguageServer($reader, $writer);
7577
Loop\run();
7678
// Just for safety
7779
exit(0);

src/LanguageServer.php

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public function __construct(ProtocolReader $reader, ProtocolWriter $writer)
6565
{
6666
parent::__construct($this, '/');
6767
$this->protocolReader = $reader;
68+
$this->protocolReader->on('close', function () {
69+
$this->shutdown();
70+
$this->exit();
71+
});
6872
$this->protocolReader->on('message', function (Message $msg) {
6973
coroutine(function () use ($msg) {
7074
// Ignore responses, this is the handler for requests and notifications

src/ProtocolReader.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
/**
99
* Must emit a "message" event with a Protocol\Message object as parameter
1010
* when a message comes in
11+
*
12+
* Must emit a "close" event when the stream closes
1113
*/
1214
interface ProtocolReader extends EmitterInterface
1315
{

src/ProtocolStreamReader.php

+10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@ public function __construct($input)
2525
{
2626
$this->input = $input;
2727

28+
$this->on('close', function () {
29+
Loop\removeReadStream($this->input);
30+
});
31+
2832
Loop\addReadStream($this->input, function () {
33+
if (feof($this->input)) {
34+
// If stream_select reported a status change for this stream,
35+
// but the stream is EOF, it means it was closed.
36+
$this->emit('close');
37+
return;
38+
}
2939
while (($c = fgetc($this->input)) !== false && $c !== '') {
3040
$this->buffer .= $c;
3141
switch ($this->parsingMode) {

0 commit comments

Comments
 (0)