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(); }
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(); }
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 } }