/** * {@inheritdoc} */ public function close() { if ($this->socket !== null) { try { $this->reader->dispose(); Socket::shutdown($this->socket); } finally { $this->socket = null; } } }
protected function createSocketChannel($socket, int $chunkSize, int $length = null) : Channel { $size = (int) \max(1, \ceil(65535 / $chunkSize)); return Channel::fromGenerator($size, function (Channel $channel) use($socket, $length, $chunkSize) { $reader = new SocketReader($socket); $remaining = $length ?? \PHP_INT_MAX; try { while ($remaining && null !== ($chunk = (yield $reader->readBuffer(\min($remaining, $chunkSize))))) { $remaining -= \strlen($chunk); (yield $channel->send($chunk)); } } finally { $reader->dispose(); } }); }
protected function socketReader($socket) : \Generator { $reader = new SocketReader($socket); $remaining = $this->length ?? \PHP_INT_MAX; $contents = ''; try { while ($remaining && null !== ($chunk = (yield $reader->read($remaining)))) { $contents .= $chunk; $remaining -= \strlen($chunk); } } finally { $reader->dispose(); if ($this->closeSource) { Socket::shutdown($socket); } } return $contents; }