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 bool shared: false}) { |
| 11 return _RawServerSocket.bind(address, port, backlog, v6Only, shared); |
11 } | 12 } |
12 } | 13 } |
13 | 14 |
14 | 15 |
15 patch class RawSocket { | 16 patch class RawSocket { |
16 /* patch */ static Future<RawSocket> connect(host, int port) { | 17 /* patch */ static Future<RawSocket> connect(host, int port) { |
17 return _RawSocket.connect(host, port); | 18 return _RawSocket.connect(host, port); |
18 } | 19 } |
19 } | 20 } |
20 | 21 |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 } | 453 } |
453 } | 454 } |
454 connectNext(); | 455 connectNext(); |
455 return completer.future; | 456 return completer.future; |
456 }); | 457 }); |
457 } | 458 } |
458 | 459 |
459 static Future<_NativeSocket> bind(host, | 460 static Future<_NativeSocket> bind(host, |
460 int port, | 461 int port, |
461 int backlog, | 462 int backlog, |
462 bool v6Only) { | 463 bool v6Only, |
| 464 bool shared) { |
463 return new Future.value(host) | 465 return new Future.value(host) |
464 .then((host) { | 466 .then((host) { |
465 if (host is _InternetAddress) return host; | 467 if (host is _InternetAddress) return host; |
466 return lookup(host) | 468 return lookup(host) |
467 .then((list) { | 469 .then((list) { |
468 if (list.length == 0) { | 470 if (list.length == 0) { |
469 throw createError(response, "Failed host lookup: '$host'"); | 471 throw createError(response, "Failed host lookup: '$host'"); |
470 } | 472 } |
471 return list[0]; | 473 return list[0]; |
472 }); | 474 }); |
473 }) | 475 }) |
474 .then((address) { | 476 .then((address) { |
475 var socket = new _NativeSocket.listen(); | 477 var socket = new _NativeSocket.listen(); |
476 socket.address = address; | 478 socket.address = address; |
| 479 |
477 var result = socket.nativeCreateBindListen(address._in_addr, | 480 var result = socket.nativeCreateBindListen(address._in_addr, |
478 port, | 481 port, |
479 backlog, | 482 backlog, |
480 v6Only); | 483 v6Only, |
| 484 shared); |
481 if (result is OSError) { | 485 if (result is OSError) { |
482 throw new SocketException("Failed to create server socket", | 486 throw new SocketException("Failed to create server socket", |
483 osError: result, | 487 osError: result, |
484 address: address, | 488 address: address, |
485 port: port); | 489 port: port); |
486 } | 490 } |
487 if (port != 0) socket.localPort = port; | 491 if (port != 0) socket.localPort = port; |
488 socket.connectToEventHandler(); | 492 socket.connectToEventHandler(); |
489 return socket; | 493 return socket; |
490 }); | 494 }); |
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1106 nativeAvailable() native "Socket_Available"; | 1110 nativeAvailable() native "Socket_Available"; |
1107 nativeRead(int len) native "Socket_Read"; | 1111 nativeRead(int len) native "Socket_Read"; |
1108 nativeRecvFrom() native "Socket_RecvFrom"; | 1112 nativeRecvFrom() native "Socket_RecvFrom"; |
1109 nativeWrite(List<int> buffer, int offset, int bytes) | 1113 nativeWrite(List<int> buffer, int offset, int bytes) |
1110 native "Socket_WriteList"; | 1114 native "Socket_WriteList"; |
1111 nativeSendTo(List<int> buffer, int offset, int bytes, | 1115 nativeSendTo(List<int> buffer, int offset, int bytes, |
1112 List<int> address, int port) | 1116 List<int> address, int port) |
1113 native "Socket_SendTo"; | 1117 native "Socket_SendTo"; |
1114 nativeCreateConnect(List<int> addr, | 1118 nativeCreateConnect(List<int> addr, |
1115 int port) native "Socket_CreateConnect"; | 1119 int port) native "Socket_CreateConnect"; |
1116 nativeCreateBindListen(List<int> addr, int port, int backlog, bool v6Only) | 1120 nativeCreateBindListen(List<int> addr, int port, int backlog, bool v6Only, |
| 1121 bool shared) |
1117 native "ServerSocket_CreateBindListen"; | 1122 native "ServerSocket_CreateBindListen"; |
1118 nativeCreateBindDatagram(List<int> addr, int port, bool reuseAddress) | 1123 nativeCreateBindDatagram(List<int> addr, int port, bool reuseAddress) |
1119 native "Socket_CreateBindDatagram"; | 1124 native "Socket_CreateBindDatagram"; |
1120 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept"; | 1125 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept"; |
1121 int nativeGetPort() native "Socket_GetPort"; | 1126 int nativeGetPort() native "Socket_GetPort"; |
1122 List nativeGetRemotePeer() native "Socket_GetRemotePeer"; | 1127 List nativeGetRemotePeer() native "Socket_GetRemotePeer"; |
1123 int nativeGetSocketId() native "Socket_GetSocketId"; | 1128 int nativeGetSocketId() native "Socket_GetSocketId"; |
1124 OSError nativeGetError() native "Socket_GetError"; | 1129 OSError nativeGetError() native "Socket_GetError"; |
1125 nativeGetOption(int option, int protocol) native "Socket_GetOption"; | 1130 nativeGetOption(int option, int protocol) native "Socket_GetOption"; |
1126 bool nativeSetOption(int option, int protocol, value) | 1131 bool nativeSetOption(int option, int protocol, value) |
1127 native "Socket_SetOption"; | 1132 native "Socket_SetOption"; |
1128 bool nativeJoinMulticast( | 1133 bool nativeJoinMulticast( |
1129 List<int> addr, List<int> interfaceAddr, int interfaceIndex) | 1134 List<int> addr, List<int> interfaceAddr, int interfaceIndex) |
1130 native "Socket_JoinMulticast"; | 1135 native "Socket_JoinMulticast"; |
1131 bool nativeLeaveMulticast( | 1136 bool nativeLeaveMulticast( |
1132 List<int> addr, List<int> interfaceAddr, int interfaceIndex) | 1137 List<int> addr, List<int> interfaceAddr, int interfaceIndex) |
1133 native "Socket_LeaveMulticast"; | 1138 native "Socket_LeaveMulticast"; |
| 1139 bool _nativeMarkSocketAsSharedHack() |
| 1140 native "Socket_MarkSocketAsSharedHack"; |
1134 } | 1141 } |
1135 | 1142 |
1136 | 1143 |
1137 class _RawServerSocket extends Stream<RawSocket> | 1144 class _RawServerSocket extends Stream<RawSocket> |
1138 implements RawServerSocket { | 1145 implements RawServerSocket { |
1139 final _NativeSocket _socket; | 1146 final _NativeSocket _socket; |
1140 StreamController<RawSocket> _controller; | 1147 StreamController<RawSocket> _controller; |
1141 ReceivePort _referencePort; | 1148 ReceivePort _referencePort; |
| 1149 bool _v6Only; |
1142 | 1150 |
1143 static Future<_RawServerSocket> bind(address, | 1151 static Future<_RawServerSocket> bind(address, |
1144 int port, | 1152 int port, |
1145 int backlog, | 1153 int backlog, |
1146 bool v6Only) { | 1154 bool v6Only, |
| 1155 bool shared) { |
1147 if (port < 0 || port > 0xFFFF) | 1156 if (port < 0 || port > 0xFFFF) |
1148 throw new ArgumentError("Invalid port $port"); | 1157 throw new ArgumentError("Invalid port $port"); |
1149 if (backlog < 0) throw new ArgumentError("Invalid backlog $backlog"); | 1158 if (backlog < 0) throw new ArgumentError("Invalid backlog $backlog"); |
1150 return _NativeSocket.bind(address, port, backlog, v6Only) | 1159 return _NativeSocket.bind(address, port, backlog, v6Only, shared) |
1151 .then((socket) => new _RawServerSocket(socket)); | 1160 .then((socket) => new _RawServerSocket(socket, v6Only)); |
1152 } | 1161 } |
1153 | 1162 |
1154 _RawServerSocket(this._socket); | 1163 _RawServerSocket(this._socket, this._v6Only); |
1155 | 1164 |
1156 StreamSubscription<RawSocket> listen(void onData(RawSocket event), | 1165 StreamSubscription<RawSocket> listen(void onData(RawSocket event), |
1157 {Function onError, | 1166 {Function onError, |
1158 void onDone(), | 1167 void onDone(), |
1159 bool cancelOnError}) { | 1168 bool cancelOnError}) { |
1160 if (_controller != null) { | 1169 if (_controller != null) { |
1161 throw new StateError("Stream was already listened to"); | 1170 throw new StateError("Stream was already listened to"); |
1162 } | 1171 } |
1163 var zone = Zone.current; | 1172 var zone = Zone.current; |
1164 _controller = new StreamController(sync: true, | 1173 _controller = new StreamController(sync: true, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1226 void _onPauseStateChange() { | 1235 void _onPauseStateChange() { |
1227 if (_controller.isPaused) { | 1236 if (_controller.isPaused) { |
1228 _pause(); | 1237 _pause(); |
1229 } else { | 1238 } else { |
1230 _resume(); | 1239 _resume(); |
1231 } | 1240 } |
1232 } | 1241 } |
1233 | 1242 |
1234 RawServerSocketReference get reference { | 1243 RawServerSocketReference get reference { |
1235 if (_referencePort == null) { | 1244 if (_referencePort == null) { |
| 1245 bool successfull = _socket._nativeMarkSocketAsSharedHack(); |
1236 _referencePort = new ReceivePort(); | 1246 _referencePort = new ReceivePort(); |
1237 _referencePort.listen((sendPort) { | 1247 _referencePort.listen((sendPort) { |
1238 sendPort.send( | 1248 sendPort.send( |
1239 [_socket.nativeGetSocketId(), | 1249 [_socket.address, |
1240 _socket.address, | 1250 _socket.port, |
1241 _socket.localPort]); | 1251 _v6Only]); |
1242 }); | 1252 }); |
1243 } | 1253 } |
1244 return new _RawServerSocketReference(_referencePort.sendPort); | 1254 return new _RawServerSocketReference(_referencePort.sendPort); |
1245 } | 1255 } |
1246 | 1256 |
1247 Map _toJSON(bool ref) => _socket._toJSON(ref); | 1257 Map _toJSON(bool ref) => _socket._toJSON(ref); |
| 1258 |
1248 void set _owner(owner) { _socket.owner = owner; } | 1259 void set _owner(owner) { _socket.owner = owner; } |
1249 } | 1260 } |
1250 | 1261 |
1251 | 1262 |
1252 class _RawServerSocketReference implements RawServerSocketReference { | 1263 class _RawServerSocketReference implements RawServerSocketReference { |
1253 final SendPort _sendPort; | 1264 final SendPort _sendPort; |
1254 | 1265 |
1255 _RawServerSocketReference(this._sendPort); | 1266 _RawServerSocketReference(this._sendPort); |
1256 | 1267 |
1257 Future<RawServerSocket> create() { | 1268 Future<RawServerSocket> create() { |
1258 var port = new ReceivePort(); | 1269 var port = new ReceivePort(); |
1259 _sendPort.send(port.sendPort); | 1270 _sendPort.send(port.sendPort); |
1260 return port.first.then((args) { | 1271 return port.first.then((List args) { |
1261 port.close(); | 1272 port.close(); |
1262 var native = new _NativeSocket.listen(); | 1273 |
1263 native.nativeSetSocketId(args[0]); | 1274 InternetAddress address = args[0]; |
1264 native.address = args[1]; | 1275 int tcpPort = args[1]; |
1265 native.localPort = args[2]; | 1276 bool v6Only = args[2]; |
1266 return new _RawServerSocket(native); | 1277 return |
| 1278 RawServerSocket.bind(address, tcpPort, v6Only: v6Only, shared: true); |
1267 }); | 1279 }); |
1268 } | 1280 } |
1269 | 1281 |
1270 int get hashCode => _sendPort.hashCode; | 1282 int get hashCode => _sendPort.hashCode; |
1271 | 1283 |
1272 bool operator==(Object other) | 1284 bool operator==(Object other) |
1273 => other is _RawServerSocketReference && _sendPort == other._sendPort; | 1285 => other is _RawServerSocketReference && _sendPort == other._sendPort; |
1274 } | 1286 } |
1275 | 1287 |
1276 | 1288 |
1277 class _RawSocket extends Stream<RawSocketEvent> | 1289 class _RawSocket extends Stream<RawSocketEvent> |
1278 implements RawSocket { | 1290 implements RawSocket { |
1279 final _NativeSocket _socket; | 1291 final _NativeSocket _socket; |
1280 StreamController<RawSocketEvent> _controller; | 1292 StreamController<RawSocketEvent> _controller; |
1281 bool _readEventsEnabled = true; | 1293 bool _readEventsEnabled = true; |
1282 bool _writeEventsEnabled = true; | 1294 bool _writeEventsEnabled = true; |
1283 | 1295 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1422 | 1434 |
1423 Map _toJSON(bool ref) => _socket._toJSON(ref); | 1435 Map _toJSON(bool ref) => _socket._toJSON(ref); |
1424 void set _owner(owner) { _socket.owner = owner; } | 1436 void set _owner(owner) { _socket.owner = owner; } |
1425 } | 1437 } |
1426 | 1438 |
1427 | 1439 |
1428 patch class ServerSocket { | 1440 patch class ServerSocket { |
1429 /* patch */ static Future<ServerSocket> bind(address, | 1441 /* patch */ static Future<ServerSocket> bind(address, |
1430 int port, | 1442 int port, |
1431 {int backlog: 0, | 1443 {int backlog: 0, |
1432 bool v6Only: false}) { | 1444 bool v6Only: false, |
1433 return _ServerSocket.bind(address, port, backlog, v6Only); | 1445 bool shared: false}) { |
| 1446 return _ServerSocket.bind(address, port, backlog, v6Only, shared); |
1434 } | 1447 } |
1435 } | 1448 } |
1436 | 1449 |
1437 | 1450 |
1438 class _ServerSocketReference implements ServerSocketReference { | 1451 class _ServerSocketReference implements ServerSocketReference { |
1439 final RawServerSocketReference _rawReference; | 1452 final RawServerSocketReference _rawReference; |
1440 | 1453 |
1441 _ServerSocketReference(this._rawReference); | 1454 _ServerSocketReference(this._rawReference); |
1442 | 1455 |
1443 Future<ServerSocket> create() { | 1456 Future<ServerSocket> create() { |
1444 return _rawReference.create().then((raw) => new _ServerSocket(raw)); | 1457 return _rawReference.create().then((raw) => new _ServerSocket(raw)); |
1445 } | 1458 } |
1446 } | 1459 } |
1447 | 1460 |
1448 | 1461 |
1449 class _ServerSocket extends Stream<Socket> | 1462 class _ServerSocket extends Stream<Socket> |
1450 implements ServerSocket { | 1463 implements ServerSocket { |
1451 final _socket; | 1464 final _socket; |
1452 | 1465 |
1453 static Future<_ServerSocket> bind(address, | 1466 static Future<_ServerSocket> bind(address, |
1454 int port, | 1467 int port, |
1455 int backlog, | 1468 int backlog, |
1456 bool v6Only) { | 1469 bool v6Only, |
1457 return _RawServerSocket.bind(address, port, backlog, v6Only) | 1470 bool shared) { |
| 1471 return _RawServerSocket.bind(address, port, backlog, v6Only, shared) |
1458 .then((socket) => new _ServerSocket(socket)); | 1472 .then((socket) => new _ServerSocket(socket)); |
1459 } | 1473 } |
1460 | 1474 |
1461 _ServerSocket(this._socket); | 1475 _ServerSocket(this._socket); |
1462 | 1476 |
1463 StreamSubscription<Socket> listen(void onData(Socket event), | 1477 StreamSubscription<Socket> listen(void onData(Socket event), |
1464 {Function onError, | 1478 {Function onError, |
1465 void onDone(), | 1479 void onDone(), |
1466 bool cancelOnError}) { | 1480 bool cancelOnError}) { |
1467 return _socket.map((rawSocket) => new _Socket(rawSocket)).listen( | 1481 return _socket.map((rawSocket) => new _Socket(rawSocket)).listen( |
1468 onData, | 1482 onData, |
1469 onError: onError, | 1483 onError: onError, |
1470 onDone: onDone, | 1484 onDone: onDone, |
1471 cancelOnError: cancelOnError); | 1485 cancelOnError: cancelOnError); |
1472 } | 1486 } |
1473 | 1487 |
1474 int get port => _socket.port; | 1488 int get port => _socket.port; |
1475 | 1489 |
1476 InternetAddress get address => _socket.address; | 1490 InternetAddress get address => _socket.address; |
1477 | 1491 |
1478 Future close() => _socket.close().then((_) => this); | 1492 Future close() => _socket.close().then((_) => this); |
1479 | 1493 |
1480 ServerSocketReference get reference { | 1494 ServerSocketReference get reference { |
1481 return new _ServerSocketReference(_socket.reference); | 1495 return new _ServerSocketReference(_socket.reference); |
1482 } | 1496 } |
1483 | 1497 |
1484 Map _toJSON(bool ref) => _socket._toJSON(ref); | 1498 Map _toJSON(bool ref) => _socket._toJSON(ref); |
| 1499 |
1485 void set _owner(owner) { _socket._owner = owner; } | 1500 void set _owner(owner) { _socket._owner = owner; } |
1486 } | 1501 } |
1487 | 1502 |
1488 | 1503 |
1489 patch class Socket { | 1504 patch class Socket { |
1490 /* patch */ static Future<Socket> connect(host, int port) { | 1505 /* patch */ static Future<Socket> connect(host, int port) { |
1491 return RawSocket.connect(host, port).then( | 1506 return RawSocket.connect(host, port).then( |
1492 (socket) => new _Socket(socket)); | 1507 (socket) => new _Socket(socket)); |
1493 } | 1508 } |
1494 } | 1509 } |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1930 String address, | 1945 String address, |
1931 List<int> in_addr, | 1946 List<int> in_addr, |
1932 int port) { | 1947 int port) { |
1933 return new Datagram( | 1948 return new Datagram( |
1934 data, | 1949 data, |
1935 new _InternetAddress(address, null, in_addr), | 1950 new _InternetAddress(address, null, in_addr), |
1936 port); | 1951 port); |
1937 } | 1952 } |
1938 | 1953 |
1939 String _socketsStats() => _SocketsObservatory.toJSON(); | 1954 String _socketsStats() => _SocketsObservatory.toJSON(); |
OLD | NEW |