|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:flutter/foundation.dart'; |
| 5 | + |
| 6 | +const int nipaplayLanDiscoveryPort = 32123; |
| 7 | + |
| 8 | +class NipaPlayLanDiscoveredServer { |
| 9 | + const NipaPlayLanDiscoveredServer({ |
| 10 | + required this.ip, |
| 11 | + required this.port, |
| 12 | + this.hostname, |
| 13 | + }); |
| 14 | + |
| 15 | + final String ip; |
| 16 | + final int port; |
| 17 | + final String? hostname; |
| 18 | + |
| 19 | + String get baseUrl => 'http://$ip:$port'; |
| 20 | +} |
| 21 | + |
| 22 | +class NipaPlayLanDiscoveryProtocol { |
| 23 | + static const int protocolVersion = 1; |
| 24 | + |
| 25 | + static const String requestType = 'nipaplay_discover'; |
| 26 | + static const String responseType = 'nipaplay_discover_response'; |
| 27 | + |
| 28 | + static Uint8List buildRequestBytes() { |
| 29 | + return Uint8List.fromList(utf8.encode(json.encode(<String, dynamic>{ |
| 30 | + 'type': requestType, |
| 31 | + 'v': protocolVersion, |
| 32 | + }))); |
| 33 | + } |
| 34 | + |
| 35 | + static Uint8List buildResponseBytes({required int webPort}) { |
| 36 | + return Uint8List.fromList(utf8.encode(json.encode(<String, dynamic>{ |
| 37 | + 'type': responseType, |
| 38 | + 'v': protocolVersion, |
| 39 | + 'app': 'NipaPlay', |
| 40 | + 'hostname': Platform.localHostname, |
| 41 | + 'os': Platform.operatingSystem, |
| 42 | + 'port': webPort, |
| 43 | + }))); |
| 44 | + } |
| 45 | + |
| 46 | + static bool isValidRequestPayload(Map<String, dynamic> payload) { |
| 47 | + return payload['type'] == requestType && payload['v'] == protocolVersion; |
| 48 | + } |
| 49 | + |
| 50 | + static NipaPlayLanDiscoveredServer? tryParseResponse(Datagram datagram) { |
| 51 | + final payload = _tryDecodeJson(datagram.data); |
| 52 | + if (payload == null) return null; |
| 53 | + if (payload['type'] != responseType) return null; |
| 54 | + if (payload['v'] != protocolVersion) return null; |
| 55 | + if (payload['app'] != 'NipaPlay') return null; |
| 56 | + |
| 57 | + final rawPort = payload['port']; |
| 58 | + final port = rawPort is int ? rawPort : int.tryParse(rawPort?.toString() ?? ''); |
| 59 | + if (port == null || port <= 0 || port > 65535) return null; |
| 60 | + |
| 61 | + final hostname = payload['hostname'] is String ? payload['hostname'] as String : null; |
| 62 | + return NipaPlayLanDiscoveredServer( |
| 63 | + ip: datagram.address.address, |
| 64 | + port: port, |
| 65 | + hostname: hostname, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + static Map<String, dynamic>? _tryDecodeJson(Uint8List data) { |
| 70 | + try { |
| 71 | + final decoded = json.decode(utf8.decode(data)); |
| 72 | + return decoded is Map<String, dynamic> ? decoded : null; |
| 73 | + } catch (_) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class NipaPlayLanDiscoveryResponder { |
| 80 | + RawDatagramSocket? _socket; |
| 81 | + int _webPort = 1180; |
| 82 | + |
| 83 | + bool get isRunning => _socket != null; |
| 84 | + |
| 85 | + Future<void> start({required int webPort}) async { |
| 86 | + _webPort = webPort; |
| 87 | + if (_socket != null) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + final socket = await RawDatagramSocket.bind( |
| 93 | + InternetAddress.anyIPv4, |
| 94 | + nipaplayLanDiscoveryPort, |
| 95 | + reuseAddress: true, |
| 96 | + ); |
| 97 | + |
| 98 | + socket.listen((event) { |
| 99 | + if (event != RawSocketEvent.read) return; |
| 100 | + Datagram? datagram; |
| 101 | + while ((datagram = socket.receive()) != null) { |
| 102 | + final payload = _tryDecodeJson(datagram!.data); |
| 103 | + if (payload == null) continue; |
| 104 | + if (!NipaPlayLanDiscoveryProtocol.isValidRequestPayload(payload)) continue; |
| 105 | + |
| 106 | + final response = NipaPlayLanDiscoveryProtocol.buildResponseBytes(webPort: _webPort); |
| 107 | + try { |
| 108 | + socket.send(response, datagram.address, datagram.port); |
| 109 | + } catch (_) { |
| 110 | + // ignore send failures |
| 111 | + } |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + _socket = socket; |
| 116 | + debugPrint('NipaPlayLanDiscoveryResponder: started on UDP $nipaplayLanDiscoveryPort'); |
| 117 | + } catch (e) { |
| 118 | + debugPrint('NipaPlayLanDiscoveryResponder: start failed: $e'); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + Future<void> stop() async { |
| 123 | + try { |
| 124 | + _socket?.close(); |
| 125 | + } catch (_) { |
| 126 | + // ignore |
| 127 | + } finally { |
| 128 | + _socket = null; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + static Map<String, dynamic>? _tryDecodeJson(Uint8List data) { |
| 133 | + try { |
| 134 | + final decoded = json.decode(utf8.decode(data)); |
| 135 | + return decoded is Map<String, dynamic> ? decoded : null; |
| 136 | + } catch (_) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments