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 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 static String toJSON() { | 325 static String toJSON() { |
326 try { | 326 try { |
327 return generateResponse(); | 327 return generateResponse(); |
328 } catch (e, s) { | 328 } catch (e, s) { |
329 return '{"type":"Error","text":"$e","stacktrace":"$s"}'; | 329 return '{"type":"Error","text":"$e","stacktrace":"$s"}'; |
330 } | 330 } |
331 } | 331 } |
332 } | 332 } |
333 | 333 |
334 | 334 |
| 335 // The NativeFieldWrapperClass1 can not be used with a mixin, due to missing |
| 336 // implicit constructor. |
| 337 class _NativeSocketNativeWrapper extends NativeFieldWrapperClass1 {} |
| 338 |
| 339 |
335 // The _NativeSocket class encapsulates an OS socket. | 340 // The _NativeSocket class encapsulates an OS socket. |
336 class _NativeSocket extends NativeFieldWrapperClass1 { | 341 class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject { |
337 // Bit flags used when communicating between the eventhandler and | 342 // Bit flags used when communicating between the eventhandler and |
338 // dart code. The EVENT flags are used to indicate events of | 343 // dart code. The EVENT flags are used to indicate events of |
339 // interest when sending a message from dart code to the | 344 // interest when sending a message from dart code to the |
340 // eventhandler. When receiving a message from the eventhandler the | 345 // eventhandler. When receiving a message from the eventhandler the |
341 // EVENT flags indicate the events that actually happened. The | 346 // EVENT flags indicate the events that actually happened. The |
342 // COMMAND flags are used to send commands from dart to the | 347 // COMMAND flags are used to send commands from dart to the |
343 // eventhandler. COMMAND flags are never received from the | 348 // eventhandler. COMMAND flags are never received from the |
344 // eventhandler. Additional flags are used to communicate other | 349 // eventhandler. Additional flags are used to communicate other |
345 // information. | 350 // information. |
346 static const int READ_EVENT = 0; | 351 static const int READ_EVENT = 0; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 static const LIST_INTERFACES = 1; | 391 static const LIST_INTERFACES = 1; |
387 static const REVERSE_LOOKUP = 2; | 392 static const REVERSE_LOOKUP = 2; |
388 | 393 |
389 // Protocol flags. | 394 // Protocol flags. |
390 static const int PROTOCOL_IPV4 = 1 << 0; | 395 static const int PROTOCOL_IPV4 = 1 << 0; |
391 static const int PROTOCOL_IPV6 = 1 << 1; | 396 static const int PROTOCOL_IPV6 = 1 << 1; |
392 | 397 |
393 static const int NORMAL_TOKEN_BATCH_SIZE = 8; | 398 static const int NORMAL_TOKEN_BATCH_SIZE = 8; |
394 static const int LISTENING_TOKEN_BATCH_SIZE = 2; | 399 static const int LISTENING_TOKEN_BATCH_SIZE = 2; |
395 | 400 |
| 401 // Use default Map so we keep order. |
| 402 static Map<int, _NativeSocket> _sockets = new Map<int, _NativeSocket>(); |
| 403 |
396 // Socket close state | 404 // Socket close state |
397 bool isClosed = false; | 405 bool isClosed = false; |
398 bool isClosing = false; | 406 bool isClosing = false; |
399 bool isClosedRead = false; | 407 bool isClosedRead = false; |
400 bool isClosedWrite = false; | 408 bool isClosedWrite = false; |
401 Completer closeCompleter = new Completer.sync(); | 409 Completer closeCompleter = new Completer.sync(); |
402 | 410 |
403 // Handlers and receive port for socket events from the event handler. | 411 // Handlers and receive port for socket events from the event handler. |
404 final List eventHandlers = new List(EVENT_COUNT + 1); | 412 final List eventHandlers = new List(EVENT_COUNT + 1); |
405 RawReceivePort eventPort; | 413 RawReceivePort eventPort; |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 if (port != 0) socket.localPort = port; | 601 if (port != 0) socket.localPort = port; |
594 return socket; | 602 return socket; |
595 }); | 603 }); |
596 } | 604 } |
597 | 605 |
598 _NativeSocket.datagram(this.address) | 606 _NativeSocket.datagram(this.address) |
599 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_UDP_SOCKET; | 607 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_UDP_SOCKET; |
600 | 608 |
601 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET | TYPE_TCP_SOCKET; | 609 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET | TYPE_TCP_SOCKET; |
602 | 610 |
603 _NativeSocket.listen() : typeFlags = TYPE_LISTENING_SOCKET | TYPE_TCP_SOCKET; | 611 _NativeSocket.listen() : super(), typeFlags = TYPE_LISTENING_SOCKET | TYPE_TCP
_SOCKET; |
604 | 612 |
605 _NativeSocket.pipe() : typeFlags = TYPE_PIPE; | 613 _NativeSocket.pipe() : typeFlags = TYPE_PIPE; |
606 | 614 |
607 _NativeSocket.watch(int id) | 615 _NativeSocket.watch(int id) |
608 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_INTERNAL_SOCKET { | 616 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_INTERNAL_SOCKET { |
609 isClosedWrite = true; | 617 isClosedWrite = true; |
610 nativeSetSocketId(id); | 618 nativeSetSocketId(id); |
611 } | 619 } |
612 | 620 |
613 bool get isListening => (typeFlags & TYPE_LISTENING_SOCKET) != 0; | 621 bool get isListening => (typeFlags & TYPE_LISTENING_SOCKET) != 0; |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 void sendToEventHandler(int data) { | 944 void sendToEventHandler(int data) { |
937 assert(!isClosing); | 945 assert(!isClosing); |
938 connectToEventHandler(); | 946 connectToEventHandler(); |
939 _EventHandler._sendData(this, eventPort, data); | 947 _EventHandler._sendData(this, eventPort, data); |
940 } | 948 } |
941 | 949 |
942 void connectToEventHandler() { | 950 void connectToEventHandler() { |
943 assert(!isClosed); | 951 assert(!isClosed); |
944 if (eventPort == null) { | 952 if (eventPort == null) { |
945 eventPort = new RawReceivePort(multiplex); | 953 eventPort = new RawReceivePort(multiplex); |
946 _SocketsObservatory.add(this); | 954 _sockets[_serviceId] = this; |
947 } | 955 } |
948 } | 956 } |
949 | 957 |
950 void disconnectFromEventHandler() { | 958 void disconnectFromEventHandler() { |
951 assert(eventPort != null); | 959 assert(eventPort != null); |
952 eventPort.close(); | 960 eventPort.close(); |
953 eventPort = null; | 961 eventPort = null; |
954 _SocketsObservatory.remove(this); | 962 _sockets.remove(_serviceId); |
955 } | 963 } |
956 | 964 |
957 // Check whether this is an error response from a native port call. | 965 // Check whether this is an error response from a native port call. |
958 static bool isErrorResponse(response) { | 966 static bool isErrorResponse(response) { |
959 return response is List && response[0] != _SUCCESS_RESPONSE; | 967 return response is List && response[0] != _SUCCESS_RESPONSE; |
960 } | 968 } |
961 | 969 |
962 // Create the appropriate error/exception from different returned | 970 // Create the appropriate error/exception from different returned |
963 // error objects. | 971 // error objects. |
964 static createError(error, | 972 static createError(error, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1046 void leaveMulticast(InternetAddress addr, NetworkInterface interface) { | 1054 void leaveMulticast(InternetAddress addr, NetworkInterface interface) { |
1047 var interfaceAddr = multicastAddress(addr, interface); | 1055 var interfaceAddr = multicastAddress(addr, interface); |
1048 var interfaceIndex = interface == null ? 0 : interface.index; | 1056 var interfaceIndex = interface == null ? 0 : interface.index; |
1049 var result = nativeLeaveMulticast( | 1057 var result = nativeLeaveMulticast( |
1050 addr._in_addr, | 1058 addr._in_addr, |
1051 interfaceAddr == null ? null : interfaceAddr._in_addr, | 1059 interfaceAddr == null ? null : interfaceAddr._in_addr, |
1052 interfaceIndex); | 1060 interfaceIndex); |
1053 if (result is OSError) throw result; | 1061 if (result is OSError) throw result; |
1054 } | 1062 } |
1055 | 1063 |
| 1064 String get _serviceTypePath => 'io/socket'; |
| 1065 String get _serviceTypeName => 'Socket'; |
| 1066 |
| 1067 Map _toJSON(bool ref) { |
| 1068 var r = { |
| 1069 'id': _servicePath, |
| 1070 'type': _serviceType(ref), |
| 1071 'name': '${address.host}:$port', |
| 1072 'user_name': '${address.host}:$port', |
| 1073 }; |
| 1074 if (ref) { |
| 1075 return r; |
| 1076 } |
| 1077 r['port'] = port; |
| 1078 r['address'] = address.host; |
| 1079 return r; |
| 1080 } |
| 1081 |
1056 void nativeSetSocketId(int id) native "Socket_SetSocketId"; | 1082 void nativeSetSocketId(int id) native "Socket_SetSocketId"; |
1057 nativeAvailable() native "Socket_Available"; | 1083 nativeAvailable() native "Socket_Available"; |
1058 nativeRead(int len) native "Socket_Read"; | 1084 nativeRead(int len) native "Socket_Read"; |
1059 nativeRecvFrom() native "Socket_RecvFrom"; | 1085 nativeRecvFrom() native "Socket_RecvFrom"; |
1060 nativeWrite(List<int> buffer, int offset, int bytes) | 1086 nativeWrite(List<int> buffer, int offset, int bytes) |
1061 native "Socket_WriteList"; | 1087 native "Socket_WriteList"; |
1062 nativeSendTo(List<int> buffer, int offset, int bytes, | 1088 nativeSendTo(List<int> buffer, int offset, int bytes, |
1063 List<int> address, int port) | 1089 List<int> address, int port) |
1064 native "Socket_SendTo"; | 1090 native "Socket_SendTo"; |
1065 nativeCreateConnect(List<int> addr, | 1091 nativeCreateConnect(List<int> addr, |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1178 _referencePort = new ReceivePort(); | 1204 _referencePort = new ReceivePort(); |
1179 _referencePort.listen((sendPort) { | 1205 _referencePort.listen((sendPort) { |
1180 sendPort.send( | 1206 sendPort.send( |
1181 [_socket.nativeGetSocketId(), | 1207 [_socket.nativeGetSocketId(), |
1182 _socket.address, | 1208 _socket.address, |
1183 _socket.localPort]); | 1209 _socket.localPort]); |
1184 }); | 1210 }); |
1185 } | 1211 } |
1186 return new _RawServerSocketReference(_referencePort.sendPort); | 1212 return new _RawServerSocketReference(_referencePort.sendPort); |
1187 } | 1213 } |
| 1214 |
| 1215 Map _toJSON(bool ref) => _socket._toJSON(ref); |
1188 } | 1216 } |
1189 | 1217 |
1190 | 1218 |
1191 class _RawServerSocketReference implements RawServerSocketReference { | 1219 class _RawServerSocketReference implements RawServerSocketReference { |
1192 final SendPort _sendPort; | 1220 final SendPort _sendPort; |
1193 | 1221 |
1194 _RawServerSocketReference(this._sendPort); | 1222 _RawServerSocketReference(this._sendPort); |
1195 | 1223 |
1196 Future<RawServerSocket> create() { | 1224 Future<RawServerSocket> create() { |
1197 var port = new ReceivePort(); | 1225 var port = new ReceivePort(); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1409 | 1437 |
1410 int get port => _socket.port; | 1438 int get port => _socket.port; |
1411 | 1439 |
1412 InternetAddress get address => _socket.address; | 1440 InternetAddress get address => _socket.address; |
1413 | 1441 |
1414 Future close() => _socket.close().then((_) => this); | 1442 Future close() => _socket.close().then((_) => this); |
1415 | 1443 |
1416 ServerSocketReference get reference { | 1444 ServerSocketReference get reference { |
1417 return new _ServerSocketReference(_socket.reference); | 1445 return new _ServerSocketReference(_socket.reference); |
1418 } | 1446 } |
| 1447 |
| 1448 Map _toJSON(bool ref) => _socket._toJSON(ref); |
1419 } | 1449 } |
1420 | 1450 |
1421 | 1451 |
1422 patch class Socket { | 1452 patch class Socket { |
1423 /* patch */ static Future<Socket> connect(host, int port) { | 1453 /* patch */ static Future<Socket> connect(host, int port) { |
1424 return RawSocket.connect(host, port).then( | 1454 return RawSocket.connect(host, port).then( |
1425 (socket) => new _Socket(socket)); | 1455 (socket) => new _Socket(socket)); |
1426 } | 1456 } |
1427 } | 1457 } |
1428 | 1458 |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1859 String address, | 1889 String address, |
1860 List<int> in_addr, | 1890 List<int> in_addr, |
1861 int port) { | 1891 int port) { |
1862 return new Datagram( | 1892 return new Datagram( |
1863 data, | 1893 data, |
1864 new _InternetAddress(address, null, in_addr), | 1894 new _InternetAddress(address, null, in_addr), |
1865 port); | 1895 port); |
1866 } | 1896 } |
1867 | 1897 |
1868 String _socketsStats() => _SocketsObservatory.toJSON(); | 1898 String _socketsStats() => _SocketsObservatory.toJSON(); |
OLD | NEW |