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