示例#1
0
 /**
  * Negotiates connection tuning parameters
  *
  * @param $channel_max
  * @param $frame_max
  * @param $heartbeat
  */
 protected function x_tune_ok($channel_max, $frame_max, $heartbeat)
 {
     $args = new AMQPWriter();
     $args->write_short($channel_max);
     $args->write_long($frame_max);
     $args->write_short($heartbeat);
     $this->send_method_frame(array(10, 31), $args);
     $this->wait_tune_ok = false;
 }
 /**
  * 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 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 AMQPWriter();
     foreach ($flags as $flag_bits) {
         $result->write_short($flag_bits);
     }
     $result->write($raw_bytes->getvalue());
     return $result->getvalue();
 }
示例#3
0
 /**
  * @param boolean $nowait
  * @return array
  */
 public function confirmSelect($nowait = false)
 {
     $writer = new AMQPWriter();
     $writer->write_bits(array($nowait));
     return array(85, 10, $writer);
 }
示例#4
0
 /**
  * Sends a heartbeat message
  */
 protected function write_heartbeat()
 {
     $pkt = new AMQPWriter();
     $pkt->write_octet(8);
     $pkt->write_short(0);
     $pkt->write_long(0);
     $pkt->write_octet(0xce);
     $this->write($pkt->getvalue());
 }
示例#5
0
 /**
  * Publish batch
  *
  * @return void
  */
 public function publish_batch()
 {
     if (empty($this->batch_messages)) {
         return;
     }
     /** @var AMQPWriter $pkt */
     $pkt = new AMQPWriter();
     /** @var AMQPMessage $msg */
     foreach ($this->batch_messages as $m) {
         $msg = $m[0];
         $exchange = isset($m[1]) ? $m[1] : '';
         $routing_key = isset($m[2]) ? $m[2] : '';
         $mandatory = isset($m[3]) ? $m[3] : false;
         $immediate = isset($m[4]) ? $m[4] : false;
         $ticket = isset($m[5]) ? $m[5] : null;
         $pkt->write($this->pre_publish($exchange, $routing_key, $mandatory, $immediate, $ticket));
         $this->connection->prepare_content($this->channel_id, 60, 0, mb_strlen($msg->body, 'ASCII'), $msg->serialize_properties(), $msg->body, $pkt);
         if ($this->next_delivery_tag > 0) {
             $this->published_messages[$this->next_delivery_tag] = $msg;
             $this->next_delivery_tag = bcadd($this->next_delivery_tag, '1', 0);
         }
     }
     //call write here
     $this->connection->write($pkt->getvalue());
     $this->batch_messages = array();
 }
示例#6
0
 /**
  * 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 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 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 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;
 }
示例#7
0
 /**
  * Write PHP array, as table. Input array format: keys are strings,
  * values are (type,value) tuples.
  *
  * @param AMQPTable|array $d Instance of AMQPTable or PHP array WITH format hints (unlike write_array())
  * @return $this
  * @throws \PhpAmqpLib\Exception\AMQPInvalidArgumentException
  */
 public function write_table($d)
 {
     $typeIsSym = !$d instanceof AMQPTable;
     //purely for back-compat purposes
     $table_data = new AMQPWriter();
     foreach ($d as $k => $va) {
         list($ftype, $v) = $va;
         $table_data->write_shortstr($k);
         $table_data->write_value($typeIsSym ? AMQPAbstractCollection::getDataTypeForSymbol($ftype) : $ftype, $v);
     }
     $table_data = $table_data->getvalue();
     $this->write_long(mb_strlen($table_data, 'ASCII'));
     $this->write($table_data);
     return $this;
 }
示例#8
0
 /**
  * @param mixed $table
  * @param mixed $integer_op
  * @param mixed $string_op
  * @return array
  */
 public function testTable($table, $integer_op, $string_op)
 {
     $writer = new AMQPWriter();
     $writer->write_table(empty($table) ? array() : $table);
     $writer->write_octet($integer_op);
     $writer->write_octet($string_op);
     return array(120, 30, $writer);
 }
示例#9
0
 public function testTable($table, $integer_op, $string_op)
 {
     $args = new AMQPWriter();
     $args->write_table($table);
     $args->write_octet($integer_op);
     $args->write_octet($string_op);
     return array(120, 30, $args);
 }
示例#10
0
 public function testWriteTableThrowsExceptionOnInvalidType()
 {
     $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPInvalidArgumentException', "Invalid type '_'");
     $this->_writer->write_table(array('x-foo' => array('_', 'bar')));
 }
示例#11
0
 protected function writeAndRead($v, $write_method, $read_method)
 {
     $w = new AMQPWriter();
     $w->{$write_method}($v);
     $r = new AMQPReader($w->getvalue());
     $this->assertEquals($v, $r->{$read_method}());
 }
示例#12
0
 public function testTableWriteReadCollection()
 {
     $w = new AMQPWriter();
     $w->write_table(new AMQPTable(array('long' => 12345, 'long_neg' => -12345, 'longlong' => 3000000000, 'longlong_neg' => -3000000000, 'float_low' => 9.2233720368548, 'float_high' => (double) 9.2233720368548E+18, 'bool_true' => true, 'bool_false' => false, 'array' => array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true), 'array_empty' => array(), 'table' => array('foo' => 'bar', 'baz' => 'boo', 'bool' => true, 'tbl' => array('bar' => 'baz'), 'arr' => array('boo', false, 5)), 'table_num' => array(1 => 5, 3 => 'foo', 786 => 674), 'array_nested' => array(1, array(2, array(3, array(4)))), 'table_nested' => array('i' => 1, 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4)))))));
     $r = new AMQPReader($w->getvalue());
     //type casting - thanks to ancient phpunit on travis
     $this->assertEquals(array('long' => 12345, 'long_neg' => -12345, 'longlong' => (string) 3000000000, 'longlong_neg' => (string) -3000000000, 'float_low' => (string) (double) 9.2233720368548, 'float_high' => (string) (double) 9.2233720368548E+18, 'bool_true' => true, 'bool_false' => false, 'array' => array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true), 'array_empty' => array(), 'table' => array('foo' => 'bar', 'baz' => 'boo', 'bool' => true, 'tbl' => array('bar' => 'baz'), 'arr' => array('boo', false, 5)), 'table_num' => array(1 => 5, 3 => 'foo', 786 => 674), 'array_nested' => array(1, array(2, array(3, array(4)))), 'table_nested' => array('i' => 1, 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4))))), $r->read_table(true)->getNativeData());
 }
示例#13
0
 public function confirmSelect($nowait = false)
 {
     $args = new AMQPWriter();
     $args->write_bit($nowait);
     return array(85, 10, $args);
 }
 /**
  * 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()
 {
     if (!empty($this->serialized_properties)) {
         return $this->serialized_properties;
     }
     $shift = 15;
     $flag_bits = 0;
     $flags = array();
     $raw_bytes = new AMQPWriter();
     foreach ($this->prop_types as $key => $prototype) {
         $val = isset($this->properties[$key]) ? $this->properties[$key] : null;
         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 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;
 }
示例#15
0
 /**
  * Write PHP array, as table. Input array format: keys are strings,
  * values are (type,value) tuples.
  */
 public function write_table($d)
 {
     $table_data = new AMQPWriter();
     foreach ($d as $k => $va) {
         list($ftype, $v) = $va;
         $table_data->write_shortstr($k);
         if ($ftype == 'S') {
             $table_data->write('S');
             $table_data->write_longstr($v);
         } elseif ($ftype == 'I') {
             $table_data->write('I');
             $table_data->write_signed_long($v);
         } elseif ($ftype == 'D') {
             // 'D' type values are passed AMQPDecimal instances.
             $table_data->write('D');
             $table_data->write_octet($v->e);
             $table_data->write_signed_long($v->n);
         } elseif ($ftype == 'T') {
             $table_data->write('T');
             $table_data->write_timestamp($v);
         } elseif ($ftype == 'F') {
             $table_data->write('F');
             $table_data->write_table($v);
         } elseif ($ftype == 'A') {
             $table_data->write('A');
             $table_data->write_array($v);
         } elseif ($ftype == 't') {
             $table_data->write('t');
             $table_data->write_octet($v ? 1 : 0);
         } else {
             throw new AMQPInvalidArgumentException(sprintf("Invalid type '%s'", $ftype));
         }
     }
     $table_data = $table_data->getvalue();
     $this->write_long(strlen($table_data));
     $this->write($table_data);
     return $this;
 }
示例#16
0
 public function basicReject($delivery_tag, $requeue)
 {
     $args = new AMQPWriter();
     $args->write_longlong($delivery_tag)->write_bit($requeue);
     return $args;
 }