onWritable() 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 onWritable ( 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.
コード例 #1
0
ファイル: SocketWriterTrait.php プロジェクト: koolkode/async
 public function write(string $bytes) : Awaitable
 {
     if ($bytes === '') {
         if (!\is_resource($this->socket)) {
             return new Failure(new StreamClosedException('Socket resource unavailable'));
         }
         return new Success(0);
     }
     $len = \strlen($bytes);
     if (!$this->writerEnabled) {
         try {
             $bytes = $this->writeBytes($bytes);
         } catch (\Throwable $e) {
             return new Failure($e);
         }
         if ($bytes === '') {
             return new Success($len);
         }
         $this->writerEnabled = true;
         if ($this->pendingWrites === null) {
             $this->pendingWrites = new \SplQueue();
         }
         if ($this->writeWatcher === null) {
             $this->writeWatcher = Loop::onWritable($this->socket, $this->createWriteWatcher());
         } else {
             try {
                 Loop::enable($this->writeWatcher);
             } catch (InvalidWatcherException $e) {
                 $this->writeWatcher = Loop::onWritable($this->socket, $this->createWriteWatcher());
             }
         }
     }
     $this->pendingWrites->enqueue($write = new PendingWrite($bytes, $len, function () {
         foreach ($this->pendingWrites as $write) {
             if (!$write->disabled) {
                 return;
             }
         }
         $this->writerEnabled = false;
         Loop::disable($this->writeWatcher);
     }));
     return $write;
 }
コード例 #2
0
ファイル: AwaitWrite.php プロジェクト: koolkode/async
 public function __construct($stream, float $timeout = 0)
 {
     if (!\is_resource($stream)) {
         throw new \InvalidArgumentException(\sprintf('Expecting stream resource, given %s', \is_object($stream) ? \get_class($stream) : \gettype($stream)));
     }
     $this->watcher = Loop::onWritable($stream, function () use($stream) {
         if ($this->state === self::PENDING) {
             Loop::cancel($this->watcher);
             if ($this->timer !== null) {
                 Loop::cancel($this->timer);
             }
             $this->resolve($stream);
         }
     });
     if ($timeout > 0) {
         $this->timer = Loop::delay($timeout * 1000, function () use($timeout) {
             if ($this->state === self::PENDING) {
                 Loop::cancel($this->watcher);
                 $this->fail(new TimeoutException(\sprintf('Await Write timed out after %.3f seconds', $timeout)));
             }
         });
     }
 }
コード例 #3
0
ファイル: WriteBytes.php プロジェクト: koolkode/async
 /**
  * Write bytes to the stream, register a writable watcher if not all bytes could be written.
  * 
  * @param resource $stream Resource to be written to.
  * @param string $bytes Bytes to be written.
  * @param int $chunkSize Write data chunk size.
  */
 protected function writeBytesToStream($stream, string $bytes, int $chunkSize)
 {
     $len = 0;
     try {
         if ($this->writeBytes($stream, $len, $bytes, $chunkSize)) {
             return $this->resolve($len);
         }
     } catch (\Throwable $e) {
         return $this->fail($e);
     }
     $retry = false;
     $this->watcher = Loop::onWritable($stream, function ($watcherId) use(&$len, &$bytes, &$retry, $stream, $chunkSize) {
         try {
             $length = $len;
             if ($this->writeBytes($stream, $len, $bytes, $chunkSize)) {
                 $this->watcher = null;
                 Loop::cancel($watcherId);
                 $retry = false;
                 return $this->resolve($len);
             }
             if ($retry && $length === $len) {
                 throw new StreamClosedException('Write failed after retry, assuming broken pipe');
             }
         } catch (\Throwable $e) {
             $this->watcher = null;
             Loop::cancel($watcherId);
             return $this->fail($e);
         }
         if ($length === $len) {
             $retry = true;
         }
     });
 }