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

Unified Diff: mojo/public/dart/bindings/lib/src/client.dart

Issue 674383002: Initial work on Dart bindings for Mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Merge. Work on templates. Created 6 years, 1 month 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 | « mojo/public/dart/bindings/lib/bindings.dart ('k') | mojo/public/dart/bindings/lib/src/codec.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/dart/bindings/lib/src/client.dart
diff --git a/mojo/public/dart/bindings/lib/src/client.dart b/mojo/public/dart/bindings/lib/src/client.dart
new file mode 100644
index 0000000000000000000000000000000000000000..88dd8e7297a44609995f1506f55ec402827bff03
--- /dev/null
+++ b/mojo/public/dart/bindings/lib/src/client.dart
@@ -0,0 +1,84 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+part of bindings;
+
+abstract class Client {
+ core.MojoMessagePipeEndpoint _endpoint;
+ core.MojoHandle _handle;
+ List _send_queue;
+ List _completer_queue;
+ var _subscription;
+
+ void handleResponse(MessageReader reader);
+
+ Client(this._endpoint) {
+ _send_queue = [];
+ _completer_queue = [];
+ _handle = new core.MojoHandle(_endpoint.handle);
+ _subscription = _handle.listen((int mojo_handle_signal) {
+ if (core.MojoHandleSignals.isReadable(mojo_handle_signal)) {
+ // Query how many bytes are available.
+ List result = _endpoint.query();
+ if ((result[0] != core.MojoResult.OK) &&
+ (result[0] != core.MojoResult.RESOURCE_EXHAUSTED)) {
+ // If something else happens, it means the handle wasn't really ready
+ // for reading, which indicates a bug in MojoHandle or the
+ // event listener.
+ throw new Exception("bytes query failed: ${result[0]}");
+ }
+ int size = result[1];
+
+ // Read the data.
+ ByteData bytes = new ByteData(size);
+ _endpoint.read(bytes);
+ var message = new Message(bytes, null);
+ var reader = new MessageReader(message);
+
+ // Complete _future_queue[0] with the received string.
+ handleResponse(reader);
+ }
+ if (core.MojoHandleSignals.isWritable(mojo_handle_signal)) {
+ if (_send_queue.length > 0) {
+ List message_completer = _send_queue.removeAt(0);
+ _endpoint.write(message_completer[0].buffer);
+ if (_endpoint.status != core.MojoResult.OK) {
+ throw new Exception("endpoint write failed");
+ }
+ _completer_queue.add(message_completer[1]);
+ }
+ if ((_send_queue.length == 0) && _handle.writeEnabled()) {
+ _handle.toggleWriteEvents();
+ }
+ }
+ }, onDone: () {
+ handle.close();
+ });
+ }
+
+ void close() {
+ _subscription.cancel();
+ }
+
+ Future enqueueMessage(Type t, int name, Object msg) {
+ var builder = new MessageBuilder(name, align(getEncodedSize(t)));
+ builder.encodeStruct(t, msg);
+ var message = builder.finish();
+
+ var completer = new Completer();
+ send_queue.add([message, completer]);
+ if ((send_queue.length > 0) && !handle.writeEnabled()) {
+ handle.toggleWriteEvents();
+ }
+ return completer.future;
+ }
+
+ // We need getters for these because they are abstract in the "Calls"
+ // mixin classes.
+ List get send_queue => _send_queue;
+ core.MojoHandle get handle => _handle;
+
+ // Need a getter for this for access in subclasses.
+ List get completer_queue => _completer_queue;
+}
« no previous file with comments | « mojo/public/dart/bindings/lib/bindings.dart ('k') | mojo/public/dart/bindings/lib/src/codec.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698