*/ private array $devices = []; /** @var array the descriptions asked (location => true) */ private array $asked = []; public string $error = ''; /** @var array{ssdp: string, mdns: string} where the questions go (tests: fakes on 127.0.0.1) */ public array $targets = ['ssdp' => Ssdp::ADDRESS . ':' . Ssdp::PORT, 'mdns' => Mdns::ADDRESS . ':' . Mdns::PORT]; /** tests: the answers of any address */ public bool $anyAddress = false; /** @var \Closure(): float */ private \Closure $clock; public function __construct(private LanHttp $http, ?\Closure $clock = null) { $this->clock = $clock ?? static fn (): float => microtime(true); } /** Starts looking for $seconds (what was found before stays until the end of this search). */ public function search(float $seconds = 6.0): bool { $this->error = ''; $this->close(); $socket = @stream_socket_server('udp://0.0.0.0:0', $errno, $errstr, STREAM_SERVER_BIND); if ($socket === false) { $this->error = 'Recherche impossible : pas de réseau (' . ($errstr ?: 'UDP') . ')'; return false; } stream_set_blocking($socket, false); $this->socket = $socket; $this->started = ($this->clock)(); $this->until = $this->started + max(3.0, $seconds); $this->sent = 0; $this->asked = []; foreach ($this->devices as $device) { $device->seen = min($device->seen, $this->started - 0.001); // seen again during this search, or gone at its end } $this->poll(); return true; } public function searching(): bool { return $this->socket !== null || $this->http->busy(); } /** @return list by name */ public function devices(): array { $list = array_values($this->devices); usort($list, fn (CastDevice $a, CastDevice $b) => strcasecmp($a->name, $b->name)); return $list; } public function device(string $id): ?CastDevice { return $this->devices[$id] ?? null; } /** Tests and the last device used: a device known without looking. */ public function remember(CastDevice $device): void { $this->devices[$device->id] = $device; } public function poll(): void { $this->http->poll(); if ($this->socket === null) { return; } $now = ($this->clock)(); while ($this->sent < count(self::RESEND) && $now - $this->started >= self::RESEND[$this->sent]) { $this->send(); $this->sent++; } for ($n = 0; $n < 64; $n++) { $data = @stream_socket_recvfrom($this->socket, 9000, 0, $peer); if (!is_string($data) || $data === '') { break; } $this->received($data, self::ip((string)$peer)); } if ($now >= $this->until) { $this->close(); // the devices that did not answer this time are gone (switched off, another network) $this->devices = array_filter($this->devices, fn (CastDevice $d) => $d->seen >= $this->started); } } private function send(): void { $ssdp = Ssdp::search(); $mdns = Mdns::query(); $okS = @stream_socket_sendto($this->socket, $ssdp, 0, $this->targets['ssdp']); $okM = @stream_socket_sendto($this->socket, $mdns, 0, $this->targets['mdns']); if ($okS === -1 && $okM === -1 && $this->sent === 0) { $this->error = 'Recherche impossible : le réseau refuse l\'envoi (Wi-Fi coupé ?)'; } } /** One datagram: an SSDP answer or an mDNS answer. */ public function received(string $data, string $from): void { if ($from !== '' && !$this->anyAddress && !HttpServer::isPrivateAddress($from)) { return; } $now = ($this->clock)(); if (str_starts_with($data, 'HTTP/') || str_starts_with($data, 'NOTIFY')) { $headers = Ssdp::parse($data); if ($headers === null) { return; } $location = $headers['location']; $host = (string)parse_url($location, PHP_URL_HOST); if (!$this->anyAddress && (!filter_var($host, FILTER_VALIDATE_IP) || !HttpServer::isPrivateAddress($host))) { return; } if (isset($this->asked[$location])) { // its description is read once a search (that marks it seen) return; } $this->asked[$location] = true; $this->http->request('GET', $location, '', ['User-Agent' => 'WinZik/1.0 UPnP/1.1'], 4.0, function (?array $r) use ($location, $host): void { $info = $r !== null && $r['status'] === 200 ? Ssdp::description($r['body'], $location) : null; if ($info === null) { return; } $id = 'dlna:' . $info['udn']; $name = $info['name']; $model = trim($info['manufacturer'] . ' ' . $info['model']); $this->devices[$id] = new CastDevice(CastDevice::DLNA, $id, $name, $model, $host, (int)(parse_url($location, PHP_URL_PORT) ?: 80), $info['avTransport'], $info['rendering'], ($this->clock)()); }); return; } foreach (Mdns::castDevices($data, $from) as $c) { if (!$this->anyAddress && !HttpServer::isPrivateAddress($c['host'])) { continue; } $id = 'cast:' . $c['id']; $this->devices[$id] = new CastDevice(CastDevice::CAST, $id, $c['name'], $c['model'], $c['host'], $c['port'], '', '', $now); } } public function close(): void { if ($this->socket !== null) { @fclose($this->socket); $this->socket = null; } } private static function ip(string $peer): string { if (preg_match('/^\[(.*)\]:\d+$/', $peer, $m)) { return $m[1]; } return (string)preg_replace('/:\d+$/', '', $peer); } }