예제 #1
0
파일: Executor.php 프로젝트: ThrusterIO/dns
 private function doQuery($nameServer, $transport, $queryData, $name) : PromiseInterface
 {
     $response = new Message();
     $deferred = new Deferred();
     $retryWithTcp = function () use($nameServer, $queryData, $name) {
         return $this->doQuery($nameServer, 'tcp', $queryData, $name);
     };
     $connection = $this->createConnection($nameServer, $transport);
     $timer = $this->loop->addTimer($this->timeout, function () use($connection, $name, $deferred) {
         $connection->close();
         $deferred->reject(new TimeoutException($name));
     });
     $connection->on('data', function ($data) use($retryWithTcp, $connection, &$response, $transport, $deferred, $timer) {
         $responseReady = $this->parser->parseChunk($data, $response);
         if (!$responseReady) {
             return;
         }
         $timer->cancel();
         if ($response->header->isTruncated()) {
             if ('tcp' === $transport) {
                 $deferred->reject(new BadServerException('The server set the truncated bit although we issued a TCP request'));
             } else {
                 $connection->end();
                 $deferred->resolve($retryWithTcp());
             }
             return;
         }
         $connection->end();
         $deferred->resolve($response);
     });
     $connection->write($queryData);
     return $deferred->promise();
 }
예제 #2
0
 private function waitForStreamOnce($stream) : PromiseInterface
 {
     $deferred = new Deferred();
     $this->loop->addWriteStream($stream, function ($stream) use($deferred) {
         $this->loop->removeWriteStream($stream);
         $deferred->resolve($stream);
     });
     return $deferred->promise();
 }
 /**
  * @inheritDoc
  */
 public function processRequest(ServerRequestInterface $request) : ExtendedPromiseInterface
 {
     $response = new Response();
     $deferred = new Deferred();
     $response = $this->getRequestMiddlewares()->getPreMiddlewares()->__invoke($request, $response, function (ServerRequestInterface $request, ResponseInterface $response) use($deferred) {
         $promise = $this->getRouter()->__invoke($request, $response, $this->getRequestMiddlewares()->getPostMiddlewares());
         $promise->then([$deferred, 'resolve'], [$deferred, 'reject'], [$deferred, 'notify']);
         return $response;
     });
     if (null !== $response) {
         $deferred->resolve($response);
     }
     return $deferred->promise();
 }
예제 #4
0
 public function tryQuery(string $nameServer, Query $query, int $retries, Deferred $deferred)
 {
     $errorCallback = function ($error) use($nameServer, $query, $retries, $deferred) {
         if (false === $error instanceof TimeoutException) {
             $deferred->reject($error);
             return;
         }
         if (0 >= $retries) {
             $error = new \RuntimeException(sprintf("DNS query for %s failed: too many retries", $query->getName()), 0, $error);
             $deferred->reject($error);
             return;
         }
         $this->tryQuery($nameServer, $query, $retries - 1, $deferred);
     };
     $this->executor->query($nameServer, $query)->then([$deferred, 'resolve'], $errorCallback);
 }
예제 #5
0
 public function toggleCrypto($socket, Deferred $deferred, $toggle)
 {
     //set_error_handler([$this, 'handleError']);
     $result = stream_socket_enable_crypto($socket, $toggle, $this->method);
     //restore_error_handler();
     if (true === $result) {
         $this->loop->removeReadStream($socket);
         $deferred->resolve();
     } elseif (false === $result) {
         $this->loop->removeReadStream($socket);
         $deferred->reject(new UnexpectedValueException(sprintf('Unable to complete SSL/TLS handshake: %s', $this->errStr), $this->errno));
     } else {
         // need more data, will retry
     }
 }