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

Unified Diff: lib/src/isolate_channel.dart

Issue 1638183002: Add IsolateChannel.connect* constructors. (Closed) Base URL: git@github.com:dart-lang/stream_channel.git@master
Patch Set: Created 4 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 | « no previous file | lib/src/isolate_channel/send_port_sink.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/isolate_channel.dart
diff --git a/lib/src/isolate_channel.dart b/lib/src/isolate_channel.dart
index c6645438d6b02daf99ff7d916df884cabc2d59e5..a466d87f3eb698d7b7af69ad031907ff043e5417 100644
--- a/lib/src/isolate_channel.dart
+++ b/lib/src/isolate_channel.dart
@@ -5,7 +5,11 @@
import 'dart:async';
import 'dart:isolate';
+import 'package:async/async.dart';
+import 'package:stack_trace/stack_trace.dart';
+
import '../stream_channel.dart';
+import 'isolate_channel/send_port_sink.dart';
/// A [StreamChannel] that communicates over a [ReceivePort]/[SendPort] pair,
/// presumably with another isolate.
@@ -22,125 +26,71 @@ import '../stream_channel.dart';
/// ensure that they always close the [sink] of every [IsolateChannel] they use
/// to avoid leaving dangling [ReceivePort]s.
class IsolateChannel<T> extends StreamChannelMixin<T> {
- /// The port that produces incoming messages.
- ///
- /// This is wrapped in a [StreamView] to produce [stream].
- final ReceivePort _receivePort;
-
- /// The port that sends outgoing messages.
- final SendPort _sendPort;
-
- Stream<T> get stream => _stream;
- final Stream<T> _stream;
-
- StreamSink<T> get sink => _sink;
- _SendPortSink<T> _sink;
+ final Stream<T> stream;
+ final StreamSink<T> sink;
- /// Creates a stream channel that receives messages from [receivePort] and
- /// sends them over [sendPort].
- IsolateChannel(ReceivePort receivePort, this._sendPort)
- : _receivePort = receivePort,
- _stream = new StreamView<T>(receivePort) {
- _sink = new _SendPortSink<T>(this);
- }
-}
-
-/// The sink for [IsolateChannel].
-///
-/// [SendPort] doesn't natively implement any sink API, so this adds that API as
-/// a wrapper. Closing this just closes the [ReceivePort].
-class _SendPortSink<T> implements StreamSink<T> {
- /// The channel that this sink is for.
- final IsolateChannel _channel;
-
- Future get done => _doneCompleter.future;
- final _doneCompleter = new Completer();
-
- /// Whether [done] has been completed.
+ /// Connects to a remote channel that was created with
+ /// [IsolateChannel.connectSend].
///
- /// This is distinct from [_closed] because [done] can complete with an error
- /// without the user explicitly calling [close].
- bool get _isDone => _doneCompleter.isCompleted;
-
- /// Whether the user has called [close].
- bool _closed = false;
-
- /// Whether we're currently adding a stream with [addStream].
- bool _inAddStream = false;
-
- _SendPortSink(this._channel);
-
- void add(T data) {
- if (_closed) throw new StateError("Cannot add event after closing.");
- if (_inAddStream) {
- throw new StateError("Cannot add event while adding stream.");
- }
- if (_isDone) return;
-
- _add(data);
- }
-
- /// A helper for [add] that doesn't check for [StateError]s.
+ /// These constructors establish a connection using only a single
+ /// [SendPort]/[ReceivePort] pair, as long as each side uses one of the
+ /// connect constructors.
///
- /// This is called from [addStream], so it shouldn't check [_inAddStream].
- void _add(T data) {
- _channel._sendPort.send(data);
- }
-
- void addError(error, [StackTrace stackTrace]) {
- if (_closed) throw new StateError("Cannot add event after closing.");
- if (_inAddStream) {
- throw new StateError("Cannot add event while adding stream.");
- }
-
- _close(error, stackTrace);
- }
-
- Future close() {
- if (_inAddStream) {
- throw new StateError("Cannot close sink while adding stream.");
- }
+ /// The connection protocol is guaranteed to remain compatible across versions
+ /// at least until the next major version release. If the protocol is
+ /// violated, the resulting channel will emit a single value on its stream and
+ /// then close.
+ factory IsolateChannel.connectReceive(ReceivePort receivePort) {
+ // We can't use a [StreamChannelCompleter] here because we need the return
+ // value to be an [IsolateChannel].
+ var streamCompleter = new StreamCompleter<T>();
+ var sinkCompleter = new StreamSinkCompleter<T>();
+ var channel = new IsolateChannel._(
+ streamCompleter.stream, sinkCompleter.sink);
+
+ // The first message across the ReceivePort should be a SendPort pointing to
+ // the remote end. If it's not, we'll make the stream emit an error
+ // complaining.
+ var subscription;
+ subscription = receivePort.listen((message) {
+ if (message is SendPort) {
+ streamCompleter.setSourceStream(
+ new SubscriptionStream<T>(subscription));
+ sinkCompleter.setDestinationSink(
+ new SendPortSink<T>(receivePort, message));
+ return;
+ }
+
+ streamCompleter.setError(
+ new StateError('Unexpected Isolate response "$message".'),
+ new Trace.current());
+ sinkCompleter.setDestinationSink(new NullStreamSink<T>());
+ subscription.cancel();
+ });
- _closed = true;
- return _close();
+ return channel;
}
- /// A helper for [close] that doesn't check for [StateError]s.
+ /// Connects to a remote channel that was created with
+ /// [IsolateChannel.connectReceive].
///
- /// This is called from [addStream], so it shouldn't check [_inAddStream]. It
- /// also forwards [error] and [stackTrace] to [done] if they're passed.
- Future _close([error, StackTrace stackTrace]) {
- if (_isDone) return done;
-
- _channel._receivePort.close();
-
- if (error != null) {
- _doneCompleter.completeError(error, stackTrace);
- } else {
- _doneCompleter.complete();
- }
-
- return done;
+ /// These constructors establish a connection using only a single
+ /// [SendPort]/[ReceivePort] pair, as long as each side uses one of the
+ /// connect constructors.
+ ///
+ /// The connection protocol is guaranteed to remain compatible across versions
+ /// at least until the next major version release.
+ factory IsolateChannel.connectSend(SendPort sendPort) {
+ var receivePort = new ReceivePort();
Bob Nystrom 2016/01/27 20:39:47 Go home, Reitveld diff algorithm, you are drunk.
+ sendPort.send(receivePort.sendPort);
+ return new IsolateChannel(receivePort, sendPort);
}
- Future addStream(Stream<T> stream) {
- if (_closed) throw new StateError("Cannot add stream after closing.");
- if (_inAddStream) {
- throw new StateError("Cannot add stream while adding stream.");
- }
- if (_isDone) return;
+ /// Creates a stream channel that receives messages from [receivePort] and
+ /// sends them over [sendPort].
+ IsolateChannel(ReceivePort receivePort, SendPort sendPort)
+ : stream = new StreamView<T>(receivePort),
+ sink = new SendPortSink<T>(receivePort, sendPort);
- _inAddStream = true;
- var completer = new Completer.sync();
- stream.listen(_add,
- onError: (error, stackTrace) {
- _close(error, stackTrace);
- completer.complete();
- },
- onDone: completer.complete,
- cancelOnError: true);
- return completer.future.then((_) {
- _inAddStream = false;
- });
- }
+ IsolateChannel._(this.stream, this.sink);
}
« no previous file with comments | « no previous file | lib/src/isolate_channel/send_port_sink.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698