| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 static final int _FIRST_COMMAND = _CLOSE_COMMAND; | 33 static final int _FIRST_COMMAND = _CLOSE_COMMAND; |
| 34 static final int _LAST_COMMAND = _SHUTDOWN_WRITE_COMMAND; | 34 static final int _LAST_COMMAND = _SHUTDOWN_WRITE_COMMAND; |
| 35 | 35 |
| 36 _SocketBase () { | 36 _SocketBase () { |
| 37 _handlerMap = new List(_LAST_EVENT + 1); | 37 _handlerMap = new List(_LAST_EVENT + 1); |
| 38 _handlerMask = 0; | 38 _handlerMask = 0; |
| 39 _canActivateHandlers = true; | 39 _canActivateHandlers = true; |
| 40 _id = -1; | 40 _id = -1; |
| 41 _EventHandler._start(); | 41 _EventHandler._start(); |
| 42 _hashCode = _nextHashCode; |
| 43 _nextHashCode = (_nextHashCode + 1) & 0xFFFFFFF; |
| 42 } | 44 } |
| 43 | 45 |
| 44 // Multiplexes socket events to the socket handlers. | 46 // Multiplexes socket events to the socket handlers. |
| 45 void _multiplex(int event_mask) { | 47 void _multiplex(int event_mask) { |
| 46 _canActivateHandlers = false; | 48 _canActivateHandlers = false; |
| 47 for (int i = _FIRST_EVENT; i <= _LAST_EVENT; i++) { | 49 for (int i = _FIRST_EVENT; i <= _LAST_EVENT; i++) { |
| 48 if (((event_mask & (1 << i)) != 0)) { | 50 if (((event_mask & (1 << i)) != 0)) { |
| 49 if ((i == _CLOSE_EVENT) && this is _Socket && _id >= 0) { | 51 if ((i == _CLOSE_EVENT) && this is _Socket && _id >= 0) { |
| 50 _closedRead = true; | 52 _closedRead = true; |
| 51 if (_closedWrite) _close(); | 53 if (_closedWrite) _close(); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 | 173 |
| 172 void _sendToEventHandler(int data) { | 174 void _sendToEventHandler(int data) { |
| 173 if (_handler === null) { | 175 if (_handler === null) { |
| 174 _handler = new ReceivePort(); | 176 _handler = new ReceivePort(); |
| 175 _handler.receive((var message, ignored) { _multiplex(message); }); | 177 _handler.receive((var message, ignored) { _multiplex(message); }); |
| 176 } | 178 } |
| 177 assert(_id >= 0); | 179 assert(_id >= 0); |
| 178 _EventHandler._sendData(_id, _handler, data); | 180 _EventHandler._sendData(_id, _handler, data); |
| 179 } | 181 } |
| 180 | 182 |
| 183 int hashCode() => _hashCode; |
| 184 |
| 181 abstract bool _isListenSocket(); | 185 abstract bool _isListenSocket(); |
| 182 abstract bool _isPipe(); | 186 abstract bool _isPipe(); |
| 183 | 187 |
| 184 // Socket id is set from native. -1 indicates that the socket was closed. | 188 // Socket id is set from native. -1 indicates that the socket was closed. |
| 185 int _id; | 189 int _id; |
| 186 | 190 |
| 187 // Dedicated ReceivePort for socket events. | 191 // Dedicated ReceivePort for socket events. |
| 188 ReceivePort _handler; | 192 ReceivePort _handler; |
| 189 | 193 |
| 190 // Poll event to handler map. | 194 // Poll event to handler map. |
| 191 List _handlerMap; | 195 List _handlerMap; |
| 192 | 196 |
| 193 // Indicates for which poll events the socket registered handlers. | 197 // Indicates for which poll events the socket registered handlers. |
| 194 int _handlerMask; | 198 int _handlerMask; |
| 195 | 199 |
| 196 // Indicates if native interrupts can be activated. | 200 // Indicates if native interrupts can be activated. |
| 197 bool _canActivateHandlers; | 201 bool _canActivateHandlers; |
| 198 | 202 |
| 199 // Holds the port of the socket, null if not known. | 203 // Holds the port of the socket, null if not known. |
| 200 int _port; | 204 int _port; |
| 205 |
| 206 // Hash code for the socket. Currently this is just a counter. |
| 207 int _hashCode; |
| 208 static int _nextHashCode = 0; |
| 201 } | 209 } |
| 202 | 210 |
| 203 | 211 |
| 204 class _ServerSocket extends _SocketBase implements ServerSocket { | 212 class _ServerSocket extends _SocketBase implements ServerSocket { |
| 205 // Constructor for server socket. First a socket object is allocated | 213 // Constructor for server socket. First a socket object is allocated |
| 206 // in which the native socket is stored. After that _createBind | 214 // in which the native socket is stored. After that _createBind |
| 207 // is called which creates a file descriptor and binds the given address | 215 // is called which creates a file descriptor and binds the given address |
| 208 // and port to the socket. Null is returned if file descriptor creation or | 216 // and port to the socket. Null is returned if file descriptor creation or |
| 209 // bind failed. | 217 // bind failed. |
| 210 factory _ServerSocket(String bindAddress, int port, int backlog) { | 218 factory _ServerSocket(String bindAddress, int port, int backlog) { |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 bool _seenFirstOutEvent = false; | 480 bool _seenFirstOutEvent = false; |
| 473 bool _closedRead = false; | 481 bool _closedRead = false; |
| 474 bool _closedWrite = false; | 482 bool _closedWrite = false; |
| 475 bool _pipe = false; | 483 bool _pipe = false; |
| 476 Function _clientConnectHandler; | 484 Function _clientConnectHandler; |
| 477 Function _clientWriteHandler; | 485 Function _clientWriteHandler; |
| 478 SocketInputStream _inputStream; | 486 SocketInputStream _inputStream; |
| 479 SocketOutputStream _outputStream; | 487 SocketOutputStream _outputStream; |
| 480 } | 488 } |
| 481 | 489 |
| OLD | NEW |