/** * {@inheritdoc} */ protected function enableWriteWatcher(StreamWatcher $watcher) { if (isset($this->streamEvents[$watcher->streamId])) { $watcher->event = $this->streamEvents[$watcher->streamId]; } else { if (!\is_resource($watcher->stream)) { throw new \Exception('Invalid resource detected'); } switch (\stream_get_meta_data($watcher->stream)['stream_type'] ?? '') { case 'STDIO': case 'tcp_socket/ssl': $watcher->event = \uv_poll_init($this->loop, $watcher->stream); break; default: $watcher->event = \uv_poll_init_socket($this->loop, $watcher->stream); } $this->streamEvents[$watcher->streamId] = $watcher->event; } $id = (int) $watcher->event; if (empty($this->writeWatchers[$id])) { if (\uv_is_active($watcher->event)) { \uv_poll_stop($watcher->event); } \uv_poll_start($watcher->event, \UV::WRITABLE | (empty($this->readWatchers[$id]) ? 0 : \UV::READABLE), $this->streamCallback); } $this->writeWatchers[$id][$watcher->id] = $watcher; }
/** * {@inheritdoc} */ public function listen(Io $io, float $timeout = 0) { $resource = $io->getResource(); $id = (int) $resource; if (!isset($this->sockets[$id]) || $io !== $this->sockets[$id]) { throw new FreedError(); } // If no poll handle exists for the socket, create one now. if (!isset($this->polls[$id])) { $this->polls[$id] = \uv_poll_init_socket($this->loopHandle, $resource); } // Begin polling for events. \uv_poll_start($this->polls[$id], $this->type, $this->pollCallback); // If a timeout is given, set up a separate timer for cancelling the poll in the future. if ($timeout) { if (self::MIN_TIMEOUT > $timeout) { $timeout = self::MIN_TIMEOUT; } if (!isset($this->timers[$id])) { $timer = \uv_timer_init($this->loopHandle); $this->handles[(int) $timer] = $id; $this->timers[$id] = $timer; } $timeout *= self::MILLISEC_PER_SEC; \uv_timer_start($this->timers[$id], $timeout, $timeout, $this->timerCallback); } elseif (isset($this->timers[$id]) && \uv_is_active($this->timers[$id])) { \uv_timer_stop($this->timers[$id]); } }