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 Stub extends core.MojoEventStreamListener { | |
8 int _outstandingResponseFutures = 0; | |
9 bool _isClosing = false; | |
10 Completer _closeCompleter; | |
11 | |
12 Stub.fromEndpoint(core.MojoMessagePipeEndpoint endpoint) | |
13 : super.fromEndpoint(endpoint); | |
14 | |
15 Stub.fromHandle(core.MojoHandle handle) : super.fromHandle(handle); | |
16 | |
17 Stub.unbound() : super.unbound(); | |
18 | |
19 Future<Message> handleMessage(ServiceMessage message); | |
20 | |
21 void handleRead() { | |
22 // Query how many bytes are available. | |
23 var result = endpoint.query(); | |
24 assert(result.status.isOk || result.status.isResourceExhausted); | |
25 if (result.bytesRead == 0) { | |
26 throw new MojoCodecError('Unexpected empty message.'); | |
27 } | |
28 | |
29 // Read the data and view as a message. | |
30 var bytes = new ByteData(result.bytesRead); | |
31 var handles = new List<core.MojoHandle>(result.handlesRead); | |
32 result = endpoint.read(bytes, result.bytesRead, handles); | |
33 assert(result.status.isOk || result.status.isResourceExhausted); | |
34 | |
35 // Prepare the response. | |
36 var message; | |
37 var responseFuture; | |
38 try { | |
39 message = new ServiceMessage.fromMessage(new Message(bytes, handles)); | |
40 responseFuture = _isClosing ? null : handleMessage(message); | |
41 } catch (e, s) { | |
42 handles.forEach((h) => h.close()); | |
43 rethrow; | |
44 } | |
45 | |
46 // If there's a response, send it. | |
47 if (responseFuture != null) { | |
48 _outstandingResponseFutures++; | |
49 responseFuture.then((response) { | |
50 _outstandingResponseFutures--; | |
51 if (isOpen) { | |
52 endpoint.write( | |
53 response.buffer, response.buffer.lengthInBytes, response.handles); | |
54 // FailedPrecondition is only used to indicate that the other end of | |
55 // the pipe has been closed. We can ignore the close here and wait for | |
56 // the PeerClosed signal on the event stream. | |
57 assert(endpoint.status.isOk || endpoint.status.isFailedPrecondition); | |
58 if (_isClosing && (_outstandingResponseFutures == 0)) { | |
59 // This was the final response future for which we needed to send | |
60 // a response. It is safe to close. | |
61 super.close().then((_) { | |
62 if (_isClosing) { | |
63 _isClosing = false; | |
64 _closeCompleter.complete(null); | |
65 _closeCompleter = null; | |
66 } | |
67 }); | |
68 } | |
69 } | |
70 }); | |
71 } else if (_isClosing && (_outstandingResponseFutures == 0)) { | |
72 // We are closing, there is no response to send for this message, and | |
73 // there are no outstanding response futures. Do the close now. | |
74 super.close().then((_) { | |
75 if (_isClosing) { | |
76 _isClosing = false; | |
77 _closeCompleter.complete(null); | |
78 _closeCompleter = null; | |
79 } | |
80 }); | |
81 } | |
82 } | |
83 | |
84 void handleWrite() { | |
85 throw 'Unexpected write signal in client.'; | |
86 } | |
87 | |
88 // NB: |immediate| should only be true when calling close() while handling an | |
89 // exception thrown from handleRead(), e.g. when we receive a malformed | |
90 // message, or when we have received the PEER_CLOSED event. | |
91 @override | |
92 Future close({bool immediate: false}) { | |
93 if (isOpen && | |
94 !immediate && | |
95 (isInHandler || (_outstandingResponseFutures > 0))) { | |
96 // Either close() is being called from within handleRead() or | |
97 // handleWrite(), or close() is being called while there are outstanding | |
98 // response futures. Defer the actual close until all response futures | |
99 // have been resolved. | |
100 _isClosing = true; | |
101 _closeCompleter = new Completer(); | |
102 return _closeCompleter.future; | |
103 } else { | |
104 return super.close(immediate: immediate).then((_) { | |
105 if (_isClosing) { | |
106 _isClosing = false; | |
107 _closeCompleter.complete(null); | |
108 _closeCompleter = null; | |
109 } | |
110 }); | |
111 } | |
112 } | |
113 | |
114 Message buildResponse(Struct response, int name) { | |
115 var header = new MessageHeader(name); | |
116 return response.serializeWithHeader(header); | |
117 } | |
118 | |
119 Message buildResponseWithId(Struct response, int name, int id, int flags) { | |
120 var header = new MessageHeader.withRequestId(name, flags, id); | |
121 return response.serializeWithHeader(header); | |
122 } | |
123 | |
124 String toString() { | |
125 var superString = super.toString(); | |
126 return "Stub(${superString})"; | |
127 } | |
128 | |
129 int get version; | |
130 } | |
OLD | NEW |