Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: runtime/bin/socket_impl.dart

Issue 9029001: Add close to input stream and cleanup socket and streams (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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("Illegal state");
Mads Ager (google) 2011/12/23 08:35:40 An error message that gave the programmer more hel
Søren Gjesse 2012/01/02 11:51:17 Done.
356 _clientWriteHandler = callback;
357 _updateOutHandler();
356 } 358 }
357 359
358 void set connectHandler(void callback()) { 360 void set connectHandler(void callback()) {
359 // TODO(ager): Make sure that write handler and connect handler do 361 if (_seenFirstOutEvent || _outputStream != null) {
360 // not compete for the out events. 362 throw new StreamException("Illegal state");
361 _setHandler(_OUT_EVENT, callback); 363 }
364 _clientConnectHandler = callback;
365 _updateOutHandler();
362 } 366 }
363 367
364 void set dataHandler(void callback()) { 368 void set dataHandler(void callback()) {
365 _setHandler(_IN_EVENT, callback); 369 if (_inputStream != null) throw new StreamException("Illegal state");
370 _dataHandler = callback;
366 } 371 }
367 372
368 void set closeHandler(void callback()) { 373 void set closeHandler(void callback()) {
369 _setHandler(_CLOSE_EVENT, callback); 374 if (_inputStream != null) throw new StreamException("Illegal state");
375 _closeHandler = callback;
370 } 376 }
371 377
372 bool _isListenSocket() => false; 378 bool _isListenSocket() => false;
379
373 bool _isPipe() => _pipe; 380 bool _isPipe() => _pipe;
374 381
375 InputStream get inputStream() { 382 InputStream get inputStream() {
376 if (_inputStream === null) { 383 if (_inputStream === null) {
384 if (_handlerMap[_IN_EVENT] !== null ||
385 _handlerMap[_CLOSE_EVENT]) {
386 throw new StreamException("Illegal state");
387 }
377 _inputStream = new SocketInputStream(this); 388 _inputStream = new SocketInputStream(this);
378 } 389 }
379 return _inputStream; 390 return _inputStream;
380 } 391 }
381 392
382 OutputStream get outputStream() { 393 OutputStream get outputStream() {
383 if (_outputStream === null) { 394 if (_outputStream === null) {
395 if (_handlerMap[_OUT_EVENT] !== null) {
396 throw new StreamException("Illegal state");
397 }
384 _outputStream = new SocketOutputStream(this); 398 _outputStream = new SocketOutputStream(this);
385 } 399 }
386 return _outputStream; 400 return _outputStream;
387 } 401 }
388 402
403 void set _writeHandler(void callback()) {
404 _setHandler(_OUT_EVENT, callback);
405 }
406
407 void set _dataHandler(void callback()) {
408 _setHandler(_IN_EVENT, callback);
409 }
410
411 void set _closeHandler(void callback()) {
412 _setHandler(_CLOSE_EVENT, callback);
413 }
414
415 void _updateOutHandler() {
416 void writeHandler() {
417 if (!_seenFirstOutEvent) {
418 // First out event is socket connected event.
419 _seenFirstOutEvent = true;
420 var connectHandler = _clientConnectHandler;
421 _clientConnectHandler = null;
422 if (_clientWriteHandler === null) _writeHandler = null;
Mads Ager (google) 2011/12/23 08:35:40 Can't we just unconditionally do '_writeHandler =
Søren Gjesse 2012/01/02 11:51:17 Good point. Refactored to only use this write hand
423 if (connectHandler !== null) connectHandler();
424 }
425
426 // Always (even for the first out event) call the write handler.
427 if (_clientWriteHandler !== null) _clientWriteHandler();
428 }
429
430 if (_clientConnectHandler === null && _clientWriteHandler === null) {
431 _writeHandler = null;
432 } else {
433 _writeHandler = writeHandler;
434 }
435 }
436
437
438 bool _seenFirstOutEvent = false;
389 bool _closedRead = false; 439 bool _closedRead = false;
390 bool _closedWrite = false; 440 bool _closedWrite = false;
391 bool _pipe = false; 441 bool _pipe = false;
442 Function _clientConnectHandler;
443 Function _clientWriteHandler;
392 SocketInputStream _inputStream; 444 SocketInputStream _inputStream;
393 SocketOutputStream _outputStream; 445 SocketOutputStream _outputStream;
394 } 446 }
395 447
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698