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

Side by Side Diff: sdk/lib/io/websocket_impl.dart

Issue 12380029: Keep track of closing socket, in WebSocket, so we don't write once the socket is closed. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 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
« no previous file with comments | « sdk/lib/io/http_impl.dart ('k') | tests/standalone/io/web_socket_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 part of dart.io; 5 part of dart.io;
6 6
7 const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; 7 const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
8 8
9 class _WebSocketMessageType { 9 class _WebSocketMessageType {
10 static const int NONE = 0; 10 static const int NONE = 0;
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 426
427 427
428 class _WebSocketImpl extends Stream<Event> implements WebSocket { 428 class _WebSocketImpl extends Stream<Event> implements WebSocket {
429 final StreamController<Event> _controller = new StreamController<Event>(); 429 final StreamController<Event> _controller = new StreamController<Event>();
430 430
431 final _WebSocketProtocolProcessor _processor = 431 final _WebSocketProtocolProcessor _processor =
432 new _WebSocketProtocolProcessor(); 432 new _WebSocketProtocolProcessor();
433 433
434 final Socket _socket; 434 final Socket _socket;
435 int _readyState = WebSocket.CONNECTING; 435 int _readyState = WebSocket.CONNECTING;
436 bool _writeClosed = false;
436 437
437 static final HttpClient _httpClient = new HttpClient(); 438 static final HttpClient _httpClient = new HttpClient();
438 439
439 static Future<WebSocket> connect(String url, [protocols]) { 440 static Future<WebSocket> connect(String url, [protocols]) {
440 Uri uri = Uri.parse(url); 441 Uri uri = Uri.parse(url);
441 if (uri.scheme != "ws" && uri.scheme != "wss") { 442 if (uri.scheme != "ws" && uri.scheme != "wss") {
442 throw new WebSocketException("Unsupported URL scheme '${uri.scheme}'"); 443 throw new WebSocketException("Unsupported URL scheme '${uri.scheme}'");
443 } 444 }
444 if (uri.userInfo != "") { 445 if (uri.userInfo != "") {
445 throw new WebSocketException("Unsupported user info '${uri.userInfo}'"); 446 throw new WebSocketException("Unsupported user info '${uri.userInfo}'");
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 if (_readyState == WebSocket.OPEN) { 539 if (_readyState == WebSocket.OPEN) {
539 _readyState = WebSocket.CLOSING; 540 _readyState = WebSocket.CLOSING;
540 if (code != WebSocketStatus.NO_STATUS_RECEIVED) { 541 if (code != WebSocketStatus.NO_STATUS_RECEIVED) {
541 _close(code); 542 _close(code);
542 } else { 543 } else {
543 _close(); 544 _close();
544 clean = false; 545 clean = false;
545 } 546 }
546 _readyState = WebSocket.CLOSED; 547 _readyState = WebSocket.CLOSED;
547 } 548 }
549 if (_readyState == WebSocket.CLOSED) return;
548 _controller.add(new _WebSocketCloseEvent(clean, code, reason)); 550 _controller.add(new _WebSocketCloseEvent(clean, code, reason));
549 _controller.close(); 551 _controller.close();
550 }; 552 };
551 553
552 _socket.listen( 554 _socket.listen(
553 (data) => _processor.update(data, 0, data.length), 555 (data) => _processor.update(data, 0, data.length),
554 onDone: () => _processor.closed(), 556 onDone: () => _processor.closed(),
555 onError: (error) => _controller.signalError(error)); 557 onError: (error) => _controller.signalError(error));
558
559 _socket.done
560 .catchError((error) {
561 if (_readyState == WebSocket.CLOSED) return;
562 _readyState = WebSocket.CLOSED;
563 _controller.signalError(error);
564 _controller.close();
565 _processor.closed();
566 _socket.destroy();
567 })
568 .whenComplete(() {
569 _writeClosed = true;
570 });
556 } 571 }
557 572
558 StreamSubscription<Event> listen(void onData(Event event), 573 StreamSubscription<Event> listen(void onData(Event event),
559 {void onError(AsyncError error), 574 {void onError(AsyncError error),
560 void onDone(), 575 void onDone(),
561 bool unsubscribeOnError}) { 576 bool unsubscribeOnError}) {
562 return _controller.stream.listen(onData, 577 return _controller.stream.listen(onData,
563 onError: onError, 578 onError: onError,
564 onDone: onDone, 579 onDone: onDone,
565 unsubscribeOnError: unsubscribeOnError); 580 unsubscribeOnError: unsubscribeOnError);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
624 opcode = _WebSocketOpcode.BINARY; 639 opcode = _WebSocketOpcode.BINARY;
625 data = message; 640 data = message;
626 } 641 }
627 } else { 642 } else {
628 opcode = _WebSocketOpcode.TEXT; 643 opcode = _WebSocketOpcode.TEXT;
629 } 644 }
630 _sendFrame(opcode, data); 645 _sendFrame(opcode, data);
631 } 646 }
632 647
633 void _sendFrame(int opcode, [List<int> data]) { 648 void _sendFrame(int opcode, [List<int> data]) {
649 if (_writeClosed) return;
634 bool mask = false; // Masking not implemented for server. 650 bool mask = false; // Masking not implemented for server.
635 int dataLength = data == null ? 0 : data.length; 651 int dataLength = data == null ? 0 : data.length;
636 // Determine the header size. 652 // Determine the header size.
637 int headerSize = (mask) ? 6 : 2; 653 int headerSize = (mask) ? 6 : 2;
638 if (dataLength > 65535) { 654 if (dataLength > 65535) {
639 headerSize += 8; 655 headerSize += 8;
640 } else if (dataLength > 125) { 656 } else if (dataLength > 125) {
641 headerSize += 2; 657 headerSize += 2;
642 } 658 }
643 List<int> header = new List<int>(headerSize); 659 List<int> header = new List<int>(headerSize);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 692
677 class _WebSocketCloseEvent implements CloseEvent { 693 class _WebSocketCloseEvent implements CloseEvent {
678 _WebSocketCloseEvent(this._wasClean, this._code, this._reason); 694 _WebSocketCloseEvent(this._wasClean, this._code, this._reason);
679 bool get wasClean => _wasClean; 695 bool get wasClean => _wasClean;
680 int get code => _code; 696 int get code => _code;
681 String get reason => _reason; 697 String get reason => _reason;
682 bool _wasClean; 698 bool _wasClean;
683 int _code; 699 int _code;
684 String _reason; 700 String _reason;
685 } 701 }
OLDNEW
« no previous file with comments | « sdk/lib/io/http_impl.dart ('k') | tests/standalone/io/web_socket_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698