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 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 bool readEventIssued = false; | 429 bool readEventIssued = false; |
430 | 430 |
431 bool sendWriteEvents = false; | 431 bool sendWriteEvents = false; |
432 bool writeEventIssued = false; | 432 bool writeEventIssued = false; |
433 bool writeAvailable = false; | 433 bool writeAvailable = false; |
434 | 434 |
435 // Statistics. | 435 // Statistics. |
436 int totalRead = 0; | 436 int totalRead = 0; |
437 int totalWritten = 0; | 437 int totalWritten = 0; |
438 | 438 |
| 439 // The owner object is the object that the Socket is being used by, e.g. |
| 440 // a HttpServer, a WebSocket connection, a process pipe, etc. |
| 441 Object owner; |
| 442 |
439 static Future<List<InternetAddress>> lookup( | 443 static Future<List<InternetAddress>> lookup( |
440 String host, {InternetAddressType type: InternetAddressType.ANY}) { | 444 String host, {InternetAddressType type: InternetAddressType.ANY}) { |
441 return _IOService.dispatch(_SOCKET_LOOKUP, [host, type._value]) | 445 return _IOService.dispatch(_SOCKET_LOOKUP, [host, type._value]) |
442 .then((response) { | 446 .then((response) { |
443 if (isErrorResponse(response)) { | 447 if (isErrorResponse(response)) { |
444 throw createError(response, "Failed host lookup: '$host'"); | 448 throw createError(response, "Failed host lookup: '$host'"); |
445 } else { | 449 } else { |
446 return response.skip(1).map((result) { | 450 return response.skip(1).map((result) { |
447 var type = new InternetAddressType._from(result[0]); | 451 var type = new InternetAddressType._from(result[0]); |
448 return new _InternetAddress(result[1], host, result[2]); | 452 return new _InternetAddress(result[1], host, result[2]); |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
953 eventPort = new RawReceivePort(multiplex); | 957 eventPort = new RawReceivePort(multiplex); |
954 _sockets[_serviceId] = this; | 958 _sockets[_serviceId] = this; |
955 } | 959 } |
956 } | 960 } |
957 | 961 |
958 void disconnectFromEventHandler() { | 962 void disconnectFromEventHandler() { |
959 assert(eventPort != null); | 963 assert(eventPort != null); |
960 eventPort.close(); | 964 eventPort.close(); |
961 eventPort = null; | 965 eventPort = null; |
962 _sockets.remove(_serviceId); | 966 _sockets.remove(_serviceId); |
| 967 // Now that we don't track this Socket anymore, we can clear the owner |
| 968 // field. |
| 969 owner = null; |
963 } | 970 } |
964 | 971 |
965 // Check whether this is an error response from a native port call. | 972 // Check whether this is an error response from a native port call. |
966 static bool isErrorResponse(response) { | 973 static bool isErrorResponse(response) { |
967 return response is List && response[0] != _SUCCESS_RESPONSE; | 974 return response is List && response[0] != _SUCCESS_RESPONSE; |
968 } | 975 } |
969 | 976 |
970 // Create the appropriate error/exception from different returned | 977 // Create the appropriate error/exception from different returned |
971 // error objects. | 978 // error objects. |
972 static createError(error, | 979 static createError(error, |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1072 'type': _serviceType(ref), | 1079 'type': _serviceType(ref), |
1073 'name': name, | 1080 'name': name, |
1074 'user_name': name, | 1081 'user_name': name, |
1075 }; | 1082 }; |
1076 if (ref) { | 1083 if (ref) { |
1077 return r; | 1084 return r; |
1078 } | 1085 } |
1079 r['port'] = port; | 1086 r['port'] = port; |
1080 r['address'] = address.host; | 1087 r['address'] = address.host; |
1081 r['fd'] = nativeGetSocketId(); | 1088 r['fd'] = nativeGetSocketId(); |
| 1089 if (owner != null) { |
| 1090 r['owner'] = owner._toJSON(true); |
| 1091 } |
1082 return r; | 1092 return r; |
1083 } | 1093 } |
1084 | 1094 |
1085 void nativeSetSocketId(int id) native "Socket_SetSocketId"; | 1095 void nativeSetSocketId(int id) native "Socket_SetSocketId"; |
1086 nativeAvailable() native "Socket_Available"; | 1096 nativeAvailable() native "Socket_Available"; |
1087 nativeRead(int len) native "Socket_Read"; | 1097 nativeRead(int len) native "Socket_Read"; |
1088 nativeRecvFrom() native "Socket_RecvFrom"; | 1098 nativeRecvFrom() native "Socket_RecvFrom"; |
1089 nativeWrite(List<int> buffer, int offset, int bytes) | 1099 nativeWrite(List<int> buffer, int offset, int bytes) |
1090 native "Socket_WriteList"; | 1100 native "Socket_WriteList"; |
1091 nativeSendTo(List<int> buffer, int offset, int bytes, | 1101 nativeSendTo(List<int> buffer, int offset, int bytes, |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1209 sendPort.send( | 1219 sendPort.send( |
1210 [_socket.nativeGetSocketId(), | 1220 [_socket.nativeGetSocketId(), |
1211 _socket.address, | 1221 _socket.address, |
1212 _socket.localPort]); | 1222 _socket.localPort]); |
1213 }); | 1223 }); |
1214 } | 1224 } |
1215 return new _RawServerSocketReference(_referencePort.sendPort); | 1225 return new _RawServerSocketReference(_referencePort.sendPort); |
1216 } | 1226 } |
1217 | 1227 |
1218 Map _toJSON(bool ref) => _socket._toJSON(ref); | 1228 Map _toJSON(bool ref) => _socket._toJSON(ref); |
| 1229 void set _owner(owner) { _socket.owner = owner; } |
1219 } | 1230 } |
1220 | 1231 |
1221 | 1232 |
1222 class _RawServerSocketReference implements RawServerSocketReference { | 1233 class _RawServerSocketReference implements RawServerSocketReference { |
1223 final SendPort _sendPort; | 1234 final SendPort _sendPort; |
1224 | 1235 |
1225 _RawServerSocketReference(this._sendPort); | 1236 _RawServerSocketReference(this._sendPort); |
1226 | 1237 |
1227 Future<RawServerSocket> create() { | 1238 Future<RawServerSocket> create() { |
1228 var port = new ReceivePort(); | 1239 var port = new ReceivePort(); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1384 | 1395 |
1385 void _onSubscriptionStateChange() { | 1396 void _onSubscriptionStateChange() { |
1386 if (_controller.hasListener) { | 1397 if (_controller.hasListener) { |
1387 _resume(); | 1398 _resume(); |
1388 } else { | 1399 } else { |
1389 _socket.close(); | 1400 _socket.close(); |
1390 } | 1401 } |
1391 } | 1402 } |
1392 | 1403 |
1393 Map _toJSON(bool ref) => _socket._toJSON(ref); | 1404 Map _toJSON(bool ref) => _socket._toJSON(ref); |
| 1405 void set _owner(owner) { _socket.owner = owner; } |
1394 } | 1406 } |
1395 | 1407 |
1396 | 1408 |
1397 patch class ServerSocket { | 1409 patch class ServerSocket { |
1398 /* patch */ static Future<ServerSocket> bind(address, | 1410 /* patch */ static Future<ServerSocket> bind(address, |
1399 int port, | 1411 int port, |
1400 {int backlog: 0, | 1412 {int backlog: 0, |
1401 bool v6Only: false}) { | 1413 bool v6Only: false}) { |
1402 return _ServerSocket.bind(address, port, backlog, v6Only); | 1414 return _ServerSocket.bind(address, port, backlog, v6Only); |
1403 } | 1415 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1444 | 1456 |
1445 InternetAddress get address => _socket.address; | 1457 InternetAddress get address => _socket.address; |
1446 | 1458 |
1447 Future close() => _socket.close().then((_) => this); | 1459 Future close() => _socket.close().then((_) => this); |
1448 | 1460 |
1449 ServerSocketReference get reference { | 1461 ServerSocketReference get reference { |
1450 return new _ServerSocketReference(_socket.reference); | 1462 return new _ServerSocketReference(_socket.reference); |
1451 } | 1463 } |
1452 | 1464 |
1453 Map _toJSON(bool ref) => _socket._toJSON(ref); | 1465 Map _toJSON(bool ref) => _socket._toJSON(ref); |
| 1466 void set _owner(owner) { _socket._owner = owner; } |
1454 } | 1467 } |
1455 | 1468 |
1456 | 1469 |
1457 patch class Socket { | 1470 patch class Socket { |
1458 /* patch */ static Future<Socket> connect(host, int port) { | 1471 /* patch */ static Future<Socket> connect(host, int port) { |
1459 return RawSocket.connect(host, port).then( | 1472 return RawSocket.connect(host, port).then( |
1460 (socket) => new _Socket(socket)); | 1473 (socket) => new _Socket(socket)); |
1461 } | 1474 } |
1462 } | 1475 } |
1463 | 1476 |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1742 _detachReady.complete(null); | 1755 _detachReady.complete(null); |
1743 } else { | 1756 } else { |
1744 if (_raw != null) { | 1757 if (_raw != null) { |
1745 _raw.shutdown(SocketDirection.SEND); | 1758 _raw.shutdown(SocketDirection.SEND); |
1746 _disableWriteEvent(); | 1759 _disableWriteEvent(); |
1747 } | 1760 } |
1748 } | 1761 } |
1749 } | 1762 } |
1750 | 1763 |
1751 Map _toJSON(bool ref) => _raw._toJSON(ref); | 1764 Map _toJSON(bool ref) => _raw._toJSON(ref); |
| 1765 void set _owner(owner) { _raw._owner = owner; } |
1752 } | 1766 } |
1753 | 1767 |
1754 | 1768 |
1755 patch class RawDatagramSocket { | 1769 patch class RawDatagramSocket { |
1756 /* patch */ static Future<RawDatagramSocket> bind( | 1770 /* patch */ static Future<RawDatagramSocket> bind( |
1757 host, int port, {bool reuseAddress: true}) { | 1771 host, int port, {bool reuseAddress: true}) { |
1758 return _RawDatagramSocket.bind(host, port, reuseAddress); | 1772 return _RawDatagramSocket.bind(host, port, reuseAddress); |
1759 } | 1773 } |
1760 } | 1774 } |
1761 | 1775 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1897 String address, | 1911 String address, |
1898 List<int> in_addr, | 1912 List<int> in_addr, |
1899 int port) { | 1913 int port) { |
1900 return new Datagram( | 1914 return new Datagram( |
1901 data, | 1915 data, |
1902 new _InternetAddress(address, null, in_addr), | 1916 new _InternetAddress(address, null, in_addr), |
1903 port); | 1917 port); |
1904 } | 1918 } |
1905 | 1919 |
1906 String _socketsStats() => _SocketsObservatory.toJSON(); | 1920 String _socketsStats() => _SocketsObservatory.toJSON(); |
OLD | NEW |