OLD | NEW |
| (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 extends core.MojoEventStreamListener { | |
8 Map<int, Completer> _completerMap; | |
9 int _nextId = 0; | |
10 | |
11 Client(core.MojoMessagePipeEndpoint endpoint) : | |
12 _completerMap = {}, | |
13 super(endpoint); | |
14 | |
15 Client.fromHandle(core.MojoHandle handle) : | |
16 _completerMap = {}, | |
17 super.fromHandle(handle); | |
18 | |
19 Client.unbound() : | |
20 _completerMap = {}, | |
21 super.unbound(); | |
22 | |
23 void handleResponse(ServiceMessage reader); | |
24 | |
25 void handleRead() { | |
26 // Query how many bytes are available. | |
27 var result = endpoint.query(); | |
28 assert(result.status.isOk || result.status.isResourceExhausted); | |
29 | |
30 // Read the data. | |
31 var bytes = new ByteData(result.bytesRead); | |
32 var handles = new List<core.MojoHandle>(result.handlesRead); | |
33 result = endpoint.read(bytes, result.bytesRead, handles); | |
34 assert(result.status.isOk || result.status.isResourceExhausted); | |
35 var message = new ServiceMessage.fromMessage(new Message(bytes, handles)); | |
36 handleResponse(message); | |
37 } | |
38 | |
39 void handleWrite() { | |
40 throw 'Unexpected write signal in client.'; | |
41 } | |
42 | |
43 void sendMessage(Struct message, int name) { | |
44 if (!isOpen) { | |
45 listen(); | |
46 } | |
47 var header = new MessageHeader(name); | |
48 var serviceMessage = message.serializeWithHeader(header); | |
49 endpoint.write(serviceMessage.buffer, | |
50 serviceMessage.buffer.lengthInBytes, | |
51 serviceMessage.handles); | |
52 if (!endpoint.status.isOk) { | |
53 throw "message pipe write failed"; | |
54 } | |
55 } | |
56 | |
57 Future sendMessageWithRequestId( | |
58 Struct message, int name, int id, int flags) { | |
59 if (!isOpen) { | |
60 listen(); | |
61 } | |
62 if (id == -1) { | |
63 id = _nextId++; | |
64 } | |
65 | |
66 var header = new MessageHeader.withRequestId(name, flags, id); | |
67 var serviceMessage = message.serializeWithHeader(header); | |
68 endpoint.write(serviceMessage.buffer, | |
69 serviceMessage.buffer.lengthInBytes, | |
70 serviceMessage.handles); | |
71 if (!endpoint.status.isOk) { | |
72 throw "message pipe write failed"; | |
73 } | |
74 | |
75 var completer = new Completer(); | |
76 _completerMap[id] = completer; | |
77 return completer.future; | |
78 } | |
79 | |
80 // Need a getter for this for access in subclasses. | |
81 Map<int, Completer> get completerMap => _completerMap; | |
82 } | |
OLD | NEW |