/** * Create a new Call wrapper object. * @param Channel $channel The channel to communicate on * @param string $method The method to call on the remote server * @param callback $deserialize A callback function to deserialize * the response * @param (optional) long $timeout Timeout in microseconds */ public function __construct(Channel $channel, $method, $deserialize, $timeout = false) { if ($timeout) { $now = Timeval::now(); $delta = new Timeval($timeout); $deadline = $now->add($delta); } else { $deadline = Timeval::infFuture(); } $this->call = new Call($channel, $method, $deadline); $this->deserialize = $deserialize; $this->metadata = null; }
/** * Create a new Call wrapper object. * * @param Channel $channel The channel to communicate on * @param string $method The method to call on the * remote server * @param callback $deserialize A callback function to deserialize * the response * @param array $options Call options (optional) */ public function __construct(Channel $channel, $method, $deserialize, $options = []) { if (isset($options['timeout']) && is_numeric($timeout = $options['timeout'])) { $now = Timeval::now(); $delta = new Timeval($timeout); $deadline = $now->add($delta); } else { $deadline = Timeval::infFuture(); } $this->call = new Call($channel, $method, $deadline); $this->deserialize = $deserialize; $this->metadata = null; if (isset($options['call_credentials_callback']) && is_callable($call_credentials_callback = $options['call_credentials_callback'])) { $call_credentials = CallCredentials::createFromPlugin($call_credentials_callback); $this->call->setCredentials($call_credentials); } }
/** * @param $timeout in microseconds * * @return bool true if channel is ready * @throw Exception if channel is in FATAL_ERROR state */ public function waitForReady($timeout) { $new_state = $this->getConnectivityState(true); if ($this->_checkConnectivityState($new_state)) { return true; } $now = Timeval::now(); $delta = new Timeval($timeout); $deadline = $now->add($delta); while ($this->channel->watchConnectivityState($new_state, $deadline)) { // state has changed before deadline $new_state = $this->getConnectivityState(); if ($this->_checkConnectivityState($new_state)) { return true; } } // deadline has passed $new_state = $this->getConnectivityState(); return $this->_checkConnectivityState($new_state); }