OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 part of bindings; | 5 part of bindings; |
6 | 6 |
7 abstract class Interface { | 7 abstract class Interface { |
8 core.MojoMessagePipeEndpoint _endpoint; | 8 core.MojoMessagePipeEndpoint _endpoint; |
9 core.MojoHandle _handle; | 9 core.MojoHandle _handle; |
10 List _sendQueue; | 10 List _sendQueue; |
| 11 bool _isOpen; |
11 | 12 |
12 Message handleMessage(MessageReader reader, Function messageHandler); | 13 Message handleMessage(MessageReader reader); |
13 | 14 |
14 Interface(this._endpoint) { | 15 Interface(this._endpoint) { |
15 _sendQueue = []; | 16 _sendQueue = []; |
16 _handle = new core.MojoHandle(_endpoint.handle); | 17 _handle = new core.MojoHandle(_endpoint.handle); |
| 18 _isOpen = false; |
17 } | 19 } |
18 | 20 |
19 StreamSubscription<int> listen(Function messageHandler) { | 21 StreamSubscription<int> listen() { |
| 22 _isOpen = true; |
20 return _handle.listen((int mojoSignal) { | 23 return _handle.listen((int mojoSignal) { |
21 if (core.MojoHandleSignals.isReadable(mojoSignal)) { | 24 if (core.MojoHandleSignals.isReadable(mojoSignal)) { |
22 // Query how many bytes are available. | 25 // Query how many bytes are available. |
23 var result = _endpoint.query(); | 26 var result = _endpoint.query(); |
24 if (!result.status.isOk && !result.status.isResourceExhausted) { | 27 if (!result.status.isOk && !result.status.isResourceExhausted) { |
25 // If something else happens, it means the handle wasn't really ready | 28 // If something else happens, it means the handle wasn't really ready |
26 // for reading, which indicates a bug in MojoHandle or the | 29 // for reading, which indicates a bug in MojoHandle or the |
27 // event listener. | 30 // event listener. |
28 throw new Exception("message pipe query failed: ${result.status}"); | 31 throw new Exception("message pipe query failed: ${result.status}"); |
29 } | 32 } |
30 | 33 |
31 // Read the data and view as a message. | 34 // Read the data and view as a message. |
32 var bytes = new ByteData(result.bytesRead); | 35 var bytes = new ByteData(result.bytesRead); |
33 var handles = new List<core.RawMojoHandle>(result.handlesRead); | 36 var handles = new List<core.RawMojoHandle>(result.handlesRead); |
34 result = _endpoint.read(bytes, result.bytesRead, handles); | 37 result = _endpoint.read(bytes, result.bytesRead, handles); |
35 if (!result.status.isOk && !result.status.isResourceExhausted) { | 38 if (!result.status.isOk && !result.status.isResourceExhausted) { |
36 // If something else happens, it means the handle wasn't really ready | 39 // If something else happens, it means the handle wasn't really ready |
37 // for reading, which indicates a bug in MojoHandle or the | 40 // for reading, which indicates a bug in MojoHandle or the |
38 // event listener. | 41 // event listener. |
39 throw new Exception("message pipe read failed: ${result.status}"); | 42 throw new Exception("message pipe read failed: ${result.status}"); |
40 } | 43 } |
41 var message = new Message(bytes, handles); | 44 var message = new Message(bytes, handles); |
42 var reader = new MessageReader(message); | 45 var reader = new MessageReader(message); |
43 | 46 |
44 // Prepare the response. | 47 // Prepare the response. |
45 var response_message = handleMessage(reader, messageHandler); | 48 var responseMessage = handleMessage(reader); |
46 // If there's a response, queue it up for sending. | 49 // If there's a response, queue it up for sending. |
47 if (response_message != null) { | 50 if (responseMessage != null) { |
48 _sendQueue.add(response_message); | 51 _sendQueue.add(responseMessage); |
49 if ((_sendQueue.length > 0) && !_handle.writeEnabled()) { | 52 if ((_sendQueue.length > 0) && !_handle.writeEnabled()) { |
50 _handle.enableWriteEvents(); | 53 _handle.enableWriteEvents(); |
51 } | 54 } |
52 } | 55 } |
53 } | 56 } |
54 if (core.MojoHandleSignals.isWritable(mojoSignal)) { | 57 if (core.MojoHandleSignals.isWritable(mojoSignal)) { |
55 if (_sendQueue.length > 0) { | 58 if (_sendQueue.length > 0) { |
56 var response_message = _sendQueue.removeAt(0); | 59 var responseMessage = _sendQueue.removeAt(0); |
57 _endpoint.write(response_message.buffer); | 60 _endpoint.write(responseMessage.buffer, |
| 61 responseMessage.buffer.lengthInBytes, |
| 62 responseMessage.handles); |
58 if (!_endpoint.status.isOk) { | 63 if (!_endpoint.status.isOk) { |
59 throw new Exception("message pipe write failed"); | 64 throw new Exception("message pipe write failed"); |
60 } | 65 } |
61 } | 66 } |
62 if ((_sendQueue.length == 0) && _handle.writeEnabled()) { | 67 if ((_sendQueue.length == 0) && _handle.writeEnabled()) { |
63 _handle.disableWriteEvents(); | 68 _handle.disableWriteEvents(); |
64 } | 69 } |
65 } | 70 } |
66 if (core.MojoHandleSignals.isNone(mojoSignal)) { | 71 if (core.MojoHandleSignals.isNone(mojoSignal)) { |
67 // The handle watcher will send MojoHandleSignals.NONE when the other | 72 // The handle watcher will send MojoHandleSignals.NONE when the other |
68 // endpoint of the pipe is closed. | 73 // endpoint of the pipe is closed. |
69 _handle.close(); | 74 _handle.close(); |
70 } | 75 } |
71 }); | 76 }); |
72 } | 77 } |
73 | 78 |
74 Message buildResponse(Type t, int name, Object response) { | 79 Message buildResponse(Type t, int name, Object response) { |
75 var builder = new MessageBuilder(name, align(getEncodedSize(t))); | 80 var builder = new MessageBuilder(name, align(getEncodedSize(t))); |
76 builder.encodeStruct(t, response); | 81 builder.encodeStruct(t, response); |
77 return builder.finish(); | 82 return builder.finish(); |
78 } | 83 } |
79 | 84 |
80 Message buildResponseWithID(Type t, int name, int id, Object response) { | 85 Message buildResponseWithID( |
| 86 Type t, int name, int id, int flags, Object response) { |
81 var builder = new MessageWithRequestIDBuilder( | 87 var builder = new MessageWithRequestIDBuilder( |
82 name, align(getEncodedSize(t)), id); | 88 name, align(getEncodedSize(t)), id, flags); |
83 builder.encodeStruct(t, response); | 89 builder.encodeStruct(t, response); |
84 return builder.finish(); | 90 return builder.finish(); |
85 } | 91 } |
| 92 |
| 93 void enqueueMessage(Type t, int name, Object msg) { |
| 94 var builder = new MessageBuilder(name, align(getEncodedSize(t))); |
| 95 builder.encodeStruct(t, msg); |
| 96 var message = builder.finish(); |
| 97 _sendQueue.add(message); |
| 98 if (!_handle.writeEnabled()) { |
| 99 _handle.enableWriteEvents(); |
| 100 } |
| 101 } |
| 102 |
| 103 Future enqueueMessageWithRequestID(Type t, int name, int id, Object msg) { |
| 104 throw new Exception("The client Mixin should not expect a response"); |
| 105 } |
| 106 |
| 107 bool get isOpen => _isOpen; |
86 } | 108 } |
OLD | NEW |