| 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.byte_stream; | 5 library channel.byte_stream; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 } | 42 } |
| 43 | 43 |
| 44 @override | 44 @override |
| 45 Future close() { | 45 Future close() { |
| 46 return output.close(); | 46 return output.close(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 @override | 49 @override |
| 50 Future<Response> sendRequest(Request request) { | 50 Future<Response> sendRequest(Request request) { |
| 51 String id = request.id; | 51 String id = request.id; |
| 52 output.writeln(JSON.encode(request.toJson())); | 52 output.write(JSON.encode(request.toJson()) + '\n'); |
| 53 return responseStream.firstWhere((Response response) => response.id == id); | 53 return responseStream.firstWhere((Response response) => response.id == id); |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 /** | 57 /** |
| 58 * Instances of the class [ByteStreamServerChannel] implement a | 58 * Instances of the class [ByteStreamServerChannel] implement a |
| 59 * [ServerCommunicationChannel] that uses a stream and a sink (typically, | 59 * [ServerCommunicationChannel] that uses a stream and a sink (typically, |
| 60 * standard input and standard output) to communicate with clients. | 60 * standard input and standard output) to communicate with clients. |
| 61 */ | 61 */ |
| 62 class ByteStreamServerChannel implements ServerCommunicationChannel { | 62 class ByteStreamServerChannel implements ServerCommunicationChannel { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 @override | 103 @override |
| 104 void sendNotification(Notification notification) { | 104 void sendNotification(Notification notification) { |
| 105 // Don't send any further notifications after the communication channel is | 105 // Don't send any further notifications after the communication channel is |
| 106 // closed. | 106 // closed. |
| 107 if (_closed.isCompleted) { | 107 if (_closed.isCompleted) { |
| 108 return; | 108 return; |
| 109 } | 109 } |
| 110 ServerCommunicationChannel.ToJson.start(); | 110 ServerCommunicationChannel.ToJson.start(); |
| 111 String jsonEncoding = JSON.encode(notification.toJson()); | 111 String jsonEncoding = JSON.encode(notification.toJson()); |
| 112 ServerCommunicationChannel.ToJson.stop(); | 112 ServerCommunicationChannel.ToJson.stop(); |
| 113 output.writeln(jsonEncoding); | 113 output.write(jsonEncoding + '\n'); |
| 114 } | 114 } |
| 115 | 115 |
| 116 @override | 116 @override |
| 117 void sendResponse(Response response) { | 117 void sendResponse(Response response) { |
| 118 // Don't send any further responses after the communication channel is | 118 // Don't send any further responses after the communication channel is |
| 119 // closed. | 119 // closed. |
| 120 if (_closed.isCompleted) { | 120 if (_closed.isCompleted) { |
| 121 return; | 121 return; |
| 122 } | 122 } |
| 123 ServerCommunicationChannel.ToJson.start(); | 123 ServerCommunicationChannel.ToJson.start(); |
| 124 String jsonEncoding = JSON.encode(response.toJson()); | 124 String jsonEncoding = JSON.encode(response.toJson()); |
| 125 ServerCommunicationChannel.ToJson.stop(); | 125 ServerCommunicationChannel.ToJson.stop(); |
| 126 output.writeln(jsonEncoding); | 126 output.write(jsonEncoding + '\n'); |
| 127 } | 127 } |
| 128 | 128 |
| 129 /** | 129 /** |
| 130 * Read a request from the given [data] and use the given function to handle | 130 * Read a request from the given [data] and use the given function to handle |
| 131 * the request. | 131 * the request. |
| 132 */ | 132 */ |
| 133 void _readRequest(Object data, void onRequest(Request request)) { | 133 void _readRequest(Object data, void onRequest(Request request)) { |
| 134 // Ignore any further requests after the communication channel is closed. | 134 // Ignore any further requests after the communication channel is closed. |
| 135 if (_closed.isCompleted) { | 135 if (_closed.isCompleted) { |
| 136 return; | 136 return; |
| 137 } | 137 } |
| 138 // Parse the string as a JSON descriptor and process the resulting | 138 // Parse the string as a JSON descriptor and process the resulting |
| 139 // structure as a request. | 139 // structure as a request. |
| 140 ServerCommunicationChannel.FromJson.start(); | 140 ServerCommunicationChannel.FromJson.start(); |
| 141 Request request = new Request.fromString(data); | 141 Request request = new Request.fromString(data); |
| 142 ServerCommunicationChannel.FromJson.stop(); | 142 ServerCommunicationChannel.FromJson.stop(); |
| 143 if (request == null) { | 143 if (request == null) { |
| 144 sendResponse(new Response.invalidRequestFormat()); | 144 sendResponse(new Response.invalidRequestFormat()); |
| 145 return; | 145 return; |
| 146 } | 146 } |
| 147 onRequest(request); | 147 onRequest(request); |
| 148 } | 148 } |
| 149 } | 149 } |
| OLD | NEW |