/**
  * Wait for some expected AMQP methods and dispatch to them.
  * Unexpected methods are queued up for later calls to this PHP
  * method.
  */
 public function wait($allowed_methods = null, $non_blocking = false, $timeout = 0)
 {
     $PROTOCOL_CONSTANTS_CLASS = self::$PROTOCOL_CONSTANTS_CLASS;
     if ($allowed_methods && $this->debug) {
         PhpAmqpLib_Helper_MiscHelper::debug_msg("waiting for " . implode(", ", $allowed_methods));
     } elseif ($this->debug) {
         PhpAmqpLib_Helper_MiscHelper::debug_msg("waiting for any method");
     }
     //Process deferred methods
     foreach ($this->method_queue as $qk => $queued_method) {
         if ($this->debug) {
             PhpAmqpLib_Helper_MiscHelper::debug_msg("checking queue method " . $qk);
         }
         $method_sig = $queued_method[0];
         if ($allowed_methods == null || in_array($method_sig, $allowed_methods)) {
             unset($this->method_queue[$qk]);
             if ($this->debug) {
                 PhpAmqpLib_Helper_MiscHelper::debug_msg("Executing queued method: {$method_sig}: " . $PROTOCOL_CONSTANTS_CLASS::$GLOBAL_METHOD_NAMES[PhpAmqpLib_Helper_MiscHelper::methodSig($method_sig)]);
             }
             return $this->dispatch($queued_method[0], $queued_method[1], $queued_method[2]);
         }
     }
     // No deferred methods?  wait for new ones
     while (true) {
         $frm = $this->next_frame($timeout);
         $frame_type = $frm[0];
         $payload = $frm[1];
         if ($frame_type != 1) {
             throw new PhpAmqpLib_Exception_AMQPRuntimeException("Expecting AMQP method, received frame type: {$frame_type} (" . $PROTOCOL_CONSTANTS_CLASS::$FRAME_TYPES[$frame_type] . ")");
         }
         if (strlen($payload) < 4) {
             throw new PhpAmqpLib_Exception_AMQPOutOfBoundsException("Method frame too short");
         }
         $method_sig_array = unpack("n2", substr($payload, 0, 4));
         $method_sig = "" . $method_sig_array[1] . "," . $method_sig_array[2];
         $args = new PhpAmqpLib_Wire_AMQPReader(substr($payload, 4));
         if ($this->debug) {
             PhpAmqpLib_Helper_MiscHelper::debug_msg("> {$method_sig}: " . $PROTOCOL_CONSTANTS_CLASS::$GLOBAL_METHOD_NAMES[PhpAmqpLib_Helper_MiscHelper::methodSig($method_sig)]);
         }
         if (in_array($method_sig, $PROTOCOL_CONSTANTS_CLASS::$CONTENT_METHODS)) {
             $content = $this->wait_content();
         } else {
             $content = null;
         }
         if ($allowed_methods == null || in_array($method_sig, $allowed_methods) || in_array($method_sig, $PROTOCOL_CONSTANTS_CLASS::$CLOSE_METHODS)) {
             return $this->dispatch($method_sig, $args, $content);
         }
         // Wasn't what we were looking for? save it for later
         if ($this->debug) {
             PhpAmqpLib_Helper_MiscHelper::debug_msg("Queueing for later: {$method_sig}: " . $PROTOCOL_CONSTANTS_CLASS::$GLOBAL_METHOD_NAMES[PhpAmqpLib_Helper_MiscHelper::methodSig($method_sig)]);
         }
         $this->method_queue[] = array($method_sig, $args, $content);
         if ($non_blocking) {
             break;
         }
     }
 }
 /**
  * Starts connection negotiation
  *
  * @param PhpAmqpLib_Wire_AMQPReader $args
  */
 protected function connection_start($args)
 {
     $this->version_major = $args->read_octet();
     $this->version_minor = $args->read_octet();
     $this->server_properties = $args->read_table();
     $this->mechanisms = explode(' ', $args->read_longstr());
     $this->locales = explode(' ', $args->read_longstr());
     if ($this->debug) {
         PhpAmqpLib_Helper_MiscHelper::debug_msg(sprintf('Start from server, version: %d.%d, properties: %s, mechanisms: %s, locales: %s', $this->version_major, $this->version_minor, self::dump_table($this->server_properties), implode(', ', $this->mechanisms), implode(', ', $this->locales)));
     }
 }
 /**
  * return a failed message
  */
 protected function basic_return($args, $msg)
 {
     $reply_code = $args->read_short();
     $reply_text = $args->read_shortstr();
     $exchange = $args->read_shortstr();
     $routing_key = $args->read_shortstr();
     if (!is_null($this->basic_return_callback)) {
         call_user_func_array($this->basic_return_callback, array($reply_code, $reply_text, $exchange, $routing_key, $msg));
     } elseif ($this->debug) {
         PhpAmqpLib_Helper_MiscHelper::debug_msg("Skipping unhandled basic_return message");
     }
 }
 /**
  * Wait for some expected AMQP methods and dispatch to them.
  * Unexpected methods are queued up for later calls to this PHP
  * method.
  *
  * @param array $allowed_methods
  * @param bool $non_blocking
  * @param int $timeout
  * @throws _PhpAmqpLib_Exception_AMQPOutOfBoundsException
  * @throws _PhpAmqpLib_Exception_AMQPRuntimeException
  * @return mixed
  */
 public function wait($allowed_methods = null, $non_blocking = false, $timeout = 0)
 {
     $PROTOCOL_CONSTANTS_CLASS = self::$PROTOCOL_CONSTANTS_CLASS;
     if ($allowed_methods && $this->debug) {
         PhpAmqpLib_Helper_MiscHelper::debug_msg('waiting for ' . implode(', ', $allowed_methods));
     } elseif ($this->debug) {
         PhpAmqpLib_Helper_MiscHelper::debug_msg('waiting for any method');
     }
     //Process deferred methods
     foreach ($this->method_queue as $qk => $queued_method) {
         if ($this->debug) {
             PhpAmqpLib_Helper_MiscHelper::debug_msg('checking queue method ' . $qk);
         }
         $method_sig = $queued_method[0];
         if ($allowed_methods == null || in_array($method_sig, $allowed_methods)) {
             unset($this->method_queue[$qk]);
             $GLOBAL_METHOD_NAMES = self::getStaticProperty($PROTOCOL_CONSTANTS_CLASS, 'GLOBAL_METHOD_NAMES');
             if ($this->debug) {
                 PhpAmqpLib_Helper_MiscHelper::debug_msg(sprintf('Executing queued method: %s: %s', $method_sig, $GLOBAL_METHOD_NAMES[PhpAmqpLib_Helper_MiscHelper::methodSig($method_sig)]));
             }
             return $this->dispatch($queued_method[0], $queued_method[1], $queued_method[2]);
         }
     }
     // No deferred methods?  wait for new ones
     while (true) {
         $frm = $this->next_frame($timeout);
         $frame_type = $frm[0];
         $payload = $frm[1];
         $FRAME_TYPES = self::getStaticProperty($PROTOCOL_CONSTANTS_CLASS, 'FRAME_TYPES');
         if ($frame_type != 1) {
             throw new PhpAmqpLib_Exception_AMQPRuntimeException(sprintf('Expecting AMQP method, received frame type: %s (%s)', $frame_type, $FRAME_TYPES[$frame_type]));
         }
         if (mb_strlen($payload, 'ASCII') < 4) {
             throw new PhpAmqpLib_Exception_AMQPOutOfBoundsException('Method frame too short');
         }
         $method_sig_array = unpack('n2', mb_substr($payload, 0, 4, 'ASCII'));
         $method_sig = '' . $method_sig_array[1] . ',' . $method_sig_array[2];
         $args = mb_substr($payload, 4, mb_strlen($payload, 'ASCII') - 4, 'ASCII');
         $GLOBAL_METHOD_NAMES = self::getStaticProperty($PROTOCOL_CONSTANTS_CLASS, 'GLOBAL_METHOD_NAMES');
         if ($this->debug) {
             PhpAmqpLib_Helper_MiscHelper::debug_msg(sprintf('> %s: %s', $method_sig, $GLOBAL_METHOD_NAMES[PhpAmqpLib_Helper_MiscHelper::methodSig($method_sig)]));
         }
         $CONTENT_METHODS = self::getStaticProperty($PROTOCOL_CONSTANTS_CLASS, 'CONTENT_METHODS');
         if (in_array($method_sig, $CONTENT_METHODS)) {
             $content = $this->wait_content();
         } else {
             $content = null;
         }
         $CLOSE_METHODS = self::getStaticProperty($PROTOCOL_CONSTANTS_CLASS, 'CLOSE_METHODS');
         if ($allowed_methods == null || in_array($method_sig, $allowed_methods) || in_array($method_sig, $CLOSE_METHODS)) {
             return $this->dispatch($method_sig, $args, $content);
         }
         // Wasn't what we were looking for? save it for later
         if ($this->debug) {
             PhpAmqpLib_Helper_MiscHelper::debug_msg('Queueing for later: $method_sig: ' . $GLOBAL_METHOD_NAMES[PhpAmqpLib_Helper_MiscHelper::methodSig($method_sig)]);
         }
         $this->method_queue[] = array($method_sig, $args, $content);
         if ($non_blocking) {
             break;
         }
     }
 }