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); |
11 } | 11 } |
12 } | 12 } |
13 | 13 |
14 | 14 |
15 patch class RawSocket { | 15 patch class RawSocket { |
16 /* patch */ static Future<RawSocket> connect(host, int port) { | 16 /* patch */ static Future<RawSocket> connect( |
17 return _RawSocket.connect(host, port); | 17 host, int port, {sourceAddress}) { |
| 18 return _RawSocket.connect(host, port, sourceAddress); |
18 } | 19 } |
19 } | 20 } |
20 | 21 |
21 | 22 |
22 patch class InternetAddress { | 23 patch class InternetAddress { |
23 /* patch */ static InternetAddress get LOOPBACK_IP_V4 { | 24 /* patch */ static InternetAddress get LOOPBACK_IP_V4 { |
24 return _InternetAddress.LOOPBACK_IP_V4; | 25 return _InternetAddress.LOOPBACK_IP_V4; |
25 } | 26 } |
26 | 27 |
27 /* patch */ static InternetAddress get LOOPBACK_IP_V6 { | 28 /* patch */ static InternetAddress get LOOPBACK_IP_V6 { |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 map.putIfAbsent( | 375 map.putIfAbsent( |
375 name, () => new _NetworkInterface(name, index)); | 376 name, () => new _NetworkInterface(name, index)); |
376 map[name].addresses.add(address); | 377 map[name].addresses.add(address); |
377 return map; | 378 return map; |
378 }); | 379 }); |
379 return map.values.toList(); | 380 return map.values.toList(); |
380 } | 381 } |
381 }); | 382 }); |
382 } | 383 } |
383 | 384 |
384 static Future<_NativeSocket> connect(host, int port) { | 385 static Future<_NativeSocket> connect(host, int port, sourceAddress) { |
| 386 if (sourceAddress != null && sourceAddress is! _InternetAddress) { |
| 387 if (sourceAddress is String) { |
| 388 sourceAddress = new InternetAddress(sourceAddress); |
| 389 } |
| 390 } |
385 return new Future.value(host) | 391 return new Future.value(host) |
386 .then((host) { | 392 .then((host) { |
387 if (host is _InternetAddress) return [host]; | 393 if (host is _InternetAddress) return [host]; |
388 return lookup(host) | 394 return lookup(host) |
389 .then((addresses) { | 395 .then((addresses) { |
390 if (addresses.isEmpty) { | 396 if (addresses.isEmpty) { |
391 throw createError(response, "Failed host lookup: '$host'"); | 397 throw createError(response, "Failed host lookup: '$host'"); |
392 } | 398 } |
393 return addresses; | 399 return addresses; |
394 }); | 400 }); |
395 }) | 401 }) |
396 .then((addresses) { | 402 .then((addresses) { |
397 assert(addresses is List); | 403 assert(addresses is List); |
398 var completer = new Completer(); | 404 var completer = new Completer(); |
399 var it = addresses.iterator; | 405 var it = addresses.iterator; |
400 var error = null; | 406 var error = null; |
401 var connecting = new HashMap(); | 407 var connecting = new HashMap(); |
402 void connectNext() { | 408 void connectNext() { |
403 if (!it.moveNext()) { | 409 if (!it.moveNext()) { |
404 if (connecting.isEmpty) { | 410 if (connecting.isEmpty) { |
405 assert(error != null); | 411 assert(error != null); |
406 completer.completeError(error); | 412 completer.completeError(error); |
407 } | 413 } |
408 return; | 414 return; |
409 } | 415 } |
410 var address = it.current; | 416 var address = it.current; |
411 var socket = new _NativeSocket.normal(); | 417 var socket = new _NativeSocket.normal(); |
412 socket.address = address; | 418 socket.address = address; |
413 var result = socket.nativeCreateConnect(address._in_addr, port); | 419 var result; |
| 420 if (sourceAddress == null) { |
| 421 result = socket.nativeCreateConnect(address._in_addr, port); |
| 422 } else { |
| 423 assert(sourceAddress is _InternetAddress); |
| 424 result = socket.nativeCreateBindConnect( |
| 425 address._in_addr, port, sourceAddress._in_addr); |
| 426 } |
414 if (result is OSError) { | 427 if (result is OSError) { |
415 // Keep first error, if present. | 428 // Keep first error, if present. |
416 if (error == null) { | 429 if (error == null) { |
417 error = createError(result, "Connection failed", address, port); | 430 error = createError(result, "Connection failed", address, port); |
418 } | 431 } |
419 connectNext(); | 432 connectNext(); |
420 } else { | 433 } else { |
421 socket.port; // Query the local port, for error messages. | 434 socket.port; // Query the local port, for error messages. |
422 // Set up timer for when we should retry the next address | 435 // Set up timer for when we should retry the next address |
423 // (if any). | 436 // (if any). |
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1109 nativeAvailable() native "Socket_Available"; | 1122 nativeAvailable() native "Socket_Available"; |
1110 nativeRead(int len) native "Socket_Read"; | 1123 nativeRead(int len) native "Socket_Read"; |
1111 nativeRecvFrom() native "Socket_RecvFrom"; | 1124 nativeRecvFrom() native "Socket_RecvFrom"; |
1112 nativeWrite(List<int> buffer, int offset, int bytes) | 1125 nativeWrite(List<int> buffer, int offset, int bytes) |
1113 native "Socket_WriteList"; | 1126 native "Socket_WriteList"; |
1114 nativeSendTo(List<int> buffer, int offset, int bytes, | 1127 nativeSendTo(List<int> buffer, int offset, int bytes, |
1115 List<int> address, int port) | 1128 List<int> address, int port) |
1116 native "Socket_SendTo"; | 1129 native "Socket_SendTo"; |
1117 nativeCreateConnect(List<int> addr, | 1130 nativeCreateConnect(List<int> addr, |
1118 int port) native "Socket_CreateConnect"; | 1131 int port) native "Socket_CreateConnect"; |
| 1132 nativeCreateBindConnect( |
| 1133 List<int> addr, int port, List<int> sourceAddr) |
| 1134 native "Socket_CreateBindConnect"; |
1119 nativeCreateBindListen(List<int> addr, int port, int backlog, bool v6Only) | 1135 nativeCreateBindListen(List<int> addr, int port, int backlog, bool v6Only) |
1120 native "ServerSocket_CreateBindListen"; | 1136 native "ServerSocket_CreateBindListen"; |
1121 nativeCreateBindDatagram(List<int> addr, int port, bool reuseAddress) | 1137 nativeCreateBindDatagram(List<int> addr, int port, bool reuseAddress) |
1122 native "Socket_CreateBindDatagram"; | 1138 native "Socket_CreateBindDatagram"; |
1123 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept"; | 1139 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept"; |
1124 int nativeGetPort() native "Socket_GetPort"; | 1140 int nativeGetPort() native "Socket_GetPort"; |
1125 List nativeGetRemotePeer() native "Socket_GetRemotePeer"; | 1141 List nativeGetRemotePeer() native "Socket_GetRemotePeer"; |
1126 int nativeGetSocketId() native "Socket_GetSocketId"; | 1142 int nativeGetSocketId() native "Socket_GetSocketId"; |
1127 OSError nativeGetError() native "Socket_GetError"; | 1143 OSError nativeGetError() native "Socket_GetError"; |
1128 nativeGetOption(int option, int protocol) native "Socket_GetOption"; | 1144 nativeGetOption(int option, int protocol) native "Socket_GetOption"; |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1280 class _RawSocket extends Stream<RawSocketEvent> | 1296 class _RawSocket extends Stream<RawSocketEvent> |
1281 implements RawSocket { | 1297 implements RawSocket { |
1282 final _NativeSocket _socket; | 1298 final _NativeSocket _socket; |
1283 StreamController<RawSocketEvent> _controller; | 1299 StreamController<RawSocketEvent> _controller; |
1284 bool _readEventsEnabled = true; | 1300 bool _readEventsEnabled = true; |
1285 bool _writeEventsEnabled = true; | 1301 bool _writeEventsEnabled = true; |
1286 | 1302 |
1287 // Flag to handle Ctrl-D closing of stdio on Mac OS. | 1303 // Flag to handle Ctrl-D closing of stdio on Mac OS. |
1288 bool _isMacOSTerminalInput = false; | 1304 bool _isMacOSTerminalInput = false; |
1289 | 1305 |
1290 static Future<RawSocket> connect(host, int port) { | 1306 static Future<RawSocket> connect(host, int port, sourceAddress) { |
1291 return _NativeSocket.connect(host, port) | 1307 return _NativeSocket.connect(host, port, sourceAddress) |
1292 .then((socket) => new _RawSocket(socket)); | 1308 .then((socket) => new _RawSocket(socket)); |
1293 } | 1309 } |
1294 | 1310 |
1295 _RawSocket(this._socket) { | 1311 _RawSocket(this._socket) { |
1296 var zone = Zone.current; | 1312 var zone = Zone.current; |
1297 _controller = new StreamController(sync: true, | 1313 _controller = new StreamController(sync: true, |
1298 onListen: _onSubscriptionStateChange, | 1314 onListen: _onSubscriptionStateChange, |
1299 onCancel: _onSubscriptionStateChange, | 1315 onCancel: _onSubscriptionStateChange, |
1300 onPause: _onPauseStateChange, | 1316 onPause: _onPauseStateChange, |
1301 onResume: _onPauseStateChange); | 1317 onResume: _onPauseStateChange); |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1483 ServerSocketReference get reference { | 1499 ServerSocketReference get reference { |
1484 return new _ServerSocketReference(_socket.reference); | 1500 return new _ServerSocketReference(_socket.reference); |
1485 } | 1501 } |
1486 | 1502 |
1487 Map _toJSON(bool ref) => _socket._toJSON(ref); | 1503 Map _toJSON(bool ref) => _socket._toJSON(ref); |
1488 void set _owner(owner) { _socket._owner = owner; } | 1504 void set _owner(owner) { _socket._owner = owner; } |
1489 } | 1505 } |
1490 | 1506 |
1491 | 1507 |
1492 patch class Socket { | 1508 patch class Socket { |
1493 /* patch */ static Future<Socket> connect(host, int port) { | 1509 /* patch */ static Future<Socket> connect(host, int port, {sourceAddress}) { |
1494 return RawSocket.connect(host, port).then( | 1510 return RawSocket.connect(host, port, sourceAddress: sourceAddress).then( |
1495 (socket) => new _Socket(socket)); | 1511 (socket) => new _Socket(socket)); |
1496 } | 1512 } |
1497 } | 1513 } |
1498 | 1514 |
1499 | 1515 |
1500 class _SocketStreamConsumer extends StreamConsumer<List<int>> { | 1516 class _SocketStreamConsumer extends StreamConsumer<List<int>> { |
1501 StreamSubscription subscription; | 1517 StreamSubscription subscription; |
1502 final _Socket socket; | 1518 final _Socket socket; |
1503 int offset; | 1519 int offset; |
1504 List<int> buffer; | 1520 List<int> buffer; |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1933 String address, | 1949 String address, |
1934 List<int> in_addr, | 1950 List<int> in_addr, |
1935 int port) { | 1951 int port) { |
1936 return new Datagram( | 1952 return new Datagram( |
1937 data, | 1953 data, |
1938 new _InternetAddress(address, null, in_addr), | 1954 new _InternetAddress(address, null, in_addr), |
1939 port); | 1955 port); |
1940 } | 1956 } |
1941 | 1957 |
1942 String _socketsStats() => _SocketsObservatory.toJSON(); | 1958 String _socketsStats() => _SocketsObservatory.toJSON(); |
OLD | NEW |