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()); |
+ } |
+} |