OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 @patch | 5 @patch |
6 class RawServerSocket { | 6 class RawServerSocket { |
7 @patch | 7 @patch |
8 static Future<RawServerSocket> bind(address, int port, | 8 static Future<RawServerSocket> bind(address, int port, |
9 {int backlog: 0, bool v6Only: false, bool shared: false}) { | 9 {int backlog: 0, bool v6Only: false, bool shared: false}) { |
10 return _RawServerSocket.bind(address, port, backlog, v6Only, shared); | 10 return _RawServerSocket.bind(address, port, backlog, v6Only, shared); |
11 } | 11 } |
12 } | 12 } |
13 | 13 |
14 @patch | 14 @patch |
15 class RawSocket { | 15 class RawSocket { |
16 @patch | 16 @patch |
17 static Future<RawSocket> connect(host, int port, {sourceAddress}) { | 17 static Future<RawSocket> connect(host, int port, |
18 return _RawSocket.connect(host, port, sourceAddress); | 18 {sourceAddress, Duration timeout}) { |
19 return _RawSocket.connect(host, port, sourceAddress, timeout); | |
19 } | 20 } |
20 } | 21 } |
21 | 22 |
22 @patch | 23 @patch |
23 class InternetAddress { | 24 class InternetAddress { |
24 @patch | 25 @patch |
25 static InternetAddress get LOOPBACK_IP_V4 { | 26 static InternetAddress get LOOPBACK_IP_V4 { |
26 return _InternetAddress.LOOPBACK_IP_V4; | 27 return _InternetAddress.LOOPBACK_IP_V4; |
27 } | 28 } |
28 | 29 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
387 if (!includeLoopback && address.isLoopback) return map; | 388 if (!includeLoopback && address.isLoopback) return map; |
388 map.putIfAbsent(name, () => new _NetworkInterface(name, index)); | 389 map.putIfAbsent(name, () => new _NetworkInterface(name, index)); |
389 map[name].addresses.add(address); | 390 map[name].addresses.add(address); |
390 return map; | 391 return map; |
391 }); | 392 }); |
392 return map.values.toList(); | 393 return map.values.toList(); |
393 } | 394 } |
394 }); | 395 }); |
395 } | 396 } |
396 | 397 |
397 static Future<_NativeSocket> connect(host, int port, sourceAddress) { | 398 static Future<_NativeSocket> connect( |
399 host, int port, sourceAddress, Duration timeout) { | |
398 _throwOnBadPort(port); | 400 _throwOnBadPort(port); |
399 if (sourceAddress != null && sourceAddress is! _InternetAddress) { | 401 if (sourceAddress != null && sourceAddress is! _InternetAddress) { |
400 if (sourceAddress is String) { | 402 if (sourceAddress is String) { |
401 sourceAddress = new InternetAddress(sourceAddress); | 403 sourceAddress = new InternetAddress(sourceAddress); |
402 } | 404 } |
403 } | 405 } |
404 return new Future.value(host).then((host) { | 406 return new Future.value(host).then((host) { |
405 if (host is _InternetAddress) return [host]; | 407 if (host is _InternetAddress) return [host]; |
406 return lookup(host).then((addresses) { | 408 return lookup(host).then((addresses) { |
407 if (addresses.isEmpty) { | 409 if (addresses.isEmpty) { |
408 throw createError(null, "Failed host lookup: '$host'"); | 410 throw createError(null, "Failed host lookup: '$host'"); |
409 } | 411 } |
410 return addresses; | 412 return addresses; |
411 }); | 413 }); |
412 }).then((addresses) { | 414 }).then((addresses) { |
413 assert(addresses is List); | 415 assert(addresses is List); |
414 var completer = new Completer(); | 416 var completer = new Completer(); |
415 var it = addresses.iterator; | 417 var it = addresses.iterator; |
416 var error = null; | 418 var error = null; |
417 var connecting = new HashMap(); | 419 var connecting = new HashMap(); |
420 Timer timeoutTimer = null; | |
421 void timeoutHandler() { | |
422 connecting.forEach((s, t) { | |
423 t.cancel(); | |
424 s.close(); | |
425 s.setHandlers(); | |
426 s.setListening(read: false, write: false); | |
427 error = createError( | |
428 null, "Connection timed out, host: ${host}, port: ${port}"); | |
429 completer.completeError(error); | |
430 }); | |
431 } | |
432 | |
418 void connectNext() { | 433 void connectNext() { |
434 if ((timeout != null) && (timeoutTimer == null)) { | |
435 timeoutTimer = new Timer(timeout, timeoutHandler); | |
436 } | |
419 if (!it.moveNext()) { | 437 if (!it.moveNext()) { |
420 if (connecting.isEmpty) { | 438 if (connecting.isEmpty) { |
421 assert(error != null); | 439 assert(error != null); |
422 completer.completeError(error); | 440 completer.completeError(error); |
zra
2017/06/21 20:32:31
It shouldn't be necessary, but I would be a little
| |
423 } | 441 } |
424 return; | 442 return; |
425 } | 443 } |
426 var address = it.current; | 444 var address = it.current; |
427 var socket = new _NativeSocket.normal(); | 445 var socket = new _NativeSocket.normal(); |
428 socket.localAddress = address; | 446 socket.localAddress = address; |
429 var result; | 447 var result; |
430 if (sourceAddress == null) { | 448 if (sourceAddress == null) { |
431 result = socket.nativeCreateConnect(address._in_addr, port); | 449 result = socket.nativeCreateConnect(address._in_addr, port); |
432 } else { | 450 } else { |
(...skipping 24 matching lines...) Expand all Loading... | |
457 // (if any). | 475 // (if any). |
458 var duration = | 476 var duration = |
459 address.isLoopback ? _RETRY_DURATION_LOOPBACK : _RETRY_DURATION; | 477 address.isLoopback ? _RETRY_DURATION_LOOPBACK : _RETRY_DURATION; |
460 var timer = new Timer(duration, connectNext); | 478 var timer = new Timer(duration, connectNext); |
461 setupResourceInfo(socket); | 479 setupResourceInfo(socket); |
462 | 480 |
463 connecting[socket] = timer; | 481 connecting[socket] = timer; |
464 // Setup handlers for receiving the first write event which | 482 // Setup handlers for receiving the first write event which |
465 // indicate that the socket is fully connected. | 483 // indicate that the socket is fully connected. |
466 socket.setHandlers(write: () { | 484 socket.setHandlers(write: () { |
467 timer.cancel(); | 485 timer.cancel(); |
zra
2017/06/21 20:32:31
And here.
| |
468 socket.setListening(read: false, write: false); | 486 socket.setListening(read: false, write: false); |
469 completer.complete(socket); | 487 completer.complete(socket); |
470 connecting.remove(socket); | 488 connecting.remove(socket); |
471 connecting.forEach((s, t) { | 489 connecting.forEach((s, t) { |
472 t.cancel(); | 490 t.cancel(); |
473 s.close(); | 491 s.close(); |
474 s.setHandlers(); | 492 s.setHandlers(); |
475 s.setListening(read: false, write: false); | 493 s.setListening(read: false, write: false); |
476 }); | 494 }); |
477 }, error: (e) { | 495 }, error: (e) { |
(...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1178 | 1196 |
1179 class _RawSocket extends Stream<RawSocketEvent> implements RawSocket { | 1197 class _RawSocket extends Stream<RawSocketEvent> implements RawSocket { |
1180 final _NativeSocket _socket; | 1198 final _NativeSocket _socket; |
1181 StreamController<RawSocketEvent> _controller; | 1199 StreamController<RawSocketEvent> _controller; |
1182 bool _readEventsEnabled = true; | 1200 bool _readEventsEnabled = true; |
1183 bool _writeEventsEnabled = true; | 1201 bool _writeEventsEnabled = true; |
1184 | 1202 |
1185 // Flag to handle Ctrl-D closing of stdio on Mac OS. | 1203 // Flag to handle Ctrl-D closing of stdio on Mac OS. |
1186 bool _isMacOSTerminalInput = false; | 1204 bool _isMacOSTerminalInput = false; |
1187 | 1205 |
1188 static Future<RawSocket> connect(host, int port, sourceAddress) { | 1206 static Future<RawSocket> connect( |
1207 host, int port, sourceAddress, Duration timeout) { | |
1189 return _NativeSocket | 1208 return _NativeSocket |
1190 .connect(host, port, sourceAddress) | 1209 .connect(host, port, sourceAddress, timeout) |
1191 .then((socket) => new _RawSocket(socket)); | 1210 .then((socket) => new _RawSocket(socket)); |
1192 } | 1211 } |
1193 | 1212 |
1194 _RawSocket(this._socket) { | 1213 _RawSocket(this._socket) { |
1195 var zone = Zone.current; | 1214 var zone = Zone.current; |
1196 _controller = new StreamController( | 1215 _controller = new StreamController( |
1197 sync: true, | 1216 sync: true, |
1198 onListen: _onSubscriptionStateChange, | 1217 onListen: _onSubscriptionStateChange, |
1199 onCancel: _onSubscriptionStateChange, | 1218 onCancel: _onSubscriptionStateChange, |
1200 onPause: _onPauseStateChange, | 1219 onPause: _onPauseStateChange, |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1359 Future close() => _socket.close().then((_) => this); | 1378 Future close() => _socket.close().then((_) => this); |
1360 | 1379 |
1361 void set _owner(owner) { | 1380 void set _owner(owner) { |
1362 _socket._owner = owner; | 1381 _socket._owner = owner; |
1363 } | 1382 } |
1364 } | 1383 } |
1365 | 1384 |
1366 @patch | 1385 @patch |
1367 class Socket { | 1386 class Socket { |
1368 @patch | 1387 @patch |
1369 static Future<Socket> connect(host, int port, {sourceAddress}) { | 1388 static Future<Socket> connect(host, int port, |
1389 {sourceAddress, Duration timeout}) { | |
1370 return RawSocket | 1390 return RawSocket |
1371 .connect(host, port, sourceAddress: sourceAddress) | 1391 .connect(host, port, sourceAddress: sourceAddress, timeout: timeout) |
1372 .then((socket) => new _Socket(socket)); | 1392 .then((socket) => new _Socket(socket)); |
1373 } | 1393 } |
1374 } | 1394 } |
1375 | 1395 |
1376 class _SocketStreamConsumer extends StreamConsumer<List<int>> { | 1396 class _SocketStreamConsumer extends StreamConsumer<List<int>> { |
1377 StreamSubscription subscription; | 1397 StreamSubscription subscription; |
1378 final _Socket socket; | 1398 final _Socket socket; |
1379 int offset; | 1399 int offset; |
1380 List<int> buffer; | 1400 List<int> buffer; |
1381 bool paused = false; | 1401 bool paused = false; |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1803 } else { | 1823 } else { |
1804 _socket.close(); | 1824 _socket.close(); |
1805 } | 1825 } |
1806 } | 1826 } |
1807 } | 1827 } |
1808 | 1828 |
1809 Datagram _makeDatagram( | 1829 Datagram _makeDatagram( |
1810 List<int> data, String address, List<int> in_addr, int port) { | 1830 List<int> data, String address, List<int> in_addr, int port) { |
1811 return new Datagram(data, new _InternetAddress(address, null, in_addr), port); | 1831 return new Datagram(data, new _InternetAddress(address, null, in_addr), port); |
1812 } | 1832 } |
OLD | NEW |