/** * Given the raw bytes containing the property-flags and * property-list from a content-frame-header, parse and insert * into a dictionary stored in this object as an attribute named * 'properties'. */ public function load_properties($raw_bytes) { $r = new AMQP_Reader($raw_bytes); // Read 16-bit shorts until we get one with a low bit set to zero $flags = array(); while (true) { $flag_bits = $r->read_short(); array_push($flags, $flag_bits); if (($flag_bits & 1) == 0) { break; } } $shift = 0; $d = array(); foreach ($this->prop_types as $key => $proptype) { if ($shift == 0) { if (!$flags) { break; } $flag_bits = array_shift($flags); $shift = 15; } if ($flag_bits & 1 << $shift) { $d[$key] = call_user_func(array($r, "read_" . $proptype)); } $shift -= 1; } $this->properties = $d; }
function wait_content() { $frm = $this->next_frame(); $frame_type = $frm[0]; $payload = $frm[1]; if ($frame_type != 2) { throw new Exception("Expecting Content header"); } $payload_reader = new AMQP_Reader(substr($payload, 0, 12)); $class_id = $payload_reader->read_short(); $weight = $payload_reader->read_short(); $body_size = $payload_reader->read_longlong(); $msg = new AMQP_Message(); $msg->load_properties(substr($payload, 12)); $body_parts = array(); $body_received = 0; while (bccomp($body_size, $body_received) == 1) { $frm = $this->next_frame(); $frame_type = $frm[0]; $payload = $frm[1]; if ($frame_type != 3) { throw new Exception("Expecting Content body, received frame type {$frame_type}"); } $body_parts[] = $payload; $body_received = bcadd($body_received, strlen($payload)); } $msg->body = implode("", $body_parts); if ($this->auto_decode and isset($msg->content_encoding)) { try { $msg->body = $msg->body->decode($msg->content_encoding); } catch (Exception $e) { self::debug_msg("Ignoring body decoding exception: " . $e->getMessage()); } } return $msg; }
/** * Read an AMQP table, and return as a PHP array. keys are strings, * values are (type,value) tuples. */ public function read_table() { $this->bitcount = $this->bits = 0; $tlen = $this->read_php_int(); if ($tlen < 0) { throw new Exception("Table is longer than supported"); } $table_data = new AMQP_Reader($this->rawread($tlen)); $result = array(); while ($table_data->tell() < $tlen) { $name = $table_data->read_shortstr(); $ftype = $table_data->rawread(1); if ($ftype == 'S') { $val = $table_data->read_longstr(); } else { if (ftype == 'I') { $val = $table_data->read_signed_long(); } else { if ($ftype == 'D') { $e = $table_data->read_octet(); $n = $table_data->read_signed_long(); $val = new AMQP_Wire_Decimal($n, $e); } else { if ($ftype == 'T') { $val = $table_data->read_timestamp(); } else { if ($ftype == 'F') { $val = $table_data->read_table(); // recursion } else { error_log("Usupported table field type {$ftype}"); $val = NULL; } } } } } $result[$name] = array($ftype, $val); } return $result; }