OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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 file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library http_multi_server.utils; | 5 library http_multi_server.utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 /// Merges all streams in [streams] into a single stream that emits all of their | 10 /// Merges all streams in [streams] into a single stream that emits all of their |
11 /// values. | 11 /// values. |
12 /// | 12 /// |
13 /// The returned stream will be closed only when every stream in [streams] is | 13 /// The returned stream will be closed only when every stream in [streams] is |
14 /// closed. | 14 /// closed. |
15 Stream mergeStreams(Iterable<Stream> streams) { | 15 Stream mergeStreams(Iterable<Stream> streams) { |
16 var subscriptions = new Set(); | 16 var subscriptions = new Set(); |
17 var controller; | 17 var controller; |
18 controller = new StreamController(onListen: () { | 18 controller = new StreamController(onListen: () { |
19 for (var stream in streams) { | 19 for (var stream in streams) { |
20 var subscription; | 20 var subscription; |
21 subscription = stream.listen(controller.add, | 21 subscription = stream.listen(controller.add, onError: (error, trace) { |
22 onError: controller.addError, | 22 // If one of the subscriptions has an error (usually IPv6 failing late), |
nweiz
2014/07/09 02:02:01
This comment should mention the tracking issue. It
| |
23 onDone: () { | 23 // then just remove it and ignore the error. |
24 subscriptions.remove(subscription); | |
nweiz
2014/07/09 02:02:01
If this isn't the last subscription, cancel it as
| |
25 | |
26 // If the last subscription errored, though, pass it on. | |
27 if (subscriptions.isEmpty) controller.addError(error, trace); | |
28 }, onDone: () { | |
24 subscriptions.remove(subscription); | 29 subscriptions.remove(subscription); |
25 if (subscriptions.isEmpty) controller.close(); | 30 if (subscriptions.isEmpty) controller.close(); |
26 }); | 31 }); |
27 subscriptions.add(subscription); | 32 subscriptions.add(subscription); |
28 } | 33 } |
29 }, onCancel: () { | 34 }, onCancel: () { |
30 for (var subscription in subscriptions) { | 35 for (var subscription in subscriptions) { |
31 subscription.cancel(); | 36 subscription.cancel(); |
32 } | 37 } |
33 }, onPause: () { | 38 }, onPause: () { |
34 for (var subscription in subscriptions) { | 39 for (var subscription in subscriptions) { |
35 subscription.pause(); | 40 subscription.pause(); |
36 } | 41 } |
37 }, onResume: () { | 42 }, onResume: () { |
38 for (var subscription in subscriptions) { | 43 for (var subscription in subscriptions) { |
39 subscription.resume(); | 44 subscription.resume(); |
40 } | 45 } |
41 }, sync: true); | 46 }, sync: true); |
42 | 47 |
43 return controller.stream; | 48 return controller.stream; |
44 } | 49 } |
45 | |
46 /// A cache for [supportsIpV6]. | |
47 bool _supportsIpV6; | |
48 | |
49 /// Returns whether this computer supports binding to IPv6 addresses. | |
50 Future<bool> get supportsIpV6 { | |
51 if (_supportsIpV6 != null) return new Future.value(_supportsIpV6); | |
52 | |
53 return ServerSocket.bind(InternetAddress.LOOPBACK_IP_V6, 0).then((socket) { | |
54 _supportsIpV6 = true; | |
55 socket.close(); | |
56 return true; | |
57 }).catchError((error) { | |
58 if (error is! SocketException) throw error; | |
59 _supportsIpV6 = false; | |
60 return false; | |
61 }); | |
62 } | |
OLD | NEW |