Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(834)

Unified Diff: pkg/analysis_server/lib/src/channel/byte_stream_channel.dart

Issue 872633002: Use 'print' to send responses/notifications to the client. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use non-blocking 'stdout', revert 'print' changes. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analysis_server/doc/api.html ('k') | pkg/analysis_server/lib/src/server/stdio_server.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/lib/src/channel/byte_stream_channel.dart
diff --git a/pkg/analysis_server/lib/src/channel/byte_stream_channel.dart b/pkg/analysis_server/lib/src/channel/byte_stream_channel.dart
index bcb86ccb7c48ddc0cbddcd83f0842c8456e14f9e..ce6eff5cad83e434ba717787b4b0aee6de53fd42 100644
--- a/pkg/analysis_server/lib/src/channel/byte_stream_channel.dart
+++ b/pkg/analysis_server/lib/src/channel/byte_stream_channel.dart
@@ -61,36 +61,14 @@ class ByteStreamClientChannel implements ClientCommunicationChannel {
* standard input and standard output) to communicate with clients.
*/
class ByteStreamServerChannel implements ServerCommunicationChannel {
- /**
- * Value of [_outputState] indicating that there is no outstanding data in
- * [_pendingOutput], and that the most recent flush of [_output] has
- * completed.
- */
- static const int _STATE_IDLE = 0;
-
- /**
- * Value of [_outputState] indicating that there is outstanding data in
- * [_pendingOutput], and that the most recent flush of [_output] has
- * completed; therefore a microtask has been scheduled to send the data.
- */
- static const int _STATE_MICROTASK_PENDING = 1;
-
- /**
- * Value of [_outputState] indicating that data has been sent to the
- * [_output] stream and flushed, but the flush has not completed, so we must
- * wait for it to complete before sending more data. There may or may not be
- * outstanding data in [_pendingOutput].
- */
- static const int _STATE_FLUSH_PENDING = 2;
-
- final Stream input;
+ final Stream _input;
final IOSink _output;
/**
* The instrumentation service that is to be used by this analysis server.
*/
- final InstrumentationService instrumentationService;
+ final InstrumentationService _instrumentationService;
/**
* Completer that will be signalled when the input stream is closed.
@@ -98,23 +76,12 @@ class ByteStreamServerChannel implements ServerCommunicationChannel {
final Completer _closed = new Completer();
/**
- * State of the output stream (see constants above).
- */
- int _outputState = _STATE_IDLE;
-
- /**
- * List of strings that need to be sent to [_output] at the next available
- * opportunity.
- */
- List<String> _pendingOutput = <String>[];
-
- /**
* True if [close] has been called.
*/
bool _closeRequested = false;
- ByteStreamServerChannel(this.input, this._output,
- this.instrumentationService);
+ ByteStreamServerChannel(this._input, this._output,
+ this._instrumentationService);
/**
* Future that will be completed when the input stream is closed.
@@ -127,20 +94,15 @@ class ByteStreamServerChannel implements ServerCommunicationChannel {
void close() {
if (!_closeRequested) {
_closeRequested = true;
- if (_outputState == _STATE_IDLE) {
- assert(!_closed.isCompleted);
- _closed.complete();
- } else {
- // Nothing to do. [_flushCompleted] will call _closed.complete() after
- // the flush completes.
- }
+ assert(!_closed.isCompleted);
+ _closed.complete();
}
}
@override
void listen(void onRequest(Request request), {Function onError, void
onDone()}) {
- input.transform(
+ _input.transform(
(new Utf8Codec()).decoder).transform(
new LineSplitter()).listen(
(String data) => _readRequest(data, onRequest),
@@ -162,7 +124,7 @@ class ByteStreamServerChannel implements ServerCommunicationChannel {
String jsonEncoding = JSON.encode(notification.toJson());
ServerCommunicationChannel.ToJson.stop();
_outputLine(jsonEncoding);
- instrumentationService.logNotification(jsonEncoding);
+ _instrumentationService.logNotification(jsonEncoding);
}
@override
@@ -176,53 +138,14 @@ class ByteStreamServerChannel implements ServerCommunicationChannel {
String jsonEncoding = JSON.encode(response.toJson());
ServerCommunicationChannel.ToJson.stop();
_outputLine(jsonEncoding);
- instrumentationService.logResponse(jsonEncoding);
- }
-
- /**
- * Callback invoked after a flush of [_output] completes. Closes the stream
- * if necessary. Otherwise schedules additional pending output.
- */
- void _flushCompleted(_) {
- assert(_outputState == _STATE_FLUSH_PENDING);
- if (_pendingOutput.isNotEmpty) {
- _output.write(_pendingOutput.join());
- _output.flush().then(_flushCompleted);
- _pendingOutput.clear();
- // Since we've done another flush, stay in _STATE_FLUSH_PENDING.
- } else {
- _outputState = _STATE_IDLE;
- if (_closeRequested) {
- assert(!_closed.isCompleted);
- _closed.complete();
- }
- }
- }
-
- /**
- * Microtask that writes pending output to the output stream and flushes it.
- */
- void _microtask() {
- assert(_outputState == _STATE_MICROTASK_PENDING);
- _output.write(_pendingOutput.join());
- _output.flush().then(_flushCompleted);
- _pendingOutput.clear();
- _outputState = _STATE_FLUSH_PENDING;
+ _instrumentationService.logResponse(jsonEncoding);
}
/**
* Send the string [s] to [_output] followed by a newline.
*/
void _outputLine(String s) {
- _pendingOutput.add(s);
- _pendingOutput.add('\n');
- if (_outputState == _STATE_IDLE) {
- // Don't send the output just yet; schedule a microtask to do it, so that
- // if caller decides to output additional lines, they will get sent in
- // the same call to _output.write().
- new Future.microtask(_microtask);
- _outputState = _STATE_MICROTASK_PENDING;
- }
+ stdout.writeln(s);
Paul Berry 2015/01/23 21:26:00 Did you mean to do _output.writeln()?
}
/**
@@ -234,7 +157,7 @@ class ByteStreamServerChannel implements ServerCommunicationChannel {
if (_closed.isCompleted) {
return;
}
- instrumentationService.logRequest(data);
+ _instrumentationService.logRequest(data);
// Parse the string as a JSON descriptor and process the resulting
// structure as a request.
ServerCommunicationChannel.FromJson.start();
« no previous file with comments | « pkg/analysis_server/doc/api.html ('k') | pkg/analysis_server/lib/src/server/stdio_server.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698