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 channel; | 5 library channel; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:analysis_server/src/protocol.dart'; | 10 import 'package:analysis_server/src/protocol.dart'; |
(...skipping 11 matching lines...) Expand all Loading... | |
22 * client, invoke the [onDone] function. | 22 * client, invoke the [onDone] function. |
23 */ | 23 */ |
24 void listen(void onRequest(Request request), {void onError(), void onDone()}); | 24 void listen(void onRequest(Request request), {void onError(), void onDone()}); |
25 | 25 |
26 /** | 26 /** |
27 * Send the given [notification] to the client. | 27 * Send the given [notification] to the client. |
28 */ | 28 */ |
29 void sendNotification(Notification notification); | 29 void sendNotification(Notification notification); |
30 | 30 |
31 /** | 31 /** |
32 * Send the given [request] to the server. | |
33 */ | |
34 void sendRequest(Request request); | |
35 | |
36 /** | |
32 * Send the given [response] to the client. | 37 * Send the given [response] to the client. |
33 */ | 38 */ |
34 void sendResponse(Response response); | 39 void sendResponse(Response response); |
35 } | 40 } |
36 | 41 |
37 /** | 42 /** |
38 * Instances of the class [WebSocketChannel] implement a [CommunicationChannel] | 43 * Instances of the class [WebSocketChannel] implement a [CommunicationChannel] |
39 * that uses a [WebSocket] to communicate with clients. | 44 * that uses a [WebSocket] to communicate with clients. |
40 */ | 45 */ |
41 class WebSocketChannel implements CommunicationChannel { | 46 class WebSocketChannel implements CommunicationChannel { |
(...skipping 13 matching lines...) Expand all Loading... | |
55 _readRequest(data, onRequest), onError: onError, onDone: onDone); | 60 _readRequest(data, onRequest), onError: onError, onDone: onDone); |
56 } | 61 } |
57 | 62 |
58 @override | 63 @override |
59 void sendNotification(Notification notification) { | 64 void sendNotification(Notification notification) { |
60 JsonEncoder encoder = const JsonEncoder(null); | 65 JsonEncoder encoder = const JsonEncoder(null); |
61 socket.add(encoder.convert(notification.toJson())); | 66 socket.add(encoder.convert(notification.toJson())); |
62 } | 67 } |
63 | 68 |
64 @override | 69 @override |
70 void sendRequest(Request request) { | |
71 JsonEncoder encoder = const JsonEncoder(null); | |
Brian Wilkerson
2014/02/26 17:50:47
Should we create one instance that is re-used each
danrubel
2014/02/26 20:10:01
Good idea. Done.
| |
72 socket.add(encoder.convert(request.toJson())); | |
73 } | |
74 | |
75 @override | |
65 void sendResponse(Response response) { | 76 void sendResponse(Response response) { |
66 JsonEncoder encoder = const JsonEncoder(null); | 77 JsonEncoder encoder = const JsonEncoder(null); |
67 socket.add(encoder.convert(response.toJson())); | 78 socket.add(encoder.convert(response.toJson())); |
68 } | 79 } |
69 | 80 |
70 /** | 81 /** |
71 * Read a request from the given [data] and use the given function to handle | 82 * Read a request from the given [data] and use the given function to handle |
72 * the request. | 83 * the request. |
73 */ | 84 */ |
74 void _readRequest(Object data, void onRequest(Request request)) { | 85 void _readRequest(Object data, void onRequest(Request request)) { |
75 if (data is List<int>) { | 86 if (data is List<int>) { |
76 sendResponse(new Response.invalidRequestFormat()); | 87 sendResponse(new Response.invalidRequestFormat()); |
77 return; | 88 return; |
78 } | 89 } |
79 if (data is String) { | 90 if (data is String) { |
80 // Parse the string as a JSON descriptor and process the resulting | 91 // Parse the string as a JSON descriptor and process the resulting |
81 // structure as a request. | 92 // structure as a request. |
82 Request request = new Request.fromString(data); | 93 Request request = new Request.fromString(data); |
83 if (request == null) { | 94 if (request == null) { |
84 sendResponse(new Response.invalidRequestFormat()); | 95 sendResponse(new Response.invalidRequestFormat()); |
85 return; | 96 return; |
86 } | 97 } |
87 onRequest(request); | 98 onRequest(request); |
88 } | 99 } |
89 } | 100 } |
90 } | 101 } |
OLD | NEW |