/**
  * {@inheritDoc}
  */
 public function receiveRequest()
 {
     try {
         $message = $this->socket->recvMulti();
     } catch (\Exception $e) {
         throw new TransportException('Unable to receive request', null, $e);
     }
     $this->waitingForResponse = true;
     $requestType = $message[0];
     if (!MessageTypes::isValid($requestType)) {
         throw new FormatException('Invalid request type "' . $requestType . '"', $message);
     }
     $request = new Request($requestType);
     $request->setHeaders(new Headers(Parser::parseHeaders(@$message[1])));
     if ($requestType !== MessageTypes::PING) {
         if (empty($message[2])) {
             throw new FormatException('Method name is empty', $message);
         }
         $request->setMethodName($message[2]);
         $arguments = array_slice($message, 3);
         $request->setArgumentsBody($arguments);
     }
     return $request;
 }
Beispiel #2
0
 private function createRequest($type, $method = null, $arguments = null, Headers $headers = null)
 {
     $request = new Request();
     $request->setType($type);
     $requestHeaders = $this->defaultHeaders ? clone $this->defaultHeaders : new Headers();
     if ($headers) {
         $requestHeaders->merge($headers);
     }
     $request->setHeaders($requestHeaders);
     if ($method) {
         $contentType = $requestHeaders->get('content-type');
         $arguments = $this->serializer->serializeArguments($method, $arguments, $contentType);
         $request->setMethodName($method)->setArgumentsBody($arguments);
     }
     return $request;
 }
Beispiel #3
0
 public function handleCall()
 {
     try {
         $request = $this->transport->receiveRequest();
         if ($request->getType() === MessageTypes::PING) {
             $response = $this->createResponse(MessageTypes::PONG, null, new Response(null, null, clone $this->headers), $request);
             $this->transport->sendResponse($response);
             return;
         }
         $this->runMethodForRequest($request);
     } catch (\Exception $exception) {
         if ($this->transport->isWaitingForResponse()) {
             $request = new Request();
             $request->setMethodName('')->setType(MessageTypes::REQUEST);
             $response = new Response();
             $this->createResponse(MessageTypes::ERROR, $exception, $response, $request);
             $this->transport->sendResponse($response);
         } else {
             if ($this->onErrorCallback) {
                 call_user_func($this->onErrorCallback, $exception);
             }
         }
     }
 }