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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 part of bindings;
6
7 abstract class Interface {
8 core.MojoMessagePipeEndpoint _endpoint;
9 core.MojoHandle _handle;
10 List _send_queue;
11
12 Future<Message> handleMessage(MessageReader reader);
13
14 Interface(this._endpoint) {
15 _send_queue = [];
16 _handle = new core.MojoHandle(_endpoint.handle);
17 _handle.listen((int mojo_handle_signal) {
18 if (core.MojoHandleSignals.isReadable(mojo_handle_signal)) {
19 // Query how many bytes are available.
20 List result = _endpoint.query();
21 if ((result[0] != core.MojoResult.OK) &&
22 (result[0] != core.MojoResult.RESOURCE_EXHAUSTED)) {
23 // If something else happens, it means the handle wasn't really ready
24 // for reading, which indicates a bug in MojoHandle or the
25 // event listener.
26 throw new Exception("bytes query failed: ${result[0]}");
27 }
28 int size = result[1];
29
30 // Read the data and view as a message.
31 ByteData bytes = new ByteData(size);
32 _endpoint.read(bytes);
33 var message = new Message(bytes, null);
34 var reader = new MessageReader(message);
35
36 // Prepare the response.
37 handleMessage(reader).then((response_message) {
38 // If there's a response, queue it up for sending.
39 if (response_message != null) {
40 _send_queue.add(response_message);
41 if ((_send_queue.length > 0) && !_handle.writeEnabled()) {
42 _handle.toggleWriteEvents();
43 }
44 }
45 });
46 }
47 if (core.MojoHandleSignals.isWritable(mojo_handle_signal)) {
48 if (_send_queue.length > 0) {
49 var response_message = _send_queue.removeAt(0);
50 _endpoint.write(response_message.buffer);
51 if (_endpoint.status != core.MojoResult.OK) {
52 throw new Exception("endpoint write failed");
53 }
54 }
55 if ((_send_queue.length == 0) && _handle.writeEnabled()) {
56 _handle.toggleWriteEvents();
57 }
58 }
59 }, onDone: () {
60 _handle.close();
61 });
62 }
63
64 void buildResponse(Completer c, Type t, int name, Object response) {
65 var builder = new MessageBuilder(name, align(getEncodedSize(t)));
66 builder.encodeStruct(t, response);
67 c.complete(builder.finish());
68 }
69 }
OLDNEW
« 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