| 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_io; | 5 library service_io; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
| 10 | 10 |
| 11 import 'package:logging/logging.dart'; | 11 import 'package:logging/logging.dart'; |
| 12 import 'package:observatory/service_common.dart'; | 12 import 'package:observatory/service_common.dart'; |
| 13 | 13 |
| 14 // Export the service library. | 14 // Export the service library. |
| 15 export 'package:observatory/service_common.dart'; | 15 export 'package:observatory/service_common.dart'; |
| 16 | 16 |
| 17 class _IOWebSocket implements CommonWebSocket { | 17 class _IOWebSocket implements CommonWebSocket { |
| 18 WebSocket _webSocket; | 18 WebSocket _webSocket; |
| 19 | 19 |
| 20 void connect(String address, | 20 void connect(String address, |
| 21 void onOpen(), | 21 void onOpen(), |
| 22 void onMessage(dynamic data), | 22 void onMessage(dynamic data), |
| 23 void onError(), | 23 void onError(), |
| 24 void onClose()) { | 24 void onClose()) { |
| 25 WebSocket.connect(address).then((WebSocket socket) { | 25 WebSocket.connect(address).then((WebSocket socket) { |
| 26 _webSocket = socket; | 26 _webSocket = socket; |
| 27 _webSocket.listen( | 27 _webSocket.listen( |
| 28 onMessage, | 28 onMessage, |
| 29 onError: (dynamic) => onError(), | 29 onError: (dynamic) => onError(), |
| 30 onDone: onClose, | 30 onDone: onClose, |
| 31 cancelOnError: true); | 31 cancelOnError: true); |
| 32 onOpen(); | 32 onOpen(); |
| 33 }).catchError((e, st) { | 33 }).catchError((e, st) { |
| 34 onError(); | 34 onError(); |
| 35 }); | 35 }); |
| 36 } | 36 } |
| 37 | 37 |
| 38 bool get isOpen => | 38 bool get isOpen => |
| 39 (_webSocket != null) && (_webSocket.readyState == WebSocket.OPEN); | 39 (_webSocket != null) && (_webSocket.readyState == WebSocket.OPEN); |
| 40 | 40 |
| 41 void send(dynamic data) { | 41 void send(dynamic data) { |
| 42 _webSocket.add(data); | 42 _webSocket.add(data); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void close() { | 45 void close() { |
| 46 if (_webSocket != null) { | 46 if (_webSocket != null) { |
| 47 _webSocket.close(); | 47 _webSocket.close(); |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 Future<ByteData> nonStringToByteData(dynamic data) { | 51 Future<ByteData> nonStringToByteData(dynamic data) { |
| 52 assert(data is Uint8List); | 52 assert(data is Uint8List); |
| 53 Logger.root.info('Binary data size in bytes: ${data.lengthInBytes}'); | 53 Logger.root.info('Binary data size in bytes: ${data.lengthInBytes}'); |
| 54 return new Future.sync(() => | 54 return new Future.sync(() => |
| 55 new ByteData.view(data.buffer, | 55 new ByteData.view(data.buffer, |
| 56 data.offsetInBytes, | 56 data.offsetInBytes, |
| 57 data.lengthInBytes)); | 57 data.lengthInBytes)); |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 /// The [WebSocketVM] communicates with a Dart VM over WebSocket. The Dart VM | 61 /// The [WebSocketVM] communicates with a Dart VM over WebSocket. The Dart VM |
| 62 /// can be embedded in Chromium or standalone. In the case of Chromium, we | 62 /// can be embedded in Chromium or standalone. In the case of Chromium, we |
| 63 /// make the service requests via the Chrome Remote Debugging Protocol. | 63 /// make the service requests via the Chrome Remote Debugging Protocol. |
| 64 class WebSocketVM extends CommonWebSocketVM { | 64 class WebSocketVM extends CommonWebSocketVM { |
| 65 WebSocketVM(WebSocketVMTarget target) : super(target, new _IOWebSocket()); | 65 WebSocketVM(WebSocketVMTarget target) : super(target, new _IOWebSocket()); |
| 66 } | 66 } |
| OLD | NEW |