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/utils.dart'; | 10 import 'src/utils.dart'; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 certificateName: certificateName, | 92 certificateName: certificateName, |
93 requestClientCertificate: requestClientCertificate)); | 93 requestClientCertificate: requestClientCertificate)); |
94 } | 94 } |
95 | 95 |
96 /// A helper method for initializing loopback servers. | 96 /// A helper method for initializing loopback servers. |
97 /// | 97 /// |
98 /// [bind] should forward to either [HttpServer.bind] or | 98 /// [bind] should forward to either [HttpServer.bind] or |
99 /// [HttpServer.bindSecure]. | 99 /// [HttpServer.bindSecure]. |
100 static Future<HttpServer> _loopback(int port, | 100 static Future<HttpServer> _loopback(int port, |
101 Future<HttpServer> bind(InternetAddress address, int port)) { | 101 Future<HttpServer> bind(InternetAddress address, int port)) { |
102 return Future.wait([ | 102 return bind(InternetAddress.LOOPBACK_IP_V4, port).then((v4Server) { |
103 supportsIpV6, | |
104 bind(InternetAddress.LOOPBACK_IP_V4, port) | |
105 ]).then((results) { | |
106 var supportsIpV6 = results[0]; | |
107 var v4Server = results[1]; | |
108 | |
109 if (!supportsIpV6) return v4Server; | |
110 | |
111 // Reuse the IPv4 server's port so that if [port] is 0, both servers use | 103 // Reuse the IPv4 server's port so that if [port] is 0, both servers use |
112 // the same ephemeral port. | 104 // the same ephemeral port. |
113 return bind(InternetAddress.LOOPBACK_IP_V6, v4Server.port) | 105 return bind(InternetAddress.LOOPBACK_IP_V6, v4Server.port) |
114 .then((v6Server) { | 106 .then((v6Server) => new HttpMultiServer([v4Server, v6Server])) |
115 return new HttpMultiServer([v4Server, v6Server]); | 107 .catchError((error) { |
| 108 // If we fail to bind to IPv6, just use IPv4. |
| 109 if (error is SocketException) return v4Server; |
| 110 throw error; |
116 }); | 111 }); |
117 }); | 112 }); |
118 } | 113 } |
119 | 114 |
120 Future close({bool force: false}) => | 115 Future close({bool force: false}) => |
121 Future.wait(_servers.map((server) => server.close(force: force))); | 116 Future.wait(_servers.map((server) => server.close(force: force))); |
122 | 117 |
123 /// Returns an HttpConnectionsInfo object summarizing the total number of | 118 /// Returns an HttpConnectionsInfo object summarizing the total number of |
124 /// current connections handled by all the servers. | 119 /// current connections handled by all the servers. |
125 HttpConnectionsInfo connectionsInfo() { | 120 HttpConnectionsInfo connectionsInfo() { |
126 var info = new HttpConnectionsInfo(); | 121 var info = new HttpConnectionsInfo(); |
127 for (var server in _servers) { | 122 for (var server in _servers) { |
128 var subInfo = server.connectionsInfo(); | 123 var subInfo = server.connectionsInfo(); |
129 info.total += subInfo.total; | 124 info.total += subInfo.total; |
130 info.active += subInfo.active; | 125 info.active += subInfo.active; |
131 info.idle += subInfo.idle; | 126 info.idle += subInfo.idle; |
132 info.closing += subInfo.closing; | 127 info.closing += subInfo.closing; |
133 } | 128 } |
134 return info; | 129 return info; |
135 } | 130 } |
136 } | 131 } |
OLD | NEW |