| 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 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 var errorHandler = _handlerMap[_ERROR_EVENT]; | 345 var errorHandler = _handlerMap[_ERROR_EVENT]; |
| 346 if (errorHandler != null) { | 346 if (errorHandler != null) { |
| 347 errorHandler(); | 347 errorHandler(); |
| 348 _setHandler(_ERROR_EVENT, null); | 348 _setHandler(_ERROR_EVENT, null); |
| 349 } | 349 } |
| 350 } | 350 } |
| 351 | 351 |
| 352 bool _createConnect(String host, int port) native "Socket_CreateConnect"; | 352 bool _createConnect(String host, int port) native "Socket_CreateConnect"; |
| 353 | 353 |
| 354 void set writeHandler(void callback()) { | 354 void set writeHandler(void callback()) { |
| 355 _setHandler(_OUT_EVENT, callback); | 355 if (_outputStream != null) throw new StreamException( |
| 356 "Cannot set write handler when output stream is used"); |
| 357 _clientWriteHandler = callback; |
| 358 _updateOutHandler(); |
| 356 } | 359 } |
| 357 | 360 |
| 358 void set connectHandler(void callback()) { | 361 void set connectHandler(void callback()) { |
| 359 // TODO(ager): Make sure that write handler and connect handler do | 362 if (_seenFirstOutEvent || _outputStream != null) { |
| 360 // not compete for the out events. | 363 throw new StreamException( |
| 361 _setHandler(_OUT_EVENT, callback); | 364 "Cannot set connect handler when already connected"); |
| 365 } |
| 366 if (_outputStream != null) { |
| 367 throw new StreamException( |
| 368 "Cannot set connect handler when output stream is used"); |
| 369 } |
| 370 _clientConnectHandler = callback; |
| 371 _updateOutHandler(); |
| 362 } | 372 } |
| 363 | 373 |
| 364 void set dataHandler(void callback()) { | 374 void set dataHandler(void callback()) { |
| 365 _setHandler(_IN_EVENT, callback); | 375 if (_inputStream != null) throw new StreamException( |
| 376 "Cannot set data handler when input stream is used"); |
| 377 _dataHandler = callback; |
| 366 } | 378 } |
| 367 | 379 |
| 368 void set closeHandler(void callback()) { | 380 void set closeHandler(void callback()) { |
| 369 _setHandler(_CLOSE_EVENT, callback); | 381 if (_inputStream != null) throw new StreamException( |
| 382 "Cannot set data handler when input stream is used"); |
| 383 _closeHandler = callback; |
| 370 } | 384 } |
| 371 | 385 |
| 372 bool _isListenSocket() => false; | 386 bool _isListenSocket() => false; |
| 387 |
| 373 bool _isPipe() => _pipe; | 388 bool _isPipe() => _pipe; |
| 374 | 389 |
| 375 InputStream get inputStream() { | 390 InputStream get inputStream() { |
| 376 if (_inputStream === null) { | 391 if (_inputStream === null) { |
| 392 if (_handlerMap[_IN_EVENT] !== null || |
| 393 _handlerMap[_CLOSE_EVENT]) { |
| 394 throw new StreamException( |
| 395 "Cannot get input stream when socket handlers are used"); |
| 396 } |
| 377 _inputStream = new SocketInputStream(this); | 397 _inputStream = new SocketInputStream(this); |
| 378 } | 398 } |
| 379 return _inputStream; | 399 return _inputStream; |
| 380 } | 400 } |
| 381 | 401 |
| 382 OutputStream get outputStream() { | 402 OutputStream get outputStream() { |
| 383 if (_outputStream === null) { | 403 if (_outputStream === null) { |
| 404 if (_handlerMap[_OUT_EVENT] !== null) { |
| 405 throw new StreamException( |
| 406 "Cannot get input stream when socket handlers are used"); |
| 407 } |
| 384 _outputStream = new SocketOutputStream(this); | 408 _outputStream = new SocketOutputStream(this); |
| 385 } | 409 } |
| 386 return _outputStream; | 410 return _outputStream; |
| 387 } | 411 } |
| 388 | 412 |
| 413 void set _writeHandler(void callback()) { |
| 414 _setHandler(_OUT_EVENT, callback); |
| 415 } |
| 416 |
| 417 void set _dataHandler(void callback()) { |
| 418 _setHandler(_IN_EVENT, callback); |
| 419 } |
| 420 |
| 421 void set _closeHandler(void callback()) { |
| 422 _setHandler(_CLOSE_EVENT, callback); |
| 423 } |
| 424 |
| 425 void _updateOutHandler() { |
| 426 void firstWriteHandler() { |
| 427 assert(!_seenFirstOutEvent); |
| 428 _seenFirstOutEvent = true; |
| 429 |
| 430 // From now on the write handler is only the client write |
| 431 // handler (connect handler cannot be called again). Change this |
| 432 // before calling any handlers as handlers can change the |
| 433 // handlers. |
| 434 if (_clientWriteHandler === null) _writeHandler = _clientWriteHandler; |
| 435 |
| 436 // First out event is socket connected event. |
| 437 if (_clientConnectHandler !== null) _clientConnectHandler(); |
| 438 _clientConnectHandler = null; |
| 439 |
| 440 // Always (even for the first out event) call the write handler. |
| 441 if (_clientWriteHandler !== null) _clientWriteHandler(); |
| 442 } |
| 443 |
| 444 if (_clientConnectHandler === null && _clientWriteHandler === null) { |
| 445 _writeHandler = null; |
| 446 } else { |
| 447 if (_seenFirstOutEvent) { |
| 448 _writeHandler = _clientWriteHandler; |
| 449 } else { |
| 450 _writeHandler = firstWriteHandler; |
| 451 } |
| 452 } |
| 453 } |
| 454 |
| 455 bool _seenFirstOutEvent = false; |
| 389 bool _closedRead = false; | 456 bool _closedRead = false; |
| 390 bool _closedWrite = false; | 457 bool _closedWrite = false; |
| 391 bool _pipe = false; | 458 bool _pipe = false; |
| 459 Function _clientConnectHandler; |
| 460 Function _clientWriteHandler; |
| 392 SocketInputStream _inputStream; | 461 SocketInputStream _inputStream; |
| 393 SocketOutputStream _outputStream; | 462 SocketOutputStream _outputStream; |
| 394 } | 463 } |
| 395 | 464 |
| OLD | NEW |