| 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; | 5 library http_multi_server; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'src/multi_headers.dart'; | 10 import 'src/multi_headers.dart'; |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 | 141 |
| 142 if (!supportsIpV6) return v4Server; | 142 if (!supportsIpV6) return v4Server; |
| 143 | 143 |
| 144 // Reuse the IPv4 server's port so that if [port] is 0, both servers use | 144 // Reuse the IPv4 server's port so that if [port] is 0, both servers use |
| 145 // the same ephemeral port. | 145 // the same ephemeral port. |
| 146 return bind(InternetAddress.LOOPBACK_IP_V6, v4Server.port) | 146 return bind(InternetAddress.LOOPBACK_IP_V6, v4Server.port) |
| 147 .then((v6Server) { | 147 .then((v6Server) { |
| 148 return new HttpMultiServer([v4Server, v6Server]); | 148 return new HttpMultiServer([v4Server, v6Server]); |
| 149 }).catchError((error) { | 149 }).catchError((error) { |
| 150 if (error is! SocketException) throw error; | 150 if (error is! SocketException) throw error; |
| 151 if (error.osError.errno != _addressInUseErrno) throw error; | 151 if (error.osError.errorCode != _addressInUseErrno) throw error; |
| 152 if (port != 0) throw error; | 152 if (port != 0) throw error; |
| 153 if (remainingRetries == 0) throw error; | 153 if (remainingRetries == 0) throw error; |
| 154 | 154 |
| 155 // A port being available on IPv4 doesn't necessarily mean that the same | 155 // A port being available on IPv4 doesn't necessarily mean that the same |
| 156 // port is available on IPv6. If it's not (which is rare in practice), | 156 // port is available on IPv6. If it's not (which is rare in practice), |
| 157 // we try again until we find one that's available on both. | 157 // we try again until we find one that's available on both. |
| 158 v4Server.close(); | 158 v4Server.close(); |
| 159 return _loopback(port, bind, remainingRetries - 1); | 159 return _loopback(port, bind, remainingRetries - 1); |
| 160 }); | 160 }); |
| 161 }); | 161 }); |
| 162 } | 162 } |
| 163 | 163 |
| 164 Future close({bool force: false}) => | 164 Future close({bool force: false}) => |
| 165 Future.wait(_servers.map((server) => server.close(force: force))); | 165 Future.wait(_servers.map((server) => server.close(force: force))); |
| 166 | 166 |
| 167 /// Returns an HttpConnectionsInfo object summarizing the total number of | 167 /// Returns an HttpConnectionsInfo object summarizing the total number of |
| 168 /// current connections handled by all the servers. | 168 /// current connections handled by all the servers. |
| 169 HttpConnectionsInfo connectionsInfo() { | 169 HttpConnectionsInfo connectionsInfo() { |
| 170 var info = new HttpConnectionsInfo(); | 170 var info = new HttpConnectionsInfo(); |
| 171 for (var server in _servers) { | 171 for (var server in _servers) { |
| 172 var subInfo = server.connectionsInfo(); | 172 var subInfo = server.connectionsInfo(); |
| 173 info.total += subInfo.total; | 173 info.total += subInfo.total; |
| 174 info.active += subInfo.active; | 174 info.active += subInfo.active; |
| 175 info.idle += subInfo.idle; | 175 info.idle += subInfo.idle; |
| 176 info.closing += subInfo.closing; | 176 info.closing += subInfo.closing; |
| 177 } | 177 } |
| 178 return info; | 178 return info; |
| 179 } | 179 } |
| 180 } | 180 } |
| OLD | NEW |