Skip to content

Commit

Permalink
[multicast_dns] Allow handling socket errors
Browse files Browse the repository at this point in the history
  • Loading branch information
loic-sharma committed Jan 17, 2025
1 parent a77fb81 commit 969f367
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/multicast_dns/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 0.3.2+8

* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.
* Adds `onSocketError` to `MDnsClient.start`.

## 0.3.2+7

Expand Down
11 changes: 10 additions & 1 deletion packages/multicast_dns/lib/multicast_dns.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@ class MDnsClient {
/// for the mDNS query. If not provided, defaults to either `224.0.0.251` or
/// or `FF02::FB`.
///
/// On socket errors, the [onSocketError] handler is called with the error
/// object and possibly a stack trace. The [onSocketError] callback must be of
/// type `void Function(Object error)` or
/// `void Function(Object error, StackTrace)`.
///
/// Subsequent calls to this method are ignored while the mDNS client is in
/// started state.
Future<void> start({
InternetAddress? listenAddress,
NetworkInterfacesFactory? interfacesFactory,
int mDnsPort = mDnsPort,
InternetAddress? mDnsAddress,
Function? onSocketError,
}) async {
listenAddress ??= InternetAddress.anyIPv4;
interfacesFactory ??= allInterfacesFactory;
Expand Down Expand Up @@ -152,7 +158,10 @@ class MDnsClient {
// Join multicast on this interface.
incoming.joinMulticast(_mDnsAddress!, interface);
}
incoming.listen((RawSocketEvent event) => _handleIncoming(event, incoming));
incoming.listen(
(RawSocketEvent event) => _handleIncoming(event, incoming),
onError: onSocketError,
);
_started = true;
_starting = false;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/multicast_dns/lib/src/lookup_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class LookupResolver {
return controller.stream;
}

/// Parses [ResoureRecord]s received and delivers them to the appropriate
/// Parses [ResourceRecord]s received and delivers them to the appropriate
/// listener(s) added via [addPendingRequest].
void handleResponse(List<ResourceRecord> response) {
for (final ResourceRecord r in response) {
Expand Down
27 changes: 26 additions & 1 deletion packages/multicast_dns/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,51 @@ void main() {
});
}
});

test('Reports socket errors', () async {
final FakeRawDatagramSocket datagramSocket = FakeRawDatagramSocket();
final MDnsClient client = MDnsClient(rawDatagramSocketFactory:
(dynamic host, int port,
{bool reuseAddress = true,
bool reusePort = true,
int ttl = 1}) async {
return datagramSocket;
});

final Completer<void> errorCompleter = Completer<void>();

await client.start(onError: (Object e, StackTrace stacktrace) {
expect(e, 'Hello world');
errorCompleter.complete();
});

datagramSocket.controller.addError('Hello world');
await errorCompleter.future;
});
}

class FakeRawDatagramSocket extends Fake implements RawDatagramSocket {
@override
InternetAddress address = InternetAddress.anyIPv4;

final StreamController<RawSocketEvent> controller =
StreamController<RawSocketEvent>();

@override
StreamSubscription<RawSocketEvent> listen(
void Function(RawSocketEvent event)? onData,
{Function? onError,
void Function()? onDone,
bool? cancelOnError}) {
return const Stream<RawSocketEvent>.empty().listen(onData,
return controller.stream.listen(onData,
onError: onError, cancelOnError: cancelOnError, onDone: onDone);
}

bool closed = false;

@override
void close() {
controller.close();
closed = true;
}

Expand Down

0 comments on commit 969f367

Please sign in to comment.