/** * {@inheritdoc} */ public function cancel(\Throwable $e) : array { if ($this->state === self::PENDING) { Loop::cancel($this->watcher); $this->fail($e); } return []; }
/** * {@inheritdoc} */ public function cancelObserver(string $watcherId) { if (isset($this->watchers[$watcherId])) { unset($this->watchers[$watcherId]); if (empty($this->watchers)) { Loop::cancel($this->timer); } } }
protected function disposeWriter(\Throwable $e = null) { $this->writerEnabled = false; if ($this->writeWatcher !== null) { try { Loop::cancel($this->writeWatcher); } finally { $this->writeWatcher = null; } } if ($this->pendingWrites) { while (!$this->pendingWrites->isEmpty()) { $write = $this->pendingWrites->dequeue(); if (!$write->disabled) { $write->fail($e ?? new StreamClosedException('Socket writer disposed')); } } } }
/** * {@inheritdoc} */ public function cancel(\Throwable $e) : array { if ($this->state === self::PENDING) { Loop::cancel($this->watcher); try { if ($this->promise instanceof Awaitable) { $this->state = self::FAILED; try { return $this->promise->cancel($e); } finally { $this->state = self::PENDING; } } } finally { $this->fail($e); } } return []; }
protected function disposeReader(\Throwable $e = null) { $this->readerEnabled = false; if ($this->readWatcher !== null) { try { Loop::cancel($this->readWatcher); } finally { $this->readWatcher = null; } } if ($this->pendingReads) { while (!$this->pendingReads->isEmpty()) { $read = $this->pendingReads->dequeue(); if (!$read->disabled) { $read->fail($e ?? new StreamClosedException('Socket reader disposed')); } } } return $this->readBuffer; }
/** * 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; } }); }