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