| 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 | 7 // Bit flags used when communicating between the eventhandler and |
| 8 /* | 8 // dart code. The EVENT flags are used to indicate events of |
| 9 * Keep these constants in sync with the native poll event identifiers. | 9 // interest when sending a message from dart code to the |
| 10 */ | 10 // eventhandler. When receiving a message from the eventhandler the |
| 11 // EVENT flags indicate the events that actually happened. The |
| 12 // COMMAND flags are used to send commands from dart to the |
| 13 // eventhandler. COMMAND flags are never received from the |
| 14 // eventhandler. Additional flags are used to communicate other |
| 15 // information. |
| 11 static final int _IN_EVENT = 0; | 16 static final int _IN_EVENT = 0; |
| 12 static final int _OUT_EVENT = 1; | 17 static final int _OUT_EVENT = 1; |
| 13 static final int _ERROR_EVENT = 2; | 18 static final int _ERROR_EVENT = 2; |
| 14 static final int _CLOSE_EVENT = 3; | 19 static final int _CLOSE_EVENT = 3; |
| 20 |
| 21 static final int _CLOSE_COMMAND = 8; |
| 22 |
| 23 // Flag send to the eventhandler saying that the file descriptor in |
| 24 // question represents a listening socket. |
| 25 static final int _LISTENING_SOCKET = 16; |
| 26 |
| 15 static final int _FIRST_EVENT = _IN_EVENT; | 27 static final int _FIRST_EVENT = _IN_EVENT; |
| 16 static final int _LAST_EVENT = _CLOSE_EVENT; | 28 static final int _LAST_EVENT = _CLOSE_EVENT; |
| 17 | 29 |
| 18 static final int _CLOSE_COMMAND = 4; | |
| 19 | |
| 20 _SocketBase () { | 30 _SocketBase () { |
| 21 _handler = new ReceivePort(); | 31 _handler = new ReceivePort(); |
| 22 _handlerMap = new List(_CLOSE_EVENT + 1); | 32 _handlerMap = new List(_CLOSE_EVENT + 1); |
| 23 _handlerMask = 0; | 33 _handlerMask = 0; |
| 24 _canActivateHandlers = true; | 34 _canActivateHandlers = true; |
| 25 _id = -1; | 35 _id = -1; |
| 26 _handler.receive((var message, ignored) { | 36 _handler.receive((var message, ignored) { |
| 27 _multiplex(message); | 37 _multiplex(message); |
| 28 }); | 38 }); |
| 29 EventHandler._start(); | 39 EventHandler._start(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 } | 75 } |
| 66 | 76 |
| 67 void _getPort() native "Socket_GetPort"; | 77 void _getPort() native "Socket_GetPort"; |
| 68 | 78 |
| 69 void setErrorHandler(void callback()) { | 79 void setErrorHandler(void callback()) { |
| 70 _setHandler(_ERROR_EVENT, callback); | 80 _setHandler(_ERROR_EVENT, callback); |
| 71 } | 81 } |
| 72 | 82 |
| 73 void _doActivateHandlers() { | 83 void _doActivateHandlers() { |
| 74 if (_canActivateHandlers && (_id >= 0)) { | 84 if (_canActivateHandlers && (_id >= 0)) { |
| 75 EventHandler._sendData(_id, _handler, _handlerMask); | 85 int data = _handlerMask; |
| 86 if (_isListenSocket()) data |= (1 << _LISTENING_SOCKET); |
| 87 EventHandler._sendData(_id, _handler, data); |
| 76 } | 88 } |
| 77 } | 89 } |
| 78 | 90 |
| 79 void _scheduleEvent(int event) { | 91 void _scheduleEvent(int event) { |
| 80 _handler.toSendPort().send([1 << event], null); | 92 _handler.toSendPort().send([1 << event], null); |
| 81 } | 93 } |
| 82 | 94 |
| 83 int get port() { | 95 int get port() { |
| 84 if (_port === null) { | 96 if (_port === null) { |
| 85 _port = _getPort(); | 97 _port = _getPort(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 99 if (_handler === null) { | 111 if (_handler === null) { |
| 100 throw new | 112 throw new |
| 101 SocketIOException("Error: close failed - invalid socket handle"); | 113 SocketIOException("Error: close failed - invalid socket handle"); |
| 102 } else { | 114 } else { |
| 103 _handler.close(); | 115 _handler.close(); |
| 104 _handler = null; | 116 _handler = null; |
| 105 } | 117 } |
| 106 } | 118 } |
| 107 } | 119 } |
| 108 | 120 |
| 121 abstract bool _isListenSocket(); |
| 109 | 122 |
| 110 /* | 123 /* |
| 111 * Socket id is set from native. -1 indicates that the socket was closed. | 124 * Socket id is set from native. -1 indicates that the socket was closed. |
| 112 */ | 125 */ |
| 113 int _id; | 126 int _id; |
| 114 | 127 |
| 115 /* | 128 /* |
| 116 * Dedicated ReceivePort for socket events. | 129 * Dedicated ReceivePort for socket events. |
| 117 */ | 130 */ |
| 118 ReceivePort _handler; | 131 ReceivePort _handler; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } | 186 } |
| 174 | 187 |
| 175 bool _accept(Socket socket) native "ServerSocket_Accept"; | 188 bool _accept(Socket socket) native "ServerSocket_Accept"; |
| 176 | 189 |
| 177 bool _createBindListen(String bindAddress, int port, int backlog) | 190 bool _createBindListen(String bindAddress, int port, int backlog) |
| 178 native "ServerSocket_CreateBindListen"; | 191 native "ServerSocket_CreateBindListen"; |
| 179 | 192 |
| 180 void setConnectionHandler(void callback()) { | 193 void setConnectionHandler(void callback()) { |
| 181 _setHandler(_IN_EVENT, callback); | 194 _setHandler(_IN_EVENT, callback); |
| 182 } | 195 } |
| 196 |
| 197 bool _isListenSocket() => true; |
| 183 } | 198 } |
| 184 | 199 |
| 185 | 200 |
| 186 class _Socket extends _SocketBase implements Socket { | 201 class _Socket extends _SocketBase implements Socket { |
| 187 /* | 202 /* |
| 188 * Constructor for socket. First a socket object is allocated | 203 * Constructor for socket. First a socket object is allocated |
| 189 * in which the native socket is stored. After that _createConnect is | 204 * in which the native socket is stored. After that _createConnect is |
| 190 * called which creates a file discriptor and connects to the given | 205 * called which creates a file discriptor and connects to the given |
| 191 * host on the given port. Null is returned if file descriptor creation | 206 * host on the given port. Null is returned if file descriptor creation |
| 192 * or connect failsed | 207 * or connect failsed |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 } | 283 } |
| 269 | 284 |
| 270 void setDataHandler(void callback()) { | 285 void setDataHandler(void callback()) { |
| 271 _setHandler(_IN_EVENT, callback); | 286 _setHandler(_IN_EVENT, callback); |
| 272 } | 287 } |
| 273 | 288 |
| 274 void setCloseHandler(void callback()) { | 289 void setCloseHandler(void callback()) { |
| 275 _setHandler(_CLOSE_EVENT, callback); | 290 _setHandler(_CLOSE_EVENT, callback); |
| 276 } | 291 } |
| 277 | 292 |
| 293 bool _isListenSocket() => false; |
| 294 |
| 278 InputStream get inputStream() { | 295 InputStream get inputStream() { |
| 279 if (_inputStream === null) { | 296 if (_inputStream === null) { |
| 280 _inputStream = new SocketInputStream(this); | 297 _inputStream = new SocketInputStream(this); |
| 281 } | 298 } |
| 282 return _inputStream; | 299 return _inputStream; |
| 283 } | 300 } |
| 284 | 301 |
| 285 OutputStream get outputStream() { | 302 OutputStream get outputStream() { |
| 286 if (_outputStream === null) { | 303 if (_outputStream === null) { |
| 287 _outputStream = new SocketOutputStream(this); | 304 _outputStream = new SocketOutputStream(this); |
| 288 } | 305 } |
| 289 return _outputStream; | 306 return _outputStream; |
| 290 } | 307 } |
| 291 | 308 |
| 292 SocketInputStream _inputStream; | 309 SocketInputStream _inputStream; |
| 293 SocketOutputStream _outputStream; | 310 SocketOutputStream _outputStream; |
| 294 } | 311 } |
| 295 | 312 |
| OLD | NEW |