/** * serialize the 'properties' attribute (a dictionary) into the * raw bytes making up a set of property flags and a property * list, suitable for putting into a content frame header. */ public function serialize_properties() { $shift = 15; $flag_bits = 0; $flags = array(); $raw_bytes = new PhpAmqpLib_Wire_AMQPWriter(); foreach ($this->prop_types as $key => $proptype) { if (isset($this->properties[$key])) { $val = $this->properties[$key]; } else { $val = null; } if ($val != null) { if ($shift == 0) { $flags[] = $flag_bits; $flag_bits = 0; $shift = 15; } $flag_bits |= 1 << $shift; if ($proptype != "bit") { $raw_bytes->{'write_' . $proptype}($val); } } $shift -= 1; } $flags[] = $flag_bits; $result = new PhpAmqpLib_Wire_AMQPWriter(); foreach ($flags as $flag_bits) { $result->write_short($flag_bits); } $result->write($raw_bytes->getvalue()); return $result->getvalue(); }
/** * Sends a heartbeat message */ protected function write_heartbeat() { $pkt = new PhpAmqpLib_Wire_AMQPWriter(); $pkt->write_octet(8); $pkt->write_short(0); $pkt->write_long(0); $pkt->write_octet(0xce); $this->write($pkt->getvalue()); }
/** * Serializes the 'properties' attribute (a dictionary) into the * raw bytes making up a set of property flags and a property * list, suitable for putting into a content frame header. * * @return string * @todo Inject the PhpAmqpLib_Wire_AMQPWriter to make the method easier to test */ public function serialize_properties() { if (!empty($this->serialized_properties)) { return $this->serialized_properties; } $shift = 15; $flag_bits = 0; $flags = array(); $raw_bytes = new PhpAmqpLib_Wire_AMQPWriter(); foreach ($this->prop_types as $key => $prototype) { $val = isset($this->properties[$key]) ? $this->properties[$key] : null; // Very important: PHP type eval is weak, use the === to test the // value content. Zero or false value should not be removed if ($val === null) { $shift -= 1; continue; } if ($shift === 0) { $flags[] = $flag_bits; $flag_bits = 0; $shift = 15; } $flag_bits |= 1 << $shift; if ($prototype != 'bit') { $raw_bytes->{'write_' . $prototype}($val); } $shift -= 1; } $flags[] = $flag_bits; $result = new PhpAmqpLib_Wire_AMQPWriter(); foreach ($flags as $flag_bits) { $result->write_short($flag_bits); } $result->write($raw_bytes->getvalue()); $this->serialized_properties = $result->getvalue(); return $this->serialized_properties; }
/** * Returns a new PhpAmqpLib_Wire_AMQPWriter or mutates the provided $pkt * * @param string $channel * @param int $class_id * @param int $weight * @param int $body_size * @param string $packed_properties * @param string $body * @param PhpAmqpLib_Wire_AMQPWriter $pkt * @return PhpAmqpLib_Wire_AMQPWriter */ public function prepare_content($channel, $class_id, $weight, $body_size, $packed_properties, $body, $pkt = null) { if (empty($pkt)) { $pkt = new PhpAmqpLib_Wire_AMQPWriter(); } // Content already prepared ? $key_cache = sprintf('%s|%s|%s|%s', $channel, $packed_properties, $class_id, $weight); if (!isset($this->prepare_content_cache[$key_cache])) { $w = new PhpAmqpLib_Wire_AMQPWriter(); $w->write_octet(2); $w->write_short($channel); $w->write_long(mb_strlen($packed_properties, 'ASCII') + 12); $w->write_short($class_id); $w->write_short($weight); $this->prepare_content_cache[$key_cache] = $w->getvalue(); if (count($this->prepare_content_cache) > $this->prepare_content_cache_max_size) { reset($this->prepare_content_cache); $old_key = key($this->prepare_content_cache); unset($this->prepare_content_cache[$old_key]); } } $pkt->write($this->prepare_content_cache[$key_cache]); $pkt->write_longlong($body_size); $pkt->write($packed_properties); $pkt->write_octet(0xce); // memory efficiency: walk the string instead of biting it. good for very large packets (close in size to memory_limit setting) $position = 0; $bodyLength = mb_strlen($body, 'ASCII'); while ($position < $bodyLength) { $payload = mb_substr($body, $position, $this->frame_max - 8, 'ASCII'); $position += $this->frame_max - 8; $pkt->write_octet(3); $pkt->write_short($channel); $pkt->write_long(mb_strlen($payload, 'ASCII')); $pkt->write($payload); $pkt->write_octet(0xce); } return $pkt; }