|
7 | 7 | use React\Dns\Protocol\BinaryDumper; |
8 | 8 | use React\EventLoop\LoopInterface; |
9 | 9 | use React\Promise\Deferred; |
10 | | -use React\Socket\Connection; |
11 | 10 | use React\Promise; |
| 11 | +use React\Stream\DuplexResourceStream; |
| 12 | +use React\Stream\Stream; |
12 | 13 |
|
13 | 14 | class Executor implements ExecutorInterface |
14 | 15 | { |
@@ -71,7 +72,7 @@ public function doQuery($nameserver, $transport, $queryData, $name) |
71 | 72 | try { |
72 | 73 | $conn = $this->createConnection($nameserver, $transport); |
73 | 74 | } catch (\Exception $e) { |
74 | | - return Promise\reject(new \RuntimeException('DNS query for ' . $name . ' failed: Unable to connect to DNS server: ' . $e->getMessage(), 0, $e)); |
| 75 | + return Promise\reject(new \RuntimeException('DNS query for ' . $name . ' failed: ' . $e->getMessage(), 0, $e)); |
75 | 76 | } |
76 | 77 |
|
77 | 78 | $deferred = new Deferred(function ($resolve, $reject) use (&$timer, $loop, &$conn, $name) { |
@@ -124,11 +125,31 @@ protected function generateId() |
124 | 125 | return mt_rand(0, 0xffff); |
125 | 126 | } |
126 | 127 |
|
| 128 | + /** |
| 129 | + * @param string $nameserver |
| 130 | + * @param string $transport |
| 131 | + * @return \React\Stream\DuplexStreamInterface |
| 132 | + */ |
127 | 133 | protected function createConnection($nameserver, $transport) |
128 | 134 | { |
129 | 135 | $fd = @stream_socket_client("$transport://$nameserver", $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT); |
130 | | - $conn = new Connection($fd, $this->loop); |
131 | | - $conn->bufferSize = null; // Temporary fix for Windows 10 users |
| 136 | + if ($fd === false) { |
| 137 | + throw new \RuntimeException('Unable to connect to DNS server: ' . $errstr, $errno); |
| 138 | + } |
| 139 | + |
| 140 | + // Instantiate stream instance around this stream resource. |
| 141 | + // This ought to be replaced with a datagram socket in the future. |
| 142 | + // Temporary work around for Windows 10: buffer whole UDP response |
| 143 | + // @coverageIgnoreStart |
| 144 | + if (!class_exists('React\Stream\Stream')) { |
| 145 | + // prefer DuplexResourceStream as of react/stream v0.7.0 |
| 146 | + $conn = new DuplexResourceStream($fd, $this->loop, -1); |
| 147 | + } else { |
| 148 | + // use legacy Stream class for react/stream < v0.7.0 |
| 149 | + $conn = new Stream($fd, $this->loop); |
| 150 | + $conn->bufferSize = null; |
| 151 | + } |
| 152 | + // @coverageIgnoreEnd |
132 | 153 |
|
133 | 154 | return $conn; |
134 | 155 | } |
|
0 commit comments