public function __construct(HttpEndpoint $endpoint, SocketServer $server, Awaitable $runner) { $this->endpoint = $endpoint; $this->server = $server; $this->runner = $runner; $this->socketFactory = $server->createSocketFactory(); }
/** * {@inheritdoc} */ public function listen(callable $action) : Awaitable { return new Coroutine(function () use($action) { $factory = clone $this->factory; $factory->setTcpNoDelay(true); $this->server = (yield $factory->createSocketServer()); $port = $this->server->getPort(); $peer = $this->server->getAddress() . ($port ? ':' . $port : ''); $context = new HttpDriverContext($peer, $factory->getPeerName(), $factory->isEncrypted(), $this->middlewares, $this->responders); $pending = new \SplObjectStorage(); try { (yield $this->server->listen(function (SocketStream $socket) use($pending, $context, $action) { $conn = new Connection($socket, $context); if ($this->logger) { $conn->setLogger($this->logger); } $pending->attach($conn); while (null !== ($next = (yield $conn->nextRequest()))) { new Coroutine($this->processRequest($conn, $context, $action, ...$next)); } })); } finally { $this->server = null; foreach ($pending as $task) { $task->cancel(new \RuntimeException('FCGI server stopped')); } } }); }
protected function clientTask(SocketServer $server, callable $callback) : \Generator { $factory = $server->createSocketFactory(); if ($this->encrypted) { $factory->setCertificateAuthority(AsyncTestCase::getKoolKodeCertAuthorityFile()); } $socket = (yield $factory->createSocketStream(2, $this->encrypted)); try { $result = $callback($this->raw ? $socket->getSocket() : $socket); if ($result instanceof \Generator) { yield from $result; } } finally { $socket->close(); } }