Chromium Code Reviews| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 _closedRead && | 84 _closedRead && |
| 85 _handlerMask == 0 && | 85 _handlerMask == 0 && |
| 86 _handler != null) { | 86 _handler != null) { |
| 87 _handler.close(); | 87 _handler.close(); |
| 88 _handler = null; | 88 _handler = null; |
| 89 } else { | 89 } else { |
| 90 _activateHandlers(); | 90 _activateHandlers(); |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 void _getPort() native "Socket_GetPort"; | 94 int _getPort() native "Socket_GetPort"; |
| 95 | 95 |
| 96 void set onError(void callback()) { | 96 void set onError(void callback()) { |
| 97 _setHandler(_ERROR_EVENT, callback); | 97 _setHandler(_ERROR_EVENT, callback); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void _activateHandlers() { | 100 void _activateHandlers() { |
| 101 if (_canActivateHandlers && (_id >= 0)) { | 101 if (_canActivateHandlers && (_id >= 0)) { |
| 102 if (_handlerMask == 0) { | 102 if (_handlerMask == 0) { |
| 103 if (_handler != null) { | 103 if (_handler != null) { |
| 104 _handler.close(); | 104 _handler.close(); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 | 199 |
| 200 // Indicates if native interrupts can be activated. | 200 // Indicates if native interrupts can be activated. |
| 201 bool _canActivateHandlers; | 201 bool _canActivateHandlers; |
| 202 | 202 |
| 203 // Holds the port of the socket, null if not known. | 203 // Holds the port of the socket, null if not known. |
| 204 int _port; | 204 int _port; |
| 205 | 205 |
| 206 // Hash code for the socket. Currently this is just a counter. | 206 // Hash code for the socket. Currently this is just a counter. |
| 207 int _hashCode; | 207 int _hashCode; |
| 208 static int _nextHashCode = 0; | 208 static int _nextHashCode = 0; |
| 209 bool _closedRead = false; | |
| 210 bool _closedWrite = false; | |
|
Bill Hesse
2012/03/14 13:24:57
Various methods in _SocketBase use these fields, s
| |
| 209 } | 211 } |
| 210 | 212 |
| 211 | 213 |
| 212 class _ServerSocket extends _SocketBase implements ServerSocket { | 214 class _ServerSocket extends _SocketBase implements ServerSocket { |
| 213 // Constructor for server socket. First a socket object is allocated | 215 // Constructor for server socket. First a socket object is allocated |
| 214 // in which the native socket is stored. After that _createBind | 216 // in which the native socket is stored. After that _createBind |
| 215 // is called which creates a file descriptor and binds the given address | 217 // is called which creates a file descriptor and binds the given address |
| 216 // and port to the socket. Null is returned if file descriptor creation or | 218 // and port to the socket. Null is returned if file descriptor creation or |
| 217 // bind failed. | 219 // bind failed. |
| 218 factory _ServerSocket(String bindAddress, int port, int backlog) { | 220 factory _ServerSocket(String bindAddress, int port, int backlog) { |
| 219 ServerSocket socket = new _ServerSocket._internal(); | 221 _ServerSocket socket = new _ServerSocket._internal(); |
| 220 if (!socket._createBindListen(bindAddress, port, backlog)) { | 222 if (!socket._createBindListen(bindAddress, port, backlog)) { |
| 221 socket.close(); | 223 socket.close(); |
| 222 return null; | 224 return null; |
| 223 } | 225 } |
| 224 if (port != 0) { | 226 if (port != 0) { |
| 225 socket._port = port; | 227 socket._port = port; |
| 226 } | 228 } |
| 227 return socket; | 229 return socket; |
| 228 } | 230 } |
| 229 | 231 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 254 } | 256 } |
| 255 | 257 |
| 256 | 258 |
| 257 class _Socket extends _SocketBase implements Socket { | 259 class _Socket extends _SocketBase implements Socket { |
| 258 // Constructor for socket. First a socket object is allocated | 260 // Constructor for socket. First a socket object is allocated |
| 259 // in which the native socket is stored. After that _createConnect is | 261 // in which the native socket is stored. After that _createConnect is |
| 260 // called which creates a file discriptor and connects to the given | 262 // called which creates a file discriptor and connects to the given |
| 261 // host on the given port. Null is returned if file descriptor creation | 263 // host on the given port. Null is returned if file descriptor creation |
| 262 // or connect failed. | 264 // or connect failed. |
| 263 factory _Socket(String host, int port) { | 265 factory _Socket(String host, int port) { |
| 264 Socket socket = new _Socket._internal(); | 266 _Socket socket = new _Socket._internal(); |
| 265 if (!socket._createConnect(host, port)) { | 267 if (!socket._createConnect(host, port)) { |
| 266 socket.close(); | 268 socket.close(); |
| 267 return null; | 269 return null; |
| 268 } | 270 } |
| 269 return socket; | 271 return socket; |
| 270 } | 272 } |
| 271 | 273 |
| 272 _Socket._internal(); | 274 _Socket._internal(); |
| 273 _Socket._internalReadOnly() : _closedWrite = true, _pipe = true; | 275 _Socket._internalReadOnly() : _pipe = true { super._closedWrite = true; } |
| 274 _Socket._internalWriteOnly() : _closedRead = true, _pipe = true; | 276 _Socket._internalWriteOnly() : _pipe = true { super._closedRead = true; } |
| 275 | 277 |
| 276 int available() { | 278 int available() { |
| 277 if (_id >= 0) { | 279 if (_id >= 0) { |
| 278 return _available(); | 280 return _available(); |
| 279 } | 281 } |
| 280 throw new | 282 throw new |
| 281 SocketIOException("Error: available failed - invalid socket handle"); | 283 SocketIOException("Error: available failed - invalid socket handle"); |
| 282 } | 284 } |
| 283 | 285 |
| 284 int _available() native "Socket_Available"; | 286 int _available() native "Socket_Available"; |
| 285 | 287 |
| 286 bool get closed() => _closed; | 288 bool get closed() => _closedWrite && _closedRead; |
|
Mads Ager (google)
2012/03/14 13:34:08
It looks to me like this should be deleted instead
Bill Hesse
2012/03/14 13:46:49
Deleted. Not surprising it didn't fail, since it
| |
| 287 | 289 |
| 288 int readList(List<int> buffer, int offset, int bytes) { | 290 int readList(List<int> buffer, int offset, int bytes) { |
| 289 if (_id >= 0) { | 291 if (_id >= 0) { |
| 290 if (bytes == 0) { | 292 if (bytes == 0) { |
| 291 return 0; | 293 return 0; |
| 292 } | 294 } |
| 293 if (offset < 0) { | 295 if (offset < 0) { |
| 294 throw new IndexOutOfRangeException(offset); | 296 throw new IndexOutOfRangeException(offset); |
| 295 } | 297 } |
| 296 if (bytes < 0) { | 298 if (bytes < 0) { |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 471 } else { | 473 } else { |
| 472 if (_seenFirstOutEvent) { | 474 if (_seenFirstOutEvent) { |
| 473 _onWrite = _clientWriteHandler; | 475 _onWrite = _clientWriteHandler; |
| 474 } else { | 476 } else { |
| 475 _onWrite = firstWriteHandler; | 477 _onWrite = firstWriteHandler; |
| 476 } | 478 } |
| 477 } | 479 } |
| 478 } | 480 } |
| 479 | 481 |
| 480 bool _seenFirstOutEvent = false; | 482 bool _seenFirstOutEvent = false; |
| 481 bool _closedRead = false; | |
| 482 bool _closedWrite = false; | |
| 483 bool _pipe = false; | 483 bool _pipe = false; |
| 484 Function _clientConnectHandler; | 484 Function _clientConnectHandler; |
| 485 Function _clientWriteHandler; | 485 Function _clientWriteHandler; |
| 486 SocketInputStream _inputStream; | 486 SocketInputStream _inputStream; |
| 487 SocketOutputStream _outputStream; | 487 SocketOutputStream _outputStream; |
| 488 } | 488 } |
| 489 | 489 |
| OLD | NEW |