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 class RawServerSocket { | 5 patch class RawServerSocket { |
6 /* patch */ static Future<RawServerSocket> bind(address, | 6 /* patch */ static Future<RawServerSocket> bind(address, |
7 int port, | 7 int port, |
8 {int backlog: 0, | 8 {int backlog: 0, |
9 bool v6Only: false}) { | 9 bool v6Only: false}) { |
10 return _RawServerSocket.bind(address, port, backlog, v6Only); | 10 return _RawServerSocket.bind(address, port, backlog, v6Only); |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
474 return map; | 474 return map; |
475 }); | 475 }); |
476 return map.values.toList(); | 476 return map.values.toList(); |
477 } | 477 } |
478 }); | 478 }); |
479 } | 479 } |
480 | 480 |
481 static Future<_NativeSocket> connect(host, int port) { | 481 static Future<_NativeSocket> connect(host, int port) { |
482 return new Future.value(host) | 482 return new Future.value(host) |
483 .then((host) { | 483 .then((host) { |
484 if (host is _InternetAddress) return host; | 484 if (host is _InternetAddress) return [host]; |
485 return lookup(host) | 485 return lookup(host) |
486 .then((list) { | 486 .then((list) { |
487 if (list.length == 0) { | 487 if (list.length == 0) { |
488 throw createError(response, "Failed host lookup: '$host'"); | 488 throw createError(response, "Failed host lookup: '$host'"); |
489 } | 489 } |
490 return list[0]; | 490 return list; |
491 }); | 491 }); |
492 }) | 492 }) |
493 .then((address) { | 493 .then((addresses) { |
494 var socket = new _NativeSocket.normal(); | 494 assert(addresses is List); |
495 socket.address = address; | 495 var completer = new Completer(); |
496 var result = socket.nativeCreateConnect( | 496 var it = addresses.iterator; |
497 address._in_addr, port); | 497 void run(error) { |
498 if (result is OSError) { | 498 if (!it.moveNext()) { |
499 throw createError(result, "Connection failed", address, port); | 499 assert(error != null); |
500 } else { | 500 completer.completeError(error); |
501 socket.port; // Query the local port, for error messages. | 501 return; |
502 var completer = new Completer(); | 502 } |
503 // Setup handlers for receiving the first write event which | 503 var address = it.current; |
504 // indicate that the socket is fully connected. | 504 var socket = new _NativeSocket.normal(); |
505 socket.setHandlers( | 505 socket.address = address; |
506 write: () { | 506 var result = socket.nativeCreateConnect(address._in_addr, port); |
507 socket.setListening(read: false, write: false); | 507 if (result is OSError) { |
508 completer.complete(socket); | 508 run(error != null ? error : |
509 }, | 509 createError(result, "Connection failed", address, port)); |
510 error: (e) { | 510 } else { |
511 socket.close(); | 511 socket.port; // Query the local port, for error messages. |
512 completer.completeError(e); | 512 // Setup handlers for receiving the first write event which |
513 } | 513 // indicate that the socket is fully connected. |
514 ); | 514 socket.setHandlers( |
515 socket.setListening(read: false, write: true); | 515 write: () { |
516 return completer.future; | 516 socket.setListening(read: false, write: false); |
517 completer.complete(socket); | |
518 }, | |
519 error: (e) { | |
520 socket.close(); | |
Søren Gjesse
2014/05/01 07:23:34
Add a comment that this keeps the first error.
Anders Johnsen
2014/05/01 08:42:38
Done.
| |
521 run(error != null ? error : e); | |
codefu
2014/05/01 18:52:02
If addresses.length > 1 and the first address conn
Anders Johnsen
2014/05/01 19:27:58
No, this error handler is only called when connect
| |
522 } | |
523 ); | |
524 socket.setListening(read: false, write: true); | |
525 } | |
517 } | 526 } |
527 run(null); | |
528 return completer.future; | |
518 }); | 529 }); |
519 } | 530 } |
520 | 531 |
521 static Future<_NativeSocket> bind(host, | 532 static Future<_NativeSocket> bind(host, |
522 int port, | 533 int port, |
523 int backlog, | 534 int backlog, |
524 bool v6Only) { | 535 bool v6Only) { |
525 return new Future.value(host) | 536 return new Future.value(host) |
526 .then((host) { | 537 .then((host) { |
527 if (host is _InternetAddress) return host; | 538 if (host is _InternetAddress) return host; |
(...skipping 1252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1780 String address, | 1791 String address, |
1781 List<int> in_addr, | 1792 List<int> in_addr, |
1782 int port) { | 1793 int port) { |
1783 return new Datagram( | 1794 return new Datagram( |
1784 data, | 1795 data, |
1785 new _InternetAddress(address, null, in_addr), | 1796 new _InternetAddress(address, null, in_addr), |
1786 port); | 1797 port); |
1787 } | 1798 } |
1788 | 1799 |
1789 String _socketsStats() => _SocketsObservatory.toJSON(); | 1800 String _socketsStats() => _SocketsObservatory.toJSON(); |
OLD | NEW |