Chromium Code Reviews| 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([String address = "127.0.0.1", | 6 /* patch */ static Future<RawServerSocket> bind([String address = "127.0.0.1", |
| 7 int port = 0, | 7 int port = 0, |
| 8 int backlog = 0]) { | 8 int backlog = 0]) { |
| 9 return _RawServerSocket.bind(address, port, backlog); | 9 return _RawServerSocket.bind(address, port, backlog); |
| 10 } | 10 } |
| 11 } | 11 } |
| 12 | 12 |
| 13 | 13 |
| 14 patch class RawSocket { | 14 patch class RawSocket { |
| 15 /* patch */ static Future<RawSocket> connect(String host, int port) { | 15 /* patch */ static Future<RawSocket> connect(host, int port) { |
| 16 return _RawSocket.connect(host, port); | 16 return _RawSocket.connect(host, port); |
| 17 } | 17 } |
| 18 } | 18 } |
| 19 | 19 |
| 20 | 20 |
| 21 patch class InternetAddress { | |
| 22 /* patch */ static Future<List<InternetAddress>> lookup(String host) { | |
| 23 return _NativeSocket.lookup(host); | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 class _InternetAddress implements InternetAddress { | |
| 28 final InternetAddressType type; | |
| 29 final String address; | |
| 30 final Uint8List _sockaddr_storage; | |
| 31 | |
| 32 _InternetAddress(InternetAddressType this.type, | |
| 33 String this.address, | |
| 34 List<int> this._sockaddr_storage); | |
| 35 | |
| 36 String toString() { | |
| 37 return "InternetAddress('$address', ${type.name})"; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 | |
| 21 // The _NativeSocket class encapsulates an OS socket. | 42 // The _NativeSocket class encapsulates an OS socket. |
| 22 class _NativeSocket extends NativeFieldWrapperClass1 { | 43 class _NativeSocket extends NativeFieldWrapperClass1 { |
| 23 // Bit flags used when communicating between the eventhandler and | 44 // Bit flags used when communicating between the eventhandler and |
| 24 // dart code. The EVENT flags are used to indicate events of | 45 // dart code. The EVENT flags are used to indicate events of |
| 25 // interest when sending a message from dart code to the | 46 // interest when sending a message from dart code to the |
| 26 // eventhandler. When receiving a message from the eventhandler the | 47 // eventhandler. When receiving a message from the eventhandler the |
| 27 // EVENT flags indicate the events that actually happened. The | 48 // EVENT flags indicate the events that actually happened. The |
| 28 // COMMAND flags are used to send commands from dart to the | 49 // COMMAND flags are used to send commands from dart to the |
| 29 // eventhandler. COMMAND flags are never received from the | 50 // eventhandler. COMMAND flags are never received from the |
| 30 // eventhandler. Additional flags are used to communicate other | 51 // eventhandler. Additional flags are used to communicate other |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 | 91 |
| 71 // The type flags for this socket. | 92 // The type flags for this socket. |
| 72 final int typeFlags; | 93 final int typeFlags; |
| 73 | 94 |
| 74 // Holds the port of the socket, null if not known. | 95 // Holds the port of the socket, null if not known. |
| 75 int localPort; | 96 int localPort; |
| 76 | 97 |
| 77 // Native port for socket services. | 98 // Native port for socket services. |
| 78 static SendPort socketService; | 99 static SendPort socketService; |
| 79 | 100 |
| 80 static Future<_NativeSocket> connect(String host, int port) { | 101 static Future<List<InternetAddress>> lookup(String host) { |
| 81 var completer = new Completer(); | |
| 82 ensureSocketService(); | 102 ensureSocketService(); |
| 83 socketService.call([HOST_NAME_LOOKUP, host]).then((response) { | 103 return socketService.call([HOST_NAME_LOOKUP, host]) |
| 84 if (isErrorResponse(response)) { | 104 .then((response) { |
|
Søren Gjesse
2013/04/18 12:59:24
You better add a comment here that this is checkin
Anders Johnsen
2013/04/19 09:45:36
Done.
| |
| 85 completer.completeError( | 105 if (response is List && |
| 86 createError(response, "Failed host name lookup")); | 106 response.length > 0 && |
| 87 } else { | 107 response.first is int) { |
| 88 var socket = new _NativeSocket.normal(); | 108 return []; |
|
Søren Gjesse
2013/04/18 12:59:24
Throw an OSError here.
Anders Johnsen
2013/04/19 09:45:36
Done.
| |
| 89 var result = socket.nativeCreateConnect(response, port); | 109 } else { |
| 90 if (result is OSError) { | 110 return response.map((result) { |
| 91 completer.completeError(createError(result, "Connection failed")); | 111 var type = new InternetAddressType._(result[0]); |
| 92 } else { | 112 return new _InternetAddress(type, result[1], result[2]); |
| 93 // Setup handlers for receiving the first write event which | 113 }).toList(); |
| 94 // indicate that the socket is fully connected. | 114 } |
| 95 socket.setHandlers( | 115 }); |
| 96 write: () { | 116 } |
| 97 socket.setListening(read: false, write: false); | 117 |
| 98 completer.complete(socket); | 118 static Future<_NativeSocket> connect(host, int port) { |
| 99 }, | 119 return new Future.value(host) |
| 100 error: (e) { | 120 .then((host) { |
| 101 socket.close(); | 121 if (host is _InternetAddress) return host; |
| 102 completer.completeError(createError(e, "Connection failed")); | 122 return lookup(host) |
| 103 } | 123 .then((list) { |
| 104 ); | 124 if (list.length == 0) { |
| 105 socket.setListening(read: false, write: true); | 125 throw createError(response, "Failed host name lookup"); |
| 106 } | 126 } |
| 107 } | 127 return list[0]; |
| 108 }); | 128 }); |
| 109 return completer.future; | 129 }) |
| 130 .then((address) { | |
| 131 ensureSocketService(); | |
| 132 var socket = new _NativeSocket.normal(); | |
| 133 var result = socket.nativeCreateConnect( | |
| 134 address._sockaddr_storage, port); | |
| 135 if (result is OSError) { | |
| 136 throw createError(result, "Connection failed"); | |
| 137 } else { | |
| 138 var completer = new Completer(); | |
| 139 // Setup handlers for receiving the first write event which | |
| 140 // indicate that the socket is fully connected. | |
| 141 socket.setHandlers( | |
| 142 write: () { | |
| 143 socket.setListening(read: false, write: false); | |
| 144 completer.complete(socket); | |
| 145 }, | |
| 146 error: (e) { | |
| 147 socket.close(); | |
| 148 completer.completeError(createError(e, "Connection failed")); | |
| 149 } | |
| 150 ); | |
| 151 socket.setListening(read: false, write: true); | |
| 152 return completer.future; | |
| 153 } | |
| 154 }); | |
| 110 } | 155 } |
| 111 | 156 |
| 112 static Future<_NativeSocket> bind(String address, | 157 static Future<_NativeSocket> bind(String address, |
| 113 int port, | 158 int port, |
| 114 int backlog) { | 159 int backlog) { |
| 115 var socket = new _NativeSocket.listen(); | 160 return lookup(address) |
| 116 var result = socket.nativeCreateBindListen(address, port, backlog); | 161 .then((list) { |
| 117 if (result is OSError) { | 162 if (list.length == 0) { |
| 118 return new Future.error( | 163 throw createError(response, "Failed host name lookup"); |
| 119 new SocketIOException("Failed to create server socket", result)); | 164 } |
| 120 } | 165 return list[0]; |
| 121 if (port != 0) socket.localPort = port; | 166 }) |
| 122 return new Future.value(socket); | 167 .then((address) { |
| 168 var socket = new _NativeSocket.listen(); | |
| 169 var result = socket.nativeCreateBindListen(address._sockaddr_storage, | |
| 170 port, | |
| 171 backlog); | |
| 172 if (result is OSError) { | |
| 173 throw new SocketIOException( | |
| 174 "Failed to create server socket", result); | |
| 175 } | |
| 176 if (port != 0) socket.localPort = port; | |
| 177 return socket; | |
| 178 }); | |
| 123 } | 179 } |
| 124 | 180 |
| 125 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET { | 181 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET { |
| 126 eventHandlers = new List(EVENT_COUNT + 1); | 182 eventHandlers = new List(EVENT_COUNT + 1); |
| 127 _EventHandler._start(); | 183 _EventHandler._start(); |
| 128 } | 184 } |
| 129 | 185 |
| 130 _NativeSocket.listen() : typeFlags = TYPE_LISTENING_SOCKET { | 186 _NativeSocket.listen() : typeFlags = TYPE_LISTENING_SOCKET { |
| 131 eventHandlers = new List(EVENT_COUNT + 1); | 187 eventHandlers = new List(EVENT_COUNT + 1); |
| 132 _EventHandler._start(); | 188 _EventHandler._start(); |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 bool setOption(SocketOption option, bool enabled) { | 451 bool setOption(SocketOption option, bool enabled) { |
| 396 if (option is! SocketOption) throw new ArgumentError(options); | 452 if (option is! SocketOption) throw new ArgumentError(options); |
| 397 if (enabled is! bool) throw new ArgumentError(enabled); | 453 if (enabled is! bool) throw new ArgumentError(enabled); |
| 398 return nativeSetOption(option._value, enabled); | 454 return nativeSetOption(option._value, enabled); |
| 399 } | 455 } |
| 400 | 456 |
| 401 nativeAvailable() native "Socket_Available"; | 457 nativeAvailable() native "Socket_Available"; |
| 402 nativeRead(int len) native "Socket_Read"; | 458 nativeRead(int len) native "Socket_Read"; |
| 403 nativeWrite(List<int> buffer, int offset, int bytes) | 459 nativeWrite(List<int> buffer, int offset, int bytes) |
| 404 native "Socket_WriteList"; | 460 native "Socket_WriteList"; |
| 405 nativeCreateConnect(String host, int port) native "Socket_CreateConnect"; | 461 nativeCreateConnect(List<int> addr, |
| 406 nativeCreateBindListen(String address, int port, int backlog) | 462 int port) native "Socket_CreateConnect"; |
| 463 nativeCreateBindListen(List<int> addr, int port, int backlog) | |
| 407 native "ServerSocket_CreateBindListen"; | 464 native "ServerSocket_CreateBindListen"; |
| 408 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept"; | 465 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept"; |
| 409 int nativeGetPort() native "Socket_GetPort"; | 466 int nativeGetPort() native "Socket_GetPort"; |
| 410 List nativeGetRemotePeer() native "Socket_GetRemotePeer"; | 467 List nativeGetRemotePeer() native "Socket_GetRemotePeer"; |
| 411 OSError nativeGetError() native "Socket_GetError"; | 468 OSError nativeGetError() native "Socket_GetError"; |
| 412 bool nativeSetOption(int option, bool enabled) native "Socket_SetOption"; | 469 bool nativeSetOption(int option, bool enabled) native "Socket_SetOption"; |
| 413 | 470 |
| 414 static SendPort newServicePort() native "Socket_NewServicePort"; | 471 static SendPort newServicePort() native "Socket_NewServicePort"; |
| 415 } | 472 } |
| 416 | 473 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 489 } | 546 } |
| 490 | 547 |
| 491 | 548 |
| 492 class _RawSocket extends Stream<RawSocketEvent> | 549 class _RawSocket extends Stream<RawSocketEvent> |
| 493 implements RawSocket { | 550 implements RawSocket { |
| 494 final _NativeSocket _socket; | 551 final _NativeSocket _socket; |
| 495 StreamController<RawSocketEvent> _controller; | 552 StreamController<RawSocketEvent> _controller; |
| 496 bool _readEventsEnabled = true; | 553 bool _readEventsEnabled = true; |
| 497 bool _writeEventsEnabled = true; | 554 bool _writeEventsEnabled = true; |
| 498 | 555 |
| 499 static Future<RawSocket> connect(String host, int port) { | 556 static Future<RawSocket> connect(host, int port) { |
| 500 return _NativeSocket.connect(host, port) | 557 return _NativeSocket.connect(host, port) |
| 501 .then((socket) => new _RawSocket(socket)); | 558 .then((socket) => new _RawSocket(socket)); |
| 502 } | 559 } |
| 503 | 560 |
| 504 _RawSocket(this._socket) { | 561 _RawSocket(this._socket) { |
| 505 _controller = new StreamController( | 562 _controller = new StreamController( |
| 506 onListen: _onSubscriptionStateChange, | 563 onListen: _onSubscriptionStateChange, |
| 507 onCancel: _onSubscriptionStateChange, | 564 onCancel: _onSubscriptionStateChange, |
| 508 onPause: _onPauseStateChange, | 565 onPause: _onPauseStateChange, |
| 509 onResume: _onPauseStateChange); | 566 onResume: _onPauseStateChange); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 643 cancelOnError: cancelOnError); | 700 cancelOnError: cancelOnError); |
| 644 } | 701 } |
| 645 | 702 |
| 646 int get port => _socket.port; | 703 int get port => _socket.port; |
| 647 | 704 |
| 648 void close() => _socket.close(); | 705 void close() => _socket.close(); |
| 649 } | 706 } |
| 650 | 707 |
| 651 | 708 |
| 652 patch class Socket { | 709 patch class Socket { |
| 653 /* patch */ static Future<Socket> connect(String host, int port) { | 710 /* patch */ static Future<Socket> connect(host, int port) { |
| 654 return RawSocket.connect(host, port).then( | 711 return RawSocket.connect(host, port).then( |
| 655 (socket) => new _Socket(socket)); | 712 (socket) => new _Socket(socket)); |
| 656 } | 713 } |
| 657 } | 714 } |
| 658 | 715 |
| 659 | 716 |
| 660 patch class SecureSocket { | 717 patch class SecureSocket { |
| 661 /* patch */ factory SecureSocket._(RawSecureSocket rawSocket) => | 718 /* patch */ factory SecureSocket._(RawSecureSocket rawSocket) => |
| 662 new _SecureSocket(rawSocket); | 719 new _SecureSocket(rawSocket); |
| 663 } | 720 } |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 947 _raw.onBadCertificate = callback; | 1004 _raw.onBadCertificate = callback; |
| 948 } | 1005 } |
| 949 | 1006 |
| 950 X509Certificate get peerCertificate { | 1007 X509Certificate get peerCertificate { |
| 951 if (_raw == null) { | 1008 if (_raw == null) { |
| 952 throw new StateError("peerCertificate called on destroyed SecureSocket"); | 1009 throw new StateError("peerCertificate called on destroyed SecureSocket"); |
| 953 } | 1010 } |
| 954 return _raw.peerCertificate; | 1011 return _raw.peerCertificate; |
| 955 } | 1012 } |
| 956 } | 1013 } |
| OLD | NEW |