| 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 { | 
| 42   /** | 47   /** | 
| 43    * The socket being wrapped. | 48    * The socket being wrapped. | 
| 44    */ | 49    */ | 
| 45   final WebSocket socket; | 50   final WebSocket socket; | 
| 46 | 51 | 
|  | 52   final JsonEncoder jsonEncoder = const JsonEncoder(null); | 
|  | 53 | 
| 47   /** | 54   /** | 
| 48    * Initialize a newly create [WebSocket] wrapper to wrap the given [socket]. | 55    * Initialize a newly create [WebSocket] wrapper to wrap the given [socket]. | 
| 49    */ | 56    */ | 
| 50   WebSocketChannel(this.socket); | 57   WebSocketChannel(this.socket); | 
| 51 | 58 | 
| 52   @override | 59   @override | 
| 53   void listen(void onRequest(Request request), {void onError(), void onDone()}) 
     { | 60   void listen(void onRequest(Request request), {void onError(), void onDone()}) 
     { | 
| 54     socket.listen((data) => | 61     socket.listen((data) => | 
| 55         _readRequest(data, onRequest), onError: onError, onDone: onDone); | 62         _readRequest(data, onRequest), onError: onError, onDone: onDone); | 
| 56   } | 63   } | 
| 57 | 64 | 
| 58   @override | 65   @override | 
| 59   void sendNotification(Notification notification) { | 66   void sendNotification(Notification notification) { | 
| 60     JsonEncoder encoder = const JsonEncoder(null); | 67     socket.add(jsonEncoder.convert(notification.toJson())); | 
| 61     socket.add(encoder.convert(notification.toJson())); | 68   } | 
|  | 69 | 
|  | 70   @override | 
|  | 71   void sendRequest(Request request) { | 
|  | 72     socket.add(jsonEncoder.convert(request.toJson())); | 
| 62   } | 73   } | 
| 63 | 74 | 
| 64   @override | 75   @override | 
| 65   void sendResponse(Response response) { | 76   void sendResponse(Response response) { | 
| 66     JsonEncoder encoder = const JsonEncoder(null); | 77     socket.add(jsonEncoder.convert(response.toJson())); | 
| 67     socket.add(encoder.convert(response.toJson())); |  | 
| 68   } | 78   } | 
| 69 | 79 | 
| 70   /** | 80   /** | 
| 71    * Read a request from the given [data] and use the given function to handle | 81    * Read a request from the given [data] and use the given function to handle | 
| 72    * the request. | 82    * the request. | 
| 73    */ | 83    */ | 
| 74   void _readRequest(Object data, void onRequest(Request request)) { | 84   void _readRequest(Object data, void onRequest(Request request)) { | 
| 75     if (data is List<int>) { | 85     if (data is List<int>) { | 
| 76       sendResponse(new Response.invalidRequestFormat()); | 86       sendResponse(new Response.invalidRequestFormat()); | 
| 77       return; | 87       return; | 
| 78     } | 88     } | 
| 79     if (data is String) { | 89     if (data is String) { | 
| 80       // Parse the string as a JSON descriptor and process the resulting | 90       // Parse the string as a JSON descriptor and process the resulting | 
| 81       // structure as a request. | 91       // structure as a request. | 
| 82       Request request = new Request.fromString(data); | 92       Request request = new Request.fromString(data); | 
| 83       if (request == null) { | 93       if (request == null) { | 
| 84         sendResponse(new Response.invalidRequestFormat()); | 94         sendResponse(new Response.invalidRequestFormat()); | 
| 85         return; | 95         return; | 
| 86       } | 96       } | 
| 87       onRequest(request); | 97       onRequest(request); | 
| 88     } | 98     } | 
| 89   } | 99   } | 
| 90 } | 100 } | 
| OLD | NEW | 
|---|