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

Unified Diff: mojo/public/dart/bindings/lib/src/interface.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/src/codec.dart ('k') | mojo/public/dart/bindings/test/codec_test.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/interface.dart
diff --git a/mojo/public/dart/bindings/lib/src/interface.dart b/mojo/public/dart/bindings/lib/src/interface.dart
new file mode 100644
index 0000000000000000000000000000000000000000..1085a28ee6016943c68673c77bac767a97a8a7a3
--- /dev/null
+++ b/mojo/public/dart/bindings/lib/src/interface.dart
@@ -0,0 +1,69 @@
+// 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 Interface {
+ core.MojoMessagePipeEndpoint _endpoint;
+ core.MojoHandle _handle;
+ List _send_queue;
+
+ Future<Message> handleMessage(MessageReader reader);
+
+ Interface(this._endpoint) {
+ _send_queue = [];
+ _handle = new core.MojoHandle(_endpoint.handle);
+ _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 and view as a message.
+ ByteData bytes = new ByteData(size);
+ _endpoint.read(bytes);
+ var message = new Message(bytes, null);
+ var reader = new MessageReader(message);
+
+ // Prepare the response.
+ handleMessage(reader).then((response_message) {
+ // If there's a response, queue it up for sending.
+ if (response_message != null) {
+ _send_queue.add(response_message);
+ if ((_send_queue.length > 0) && !_handle.writeEnabled()) {
+ _handle.toggleWriteEvents();
+ }
+ }
+ });
+ }
+ if (core.MojoHandleSignals.isWritable(mojo_handle_signal)) {
+ if (_send_queue.length > 0) {
+ var response_message = _send_queue.removeAt(0);
+ _endpoint.write(response_message.buffer);
+ if (_endpoint.status != core.MojoResult.OK) {
+ throw new Exception("endpoint write failed");
+ }
+ }
+ if ((_send_queue.length == 0) && _handle.writeEnabled()) {
+ _handle.toggleWriteEvents();
+ }
+ }
+ }, onDone: () {
+ _handle.close();
+ });
+ }
+
+ void buildResponse(Completer c, Type t, int name, Object response) {
+ var builder = new MessageBuilder(name, align(getEncodedSize(t)));
+ builder.encodeStruct(t, response);
+ c.complete(builder.finish());
+ }
+}
« no previous file with comments | « mojo/public/dart/bindings/lib/src/codec.dart ('k') | mojo/public/dart/bindings/test/codec_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698