OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:mirrors'; | 8 import 'dart:mirrors'; |
9 | 9 |
10 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 156 |
157 /// Repeatedly finds a probably-unused port on localhost and passes it to | 157 /// Repeatedly finds a probably-unused port on localhost and passes it to |
158 /// [tryPort] until it binds successfully. | 158 /// [tryPort] until it binds successfully. |
159 /// | 159 /// |
160 /// [tryPort] should return a non-`null` value or a Future completing to a | 160 /// [tryPort] should return a non-`null` value or a Future completing to a |
161 /// non-`null` value once it binds successfully. This value will be returned | 161 /// non-`null` value once it binds successfully. This value will be returned |
162 /// by [getUnusedPort] in turn. | 162 /// by [getUnusedPort] in turn. |
163 /// | 163 /// |
164 /// This is necessary for ensuring that our port binding isn't flaky for | 164 /// This is necessary for ensuring that our port binding isn't flaky for |
165 /// applications that don't print out the bound port. | 165 /// applications that don't print out the bound port. |
166 Future getUnusedPort(tryPort(int port)) { | 166 Future/*<T>*/ getUnusedPort/*<T>*/(/*=T*/ tryPort(int port)) { |
167 var value; | 167 var/*=T*/ value; |
168 return Future.doWhile(() async { | 168 return Future.doWhile(() async { |
169 value = await tryPort(await getUnsafeUnusedPort()); | 169 value = await tryPort(await getUnsafeUnusedPort()); |
170 return value == null; | 170 return value == null; |
171 }).then((_) => value); | 171 }).then((_) => value); |
172 } | 172 } |
173 | 173 |
174 /// Returns a port that is probably, but not definitely, not in use. | 174 /// Returns a port that is probably, but not definitely, not in use. |
175 /// | 175 /// |
176 /// This has a built-in race condition: another process may bind this port at | 176 /// This has a built-in race condition: another process may bind this port at |
177 /// any time after this call has returned. If at all possible, callers should | 177 /// any time after this call has returned. If at all possible, callers should |
178 /// use [getUnusedPort] instead. | 178 /// use [getUnusedPort] instead. |
179 Future<int> getUnsafeUnusedPort() async { | 179 Future<int> getUnsafeUnusedPort() async { |
180 var socket = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); | 180 var socket = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); |
181 var port = socket.port; | 181 var port = socket.port; |
182 await socket.close(); | 182 await socket.close(); |
183 return port; | 183 return port; |
184 } | 184 } |
OLD | NEW |