@@ -48,14 +48,13 @@ class MDnsClient {
4848
4949 bool _starting = false ;
5050 bool _started = false ;
51- RawDatagramSocket _incoming;
5251 final List <RawDatagramSocket > _sockets = < RawDatagramSocket > [];
5352 final LookupResolver _resolver = LookupResolver ();
5453 final ResourceRecordCache _cache = ResourceRecordCache ();
5554 final RawDatagramSocketFactory _rawDatagramSocketFactory;
5655
57- InternetAddress _mDnsAddress;
58- int _mDnsPort;
56+ InternetAddress ? _mDnsAddress;
57+ int ? _mDnsPort;
5958
6059 /// Find all network interfaces with an the [InternetAddressType] specified.
6160 static NetworkInterfacesFactory allInterfacesFactory =
@@ -82,14 +81,14 @@ class MDnsClient {
8281 /// for the mDNS query. If not provided, defaults to either `224.0.0.251` or
8382 /// or `FF02::FB` .
8483 Future <void > start ({
85- InternetAddress listenAddress,
86- NetworkInterfacesFactory interfacesFactory,
84+ InternetAddress ? listenAddress,
85+ NetworkInterfacesFactory ? interfacesFactory,
8786 int mDnsPort = mDnsPort,
88- InternetAddress mDnsAddress,
87+ InternetAddress ? mDnsAddress,
8988 }) async {
9089 listenAddress ?? = InternetAddress .anyIPv4;
9190 interfacesFactory ?? = allInterfacesFactory;
92- _mDnsPort = mDnsPort;
91+ final int selectedMDnsPort = _mDnsPort = mDnsPort;
9392 _mDnsAddress = mDnsAddress;
9493
9594 assert (listenAddress.address == InternetAddress .anyIPv4.address ||
@@ -101,32 +100,32 @@ class MDnsClient {
101100 _starting = true ;
102101
103102 // Listen on all addresses.
104- _incoming = await _rawDatagramSocketFactory (
103+ final RawDatagramSocket incoming = await _rawDatagramSocketFactory (
105104 listenAddress.address,
106- _mDnsPort ,
105+ selectedMDnsPort ,
107106 reuseAddress: true ,
108107 reusePort: true ,
109108 ttl: 255 ,
110109 );
111110
112111 // Can't send to IPv6 any address.
113- if (_incoming .address != InternetAddress .anyIPv6) {
114- _sockets.add (_incoming );
112+ if (incoming .address != InternetAddress .anyIPv6) {
113+ _sockets.add (incoming );
115114 }
116115
117- _mDnsAddress ?? = _incoming .address.type == InternetAddressType .IPv4
116+ _mDnsAddress ?? = incoming .address.type == InternetAddressType .IPv4
118117 ? mDnsAddressIPv4
119118 : mDnsAddressIPv6;
120119
121120 final List <NetworkInterface > interfaces =
122- await interfacesFactory (listenAddress.type);
121+ ( await interfacesFactory (listenAddress.type)). toList ( );
123122
124123 for (NetworkInterface interface in interfaces) {
125124 // Create a socket for sending on each adapter.
126125 final InternetAddress targetAddress = interface .addresses[0 ];
127126 final RawDatagramSocket socket = await _rawDatagramSocketFactory (
128127 targetAddress,
129- _mDnsPort ,
128+ selectedMDnsPort ,
130129 reuseAddress: true ,
131130 reusePort: true ,
132131 ttl: 255 ,
@@ -147,9 +146,9 @@ class MDnsClient {
147146 ));
148147 }
149148 // Join multicast on this interface.
150- _incoming .joinMulticast (_mDnsAddress, interface );
149+ incoming .joinMulticast (_mDnsAddress! , interface );
151150 }
152- _incoming .listen (_handleIncoming);
151+ incoming .listen (( RawSocketEvent event) => _handleIncoming (event, incoming) );
153152 _started = true ;
154153 _starting = false ;
155154 }
@@ -186,7 +185,8 @@ class MDnsClient {
186185 ResourceRecordQuery query, {
187186 Duration timeout = const Duration (seconds: 5 ),
188187 }) {
189- if (! _started) {
188+ final int ? selectedMDnsPort = _mDnsPort;
189+ if (! _started || selectedMDnsPort == null ) {
190190 throw StateError ('mDNS client must be started before calling lookup.' );
191191 }
192192 // Look for entries in the cache.
@@ -207,18 +207,21 @@ class MDnsClient {
207207 // Send the request on all interfaces.
208208 final List <int > packet = query.encode ();
209209 for (RawDatagramSocket socket in _sockets) {
210- socket.send (packet, _mDnsAddress, _mDnsPort );
210+ socket.send (packet, _mDnsAddress! , selectedMDnsPort );
211211 }
212212 return results;
213213 }
214214
215215 // Process incoming datagrams.
216- void _handleIncoming (RawSocketEvent event) {
216+ void _handleIncoming (RawSocketEvent event, RawDatagramSocket incoming ) {
217217 if (event == RawSocketEvent .read) {
218- final Datagram datagram = _incoming.receive ();
218+ final Datagram ? datagram = incoming.receive ();
219+ if (datagram == null ) {
220+ return ;
221+ }
219222
220223 // Check for published responses.
221- final List <ResourceRecord > response = decodeMDnsResponse (datagram.data);
224+ final List <ResourceRecord >? response = decodeMDnsResponse (datagram.data);
222225 if (response != null ) {
223226 _cache.updateRecords (response);
224227 _resolver.handleResponse (response);
0 commit comments