private function commandParser($client) : \Generator
{
$readWatcherId = yield;
$buffer = "";
$length = null;
do {
yield;
$data = @fread($client, 8192);
if ($data == "" && (!is_resource($client) || @feof($client))) {
\Amp\cancel($readWatcherId);
return;
}
$buffer .= $data;
do {
if (!isset($length)) {
if (!isset($buffer[3])) {
break;
}
$length = unpack("Nlength", substr($buffer, 0, 4))["length"];
$buffer = substr($buffer, 4);
}
if (!isset($buffer[$length - 1])) {
break;
}
$message = @\json_decode(substr($buffer, 0, $length), true);
$buffer = (string) substr($buffer, $length);
$length = null;
if (!isset($message["action"])) {
continue;
}
switch ($message["action"]) {
case "restart":
$this->restart();
break;
case "stop":
\Amp\resolve($this->stop())->when('Amp\\stop');
break;
}
} while (1);
} while (1);
}