| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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; |
| 11 static const int BINARY = 1; | 11 static const int BINARY = 1; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 static const int FAILURE = 6; | 55 static const int FAILURE = 6; |
| 56 | 56 |
| 57 _WebSocketProtocolProcessor() { | 57 _WebSocketProtocolProcessor() { |
| 58 _prepareForNextFrame(); | 58 _prepareForNextFrame(); |
| 59 _currentMessageType = _WebSocketMessageType.NONE; | 59 _currentMessageType = _WebSocketMessageType.NONE; |
| 60 } | 60 } |
| 61 | 61 |
| 62 /** | 62 /** |
| 63 * Process data received from the underlying communication channel. | 63 * Process data received from the underlying communication channel. |
| 64 */ | 64 */ |
| 65 void update(List<int> buffer, int offset, int count) { | 65 void update(List<int> buffer) { |
| 66 int index = offset; | 66 int index = 0; |
| 67 int lastIndex = offset + count; | 67 int lastIndex = buffer.length; |
| 68 try { | 68 try { |
| 69 if (_state == CLOSED) { | 69 if (_state == CLOSED) { |
| 70 throw new WebSocketException("Data on closed connection"); | 70 throw new WebSocketException("Data on closed connection"); |
| 71 } | 71 } |
| 72 if (_state == FAILURE) { | 72 if (_state == FAILURE) { |
| 73 throw new WebSocketException("Data on failed connection"); | 73 throw new WebSocketException("Data on failed connection"); |
| 74 } | 74 } |
| 75 while ((index < lastIndex) && _state != CLOSED && _state != FAILURE) { | 75 while ((index < lastIndex) && _state != CLOSED && _state != FAILURE) { |
| 76 int byte = buffer[index]; | 76 int byte = buffer[index]; |
| 77 switch (_state) { | 77 switch (_state) { |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 | 368 |
| 369 void _startProcessing(List<int> unparsedData) { | 369 void _startProcessing(List<int> unparsedData) { |
| 370 _WebSocketProtocolProcessor processor = new _WebSocketProtocolProcessor(); | 370 _WebSocketProtocolProcessor processor = new _WebSocketProtocolProcessor(); |
| 371 processor.onMessageStart = _onWebSocketMessageStart; | 371 processor.onMessageStart = _onWebSocketMessageStart; |
| 372 processor.onMessageData = _onWebSocketMessageData; | 372 processor.onMessageData = _onWebSocketMessageData; |
| 373 processor.onMessageEnd = _onWebSocketMessageEnd; | 373 processor.onMessageEnd = _onWebSocketMessageEnd; |
| 374 processor.onPing = _onWebSocketPing; | 374 processor.onPing = _onWebSocketPing; |
| 375 processor.onPong = _onWebSocketPong; | 375 processor.onPong = _onWebSocketPong; |
| 376 processor.onClosed = _onWebSocketClosed; | 376 processor.onClosed = _onWebSocketClosed; |
| 377 if (unparsedData != null) { | 377 if (unparsedData != null) { |
| 378 processor.update(unparsedData, 0, unparsedData.length); | 378 processor.update(unparsedData); |
| 379 } | 379 } |
| 380 _socket.onData = () { | 380 _socket.onData = () { |
| 381 int available = _socket.available(); | 381 processor.update(_socket.read()); |
| 382 List<int> data = new List<int>.fixedLength(available); | |
| 383 int read = _socket.readList(data, 0, available); | |
| 384 processor.update(data, 0, read); | |
| 385 }; | 382 }; |
| 386 _socket.onClosed = () { | 383 _socket.onClosed = () { |
| 387 processor.closed(); | 384 processor.closed(); |
| 388 if (_closeSent) { | 385 if (_closeSent) { |
| 389 // Got socket close in response to close frame. Don't treat | 386 // Got socket close in response to close frame. Don't treat |
| 390 // that as an error. | 387 // that as an error. |
| 391 if (_closeTimer != null) _closeTimer.cancel(); | 388 if (_closeTimer != null) _closeTimer.cancel(); |
| 392 } else { | 389 } else { |
| 393 if (_onClosed != null) _onClosed(WebSocketStatus.ABNORMAL_CLOSURE, | 390 if (_onClosed != null) _onClosed(WebSocketStatus.ABNORMAL_CLOSURE, |
| 394 "Unexpected close"); | 391 "Unexpected close"); |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 865 | 862 |
| 866 class _WebSocketCloseEvent implements CloseEvent { | 863 class _WebSocketCloseEvent implements CloseEvent { |
| 867 _WebSocketCloseEvent(this._wasClean, this._code, this._reason); | 864 _WebSocketCloseEvent(this._wasClean, this._code, this._reason); |
| 868 bool get wasClean => _wasClean; | 865 bool get wasClean => _wasClean; |
| 869 int get code => _code; | 866 int get code => _code; |
| 870 String get reason => _reason; | 867 String get reason => _reason; |
| 871 bool _wasClean; | 868 bool _wasClean; |
| 872 int _code; | 869 int _code; |
| 873 String _reason; | 870 String _reason; |
| 874 } | 871 } |
| OLD | NEW |