OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library http_multi_server.utils; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 /// Merges all streams in [streams] into a single stream that emits all of their |
| 11 /// values. |
| 12 /// |
| 13 /// The returned stream will be closed only when every stream in [streams] is |
| 14 /// closed. |
| 15 Stream mergeStreams(Iterable<Stream> streams) { |
| 16 var subscriptions = new Set(); |
| 17 var controller; |
| 18 controller = new StreamController(onListen: () { |
| 19 for (var stream in streams) { |
| 20 var subscription; |
| 21 subscription = stream.listen(controller.add, |
| 22 onError: controller.addError, |
| 23 onDone: () { |
| 24 subscriptions.remove(subscription); |
| 25 if (subscriptions.isEmpty) controller.close(); |
| 26 }); |
| 27 subscriptions.add(subscription); |
| 28 } |
| 29 }, onCancel: () { |
| 30 for (var subscription in subscriptions) { |
| 31 subscription.cancel(); |
| 32 } |
| 33 }, onPause: () { |
| 34 for (var subscription in subscriptions) { |
| 35 subscription.pause(); |
| 36 } |
| 37 }, onResume: () { |
| 38 for (var subscription in subscriptions) { |
| 39 subscription.resume(); |
| 40 } |
| 41 }, sync: true); |
| 42 |
| 43 return controller.stream; |
| 44 } |
| 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 |