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

Side by Side 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 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 Client {
8 core.MojoMessagePipeEndpoint _endpoint;
9 core.MojoHandle _handle;
10 List _send_queue;
11 List _completer_queue;
12 var _subscription;
13
14 void handleResponse(MessageReader reader);
15
16 Client(this._endpoint) {
17 _send_queue = [];
18 _completer_queue = [];
19 _handle = new core.MojoHandle(_endpoint.handle);
20 _subscription = _handle.listen((int mojo_handle_signal) {
21 if (core.MojoHandleSignals.isReadable(mojo_handle_signal)) {
22 // Query how many bytes are available.
23 List result = _endpoint.query();
24 if ((result[0] != core.MojoResult.OK) &&
25 (result[0] != core.MojoResult.RESOURCE_EXHAUSTED)) {
26 // If something else happens, it means the handle wasn't really ready
27 // for reading, which indicates a bug in MojoHandle or the
28 // event listener.
29 throw new Exception("bytes query failed: ${result[0]}");
30 }
31 int size = result[1];
32
33 // Read the data.
34 ByteData bytes = new ByteData(size);
35 _endpoint.read(bytes);
36 var message = new Message(bytes, null);
37 var reader = new MessageReader(message);
38
39 // Complete _future_queue[0] with the received string.
40 handleResponse(reader);
41 }
42 if (core.MojoHandleSignals.isWritable(mojo_handle_signal)) {
43 if (_send_queue.length > 0) {
44 List message_completer = _send_queue.removeAt(0);
45 _endpoint.write(message_completer[0].buffer);
46 if (_endpoint.status != core.MojoResult.OK) {
47 throw new Exception("endpoint write failed");
48 }
49 _completer_queue.add(message_completer[1]);
50 }
51 if ((_send_queue.length == 0) && _handle.writeEnabled()) {
52 _handle.toggleWriteEvents();
53 }
54 }
55 }, onDone: () {
56 handle.close();
57 });
58 }
59
60 void close() {
61 _subscription.cancel();
62 }
63
64 Future enqueueMessage(Type t, int name, Object msg) {
65 var builder = new MessageBuilder(name, align(getEncodedSize(t)));
66 builder.encodeStruct(t, msg);
67 var message = builder.finish();
68
69 var completer = new Completer();
70 send_queue.add([message, completer]);
71 if ((send_queue.length > 0) && !handle.writeEnabled()) {
72 handle.toggleWriteEvents();
73 }
74 return completer.future;
75 }
76
77 // We need getters for these because they are abstract in the "Calls"
78 // mixin classes.
79 List get send_queue => _send_queue;
80 core.MojoHandle get handle => _handle;
81
82 // Need a getter for this for access in subclasses.
83 List get completer_queue => _completer_queue;
84 }
OLDNEW
« 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