/** * Register a listener to be notified when a stream is ready to write. * * @param stream $stream The PHP stream resource to check. * @param callable $listener Invoked when the stream is ready. */ public function addWriteStream($stream, callable $listener) { $this->emit('addWriteStream', [$stream, $listener]); $this->loop->addWriteStream($stream, function ($stream) use($listener) { $this->emit('writeStreamTick', [$stream, $listener]); $listener($stream, $this); }); }
/** * @param string $message */ public function send($message) { if ($this->closed) { return; } $this->messages[] = $message; if (!$this->listening) { $this->listening = true; $this->loop->addWriteStream($this->fileDescriptor, $this->writeListener); } }
/** * Add event listener to event loop. * * @param $fd * @param $flag * @param $func * @param array $args * @return bool */ public function add($fd, $flag, $func, $args = array()) { switch ($flag) { case EventInterface::EV_READ: return $this->_loop->addReadStream($fd, $func); case EventInterface::EV_WRITE: return $this->_loop->addWriteStream($fd, $func); case EventInterface::EV_SIGNAL: return $this->_loop->addSignal($fd, $func); case EventInterface::EV_TIMER: return $this->_loop->addPeriodicTimer($fd, $func); case EventInterface::EV_TIMER_ONCE: return $this->_loop->addTimer($fd, $func); } return false; }
public function testRunWaitsForFutureTickEvents() { $stream = $this->createStream(); $this->loop->addWriteStream($stream, function () use($stream) { $this->loop->removeStream($stream); $this->loop->futureTick(function () { echo 'future-tick' . PHP_EOL; }); }); $this->expectOutputString('future-tick' . PHP_EOL); $this->loop->run(); }
/** * Watch a stream resource to become writable and trigger the callback when actionable * * @param resource $stream A stream resource to watch for writability * @param callable $callback Any valid PHP callable * @param bool $enableNow Should the watcher be enabled now or held for later use? * @return int */ public function onWritable($stream, callable $callback, $enableNow = true) { $watcherId = null; $this->reactor->addWriteStream($stream, function () use($callback, &$watcherId, $stream) { if (isset($this->disabledWatchers[$watcherId])) { return null; } return call_user_func($callback, $watcherId, $stream, $this); }); $watcherId = $this->registerWatcher(self::WATCHER_TYPE_WRITE, $stream); if (!$enableNow) { $this->disable($watcherId); } return $watcherId; }
/** * Asynchronously sends buffered data over the wire. * * - Calls {@link eventLoops}'s addWriteStream() with client's stream. * - Consecutive calls will return the same instance of promise. * * @return Promise\PromiseInterface */ protected function flushWriteBuffer() { if ($this->flushWriteBufferPromise) { return $this->flushWriteBufferPromise; } else { $deferred = new Promise\Deferred(); $this->eventLoop->addWriteStream($this->getStream(), function ($stream) use($deferred) { try { $this->write(); if ($this->writeBuffer->isEmpty()) { $this->eventLoop->removeWriteStream($stream); $this->flushWriteBufferPromise = null; $deferred->resolve(true); } } catch (\Exception $e) { $this->eventLoop->removeWriteStream($stream); $this->flushWriteBufferPromise = null; $deferred->reject($e); } }); return $this->flushWriteBufferPromise = $deferred->promise(); } }