/** * Read the next frame. * * @recoil-coroutine * * @return IncomingFrame The value read from the channel. * @throws ChannelClosedException The channel has been closed. */ public function read() : Generator { if ($this->stream === null) { throw ChannelClosedException::channelClosed(); } $buffer = ''; $required = 0; try { feed: $frame = $this->parser->feed($buffer, $required); } catch (RecoilAmqpException $e) { assert(Debug::dumpException('#' . (int) $this->stream, $e)); throw $e; } if ($frame === null) { // @todo handle stream close and throw channel closed exception $buffer = (yield Recoil::read($this->stream, $required)); if ($buffer === '') { \fclose($this->stream); $this->stream = null; throw ChannelClosedException::channelClosed(); } assert(Debug::dumpIncomingHex('#' . (int) $this->stream, $buffer)); goto feed; } assert(Debug::dumpIncomingFrame('#' . (int) $this->stream, $frame)); return $frame; }
private function readLoop() : Generator { try { while ($this->state === ConnectionState::OPEN) { $frame = (yield $this->transport->read()); $this->heartbeatsSinceFrameReceived = 0; if ($frame instanceof HeartbeatFrame) { continue; } elseif ($frame->frameChannelId > 0) { $channelManager = $this->channels[$frame->frameChannelId] ?? null; if ($channelManager === null) { throw ProtocolException::unexpectedFrame($frame, \sprintf('channel #%s is closed', $frame->frameChannelId)); } (yield $channelManager->dispatch($frame)); } elseif ($frame instanceof ConnectionCloseFrame) { (yield $this->transport->close()); throw AmqpException::create($frame->replyText, $frame->replyCode); } else { throw ProtocolException::unexpectedFrame($frame, 'received on channel #0'); } } while ($this->state === ConnectionState::CLOSING) { $frame = (yield $this->transport->read()); if ($frame instanceof ConnectionCloseOkFrame) { (yield $this->transport->close()); (yield $this->onClose()); } } } catch (ChannelClosedException $e) { (yield $this->onClose(SingleConnectionException::closedUnexpectedly($this->options, $e))); } catch (Throwable $e) { assert(Debug::dumpException('read-loop', $e)); (yield $this->onClose($e)); } }