Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1396)

Unified Diff: runtime/bin/socket_patch.dart

Issue 2946323002: Added timeout parameter to RawSocket and Socket connect, which allows for the specification of a ma… (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/_internal/js_runtime/lib/io_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/socket_patch.dart
diff --git a/runtime/bin/socket_patch.dart b/runtime/bin/socket_patch.dart
index bbfceb8687e1b481dc7388dfd0dee6ee59bd8540..34a97c8f6fde70a45a5721eaee3a66ab85508709 100644
--- a/runtime/bin/socket_patch.dart
+++ b/runtime/bin/socket_patch.dart
@@ -14,8 +14,9 @@ class RawServerSocket {
@patch
class RawSocket {
@patch
- static Future<RawSocket> connect(host, int port, {sourceAddress}) {
- return _RawSocket.connect(host, port, sourceAddress);
+ static Future<RawSocket> connect(host, int port,
+ {sourceAddress, Duration timeout}) {
+ return _RawSocket.connect(host, port, sourceAddress, timeout);
}
}
@@ -394,7 +395,8 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
});
}
- static Future<_NativeSocket> connect(host, int port, sourceAddress) {
+ static Future<_NativeSocket> connect(
+ host, int port, sourceAddress, Duration timeout) {
_throwOnBadPort(port);
if (sourceAddress != null && sourceAddress is! _InternetAddress) {
if (sourceAddress is String) {
@@ -415,7 +417,23 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
var it = addresses.iterator;
var error = null;
var connecting = new HashMap();
+ Timer timeoutTimer = null;
+ void timeoutHandler() {
+ connecting.forEach((s, t) {
+ t.cancel();
+ s.close();
+ s.setHandlers();
+ s.setListening(read: false, write: false);
+ error = createError(
+ null, "Connection timed out, host: ${host}, port: ${port}");
+ completer.completeError(error);
+ });
+ }
+
void connectNext() {
+ if ((timeout != null) && (timeoutTimer == null)) {
+ timeoutTimer = new Timer(timeout, timeoutHandler);
+ }
if (!it.moveNext()) {
if (connecting.isEmpty) {
assert(error != null);
@@ -1185,9 +1203,10 @@ class _RawSocket extends Stream<RawSocketEvent> implements RawSocket {
// Flag to handle Ctrl-D closing of stdio on Mac OS.
bool _isMacOSTerminalInput = false;
- static Future<RawSocket> connect(host, int port, sourceAddress) {
+ static Future<RawSocket> connect(
+ host, int port, sourceAddress, Duration timeout) {
return _NativeSocket
- .connect(host, port, sourceAddress)
+ .connect(host, port, sourceAddress, timeout)
.then((socket) => new _RawSocket(socket));
}
@@ -1366,9 +1385,10 @@ class _ServerSocket extends Stream<Socket> implements ServerSocket {
@patch
class Socket {
@patch
- static Future<Socket> connect(host, int port, {sourceAddress}) {
+ static Future<Socket> connect(host, int port,
+ {sourceAddress, Duration timeout}) {
return RawSocket
- .connect(host, port, sourceAddress: sourceAddress)
+ .connect(host, port, sourceAddress: sourceAddress, timeout: timeout)
.then((socket) => new _Socket(socket));
}
}
« no previous file with comments | « no previous file | sdk/lib/_internal/js_runtime/lib/io_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698