| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 | 5 |
| 6 class _SocketBase { | 6 class _SocketBase { |
| 7 // Bit flags used when communicating between the eventhandler and | 7 // Bit flags used when communicating between the eventhandler and |
| 8 // dart code. The EVENT flags are used to indicate events of | 8 // dart code. The EVENT flags are used to indicate events of |
| 9 // interest when sending a message from dart code to the | 9 // interest when sending a message from dart code to the |
| 10 // eventhandler. When receiving a message from the eventhandler the | 10 // eventhandler. When receiving a message from the eventhandler the |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 | 41 |
| 42 /* | 42 /* |
| 43 * Multiplexes socket events to the right socket handler. | 43 * Multiplexes socket events to the right socket handler. |
| 44 */ | 44 */ |
| 45 void _multiplex(List<int> message) { | 45 void _multiplex(List<int> message) { |
| 46 assert(message.length == 1); | 46 assert(message.length == 1); |
| 47 _canActivateHandlers = false; | 47 _canActivateHandlers = false; |
| 48 int event_mask = message[0]; | 48 int event_mask = message[0]; |
| 49 for (int i = _FIRST_EVENT; i <= _LAST_EVENT; i++) { | 49 for (int i = _FIRST_EVENT; i <= _LAST_EVENT; i++) { |
| 50 if (((event_mask & (1 << i)) != 0) && _handlerMap[i] !== null) { | 50 if (((event_mask & (1 << i)) != 0) && _handlerMap[i] != null) { |
| 51 var handleEvent = _handlerMap[i]; | 51 var handleEvent = _handlerMap[i]; |
| 52 | 52 |
| 53 // Unregister the out handler before executing it. | 53 // Unregister the out handler before executing it. |
| 54 if (i == _OUT_EVENT) _setHandler(i, null); | 54 if (i == _OUT_EVENT) _setHandler(i, null); |
| 55 | 55 |
| 56 // Don't call the in handler if there is no data available | 56 // Don't call the in handler if there is no data available |
| 57 // after all. | 57 // after all. |
| 58 if (i == _IN_EVENT && this is _Socket && available() == 0) continue; | 58 if (i == _IN_EVENT && this is _Socket && available() == 0) continue; |
| 59 | 59 |
| 60 handleEvent(); | 60 handleEvent(); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 _canActivateHandlers = true; | 63 _canActivateHandlers = true; |
| 64 _doActivateHandlers(); | 64 _activateHandlers(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void _setHandler(int event, void callback()) { | 67 void _setHandler(int event, void callback()) { |
| 68 if (callback === null) { | 68 if (callback == null) { |
| 69 _handlerMask &= ~(1 << event); | 69 _handlerMask &= ~(1 << event); |
| 70 } else { | 70 } else { |
| 71 _handlerMask |= (1 << event); | 71 _handlerMask |= (1 << event); |
| 72 } | 72 } |
| 73 _handlerMap[event] = callback; | 73 _handlerMap[event] = callback; |
| 74 _doActivateHandlers(); | 74 _activateHandlers(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void _getPort() native "Socket_GetPort"; | 77 void _getPort() native "Socket_GetPort"; |
| 78 | 78 |
| 79 void setErrorHandler(void callback()) { | 79 void setErrorHandler(void callback()) { |
| 80 _setHandler(_ERROR_EVENT, callback); | 80 _setHandler(_ERROR_EVENT, callback); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void _doActivateHandlers() { | 83 void _activateHandlers() { |
| 84 if (_canActivateHandlers && (_id >= 0)) { | 84 if (_canActivateHandlers && (_id >= 0)) { |
| 85 int data = _handlerMask; | 85 int data = _handlerMask; |
| 86 if (_isListenSocket()) data |= (1 << _LISTENING_SOCKET); | 86 if (_isListenSocket()) data |= (1 << _LISTENING_SOCKET); |
| 87 EventHandler._sendData(_id, _handler, data); | 87 EventHandler._sendData(_id, _handler, data); |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 void _scheduleEvent(int event) { | 91 void _scheduleEvent(int event) { |
| 92 _handler.toSendPort().send([1 << event], null); | 92 _handler.toSendPort().send([1 << event], null); |
| 93 } | 93 } |
| 94 | 94 |
| 95 int get port() { | 95 int get port() { |
| 96 if (_port === null) { | 96 if (_port === null) { |
| 97 _port = _getPort(); | 97 _port = _getPort(); |
| 98 } | 98 } |
| 99 return _port; | 99 return _port; |
| 100 } | 100 } |
| 101 | 101 |
| 102 void close() { | 102 void close() { |
| 103 if (_id >= 0) { | 103 if (_id >= 0) { |
| 104 EventHandler._sendData(_id, _handler, 1 << _CLOSE_COMMAND); | 104 EventHandler._sendData(_id, _handler, 1 << _CLOSE_COMMAND); |
| 105 _handler.close(); | 105 _handler.close(); |
| 106 _handler = null; | 106 _handler = null; |
| 107 _id = -1; | 107 _id = -1; |
| 108 } else { | 108 } else if (_handler != null) { |
| 109 // This is to support closing sockets created but never assigned | 109 // This is to support closing sockets created but never assigned |
| 110 // any actual socket. | 110 // any actual socket. |
| 111 if (_handler === null) { | 111 _handler.close(); |
| 112 throw new | 112 _handler = null; |
| 113 SocketIOException("Error: close failed - invalid socket handle"); | |
| 114 } else { | |
| 115 _handler.close(); | |
| 116 _handler = null; | |
| 117 } | |
| 118 } | 113 } |
| 119 } | 114 } |
| 120 | 115 |
| 121 abstract bool _isListenSocket(); | 116 abstract bool _isListenSocket(); |
| 122 | 117 |
| 123 /* | 118 /* |
| 124 * Socket id is set from native. -1 indicates that the socket was closed. | 119 * Socket id is set from native. -1 indicates that the socket was closed. |
| 125 */ | 120 */ |
| 126 int _id; | 121 int _id; |
| 127 | 122 |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 if (_outputStream === null) { | 298 if (_outputStream === null) { |
| 304 _outputStream = new SocketOutputStream(this); | 299 _outputStream = new SocketOutputStream(this); |
| 305 } | 300 } |
| 306 return _outputStream; | 301 return _outputStream; |
| 307 } | 302 } |
| 308 | 303 |
| 309 SocketInputStream _inputStream; | 304 SocketInputStream _inputStream; |
| 310 SocketOutputStream _outputStream; | 305 SocketOutputStream _outputStream; |
| 311 } | 306 } |
| 312 | 307 |
| OLD | NEW |