public function dispatch() { do { if (!($recv = $this->zmq->recvMulti())) { throw new SocketException('Lost remote after ' . $this->timeout . 'ms'); } if (strlen($recv[count($recv) - 2]) !== 0) { throw new SocketException('Expected second to last argument to be an empty buffer, but it is not'); } $envelope = array_slice($recv, 0, count($recv) - 2); $event = Event::deserialize($envelope, $recv[count($recv) - 1]); $channel = Channel::get($event->header['response_to']); if ($channel) { $channel->invoke($event); } } while (Channel::count() > 0); }
public function async($name, array $args, &$response) { $event = new Request($name, $args); $this->context->hookBeforeSendRequest($event, $this); Channel::startRequest($this->socket, $event, $response); }
<?php require 'vendor/autoload.php'; use ZeroRPC\Client; use ZeroRPC\Channel; $client = new Client("tcp://127.0.0.1:1234"); try { $client->should_response_error(); } catch (ZeroRPC\RemoteException $e) { print $e; } try { $client->sleep(3); } catch (ZeroRPC\TimeoutException $e) { print $e; } try { $client->async("sleep", array(1), $null_); $client->async("sleep", array(2), $null_); Channel::dispatch(); } catch (ZeroRPC\TimeoutException $e) { print $e; }
<?php require 'vendor/autoload.php'; use ZeroRPC\Client; use ZeroRPC\Channel; $clientA = new Client("tcp://127.0.0.1:1234"); $clientA->setTimeout(3500); $clientB = new Client("tcp://127.0.0.1:2345"); $clientB->setTimeout(3500); // normal example $time = $clientA->strftime("%Y/%m/%d %H:%M:%S"); $clientA->async("strftime", array("%Y/%m/%d %H:%M:%S"), $async_time); Channel::dispatch(); assert($time == $async_time); print "Time is {$async_time}!" . PHP_EOL; // sync example print "Example 1: start sync call:" . PHP_EOL; $start = microtime(true); $clientA->sleep(3); $clientB->sleep(2); print 'cost ' . (microtime(true) - $start) . ' s' . PHP_EOL; // async example print "Example 2: start async call:" . PHP_EOL; $start = microtime(true); $clientA->async("sleep", array(3), $sleep1); $clientB->async("sleep", array(2), $sleep2); Channel::dispatch(3500); print 'cost ' . (microtime(true) - $start) . ' s' . PHP_EOL;