protected function createReadWatcher() : callable
{
return function ($id) {
while (!$this->pendingReads->isEmpty()) {
$read = $this->pendingReads->bottom();
if ($read->disabled) {
$this->pendingReads->dequeue();
continue;
}
$len = \strlen($this->readBuffer);
try {
$chunk = $this->fillReadBuffer($len, $read->length);
} catch (\Throwable $e) {
$this->readerEnabled = false;
Loop::disable($id);
while (!$this->pendingReads->isEmpty()) {
$this->pendingReads->dequeue()->fail($e);
}
return;
}
if ($chunk === $this) {
return;
}
if ($chunk === null) {
$this->readerEnabled = false;
Loop::disable($id);
while (!$this->pendingReads->isEmpty()) {
$this->pendingReads->dequeue()->resolve(null);
}
return;
}
$read = $this->pendingReads->dequeue();
if ($len > $read->length) {
$this->readBuffer = \substr($chunk, $read->length);
$read->resolve(\substr($chunk, 0, $read->length));
} else {
$this->readBuffer = '';
$read->resolve($chunk);
}
}
$this->readerEnabled = false;
Loop::disable($id);
};
}