/** * Waits for a frame from the server * * @param int $timeout * @return array * @throws \Exception * @throws \PhpAmqpLib\Exception\AMQPTimeoutException * @throws \PhpAmqpLib\Exception\AMQPRuntimeException */ protected function wait_frame($timeout = 0) { if (is_null($this->input)) { $this->setIsConnected(false); throw new AMQPRuntimeException('Broken pipe or closed connection'); } $currentTimeout = $this->input->getTimeout(); $this->input->setTimeout($timeout); try { // frame_type + channel_id + size $this->wait_frame_reader->reuse($this->input->read(AMQPReader::OCTET + AMQPReader::SHORT + AMQPReader::LONG)); $frame_type = $this->wait_frame_reader->read_octet(); $channel = $this->wait_frame_reader->read_short(); $size = $this->wait_frame_reader->read_long(); // payload + ch $this->wait_frame_reader->reuse($this->input->read(AMQPReader::OCTET + (int) $size)); $payload = $this->wait_frame_reader->read($size); $ch = $this->wait_frame_reader->read_octet(); } catch (AMQPTimeoutException $e) { $this->input->setTimeout($currentTimeout); throw $e; } $this->input->setTimeout($currentTimeout); if ($ch != 0xce) { throw new AMQPRuntimeException(sprintf('Framing error, unexpected byte: %x', $ch)); } return array($frame_type, $channel, $payload); }
/** * @return AMQPMessage * @throws \PhpAmqpLib\Exception\AMQPRuntimeException */ public function wait_content() { list($frame_type, $payload) = $this->next_frame(); $this->validate_header_frame($frame_type); $this->wait_content_reader->reuse(mb_substr($payload, 0, 12, 'ASCII')); // $payload_reader = new AMQPReader(substr($payload,0,12)); $class_id = $this->wait_content_reader->read_short(); $weight = $this->wait_content_reader->read_short(); $body_size = $this->wait_content_reader->read_longlong(); //hack to avoid creating new instances of AMQPReader; $this->msg_property_reader->reuse(mb_substr($payload, 12, mb_strlen($payload, 'ASCII') - 12, 'ASCII')); $msg = new AMQPMessage(); $msg->load_properties($this->msg_property_reader); $msg->body_size = $body_size; list($msg_body, $is_truncated) = $this->build_msg_body($body_size); $msg->body = $msg_body; $msg->is_truncated = $is_truncated; if ($this->auto_decode && isset($msg->content_encoding)) { try { $msg->body = $msg->body->decode($msg->content_encoding); } catch (\Exception $e) { $this->debug->debug_msg('Ignoring body decoding exception: ' . $e->getMessage()); } } return $msg; }
/** * @return AMQPMessage * @throws \PhpAmqpLib\Exception\AMQPRuntimeException */ public function wait_content() { list($frame_type, $payload) = $this->next_frame(); $this->validate_header_frame($frame_type); $this->wait_content_reader->reuse(mb_substr($payload, 0, 12, 'ASCII')); // $payload_reader = new AMQPReader(substr($payload,0,12)); $class_id = $this->wait_content_reader->read_short(); $weight = $this->wait_content_reader->read_short(); //hack to avoid creating new instances of AMQPReader; $this->msg_property_reader->reuse(mb_substr($payload, 12, mb_strlen($payload, 'ASCII') - 12, 'ASCII')); return $this->createMessage($this->msg_property_reader, $this->wait_content_reader); }
/** * @dataProvider propertiesDataProvider */ public function testSerializeProperties(array $expected, array $properties) { /** @var AMQPReader $reader */ $reader = new AMQPReader(null); /** @var AMQPMessage $message */ $message = new AMQPMessage('', $properties); /** @var string $encodedData */ $encodedData = $message->serialize_properties(); // Bypasses the network part and injects the encoded data into the reader $reader->reuse($encodedData); // Injects the reader into the message $message->load_properties($reader); $this->assertEquals($expected, $message->get_properties()); }
/** * @return AMQPMessage * @throws \PhpAmqpLib\Exception\AMQPRuntimeException */ public function wait_content() { $frm = $this->next_frame(); $frame_type = $frm[0]; $payload = $frm[1]; if ($frame_type != 2) { throw new AMQPRuntimeException('Expecting Content header'); } $this->wait_content_reader->reuse(mb_substr($payload, 0, 12, 'ASCII')); // $payload_reader = new AMQPReader(substr($payload,0,12)); $class_id = $this->wait_content_reader->read_short(); $weight = $this->wait_content_reader->read_short(); $body_size = $this->wait_content_reader->read_longlong(); //hack to avoid creating new instances of AMQPReader; $this->msg_property_reader->reuse(mb_substr($payload, 12, mb_strlen($payload, 'ASCII') - 12, 'ASCII')); $msg = new AMQPMessage(); $msg->load_properties($this->msg_property_reader); $msg->body_size = $body_size; $body_parts = array(); $body_received = 0; while (bccomp($body_size, $body_received, 0) == 1) { $frm = $this->next_frame(); $frame_type = $frm[0]; $payload = $frm[1]; if ($frame_type != 3) { $PROTOCOL_CONSTANTS_CLASS = self::$PROTOCOL_CONSTANTS_CLASS; throw new AMQPRuntimeException(sprintf('Expecting Content body, received frame type %s (%s)', $frame_type, $PROTOCOL_CONSTANTS_CLASS::$FRAME_TYPES[$frame_type])); } $body_received = bcadd($body_received, mb_strlen($payload, 'ASCII'), 0); if (!is_null($this->body_size_max) && $body_received > $this->body_size_max) { $msg->is_truncated = true; continue; } $body_parts[] = $payload; } $msg->body = implode('', $body_parts); if ($this->auto_decode && isset($msg->content_encoding)) { try { $msg->body = $msg->body->decode($msg->content_encoding); } catch (\Exception $e) { if ($this->debug) { MiscHelper::debug_msg('Ignoring body decoding exception: ' . $e->getMessage()); } } } return $msg; }