OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library service_common; | 5 library service_common; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 | 10 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 abstract class CommonWebSocketVM extends VM { | 80 abstract class CommonWebSocketVM extends VM { |
81 final Completer _connected = new Completer(); | 81 final Completer _connected = new Completer(); |
82 final Completer _disconnected = new Completer(); | 82 final Completer _disconnected = new Completer(); |
83 final WebSocketVMTarget target; | 83 final WebSocketVMTarget target; |
84 final Map<String, _WebSocketRequest> _delayedRequests = | 84 final Map<String, _WebSocketRequest> _delayedRequests = |
85 new Map<String, _WebSocketRequest>(); | 85 new Map<String, _WebSocketRequest>(); |
86 final Map<String, _WebSocketRequest> _pendingRequests = | 86 final Map<String, _WebSocketRequest> _pendingRequests = |
87 new Map<String, _WebSocketRequest>(); | 87 new Map<String, _WebSocketRequest>(); |
88 int _requestSerial = 0; | 88 int _requestSerial = 0; |
89 bool _hasInitiatedConnect = false; | 89 bool _hasInitiatedConnect = false; |
| 90 bool _hasFinishedConnect = false; |
90 Utf8Decoder _utf8Decoder = new Utf8Decoder(); | 91 Utf8Decoder _utf8Decoder = new Utf8Decoder(); |
91 | 92 |
92 CommonWebSocket _webSocket; | 93 CommonWebSocket _webSocket; |
93 | 94 |
94 CommonWebSocketVM(this.target, this._webSocket) { | 95 CommonWebSocketVM(this.target, this._webSocket) { |
95 assert(target != null); | 96 assert(target != null); |
96 } | 97 } |
97 | 98 |
98 void _notifyConnect() { | 99 void _notifyConnect() { |
| 100 _hasFinishedConnect = true; |
99 if (!_connected.isCompleted) { | 101 if (!_connected.isCompleted) { |
100 Logger.root.info('WebSocketVM connection opened: ${target.networkAddress}'
); | 102 Logger.root.info('WebSocketVM connection opened: ${target.networkAddress}'
); |
101 _connected.complete(this); | 103 _connected.complete(this); |
102 } | 104 } |
103 } | 105 } |
104 Future get onConnect => _connected.future; | 106 Future get onConnect => _connected.future; |
105 void _notifyDisconnect() { | 107 void _notifyDisconnect() { |
| 108 if (!_hasFinishedConnect) { |
| 109 return; |
| 110 } |
106 if (!_disconnected.isCompleted) { | 111 if (!_disconnected.isCompleted) { |
107 Logger.root.info('WebSocketVM connection error: ${target.networkAddress}')
; | 112 Logger.root.info('WebSocketVM connection error: ${target.networkAddress}')
; |
108 _disconnected.complete(this); | 113 _disconnected.complete(this); |
109 } | 114 } |
110 } | 115 } |
111 Future get onDisconnect => _disconnected.future; | 116 Future get onDisconnect => _disconnected.future; |
112 | 117 |
113 void disconnect() { | 118 void disconnect() { |
114 if (_hasInitiatedConnect) { | 119 if (_hasInitiatedConnect) { |
115 _webSocket.close(); | 120 _webSocket.close(); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 'query': request.id | 281 'query': request.id |
277 } | 282 } |
278 }); | 283 }); |
279 } else { | 284 } else { |
280 message = JSON.encode({'seq': serial, 'request': request.id}); | 285 message = JSON.encode({'seq': serial, 'request': request.id}); |
281 } | 286 } |
282 // Send message. | 287 // Send message. |
283 _webSocket.send(message); | 288 _webSocket.send(message); |
284 } | 289 } |
285 } | 290 } |
OLD | NEW |