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

Side by Side Diff: pkg/mdns/lib/src/native_protocol_client.dart

Issue 1413713011: Revert "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: Created 5 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « pkg/mdns/lib/src/native_extension_client.dart ('k') | pkg/mdns/test/decode_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE.md file.
4
5 library mdns.src.native_protocol_client;
6
7 import 'dart:async';
8 import 'dart:collection';
9 import 'dart:io';
10
11 import 'package:mdns/mdns.dart';
12 import 'package:mdns/src/constants.dart';
13 import 'package:mdns/src/lookup_resolver.dart';
14 import 'package:mdns/src/packet.dart';
15
16 // Implementation of mDNS client using the native protocol.
17 class NativeProtocolMDnsClient implements MDnsClient {
18 bool _starting = false;
19 bool _started = false;
20 RawDatagramSocket _incoming;
21 final List<RawDatagramSocket> _sockets = <RawDatagramSocket>[];
22 final LookupResolver _resolver = new LookupResolver();
23
24 /// Start the mDNS client.
25 Future start() async {
26 if (_started && _starting) {
27 throw new StateError('mDNS client already started');
28 }
29 _starting = true;
30
31 // Listen on all addresses.
32 _incoming = await RawDatagramSocket.bind(
33 InternetAddress.ANY_IP_V4, mDnsPort, reuseAddress: true);
34
35 // Find all network interfaces with an IPv4 address.
36 var interfaces =
37 await NetworkInterface.list(type: InternetAddressType.IP_V4);
38 for (NetworkInterface interface in interfaces) {
39 // Create a socket for sending on each adapter.
40 var socket = await RawDatagramSocket.bind(
41 interface.addresses[0], mDnsPort, reuseAddress: true);
42 _sockets.add(socket);
43
44 // Join multicast on this interface.
45 _incoming.joinMulticast(mDnsAddress, interface);
46 }
47 _incoming.listen(_handleIncoming);
48
49 _starting = false;
50 _started = true;
51 }
52
53 void stop() {
54 if (!_started) return;
55 if (_starting) {
56 throw new StateError('Cannot stop mDNS client wile it is starting');
57 }
58
59 _sockets.forEach((socket) => socket.close());
60 _incoming.close();
61
62 _started = false;
63 }
64
65 Future<InternetAddress> lookup(
66 String hostname, {Duration timeout: const Duration(seconds: 5)}) {
67 if (!_started) {
68 throw new StateError('mDNS client is not started');
69 }
70
71 // Add the pending request before sending the query.
72 var future = _resolver.addPendingRequest(hostname, timeout);
73
74 // Send the request on all interfaces.
75 List<int> packet = encodeMDnsQuery(hostname);
76 for (int i = 0; i < _sockets.length; i++) {
77 _sockets[i].send(packet, mDnsAddress, mDnsPort);
78 }
79
80 return future;
81 }
82
83 // Process incoming datagrams.
84 _handleIncoming(event) {
85 if (event == RawSocketEvent.READ) {
86 var data = _incoming.receive();
87 var response = decodeMDnsResponse(data.data);
88 if (response != null) {
89 _resolver.handleResponse(response);
90 }
91 }
92 }
93 }
OLDNEW
« no previous file with comments | « pkg/mdns/lib/src/native_extension_client.dart ('k') | pkg/mdns/test/decode_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698