2017-06-23 07:34:11 +02:00
|
|
|
<?php
|
|
|
|
|
2017-06-27 18:30:28 +02:00
|
|
|
namespace Amp\Dns\Internal;
|
2017-06-23 07:34:11 +02:00
|
|
|
|
2017-06-23 08:39:49 +02:00
|
|
|
use Amp;
|
2017-06-23 07:34:11 +02:00
|
|
|
use Amp\Deferred;
|
2019-01-04 18:20:52 +01:00
|
|
|
use Amp\Dns\DnsException;
|
2017-06-27 18:30:28 +02:00
|
|
|
use Amp\Dns\TimeoutException;
|
2017-06-23 07:34:11 +02:00
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Parser\Parser;
|
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
|
|
|
use LibDNS\Decoder\DecoderFactory;
|
|
|
|
use LibDNS\Encoder\EncoderFactory;
|
|
|
|
use LibDNS\Messages\Message;
|
|
|
|
use function Amp\call;
|
|
|
|
|
2017-06-23 21:07:59 +02:00
|
|
|
/** @internal */
|
2019-01-25 02:27:47 +01:00
|
|
|
class TcpSocket extends Socket
|
|
|
|
{
|
2017-06-23 08:39:49 +02:00
|
|
|
/** @var \LibDNS\Encoder\Encoder */
|
2017-06-23 07:34:11 +02:00
|
|
|
private $encoder;
|
|
|
|
|
2017-06-23 08:39:49 +02:00
|
|
|
/** @var \SplQueue */
|
2017-06-23 07:34:11 +02:00
|
|
|
private $queue;
|
|
|
|
|
2017-06-23 08:39:49 +02:00
|
|
|
/** @var Parser */
|
2017-06-23 07:34:11 +02:00
|
|
|
private $parser;
|
|
|
|
|
2017-06-23 12:49:16 +02:00
|
|
|
/** @var bool */
|
|
|
|
private $isAlive = true;
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
public static function connect(string $uri, int $timeout = 5000): Promise
|
|
|
|
{
|
2017-06-23 07:34:11 +02:00
|
|
|
if (!$socket = @\stream_socket_client($uri, $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT)) {
|
2019-01-04 18:20:52 +01:00
|
|
|
throw new DnsException(\sprintf(
|
2017-06-23 07:34:11 +02:00
|
|
|
"Connection to %s failed: [Error #%d] %s",
|
|
|
|
$uri,
|
|
|
|
$errno,
|
|
|
|
$errstr
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
\stream_set_blocking($socket, false);
|
|
|
|
|
2017-06-23 08:39:49 +02:00
|
|
|
return call(function () use ($uri, $socket, $timeout) {
|
|
|
|
$deferred = new Deferred;
|
2017-06-23 07:34:11 +02:00
|
|
|
|
2017-06-23 08:39:49 +02:00
|
|
|
$watcher = Loop::onWritable($socket, static function () use ($socket, $deferred) {
|
|
|
|
$deferred->resolve(new self($socket));
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
return yield Promise\timeout($deferred->promise(), $timeout);
|
|
|
|
} catch (Amp\TimeoutException $e) {
|
|
|
|
throw new TimeoutException("Name resolution timed out, could not connect to server at $uri");
|
|
|
|
} finally {
|
|
|
|
Loop::cancel($watcher);
|
|
|
|
}
|
|
|
|
});
|
2017-06-23 07:34:11 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
public static function parser(callable $callback): \Generator
|
|
|
|
{
|
2017-06-23 07:34:11 +02:00
|
|
|
$decoder = (new DecoderFactory)->create();
|
2017-06-23 08:39:49 +02:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
$length = yield 2;
|
|
|
|
$length = \unpack("n", $length)[1];
|
|
|
|
|
|
|
|
$rawData = yield $length;
|
|
|
|
$callback($decoder->decode($rawData));
|
|
|
|
}
|
2017-06-23 07:34:11 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
protected function __construct($socket)
|
|
|
|
{
|
2017-06-23 07:34:11 +02:00
|
|
|
parent::__construct($socket);
|
2017-06-23 08:39:49 +02:00
|
|
|
|
2017-06-23 07:34:11 +02:00
|
|
|
$this->encoder = (new EncoderFactory)->create();
|
|
|
|
$this->queue = new \SplQueue;
|
|
|
|
$this->parser = new Parser(self::parser([$this->queue, 'push']));
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
protected function send(Message $message): Promise
|
|
|
|
{
|
2017-06-23 07:34:11 +02:00
|
|
|
$data = $this->encoder->encode($message);
|
2017-06-23 12:49:16 +02:00
|
|
|
$promise = $this->write(\pack("n", \strlen($data)) . $data);
|
|
|
|
$promise->onResolve(function ($error) {
|
|
|
|
if ($error) {
|
|
|
|
$this->isAlive = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $promise;
|
2017-06-23 07:34:11 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
protected function receive(): Promise
|
|
|
|
{
|
2017-06-23 07:34:11 +02:00
|
|
|
if ($this->queue->isEmpty()) {
|
|
|
|
return call(function () {
|
|
|
|
do {
|
2017-06-29 12:36:09 +02:00
|
|
|
$chunk = yield $this->read();
|
2017-06-23 07:34:11 +02:00
|
|
|
|
|
|
|
if ($chunk === null) {
|
2017-06-23 12:49:16 +02:00
|
|
|
$this->isAlive = false;
|
2019-01-04 18:20:52 +01:00
|
|
|
throw new DnsException("Reading from the server failed");
|
2017-06-23 07:34:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->parser->push($chunk);
|
|
|
|
} while ($this->queue->isEmpty());
|
|
|
|
|
|
|
|
return $this->queue->shift();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Success($this->queue->shift());
|
|
|
|
}
|
2017-06-23 12:49:16 +02:00
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
public function isAlive(): bool
|
|
|
|
{
|
2017-06-23 12:49:16 +02:00
|
|
|
return $this->isAlive;
|
|
|
|
}
|
2017-06-23 08:39:49 +02:00
|
|
|
}
|