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(); }
public function handleWrite() { if (!is_resource($this->stream)) { $this->emit('error', [new \RuntimeException('Tried to write to invalid stream.'), $this]); return; } set_error_handler([$this, 'errorHandler']); $sent = fwrite($this->stream, $this->data); restore_error_handler(); if (false === $sent) { $this->emit('error', [new \ErrorException($this->lastError['message'], 0, $this->lastError['number'], $this->lastError['file'], $this->lastError['line']), $this]); return; } if (0 === $sent && feof($this->stream)) { $this->emit('error', [new \RuntimeException('Tried to write to closed stream.'), $this]); return; } $len = strlen($this->data); $this->data = (string) substr($this->data, $sent); if ($len >= $this->softLimit && $len - $sent < $this->getSoftLimit()) { $this->emit('drain', [$this]); } if (0 === strlen($this->data)) { $this->loop->removeWriteStream($this->stream); $this->setListening(false); $this->emit('full-drain', [$this]); } }
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 } }
/** * @inheritdoc */ public function close() : self { if (false === $this->isReadable() && false === $this->closing) { return $this; } $this->closing = false; $this->readable = false; $this->writable = false; $this->emit('end', [$this]); $this->emit('close', [$this]); $this->loop->removeStream($this->stream); $this->buffer->removeListeners(); $this->removeListeners(); $this->handleClose(); return $this; }
public function shutdown() { $this->loop->removeStream($this->socket); fclose($this->socket); $this->removeListeners(); }
public function __construct(EventLoopInterface $loop) { $this->loop = $loop; $this->loop->addChild([$this, 'handleExit']); }