Visitar URL original
migrate mDNS to null safety (#297) · creatorpiyush/packages@f1cbeca · GitHub
Skip to content

Commit f1cbeca

Browse files
author
Jonah Williams
authored
migrate mDNS to null safety (flutter#297)
1 parent 0559e4b commit f1cbeca

11 files changed

Lines changed: 110 additions & 102 deletions

‎packages/multicast_dns/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.0
2+
3+
* Migrate package to null safety.
4+
15
## 0.2.2
26
* Fixes parsing of TXT records. Continues parsing on non-utf8 strings.
37

‎packages/multicast_dns/lib/multicast_dns.dart‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

‎packages/multicast_dns/lib/src/constants.dart‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ final InternetAddress mDnsAddressIPv6 = InternetAddress('FF02::FB');
1414
const int mDnsPort = 5353;
1515

1616
/// Enumeration of supported resource record class types.
17-
class ResourceRecordClass {
17+
abstract class ResourceRecordClass {
1818
// This class is intended to be used as a namespace, and should not be
1919
// extended directly.
20-
factory ResourceRecordClass._() => null;
20+
ResourceRecordClass._();
2121

2222
/// Internet address class ("IN").
2323
static const int internet = 1;
2424
}
2525

2626
/// Enumeration of DNS question types.
27-
class QuestionType {
27+
abstract class QuestionType {
2828
// This class is intended to be used as a namespace, and should not be
2929
// extended directly.
30-
factory QuestionType._() => null;
30+
QuestionType._();
3131

3232
/// "QU" Question.
3333
static const int unicast = 0x8000;

‎packages/multicast_dns/lib/src/lookup_resolver.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PendingRequest extends LinkedListEntry<PendingRequest> {
2525
final StreamController<ResourceRecord> controller;
2626

2727
/// The timer for the request.
28-
Timer timer;
28+
Timer? timer;
2929
}
3030

3131
/// Class for keeping track of pending lookups and processing incoming
@@ -89,7 +89,7 @@ class LookupResolver {
8989
while (_pendingRequests.isNotEmpty) {
9090
final PendingRequest request = _pendingRequests.first;
9191
request.unlink();
92-
request.timer.cancel();
92+
request.timer?.cancel();
9393
request.controller.close();
9494
}
9595
}

‎packages/multicast_dns/lib/src/native_protocol_client.dart‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ResourceRecordCache {
2929
for (final SplayTreeMap<String, List<ResourceRecord>> map
3030
in _cache.values) {
3131
for (final List<ResourceRecord> records in map.values) {
32-
count += records?.length;
32+
count += records.length;
3333
}
3434
}
3535
return count;
@@ -45,14 +45,15 @@ class ResourceRecordCache {
4545
// TODO(dnfield): Update this to use set literal syntax when we're able to bump the SDK constraint.
4646
seenRecordTypes[record.resourceRecordType] ??=
4747
Set<String>(); // ignore: prefer_collection_literals
48-
if (seenRecordTypes[record.resourceRecordType].add(record.name)) {
48+
if (seenRecordTypes[record.resourceRecordType]!.add(record.name)) {
4949
_cache[record.resourceRecordType] ??=
5050
SplayTreeMap<String, List<ResourceRecord>>();
5151

52-
_cache[record.resourceRecordType]
53-
[record.name] = <ResourceRecord>[record];
52+
_cache[record.resourceRecordType]![record.name] = <ResourceRecord>[
53+
record
54+
];
5455
} else {
55-
_cache[record.resourceRecordType][record.name].add(record);
56+
_cache[record.resourceRecordType]![record.name]!.add(record);
5657
}
5758
}
5859
}
@@ -62,12 +63,12 @@ class ResourceRecordCache {
6263
String name, int type, List<T> results) {
6364
assert(ResourceRecordType.debugAssertValid(type));
6465
final int time = DateTime.now().millisecondsSinceEpoch;
65-
final SplayTreeMap<String, List<ResourceRecord>> candidates = _cache[type];
66+
final SplayTreeMap<String, List<ResourceRecord>>? candidates = _cache[type];
6667
if (candidates == null) {
6768
return;
6869
}
6970

70-
final List<ResourceRecord> candidateRecords = candidates[name];
71+
final List<ResourceRecord>? candidateRecords = candidates[name];
7172
if (candidateRecords == null) {
7273
return;
7374
}

‎packages/multicast_dns/lib/src/packet.dart‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const int _kHeaderSize = 12;
2323
/// Will attempt to append 'local' if the name is something like '_http._tcp',
2424
/// and '._tcp.local' if name is something like '_http'.
2525
List<String> processDnsNameParts(String name) {
26-
assert(name != null);
2726
final List<String> parts = name.split('.');
2827
if (parts.length == 1) {
2928
return <String>[parts[0], '_tcp', 'local'];
@@ -47,9 +46,7 @@ List<int> encodeMDnsQuery(
4746
int type = ResourceRecordType.addressIPv4,
4847
bool multicast = true,
4948
}) {
50-
assert(name != null);
5149
assert(ResourceRecordType.debugAssertValid(type));
52-
assert(multicast != null);
5350

5451
final List<String> nameParts = processDnsNameParts(name);
5552
final List<List<int>> rawNameParts =
@@ -179,7 +176,7 @@ _FQDNReadResult _readFQDN(
179176
/// If decoding fails (e.g. due to an invalid packet), `null` is returned.
180177
///
181178
/// See https://tools.ietf.org/html/rfc1035 for format.
182-
ResourceRecordQuery decodeMDnsQuery(List<int> packet) {
179+
ResourceRecordQuery? decodeMDnsQuery(List<int> packet) {
183180
final int length = packet.length;
184181
if (length < _kHeaderSize) {
185182
return null;
@@ -214,7 +211,7 @@ ResourceRecordQuery decodeMDnsQuery(List<int> packet) {
214211
/// If decoding fails (e.g. due to an invalid packet) `null` is returned.
215212
///
216213
/// See https://tools.ietf.org/html/rfc1035 for the format.
217-
List<ResourceRecord> decodeMDnsResponse(List<int> packet) {
214+
List<ResourceRecord>? decodeMDnsResponse(List<int> packet) {
218215
final int length = packet.length;
219216
if (length < _kHeaderSize) {
220217
return null;
@@ -242,7 +239,7 @@ List<ResourceRecord> decodeMDnsResponse(List<int> packet) {
242239
}
243240
}
244241

245-
ResourceRecord readResourceRecord() {
242+
ResourceRecord? readResourceRecord() {
246243
// First read the FQDN.
247244
final _FQDNReadResult result = _readFQDN(data, packetBytes, offset, length);
248245
final String fqdn = result.fqdn;
@@ -319,7 +316,6 @@ List<ResourceRecord> decodeMDnsResponse(List<int> packet) {
319316
priority: priority,
320317
weight: weight,
321318
);
322-
break;
323319
case ResourceRecordType.serverPointer:
324320
checkLength(offset + readDataLength);
325321
final _FQDNReadResult result =
@@ -373,7 +369,7 @@ List<ResourceRecord> decodeMDnsResponse(List<int> packet) {
373369
offset += 4;
374370
}
375371
for (int i = 0; i < remainingCount; i++) {
376-
final ResourceRecord record = readResourceRecord();
372+
final ResourceRecord? record = readResourceRecord();
377373
if (record != null) {
378374
result.add(record);
379375
}
@@ -391,7 +387,7 @@ class MDnsDecodeException implements Exception {
391387
/// specified [offset].
392388
///
393389
/// The [offset] parameter should not be null.
394-
const MDnsDecodeException(this.offset) : assert(offset != null);
390+
const MDnsDecodeException(this.offset);
395391

396392
/// The offset in the packet at which the exception occurred.
397393
final int offset;

0 commit comments

Comments
 (0)