Interop\Async\Loop::onReadable PHP Method

onReadable() public static method

Warning: Closing resources locally, e.g. with fclose, might not invoke the callback. Be sure to cancel the watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed resources are therefore undefined behavior. Multiple watchers on the same stream MAY be executed in any order.
public static onReadable ( resource $stream, callable $callback, mixed $data = null ) : string
$stream resource The stream to monitor.
$callback callable
$data mixed Arbitrary data given to the callback function as the `$data` parameter.
return string An unique identifier that can be used to cancel, enable or disable the watcher.
    public static function onReadable($stream, callable $callback, $data = null)
    {
        $driver = self::$driver ?: self::get();
        return $driver->onReadable($stream, $callback, $data);
    }

Usage Example

Example #1
0
 public function read(int $length = 8192) : Awaitable
 {
     if (!$this->readerEnabled) {
         $len = \strlen($this->readBuffer);
         try {
             $chunk = $this->fillReadBuffer($len, $length);
         } catch (\Throwable $e) {
             return new Failure($e);
         }
         if ($chunk === null) {
             return new Success(null);
         }
         if ($chunk !== $this) {
             if ($len > $length) {
                 $this->readBuffer = \substr($chunk, $length);
                 return new Success(\substr($chunk, 0, $length));
             }
             $this->readBuffer = '';
             return new Success($chunk);
         }
         $this->readerEnabled = true;
         if ($this->pendingReads === null) {
             $this->pendingReads = new \SplQueue();
         }
         if ($this->readWatcher === null) {
             $this->readWatcher = Loop::onReadable($this->socket, $this->createReadWatcher());
         } else {
             try {
                 Loop::enable($this->readWatcher);
             } catch (InvalidWatcherException $e) {
                 $this->readWatcher = Loop::onReadable($this->socket, $this->createReadWatcher());
             }
         }
     }
     $this->pendingReads->enqueue($read = new PendingRead($length, function () {
         foreach ($this->pendingReads as $read) {
             if (!$read->disabled) {
                 return;
             }
         }
         $this->readerEnabled = false;
         Loop::disable($this->readWatcher);
     }));
     return $read;
 }
All Usage Examples Of Interop\Async\Loop::onReadable