Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Unified Diff: pkg/mdns/lib/src/native_extension_client.dart

Issue 1407123010: Extend the mDNS package with a native extension used on Mac OS (Closed) Base URL: git@github.com:dart-lang/fletch.git@master
Patch Set: Uploaded binary for Linux and added README Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/mdns/lib/src/native_extension_client.dart
diff --git a/pkg/mdns/lib/src/native_extension_client.dart b/pkg/mdns/lib/src/native_extension_client.dart
new file mode 100644
index 0000000000000000000000000000000000000000..b45baccdeab0e1f5b557aca3500c0979dc0dcb3c
--- /dev/null
+++ b/pkg/mdns/lib/src/native_extension_client.dart
@@ -0,0 +1,98 @@
+// Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE.md file.
+
+library mdns.src.native_extension_client;
+
+import 'dart:async';
+import 'dart:io';
+import 'dart:isolate';
+
+import 'package:mdns/mdns.dart';
+import 'package:mdns/src/lookup_resolver.dart';
+import 'package:mdns/src/packet.dart';
+
+import "dart-ext:../native/mdns_extension_lib";
+
+// Requests Ids. This should be aligned with the C code.
+enum RequestType {
+ echoRequest, // 0
+ lookupRequest, // 1
+}
+
+// Implementation of mDNS client using a native extension.
+class NativeExtensionMDnsClient implements MDnsClient {
+ bool _starting = false;
+ bool _started = false;
+ SendPort _service;
+ ReceivePort _incoming;
+ final LookupResolver _resolver = new LookupResolver();
+
+ /// Start the mDNS client.
+ Future start() async {
ricow1 2015/10/30 10:28:33 this does not return a future
Søren Gjesse 2015/10/30 12:13:16 It is "async", so it does.
+ if (_started && _starting) {
+ throw new StateError('mDNS client already started');
+ }
+ _starting = true;
+
+ _service = servicePort();
+ _incoming = new ReceivePort();
+ _incoming.listen(_handleIncoming);
+
+ _starting = false;
+ _started = true;
+ }
+
+ void stop() {
+ if (!_started) return;
+ if (_starting) {
+ throw new StateError('Cannot stop mDNS client wile it is starting');
+ }
+
+ _incoming.close();
+
+ _started = false;
+ }
+
+ Future<InternetAddress> lookup(
+ String hostname, {Duration timeout: const Duration(seconds: 5)}) {
+ if (!_started) {
+ throw new StateError('mDNS client is not started');
+ }
+
+ // Add the pending request before sending the query.
+ var future = _resolver.addPendingRequest(hostname, timeout);
+
+ // Send the request.
+ _service.send([_incoming.sendPort,
+ RequestType.lookupRequest.index,
+ hostname]);
+
+ return future;
+ }
+
+ // Process incoming datagrams.
+ _handleIncoming(response) {
+ if (response is List && response.length == 2) {
ricow1 2015/10/30 10:28:33 should we do something different if we get a non w
Søren Gjesse 2015/10/30 12:13:16 Added a print for errors for now. Documented the
+ if (response[0] is String &&
+ response[1] is List && response[1].length == 4) {
+ response = new DecodeResult(response[0],
+ new InternetAddress(response[1].join('.')));
+ _resolver.handleResponse([response]);
+ }
+ }
+ }
+}
+
+Future nativeExtensionEchoTest(dynamic message) async {
+ SendPort service = servicePort();
+ ReceivePort port = new ReceivePort();
+ try {
+ service.send([port.sendPort, RequestType.echoRequest.index, message]);
+ return await port.first;
+ } finally {
+ port.close();
+ }
+}
+
+SendPort servicePort() native 'MDnsExtension_ServicePort';

Powered by Google App Engine
This is Rietveld 408576698