cannam@49
|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
cannam@49
|
2 // Licensed under the MIT License:
|
cannam@49
|
3 //
|
cannam@49
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
cannam@49
|
5 // of this software and associated documentation files (the "Software"), to deal
|
cannam@49
|
6 // in the Software without restriction, including without limitation the rights
|
cannam@49
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
cannam@49
|
8 // copies of the Software, and to permit persons to whom the Software is
|
cannam@49
|
9 // furnished to do so, subject to the following conditions:
|
cannam@49
|
10 //
|
cannam@49
|
11 // The above copyright notice and this permission notice shall be included in
|
cannam@49
|
12 // all copies or substantial portions of the Software.
|
cannam@49
|
13 //
|
cannam@49
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
cannam@49
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
cannam@49
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
cannam@49
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
cannam@49
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
cannam@49
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
cannam@49
|
20 // THE SOFTWARE.
|
cannam@49
|
21
|
cannam@49
|
22 #ifndef CAPNP_CAPABILITY_H_
|
cannam@49
|
23 #define CAPNP_CAPABILITY_H_
|
cannam@49
|
24
|
cannam@49
|
25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
|
cannam@49
|
26 #pragma GCC system_header
|
cannam@49
|
27 #endif
|
cannam@49
|
28
|
cannam@49
|
29 #if CAPNP_LITE
|
cannam@49
|
30 #error "RPC APIs, including this header, are not available in lite mode."
|
cannam@49
|
31 #endif
|
cannam@49
|
32
|
cannam@49
|
33 #include <kj/async.h>
|
cannam@49
|
34 #include <kj/vector.h>
|
cannam@49
|
35 #include "any.h"
|
cannam@49
|
36 #include "pointer-helpers.h"
|
cannam@49
|
37
|
cannam@49
|
38 namespace capnp {
|
cannam@49
|
39
|
cannam@49
|
40 template <typename Results>
|
cannam@49
|
41 class Response;
|
cannam@49
|
42
|
cannam@49
|
43 template <typename T>
|
cannam@49
|
44 class RemotePromise: public kj::Promise<Response<T>>, public T::Pipeline {
|
cannam@49
|
45 // A Promise which supports pipelined calls. T is typically a struct type. T must declare
|
cannam@49
|
46 // an inner "mix-in" type "Pipeline" which implements pipelining; RemotePromise simply
|
cannam@49
|
47 // multiply-inherits that type along with Promise<Response<T>>. T::Pipeline must be movable,
|
cannam@49
|
48 // but does not need to be copyable (i.e. just like Promise<T>).
|
cannam@49
|
49 //
|
cannam@49
|
50 // The promise is for an owned pointer so that the RPC system can allocate the MessageReader
|
cannam@49
|
51 // itself.
|
cannam@49
|
52
|
cannam@49
|
53 public:
|
cannam@49
|
54 inline RemotePromise(kj::Promise<Response<T>>&& promise, typename T::Pipeline&& pipeline)
|
cannam@49
|
55 : kj::Promise<Response<T>>(kj::mv(promise)),
|
cannam@49
|
56 T::Pipeline(kj::mv(pipeline)) {}
|
cannam@49
|
57 inline RemotePromise(decltype(nullptr))
|
cannam@49
|
58 : kj::Promise<Response<T>>(nullptr),
|
cannam@49
|
59 T::Pipeline(nullptr) {}
|
cannam@49
|
60 KJ_DISALLOW_COPY(RemotePromise);
|
cannam@49
|
61 RemotePromise(RemotePromise&& other) = default;
|
cannam@49
|
62 RemotePromise& operator=(RemotePromise&& other) = default;
|
cannam@49
|
63 };
|
cannam@49
|
64
|
cannam@49
|
65 class LocalClient;
|
cannam@49
|
66 namespace _ { // private
|
cannam@49
|
67 struct RawSchema;
|
cannam@49
|
68 struct RawBrandedSchema;
|
cannam@49
|
69 extern const RawSchema NULL_INTERFACE_SCHEMA; // defined in schema.c++
|
cannam@49
|
70 class CapabilityServerSetBase;
|
cannam@49
|
71 } // namespace _ (private)
|
cannam@49
|
72
|
cannam@49
|
73 struct Capability {
|
cannam@49
|
74 // A capability without type-safe methods. Typed capability clients wrap `Client` and typed
|
cannam@49
|
75 // capability servers subclass `Server` to dispatch to the regular, typed methods.
|
cannam@49
|
76
|
cannam@49
|
77 class Client;
|
cannam@49
|
78 class Server;
|
cannam@49
|
79
|
cannam@49
|
80 struct _capnpPrivate {
|
cannam@49
|
81 struct IsInterface;
|
cannam@49
|
82 static constexpr uint64_t typeId = 0x3;
|
cannam@49
|
83 static constexpr Kind kind = Kind::INTERFACE;
|
cannam@49
|
84 static constexpr _::RawSchema const* schema = &_::NULL_INTERFACE_SCHEMA;
|
cannam@49
|
85
|
cannam@49
|
86 static const _::RawBrandedSchema* const brand;
|
cannam@49
|
87 // Can't quite declare this one inline without including generated-header-support.h. Avoiding
|
cannam@49
|
88 // for now by declaring out-of-line.
|
cannam@49
|
89 // TODO(cleanup): Split RawSchema stuff into its own header that can be included here, or
|
cannam@49
|
90 // something.
|
cannam@49
|
91 };
|
cannam@49
|
92 };
|
cannam@49
|
93
|
cannam@49
|
94 // =======================================================================================
|
cannam@49
|
95 // Capability clients
|
cannam@49
|
96
|
cannam@49
|
97 class RequestHook;
|
cannam@49
|
98 class ResponseHook;
|
cannam@49
|
99 class PipelineHook;
|
cannam@49
|
100 class ClientHook;
|
cannam@49
|
101
|
cannam@49
|
102 template <typename Params, typename Results>
|
cannam@49
|
103 class Request: public Params::Builder {
|
cannam@49
|
104 // A call that hasn't been sent yet. This class extends a Builder for the call's "Params"
|
cannam@49
|
105 // structure with a method send() that actually sends it.
|
cannam@49
|
106 //
|
cannam@49
|
107 // Given a Cap'n Proto method `foo(a :A, b :B): C`, the generated client interface will have
|
cannam@49
|
108 // a method `Request<FooParams, C> fooRequest()` (as well as a convenience method
|
cannam@49
|
109 // `RemotePromise<C> foo(A::Reader a, B::Reader b)`).
|
cannam@49
|
110
|
cannam@49
|
111 public:
|
cannam@49
|
112 inline Request(typename Params::Builder builder, kj::Own<RequestHook>&& hook)
|
cannam@49
|
113 : Params::Builder(builder), hook(kj::mv(hook)) {}
|
cannam@49
|
114 inline Request(decltype(nullptr)): Params::Builder(nullptr) {}
|
cannam@49
|
115
|
cannam@49
|
116 RemotePromise<Results> send() KJ_WARN_UNUSED_RESULT;
|
cannam@49
|
117 // Send the call and return a promise for the results.
|
cannam@49
|
118
|
cannam@49
|
119 private:
|
cannam@49
|
120 kj::Own<RequestHook> hook;
|
cannam@49
|
121
|
cannam@49
|
122 friend class Capability::Client;
|
cannam@49
|
123 friend struct DynamicCapability;
|
cannam@49
|
124 template <typename, typename>
|
cannam@49
|
125 friend class CallContext;
|
cannam@49
|
126 friend class RequestHook;
|
cannam@49
|
127 };
|
cannam@49
|
128
|
cannam@49
|
129 template <typename Results>
|
cannam@49
|
130 class Response: public Results::Reader {
|
cannam@49
|
131 // A completed call. This class extends a Reader for the call's answer structure. The Response
|
cannam@49
|
132 // is move-only -- once it goes out-of-scope, the underlying message will be freed.
|
cannam@49
|
133
|
cannam@49
|
134 public:
|
cannam@49
|
135 inline Response(typename Results::Reader reader, kj::Own<ResponseHook>&& hook)
|
cannam@49
|
136 : Results::Reader(reader), hook(kj::mv(hook)) {}
|
cannam@49
|
137
|
cannam@49
|
138 private:
|
cannam@49
|
139 kj::Own<ResponseHook> hook;
|
cannam@49
|
140
|
cannam@49
|
141 template <typename, typename>
|
cannam@49
|
142 friend class Request;
|
cannam@49
|
143 friend class ResponseHook;
|
cannam@49
|
144 };
|
cannam@49
|
145
|
cannam@49
|
146 class Capability::Client {
|
cannam@49
|
147 // Base type for capability clients.
|
cannam@49
|
148
|
cannam@49
|
149 public:
|
cannam@49
|
150 typedef Capability Reads;
|
cannam@49
|
151 typedef Capability Calls;
|
cannam@49
|
152
|
cannam@49
|
153 Client(decltype(nullptr));
|
cannam@49
|
154 // If you need to declare a Client before you have anything to assign to it (perhaps because
|
cannam@49
|
155 // the assignment is going to occur in an if/else scope), you can start by initializing it to
|
cannam@49
|
156 // `nullptr`. The resulting client is not meant to be called and throws exceptions from all
|
cannam@49
|
157 // methods.
|
cannam@49
|
158
|
cannam@49
|
159 template <typename T, typename = kj::EnableIf<kj::canConvert<T*, Capability::Server*>()>>
|
cannam@49
|
160 Client(kj::Own<T>&& server);
|
cannam@49
|
161 // Make a client capability that wraps the given server capability. The server's methods will
|
cannam@49
|
162 // only be executed in the given EventLoop, regardless of what thread calls the client's methods.
|
cannam@49
|
163
|
cannam@49
|
164 template <typename T, typename = kj::EnableIf<kj::canConvert<T*, Client*>()>>
|
cannam@49
|
165 Client(kj::Promise<T>&& promise);
|
cannam@49
|
166 // Make a client from a promise for a future client. The resulting client queues calls until the
|
cannam@49
|
167 // promise resolves.
|
cannam@49
|
168
|
cannam@49
|
169 Client(kj::Exception&& exception);
|
cannam@49
|
170 // Make a broken client that throws the given exception from all calls.
|
cannam@49
|
171
|
cannam@49
|
172 Client(Client& other);
|
cannam@49
|
173 Client& operator=(Client& other);
|
cannam@49
|
174 // Copies by reference counting. Warning: This refcounting is not thread-safe. All copies of
|
cannam@49
|
175 // the client must remain in one thread.
|
cannam@49
|
176
|
cannam@49
|
177 Client(Client&&) = default;
|
cannam@49
|
178 Client& operator=(Client&&) = default;
|
cannam@49
|
179 // Move constructor avoids reference counting.
|
cannam@49
|
180
|
cannam@49
|
181 explicit Client(kj::Own<ClientHook>&& hook);
|
cannam@49
|
182 // For use by the RPC implementation: Wrap a ClientHook.
|
cannam@49
|
183
|
cannam@49
|
184 template <typename T>
|
cannam@49
|
185 typename T::Client castAs();
|
cannam@49
|
186 // Reinterpret the capability as implementing the given interface. Note that no error will occur
|
cannam@49
|
187 // here if the capability does not actually implement this interface, but later method calls will
|
cannam@49
|
188 // fail. It's up to the application to decide how indicate that additional interfaces are
|
cannam@49
|
189 // supported.
|
cannam@49
|
190 //
|
cannam@49
|
191 // TODO(perf): GCC 4.8 / Clang 3.3: rvalue-qualified version for better performance.
|
cannam@49
|
192
|
cannam@49
|
193 template <typename T>
|
cannam@49
|
194 typename T::Client castAs(InterfaceSchema schema);
|
cannam@49
|
195 // Dynamic version. `T` must be `DynamicCapability`, and you must `#include <capnp/dynamic.h>`.
|
cannam@49
|
196
|
cannam@49
|
197 kj::Promise<void> whenResolved();
|
cannam@49
|
198 // If the capability is actually only a promise, the returned promise resolves once the
|
cannam@49
|
199 // capability itself has resolved to its final destination (or propagates the exception if
|
cannam@49
|
200 // the capability promise is rejected). This is mainly useful for error-checking in the case
|
cannam@49
|
201 // where no calls are being made. There is no reason to wait for this before making calls; if
|
cannam@49
|
202 // the capability does not resolve, the call results will propagate the error.
|
cannam@49
|
203
|
cannam@49
|
204 Request<AnyPointer, AnyPointer> typelessRequest(
|
cannam@49
|
205 uint64_t interfaceId, uint16_t methodId,
|
cannam@49
|
206 kj::Maybe<MessageSize> sizeHint);
|
cannam@49
|
207 // Make a request without knowing the types of the params or results. You specify the type ID
|
cannam@49
|
208 // and method number manually.
|
cannam@49
|
209
|
cannam@49
|
210 // TODO(someday): method(s) for Join
|
cannam@49
|
211
|
cannam@49
|
212 protected:
|
cannam@49
|
213 Client() = default;
|
cannam@49
|
214
|
cannam@49
|
215 template <typename Params, typename Results>
|
cannam@49
|
216 Request<Params, Results> newCall(uint64_t interfaceId, uint16_t methodId,
|
cannam@49
|
217 kj::Maybe<MessageSize> sizeHint);
|
cannam@49
|
218
|
cannam@49
|
219 private:
|
cannam@49
|
220 kj::Own<ClientHook> hook;
|
cannam@49
|
221
|
cannam@49
|
222 static kj::Own<ClientHook> makeLocalClient(kj::Own<Capability::Server>&& server);
|
cannam@49
|
223
|
cannam@49
|
224 template <typename, Kind>
|
cannam@49
|
225 friend struct _::PointerHelpers;
|
cannam@49
|
226 friend struct DynamicCapability;
|
cannam@49
|
227 friend class Orphanage;
|
cannam@49
|
228 friend struct DynamicStruct;
|
cannam@49
|
229 friend struct DynamicList;
|
cannam@49
|
230 template <typename, Kind>
|
cannam@49
|
231 friend struct List;
|
cannam@49
|
232 friend class _::CapabilityServerSetBase;
|
cannam@49
|
233 friend class ClientHook;
|
cannam@49
|
234 };
|
cannam@49
|
235
|
cannam@49
|
236 // =======================================================================================
|
cannam@49
|
237 // Capability servers
|
cannam@49
|
238
|
cannam@49
|
239 class CallContextHook;
|
cannam@49
|
240
|
cannam@49
|
241 template <typename Params, typename Results>
|
cannam@49
|
242 class CallContext: public kj::DisallowConstCopy {
|
cannam@49
|
243 // Wrapper around CallContextHook with a specific return type.
|
cannam@49
|
244 //
|
cannam@49
|
245 // Methods of this class may only be called from within the server's event loop, not from other
|
cannam@49
|
246 // threads.
|
cannam@49
|
247 //
|
cannam@49
|
248 // The CallContext becomes invalid as soon as the call reports completion.
|
cannam@49
|
249
|
cannam@49
|
250 public:
|
cannam@49
|
251 explicit CallContext(CallContextHook& hook);
|
cannam@49
|
252
|
cannam@49
|
253 typename Params::Reader getParams();
|
cannam@49
|
254 // Get the params payload.
|
cannam@49
|
255
|
cannam@49
|
256 void releaseParams();
|
cannam@49
|
257 // Release the params payload. getParams() will throw an exception after this is called.
|
cannam@49
|
258 // Releasing the params may allow the RPC system to free up buffer space to handle other
|
cannam@49
|
259 // requests. Long-running asynchronous methods should try to call this as early as is
|
cannam@49
|
260 // convenient.
|
cannam@49
|
261
|
cannam@49
|
262 typename Results::Builder getResults(kj::Maybe<MessageSize> sizeHint = nullptr);
|
cannam@49
|
263 typename Results::Builder initResults(kj::Maybe<MessageSize> sizeHint = nullptr);
|
cannam@49
|
264 void setResults(typename Results::Reader value);
|
cannam@49
|
265 void adoptResults(Orphan<Results>&& value);
|
cannam@49
|
266 Orphanage getResultsOrphanage(kj::Maybe<MessageSize> sizeHint = nullptr);
|
cannam@49
|
267 // Manipulate the results payload. The "Return" message (part of the RPC protocol) will
|
cannam@49
|
268 // typically be allocated the first time one of these is called. Some RPC systems may
|
cannam@49
|
269 // allocate these messages in a limited space (such as a shared memory segment), therefore the
|
cannam@49
|
270 // application should delay calling these as long as is convenient to do so (but don't delay
|
cannam@49
|
271 // if doing so would require extra copies later).
|
cannam@49
|
272 //
|
cannam@49
|
273 // `sizeHint` indicates a guess at the message size. This will usually be used to decide how
|
cannam@49
|
274 // much space to allocate for the first message segment (don't worry: only space that is actually
|
cannam@49
|
275 // used will be sent on the wire). If omitted, the system decides. The message root pointer
|
cannam@49
|
276 // should not be included in the size. So, if you are simply going to copy some existing message
|
cannam@49
|
277 // directly into the results, just call `.totalSize()` and pass that in.
|
cannam@49
|
278
|
cannam@49
|
279 template <typename SubParams>
|
cannam@49
|
280 kj::Promise<void> tailCall(Request<SubParams, Results>&& tailRequest);
|
cannam@49
|
281 // Resolve the call by making a tail call. `tailRequest` is a request that has been filled in
|
cannam@49
|
282 // but not yet sent. The context will send the call, then fill in the results with the result
|
cannam@49
|
283 // of the call. If tailCall() is used, {get,init,set,adopt}Results (above) *must not* be called.
|
cannam@49
|
284 //
|
cannam@49
|
285 // The RPC implementation may be able to optimize a tail call to another machine such that the
|
cannam@49
|
286 // results never actually pass through this machine. Even if no such optimization is possible,
|
cannam@49
|
287 // `tailCall()` may allow pipelined calls to be forwarded optimistically to the new call site.
|
cannam@49
|
288 //
|
cannam@49
|
289 // In general, this should be the last thing a method implementation calls, and the promise
|
cannam@49
|
290 // returned from `tailCall()` should then be returned by the method implementation.
|
cannam@49
|
291
|
cannam@49
|
292 void allowCancellation();
|
cannam@49
|
293 // Indicate that it is OK for the RPC system to discard its Promise for this call's result if
|
cannam@49
|
294 // the caller cancels the call, thereby transitively canceling any asynchronous operations the
|
cannam@49
|
295 // call implementation was performing. This is not done by default because it could represent a
|
cannam@49
|
296 // security risk: applications must be carefully written to ensure that they do not end up in
|
cannam@49
|
297 // a bad state if an operation is canceled at an arbitrary point. However, for long-running
|
cannam@49
|
298 // method calls that hold significant resources, prompt cancellation is often useful.
|
cannam@49
|
299 //
|
cannam@49
|
300 // Keep in mind that asynchronous cancellation cannot occur while the method is synchronously
|
cannam@49
|
301 // executing on a local thread. The method must perform an asynchronous operation or call
|
cannam@49
|
302 // `EventLoop::current().evalLater()` to yield control.
|
cannam@49
|
303 //
|
cannam@49
|
304 // Note: You might think that we should offer `onCancel()` and/or `isCanceled()` methods that
|
cannam@49
|
305 // provide notification when the caller cancels the request without forcefully killing off the
|
cannam@49
|
306 // promise chain. Unfortunately, this composes poorly with promise forking: the canceled
|
cannam@49
|
307 // path may be just one branch of a fork of the result promise. The other branches still want
|
cannam@49
|
308 // the call to continue. Promise forking is used within the Cap'n Proto implementation -- in
|
cannam@49
|
309 // particular each pipelined call forks the result promise. So, if a caller made a pipelined
|
cannam@49
|
310 // call and then dropped the original object, the call should not be canceled, but it would be
|
cannam@49
|
311 // excessively complicated for the framework to avoid notififying of cancellation as long as
|
cannam@49
|
312 // pipelined calls still exist.
|
cannam@49
|
313
|
cannam@49
|
314 private:
|
cannam@49
|
315 CallContextHook* hook;
|
cannam@49
|
316
|
cannam@49
|
317 friend class Capability::Server;
|
cannam@49
|
318 friend struct DynamicCapability;
|
cannam@49
|
319 };
|
cannam@49
|
320
|
cannam@49
|
321 class Capability::Server {
|
cannam@49
|
322 // Objects implementing a Cap'n Proto interface must subclass this. Typically, such objects
|
cannam@49
|
323 // will instead subclass a typed Server interface which will take care of implementing
|
cannam@49
|
324 // dispatchCall().
|
cannam@49
|
325
|
cannam@49
|
326 public:
|
cannam@49
|
327 typedef Capability Serves;
|
cannam@49
|
328
|
cannam@49
|
329 virtual kj::Promise<void> dispatchCall(uint64_t interfaceId, uint16_t methodId,
|
cannam@49
|
330 CallContext<AnyPointer, AnyPointer> context) = 0;
|
cannam@49
|
331 // Call the given method. `params` is the input struct, and should be released as soon as it
|
cannam@49
|
332 // is no longer needed. `context` may be used to allocate the output struct and deal with
|
cannam@49
|
333 // cancellation.
|
cannam@49
|
334
|
cannam@49
|
335 // TODO(someday): Method which can optionally be overridden to implement Join when the object is
|
cannam@49
|
336 // a proxy.
|
cannam@49
|
337
|
cannam@49
|
338 protected:
|
cannam@49
|
339 inline Capability::Client thisCap();
|
cannam@49
|
340 // Get a capability pointing to this object, much like the `this` keyword.
|
cannam@49
|
341 //
|
cannam@49
|
342 // The effect of this method is undefined if:
|
cannam@49
|
343 // - No capability client has been created pointing to this object. (This is always the case in
|
cannam@49
|
344 // the server's constructor.)
|
cannam@49
|
345 // - The capability client pointing at this object has been destroyed. (This is always the case
|
cannam@49
|
346 // in the server's destructor.)
|
cannam@49
|
347 // - Multiple capability clients have been created around the same server (possible if the server
|
cannam@49
|
348 // is refcounted, which is not recommended since the client itself provides refcounting).
|
cannam@49
|
349
|
cannam@49
|
350 template <typename Params, typename Results>
|
cannam@49
|
351 CallContext<Params, Results> internalGetTypedContext(
|
cannam@49
|
352 CallContext<AnyPointer, AnyPointer> typeless);
|
cannam@49
|
353 kj::Promise<void> internalUnimplemented(const char* actualInterfaceName,
|
cannam@49
|
354 uint64_t requestedTypeId);
|
cannam@49
|
355 kj::Promise<void> internalUnimplemented(const char* interfaceName,
|
cannam@49
|
356 uint64_t typeId, uint16_t methodId);
|
cannam@49
|
357 kj::Promise<void> internalUnimplemented(const char* interfaceName, const char* methodName,
|
cannam@49
|
358 uint64_t typeId, uint16_t methodId);
|
cannam@49
|
359
|
cannam@49
|
360 private:
|
cannam@49
|
361 ClientHook* thisHook = nullptr;
|
cannam@49
|
362 friend class LocalClient;
|
cannam@49
|
363 };
|
cannam@49
|
364
|
cannam@49
|
365 // =======================================================================================
|
cannam@49
|
366
|
cannam@49
|
367 class ReaderCapabilityTable: private _::CapTableReader {
|
cannam@49
|
368 // Class which imbues Readers with the ability to read capabilities.
|
cannam@49
|
369 //
|
cannam@49
|
370 // In Cap'n Proto format, the encoding of a capability pointer is simply an integer index into
|
cannam@49
|
371 // an external table. Since these pointers fundamentally point outside the message, a
|
cannam@49
|
372 // MessageReader by default has no idea what they point at, and therefore reading capabilities
|
cannam@49
|
373 // from such a reader will throw exceptions.
|
cannam@49
|
374 //
|
cannam@49
|
375 // In order to be able to read capabilities, you must first attach a capability table, using
|
cannam@49
|
376 // this class. By "imbuing" a Reader, you get a new Reader which will interpret capability
|
cannam@49
|
377 // pointers by treating them as indexes into the ReaderCapabilityTable.
|
cannam@49
|
378 //
|
cannam@49
|
379 // Note that when using Cap'n Proto's RPC system, this is handled automatically.
|
cannam@49
|
380
|
cannam@49
|
381 public:
|
cannam@49
|
382 explicit ReaderCapabilityTable(kj::Array<kj::Maybe<kj::Own<ClientHook>>> table);
|
cannam@49
|
383 KJ_DISALLOW_COPY(ReaderCapabilityTable);
|
cannam@49
|
384
|
cannam@49
|
385 template <typename T>
|
cannam@49
|
386 T imbue(T reader);
|
cannam@49
|
387 // Return a reader equivalent to `reader` except that when reading capability-valued fields,
|
cannam@49
|
388 // the capabilities are looked up in this table.
|
cannam@49
|
389
|
cannam@49
|
390 private:
|
cannam@49
|
391 kj::Array<kj::Maybe<kj::Own<ClientHook>>> table;
|
cannam@49
|
392
|
cannam@49
|
393 kj::Maybe<kj::Own<ClientHook>> extractCap(uint index) override;
|
cannam@49
|
394 };
|
cannam@49
|
395
|
cannam@49
|
396 class BuilderCapabilityTable: private _::CapTableBuilder {
|
cannam@49
|
397 // Class which imbues Builders with the ability to read and write capabilities.
|
cannam@49
|
398 //
|
cannam@49
|
399 // This is much like ReaderCapabilityTable, except for builders. The table starts out empty,
|
cannam@49
|
400 // but capabilities can be added to it over time.
|
cannam@49
|
401
|
cannam@49
|
402 public:
|
cannam@49
|
403 BuilderCapabilityTable();
|
cannam@49
|
404 KJ_DISALLOW_COPY(BuilderCapabilityTable);
|
cannam@49
|
405
|
cannam@49
|
406 inline kj::ArrayPtr<kj::Maybe<kj::Own<ClientHook>>> getTable() { return table; }
|
cannam@49
|
407
|
cannam@49
|
408 template <typename T>
|
cannam@49
|
409 T imbue(T builder);
|
cannam@49
|
410 // Return a builder equivalent to `builder` except that when reading capability-valued fields,
|
cannam@49
|
411 // the capabilities are looked up in this table.
|
cannam@49
|
412
|
cannam@49
|
413 private:
|
cannam@49
|
414 kj::Vector<kj::Maybe<kj::Own<ClientHook>>> table;
|
cannam@49
|
415
|
cannam@49
|
416 kj::Maybe<kj::Own<ClientHook>> extractCap(uint index) override;
|
cannam@49
|
417 uint injectCap(kj::Own<ClientHook>&& cap) override;
|
cannam@49
|
418 void dropCap(uint index) override;
|
cannam@49
|
419 };
|
cannam@49
|
420
|
cannam@49
|
421 // =======================================================================================
|
cannam@49
|
422
|
cannam@49
|
423 namespace _ { // private
|
cannam@49
|
424
|
cannam@49
|
425 class CapabilityServerSetBase {
|
cannam@49
|
426 public:
|
cannam@49
|
427 Capability::Client addInternal(kj::Own<Capability::Server>&& server, void* ptr);
|
cannam@49
|
428 kj::Promise<void*> getLocalServerInternal(Capability::Client& client);
|
cannam@49
|
429 };
|
cannam@49
|
430
|
cannam@49
|
431 } // namespace _ (private)
|
cannam@49
|
432
|
cannam@49
|
433 template <typename T>
|
cannam@49
|
434 class CapabilityServerSet: private _::CapabilityServerSetBase {
|
cannam@49
|
435 // Allows a server to recognize its own capabilities when passed back to it, and obtain the
|
cannam@49
|
436 // underlying Server objects associated with them.
|
cannam@49
|
437 //
|
cannam@49
|
438 // All objects in the set must have the same interface type T. The objects may implement various
|
cannam@49
|
439 // interfaces derived from T (and in fact T can be `capnp::Capability` to accept all objects),
|
cannam@49
|
440 // but note that if you compile with RTTI disabled then you will not be able to down-cast through
|
cannam@49
|
441 // virtual inheritance, and all inheritance between server interfaces is virtual. So, with RTTI
|
cannam@49
|
442 // disabled, you will likely need to set T to be the most-derived Cap'n Proto interface type,
|
cannam@49
|
443 // and you server class will need to be directly derived from that, so that you can use
|
cannam@49
|
444 // static_cast (or kj::downcast) to cast to it after calling getLocalServer(). (If you compile
|
cannam@49
|
445 // with RTTI, then you can freely dynamic_cast and ignore this issue!)
|
cannam@49
|
446
|
cannam@49
|
447 public:
|
cannam@49
|
448 CapabilityServerSet() = default;
|
cannam@49
|
449 KJ_DISALLOW_COPY(CapabilityServerSet);
|
cannam@49
|
450
|
cannam@49
|
451 typename T::Client add(kj::Own<typename T::Server>&& server);
|
cannam@49
|
452 // Create a new capability Client for the given Server and also add this server to the set.
|
cannam@49
|
453
|
cannam@49
|
454 kj::Promise<kj::Maybe<typename T::Server&>> getLocalServer(typename T::Client& client);
|
cannam@49
|
455 // Given a Client pointing to a server previously passed to add(), return the corresponding
|
cannam@49
|
456 // Server. This returns a promise because if the input client is itself a promise, this must
|
cannam@49
|
457 // wait for it to resolve. Keep in mind that the server will be deleted when all clients are
|
cannam@49
|
458 // gone, so the caller should make sure to keep the client alive (hence why this method only
|
cannam@49
|
459 // accepts an lvalue input).
|
cannam@49
|
460 };
|
cannam@49
|
461
|
cannam@49
|
462 // =======================================================================================
|
cannam@49
|
463 // Hook interfaces which must be implemented by the RPC system. Applications never call these
|
cannam@49
|
464 // directly; the RPC system implements them and the types defined earlier in this file wrap them.
|
cannam@49
|
465
|
cannam@49
|
466 class RequestHook {
|
cannam@49
|
467 // Hook interface implemented by RPC system representing a request being built.
|
cannam@49
|
468
|
cannam@49
|
469 public:
|
cannam@49
|
470 virtual RemotePromise<AnyPointer> send() = 0;
|
cannam@49
|
471 // Send the call and return a promise for the result.
|
cannam@49
|
472
|
cannam@49
|
473 virtual const void* getBrand() = 0;
|
cannam@49
|
474 // Returns a void* that identifies who made this request. This can be used by an RPC adapter to
|
cannam@49
|
475 // discover when tail call is going to be sent over its own connection and therefore can be
|
cannam@49
|
476 // optimized into a remote tail call.
|
cannam@49
|
477
|
cannam@49
|
478 template <typename T, typename U>
|
cannam@49
|
479 inline static kj::Own<RequestHook> from(Request<T, U>&& request) {
|
cannam@49
|
480 return kj::mv(request.hook);
|
cannam@49
|
481 }
|
cannam@49
|
482 };
|
cannam@49
|
483
|
cannam@49
|
484 class ResponseHook {
|
cannam@49
|
485 // Hook interface implemented by RPC system representing a response.
|
cannam@49
|
486 //
|
cannam@49
|
487 // At present this class has no methods. It exists only for garbage collection -- when the
|
cannam@49
|
488 // ResponseHook is destroyed, the results can be freed.
|
cannam@49
|
489
|
cannam@49
|
490 public:
|
cannam@49
|
491 virtual ~ResponseHook() noexcept(false);
|
cannam@49
|
492 // Just here to make sure the type is dynamic.
|
cannam@49
|
493
|
cannam@49
|
494 template <typename T>
|
cannam@49
|
495 inline static kj::Own<ResponseHook> from(Response<T>&& response) {
|
cannam@49
|
496 return kj::mv(response.hook);
|
cannam@49
|
497 }
|
cannam@49
|
498 };
|
cannam@49
|
499
|
cannam@49
|
500 // class PipelineHook is declared in any.h because it is needed there.
|
cannam@49
|
501
|
cannam@49
|
502 class ClientHook {
|
cannam@49
|
503 public:
|
cannam@49
|
504 ClientHook();
|
cannam@49
|
505
|
cannam@49
|
506 virtual Request<AnyPointer, AnyPointer> newCall(
|
cannam@49
|
507 uint64_t interfaceId, uint16_t methodId, kj::Maybe<MessageSize> sizeHint) = 0;
|
cannam@49
|
508 // Start a new call, allowing the client to allocate request/response objects as it sees fit.
|
cannam@49
|
509 // This version is used when calls are made from application code in the local process.
|
cannam@49
|
510
|
cannam@49
|
511 struct VoidPromiseAndPipeline {
|
cannam@49
|
512 kj::Promise<void> promise;
|
cannam@49
|
513 kj::Own<PipelineHook> pipeline;
|
cannam@49
|
514 };
|
cannam@49
|
515
|
cannam@49
|
516 virtual VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId,
|
cannam@49
|
517 kj::Own<CallContextHook>&& context) = 0;
|
cannam@49
|
518 // Call the object, but the caller controls allocation of the request/response objects. If the
|
cannam@49
|
519 // callee insists on allocating these objects itself, it must make a copy. This version is used
|
cannam@49
|
520 // when calls come in over the network via an RPC system. Note that even if the returned
|
cannam@49
|
521 // `Promise<void>` is discarded, the call may continue executing if any pipelined calls are
|
cannam@49
|
522 // waiting for it.
|
cannam@49
|
523 //
|
cannam@49
|
524 // Since the caller of this method chooses the CallContext implementation, it is the caller's
|
cannam@49
|
525 // responsibility to ensure that the returned promise is not canceled unless allowed via
|
cannam@49
|
526 // the context's `allowCancellation()`.
|
cannam@49
|
527 //
|
cannam@49
|
528 // The call must not begin synchronously; the callee must arrange for the call to begin in a
|
cannam@49
|
529 // later turn of the event loop. Otherwise, application code may call back and affect the
|
cannam@49
|
530 // callee's state in an unexpected way.
|
cannam@49
|
531
|
cannam@49
|
532 virtual kj::Maybe<ClientHook&> getResolved() = 0;
|
cannam@49
|
533 // If this ClientHook is a promise that has already resolved, returns the inner, resolved version
|
cannam@49
|
534 // of the capability. The caller may permanently replace this client with the resolved one if
|
cannam@49
|
535 // desired. Returns null if the client isn't a promise or hasn't resolved yet -- use
|
cannam@49
|
536 // `whenMoreResolved()` to distinguish between them.
|
cannam@49
|
537
|
cannam@49
|
538 virtual kj::Maybe<kj::Promise<kj::Own<ClientHook>>> whenMoreResolved() = 0;
|
cannam@49
|
539 // If this client is a settled reference (not a promise), return nullptr. Otherwise, return a
|
cannam@49
|
540 // promise that eventually resolves to a new client that is closer to being the final, settled
|
cannam@49
|
541 // client (i.e. the value eventually returned by `getResolved()`). Calling this repeatedly
|
cannam@49
|
542 // should eventually produce a settled client.
|
cannam@49
|
543
|
cannam@49
|
544 kj::Promise<void> whenResolved();
|
cannam@49
|
545 // Repeatedly calls whenMoreResolved() until it returns nullptr.
|
cannam@49
|
546
|
cannam@49
|
547 virtual kj::Own<ClientHook> addRef() = 0;
|
cannam@49
|
548 // Return a new reference to the same capability.
|
cannam@49
|
549
|
cannam@49
|
550 virtual const void* getBrand() = 0;
|
cannam@49
|
551 // Returns a void* that identifies who made this client. This can be used by an RPC adapter to
|
cannam@49
|
552 // discover when a capability it needs to marshal is one that it created in the first place, and
|
cannam@49
|
553 // therefore it can transfer the capability without proxying.
|
cannam@49
|
554
|
cannam@49
|
555 static const uint NULL_CAPABILITY_BRAND;
|
cannam@49
|
556 // Value is irrelevant; used for pointer.
|
cannam@49
|
557
|
cannam@49
|
558 inline bool isNull() { return getBrand() == &NULL_CAPABILITY_BRAND; }
|
cannam@49
|
559 // Returns true if the capability was created as a result of assigning a Client to null or by
|
cannam@49
|
560 // reading a null pointer out of a Cap'n Proto message.
|
cannam@49
|
561
|
cannam@49
|
562 virtual void* getLocalServer(_::CapabilityServerSetBase& capServerSet);
|
cannam@49
|
563 // If this is a local capability created through `capServerSet`, return the underlying Server.
|
cannam@49
|
564 // Otherwise, return nullptr. Default implementation (which everyone except LocalClient should
|
cannam@49
|
565 // use) always returns nullptr.
|
cannam@49
|
566
|
cannam@49
|
567 static kj::Own<ClientHook> from(Capability::Client client) { return kj::mv(client.hook); }
|
cannam@49
|
568 };
|
cannam@49
|
569
|
cannam@49
|
570 class CallContextHook {
|
cannam@49
|
571 // Hook interface implemented by RPC system to manage a call on the server side. See
|
cannam@49
|
572 // CallContext<T>.
|
cannam@49
|
573
|
cannam@49
|
574 public:
|
cannam@49
|
575 virtual AnyPointer::Reader getParams() = 0;
|
cannam@49
|
576 virtual void releaseParams() = 0;
|
cannam@49
|
577 virtual AnyPointer::Builder getResults(kj::Maybe<MessageSize> sizeHint) = 0;
|
cannam@49
|
578 virtual kj::Promise<void> tailCall(kj::Own<RequestHook>&& request) = 0;
|
cannam@49
|
579 virtual void allowCancellation() = 0;
|
cannam@49
|
580
|
cannam@49
|
581 virtual kj::Promise<AnyPointer::Pipeline> onTailCall() = 0;
|
cannam@49
|
582 // If `tailCall()` is called, resolves to the PipelineHook from the tail call. An
|
cannam@49
|
583 // implementation of `ClientHook::call()` is allowed to call this at most once.
|
cannam@49
|
584
|
cannam@49
|
585 virtual ClientHook::VoidPromiseAndPipeline directTailCall(kj::Own<RequestHook>&& request) = 0;
|
cannam@49
|
586 // Call this when you would otherwise call onTailCall() immediately followed by tailCall().
|
cannam@49
|
587 // Implementations of tailCall() should typically call directTailCall() and then fulfill the
|
cannam@49
|
588 // promise fulfiller for onTailCall() with the returned pipeline.
|
cannam@49
|
589
|
cannam@49
|
590 virtual kj::Own<CallContextHook> addRef() = 0;
|
cannam@49
|
591 };
|
cannam@49
|
592
|
cannam@49
|
593 kj::Own<ClientHook> newLocalPromiseClient(kj::Promise<kj::Own<ClientHook>>&& promise);
|
cannam@49
|
594 // Returns a ClientHook that queues up calls until `promise` resolves, then forwards them to
|
cannam@49
|
595 // the new client. This hook's `getResolved()` and `whenMoreResolved()` methods will reflect the
|
cannam@49
|
596 // redirection to the eventual replacement client.
|
cannam@49
|
597
|
cannam@49
|
598 kj::Own<PipelineHook> newLocalPromisePipeline(kj::Promise<kj::Own<PipelineHook>>&& promise);
|
cannam@49
|
599 // Returns a PipelineHook that queues up calls until `promise` resolves, then forwards them to
|
cannam@49
|
600 // the new pipeline.
|
cannam@49
|
601
|
cannam@49
|
602 kj::Own<ClientHook> newBrokenCap(kj::StringPtr reason);
|
cannam@49
|
603 kj::Own<ClientHook> newBrokenCap(kj::Exception&& reason);
|
cannam@49
|
604 // Helper function that creates a capability which simply throws exceptions when called.
|
cannam@49
|
605
|
cannam@49
|
606 kj::Own<PipelineHook> newBrokenPipeline(kj::Exception&& reason);
|
cannam@49
|
607 // Helper function that creates a pipeline which simply throws exceptions when called.
|
cannam@49
|
608
|
cannam@49
|
609 Request<AnyPointer, AnyPointer> newBrokenRequest(
|
cannam@49
|
610 kj::Exception&& reason, kj::Maybe<MessageSize> sizeHint);
|
cannam@49
|
611 // Helper function that creates a Request object that simply throws exceptions when sent.
|
cannam@49
|
612
|
cannam@49
|
613 // =======================================================================================
|
cannam@49
|
614 // Extend PointerHelpers for interfaces
|
cannam@49
|
615
|
cannam@49
|
616 namespace _ { // private
|
cannam@49
|
617
|
cannam@49
|
618 template <typename T>
|
cannam@49
|
619 struct PointerHelpers<T, Kind::INTERFACE> {
|
cannam@49
|
620 static inline typename T::Client get(PointerReader reader) {
|
cannam@49
|
621 return typename T::Client(reader.getCapability());
|
cannam@49
|
622 }
|
cannam@49
|
623 static inline typename T::Client get(PointerBuilder builder) {
|
cannam@49
|
624 return typename T::Client(builder.getCapability());
|
cannam@49
|
625 }
|
cannam@49
|
626 static inline void set(PointerBuilder builder, typename T::Client&& value) {
|
cannam@49
|
627 builder.setCapability(kj::mv(value.Capability::Client::hook));
|
cannam@49
|
628 }
|
cannam@49
|
629 static inline void set(PointerBuilder builder, typename T::Client& value) {
|
cannam@49
|
630 builder.setCapability(value.Capability::Client::hook->addRef());
|
cannam@49
|
631 }
|
cannam@49
|
632 static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
|
cannam@49
|
633 builder.adopt(kj::mv(value.builder));
|
cannam@49
|
634 }
|
cannam@49
|
635 static inline Orphan<T> disown(PointerBuilder builder) {
|
cannam@49
|
636 return Orphan<T>(builder.disown());
|
cannam@49
|
637 }
|
cannam@49
|
638 };
|
cannam@49
|
639
|
cannam@49
|
640 } // namespace _ (private)
|
cannam@49
|
641
|
cannam@49
|
642 // =======================================================================================
|
cannam@49
|
643 // Extend List for interfaces
|
cannam@49
|
644
|
cannam@49
|
645 template <typename T>
|
cannam@49
|
646 struct List<T, Kind::INTERFACE> {
|
cannam@49
|
647 List() = delete;
|
cannam@49
|
648
|
cannam@49
|
649 class Reader {
|
cannam@49
|
650 public:
|
cannam@49
|
651 typedef List<T> Reads;
|
cannam@49
|
652
|
cannam@49
|
653 Reader() = default;
|
cannam@49
|
654 inline explicit Reader(_::ListReader reader): reader(reader) {}
|
cannam@49
|
655
|
cannam@49
|
656 inline uint size() const { return reader.size() / ELEMENTS; }
|
cannam@49
|
657 inline typename T::Client operator[](uint index) const {
|
cannam@49
|
658 KJ_IREQUIRE(index < size());
|
cannam@49
|
659 return typename T::Client(reader.getPointerElement(index * ELEMENTS).getCapability());
|
cannam@49
|
660 }
|
cannam@49
|
661
|
cannam@49
|
662 typedef _::IndexingIterator<const Reader, typename T::Client> Iterator;
|
cannam@49
|
663 inline Iterator begin() const { return Iterator(this, 0); }
|
cannam@49
|
664 inline Iterator end() const { return Iterator(this, size()); }
|
cannam@49
|
665
|
cannam@49
|
666 private:
|
cannam@49
|
667 _::ListReader reader;
|
cannam@49
|
668 template <typename U, Kind K>
|
cannam@49
|
669 friend struct _::PointerHelpers;
|
cannam@49
|
670 template <typename U, Kind K>
|
cannam@49
|
671 friend struct List;
|
cannam@49
|
672 friend class Orphanage;
|
cannam@49
|
673 template <typename U, Kind K>
|
cannam@49
|
674 friend struct ToDynamic_;
|
cannam@49
|
675 };
|
cannam@49
|
676
|
cannam@49
|
677 class Builder {
|
cannam@49
|
678 public:
|
cannam@49
|
679 typedef List<T> Builds;
|
cannam@49
|
680
|
cannam@49
|
681 Builder() = delete;
|
cannam@49
|
682 inline Builder(decltype(nullptr)) {}
|
cannam@49
|
683 inline explicit Builder(_::ListBuilder builder): builder(builder) {}
|
cannam@49
|
684
|
cannam@49
|
685 inline operator Reader() const { return Reader(builder.asReader()); }
|
cannam@49
|
686 inline Reader asReader() const { return Reader(builder.asReader()); }
|
cannam@49
|
687
|
cannam@49
|
688 inline uint size() const { return builder.size() / ELEMENTS; }
|
cannam@49
|
689 inline typename T::Client operator[](uint index) {
|
cannam@49
|
690 KJ_IREQUIRE(index < size());
|
cannam@49
|
691 return typename T::Client(builder.getPointerElement(index * ELEMENTS).getCapability());
|
cannam@49
|
692 }
|
cannam@49
|
693 inline void set(uint index, typename T::Client value) {
|
cannam@49
|
694 KJ_IREQUIRE(index < size());
|
cannam@49
|
695 builder.getPointerElement(index * ELEMENTS).setCapability(kj::mv(value.hook));
|
cannam@49
|
696 }
|
cannam@49
|
697 inline void adopt(uint index, Orphan<T>&& value) {
|
cannam@49
|
698 KJ_IREQUIRE(index < size());
|
cannam@49
|
699 builder.getPointerElement(index * ELEMENTS).adopt(kj::mv(value));
|
cannam@49
|
700 }
|
cannam@49
|
701 inline Orphan<T> disown(uint index) {
|
cannam@49
|
702 KJ_IREQUIRE(index < size());
|
cannam@49
|
703 return Orphan<T>(builder.getPointerElement(index * ELEMENTS).disown());
|
cannam@49
|
704 }
|
cannam@49
|
705
|
cannam@49
|
706 typedef _::IndexingIterator<Builder, typename T::Client> Iterator;
|
cannam@49
|
707 inline Iterator begin() { return Iterator(this, 0); }
|
cannam@49
|
708 inline Iterator end() { return Iterator(this, size()); }
|
cannam@49
|
709
|
cannam@49
|
710 private:
|
cannam@49
|
711 _::ListBuilder builder;
|
cannam@49
|
712 friend class Orphanage;
|
cannam@49
|
713 template <typename U, Kind K>
|
cannam@49
|
714 friend struct ToDynamic_;
|
cannam@49
|
715 };
|
cannam@49
|
716
|
cannam@49
|
717 private:
|
cannam@49
|
718 inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
|
cannam@49
|
719 return builder.initList(ElementSize::POINTER, size * ELEMENTS);
|
cannam@49
|
720 }
|
cannam@49
|
721 inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
|
cannam@49
|
722 return builder.getList(ElementSize::POINTER, defaultValue);
|
cannam@49
|
723 }
|
cannam@49
|
724 inline static _::ListReader getFromPointer(
|
cannam@49
|
725 const _::PointerReader& reader, const word* defaultValue) {
|
cannam@49
|
726 return reader.getList(ElementSize::POINTER, defaultValue);
|
cannam@49
|
727 }
|
cannam@49
|
728
|
cannam@49
|
729 template <typename U, Kind k>
|
cannam@49
|
730 friend struct List;
|
cannam@49
|
731 template <typename U, Kind K>
|
cannam@49
|
732 friend struct _::PointerHelpers;
|
cannam@49
|
733 };
|
cannam@49
|
734
|
cannam@49
|
735 // =======================================================================================
|
cannam@49
|
736 // Inline implementation details
|
cannam@49
|
737
|
cannam@49
|
738 template <typename Params, typename Results>
|
cannam@49
|
739 RemotePromise<Results> Request<Params, Results>::send() {
|
cannam@49
|
740 auto typelessPromise = hook->send();
|
cannam@49
|
741 hook = nullptr; // prevent reuse
|
cannam@49
|
742
|
cannam@49
|
743 // Convert the Promise to return the correct response type.
|
cannam@49
|
744 // Explicitly upcast to kj::Promise to make clear that calling .then() doesn't invalidate the
|
cannam@49
|
745 // Pipeline part of the RemotePromise.
|
cannam@49
|
746 auto typedPromise = kj::implicitCast<kj::Promise<Response<AnyPointer>>&>(typelessPromise)
|
cannam@49
|
747 .then([](Response<AnyPointer>&& response) -> Response<Results> {
|
cannam@49
|
748 return Response<Results>(response.getAs<Results>(), kj::mv(response.hook));
|
cannam@49
|
749 });
|
cannam@49
|
750
|
cannam@49
|
751 // Wrap the typeless pipeline in a typed wrapper.
|
cannam@49
|
752 typename Results::Pipeline typedPipeline(
|
cannam@49
|
753 kj::mv(kj::implicitCast<AnyPointer::Pipeline&>(typelessPromise)));
|
cannam@49
|
754
|
cannam@49
|
755 return RemotePromise<Results>(kj::mv(typedPromise), kj::mv(typedPipeline));
|
cannam@49
|
756 }
|
cannam@49
|
757
|
cannam@49
|
758 inline Capability::Client::Client(kj::Own<ClientHook>&& hook): hook(kj::mv(hook)) {}
|
cannam@49
|
759 template <typename T, typename>
|
cannam@49
|
760 inline Capability::Client::Client(kj::Own<T>&& server)
|
cannam@49
|
761 : hook(makeLocalClient(kj::mv(server))) {}
|
cannam@49
|
762 template <typename T, typename>
|
cannam@49
|
763 inline Capability::Client::Client(kj::Promise<T>&& promise)
|
cannam@49
|
764 : hook(newLocalPromiseClient(promise.then([](T&& t) { return kj::mv(t.hook); }))) {}
|
cannam@49
|
765 inline Capability::Client::Client(Client& other): hook(other.hook->addRef()) {}
|
cannam@49
|
766 inline Capability::Client& Capability::Client::operator=(Client& other) {
|
cannam@49
|
767 hook = other.hook->addRef();
|
cannam@49
|
768 return *this;
|
cannam@49
|
769 }
|
cannam@49
|
770 template <typename T>
|
cannam@49
|
771 inline typename T::Client Capability::Client::castAs() {
|
cannam@49
|
772 return typename T::Client(hook->addRef());
|
cannam@49
|
773 }
|
cannam@49
|
774 inline kj::Promise<void> Capability::Client::whenResolved() {
|
cannam@49
|
775 return hook->whenResolved();
|
cannam@49
|
776 }
|
cannam@49
|
777 inline Request<AnyPointer, AnyPointer> Capability::Client::typelessRequest(
|
cannam@49
|
778 uint64_t interfaceId, uint16_t methodId,
|
cannam@49
|
779 kj::Maybe<MessageSize> sizeHint) {
|
cannam@49
|
780 return newCall<AnyPointer, AnyPointer>(interfaceId, methodId, sizeHint);
|
cannam@49
|
781 }
|
cannam@49
|
782 template <typename Params, typename Results>
|
cannam@49
|
783 inline Request<Params, Results> Capability::Client::newCall(
|
cannam@49
|
784 uint64_t interfaceId, uint16_t methodId, kj::Maybe<MessageSize> sizeHint) {
|
cannam@49
|
785 auto typeless = hook->newCall(interfaceId, methodId, sizeHint);
|
cannam@49
|
786 return Request<Params, Results>(typeless.template getAs<Params>(), kj::mv(typeless.hook));
|
cannam@49
|
787 }
|
cannam@49
|
788
|
cannam@49
|
789 template <typename Params, typename Results>
|
cannam@49
|
790 inline CallContext<Params, Results>::CallContext(CallContextHook& hook): hook(&hook) {}
|
cannam@49
|
791 template <typename Params, typename Results>
|
cannam@49
|
792 inline typename Params::Reader CallContext<Params, Results>::getParams() {
|
cannam@49
|
793 return hook->getParams().template getAs<Params>();
|
cannam@49
|
794 }
|
cannam@49
|
795 template <typename Params, typename Results>
|
cannam@49
|
796 inline void CallContext<Params, Results>::releaseParams() {
|
cannam@49
|
797 hook->releaseParams();
|
cannam@49
|
798 }
|
cannam@49
|
799 template <typename Params, typename Results>
|
cannam@49
|
800 inline typename Results::Builder CallContext<Params, Results>::getResults(
|
cannam@49
|
801 kj::Maybe<MessageSize> sizeHint) {
|
cannam@49
|
802 // `template` keyword needed due to: http://llvm.org/bugs/show_bug.cgi?id=17401
|
cannam@49
|
803 return hook->getResults(sizeHint).template getAs<Results>();
|
cannam@49
|
804 }
|
cannam@49
|
805 template <typename Params, typename Results>
|
cannam@49
|
806 inline typename Results::Builder CallContext<Params, Results>::initResults(
|
cannam@49
|
807 kj::Maybe<MessageSize> sizeHint) {
|
cannam@49
|
808 // `template` keyword needed due to: http://llvm.org/bugs/show_bug.cgi?id=17401
|
cannam@49
|
809 return hook->getResults(sizeHint).template initAs<Results>();
|
cannam@49
|
810 }
|
cannam@49
|
811 template <typename Params, typename Results>
|
cannam@49
|
812 inline void CallContext<Params, Results>::setResults(typename Results::Reader value) {
|
cannam@49
|
813 hook->getResults(value.totalSize()).template setAs<Results>(value);
|
cannam@49
|
814 }
|
cannam@49
|
815 template <typename Params, typename Results>
|
cannam@49
|
816 inline void CallContext<Params, Results>::adoptResults(Orphan<Results>&& value) {
|
cannam@49
|
817 hook->getResults(nullptr).adopt(kj::mv(value));
|
cannam@49
|
818 }
|
cannam@49
|
819 template <typename Params, typename Results>
|
cannam@49
|
820 inline Orphanage CallContext<Params, Results>::getResultsOrphanage(
|
cannam@49
|
821 kj::Maybe<MessageSize> sizeHint) {
|
cannam@49
|
822 return Orphanage::getForMessageContaining(hook->getResults(sizeHint));
|
cannam@49
|
823 }
|
cannam@49
|
824 template <typename Params, typename Results>
|
cannam@49
|
825 template <typename SubParams>
|
cannam@49
|
826 inline kj::Promise<void> CallContext<Params, Results>::tailCall(
|
cannam@49
|
827 Request<SubParams, Results>&& tailRequest) {
|
cannam@49
|
828 return hook->tailCall(kj::mv(tailRequest.hook));
|
cannam@49
|
829 }
|
cannam@49
|
830 template <typename Params, typename Results>
|
cannam@49
|
831 inline void CallContext<Params, Results>::allowCancellation() {
|
cannam@49
|
832 hook->allowCancellation();
|
cannam@49
|
833 }
|
cannam@49
|
834
|
cannam@49
|
835 template <typename Params, typename Results>
|
cannam@49
|
836 CallContext<Params, Results> Capability::Server::internalGetTypedContext(
|
cannam@49
|
837 CallContext<AnyPointer, AnyPointer> typeless) {
|
cannam@49
|
838 return CallContext<Params, Results>(*typeless.hook);
|
cannam@49
|
839 }
|
cannam@49
|
840
|
cannam@49
|
841 Capability::Client Capability::Server::thisCap() {
|
cannam@49
|
842 return Client(thisHook->addRef());
|
cannam@49
|
843 }
|
cannam@49
|
844
|
cannam@49
|
845 template <typename T>
|
cannam@49
|
846 T ReaderCapabilityTable::imbue(T reader) {
|
cannam@49
|
847 return T(_::PointerHelpers<FromReader<T>>::getInternalReader(reader).imbue(this));
|
cannam@49
|
848 }
|
cannam@49
|
849
|
cannam@49
|
850 template <typename T>
|
cannam@49
|
851 T BuilderCapabilityTable::imbue(T builder) {
|
cannam@49
|
852 return T(_::PointerHelpers<FromBuilder<T>>::getInternalBuilder(kj::mv(builder)).imbue(this));
|
cannam@49
|
853 }
|
cannam@49
|
854
|
cannam@49
|
855 template <typename T>
|
cannam@49
|
856 typename T::Client CapabilityServerSet<T>::add(kj::Own<typename T::Server>&& server) {
|
cannam@49
|
857 void* ptr = reinterpret_cast<void*>(server.get());
|
cannam@49
|
858 // Clang insists that `castAs` is a template-dependent member and therefore we need the
|
cannam@49
|
859 // `template` keyword here, but AFAICT this is wrong: addImpl() is not a template.
|
cannam@49
|
860 return addInternal(kj::mv(server), ptr).template castAs<T>();
|
cannam@49
|
861 }
|
cannam@49
|
862
|
cannam@49
|
863 template <typename T>
|
cannam@49
|
864 kj::Promise<kj::Maybe<typename T::Server&>> CapabilityServerSet<T>::getLocalServer(
|
cannam@49
|
865 typename T::Client& client) {
|
cannam@49
|
866 return getLocalServerInternal(client)
|
cannam@49
|
867 .then([](void* server) -> kj::Maybe<typename T::Server&> {
|
cannam@49
|
868 if (server == nullptr) {
|
cannam@49
|
869 return nullptr;
|
cannam@49
|
870 } else {
|
cannam@49
|
871 return *reinterpret_cast<typename T::Server*>(server);
|
cannam@49
|
872 }
|
cannam@49
|
873 });
|
cannam@49
|
874 }
|
cannam@49
|
875
|
cannam@49
|
876 template <typename T>
|
cannam@49
|
877 struct Orphanage::GetInnerReader<T, Kind::INTERFACE> {
|
cannam@49
|
878 static inline kj::Own<ClientHook> apply(typename T::Client t) {
|
cannam@49
|
879 return ClientHook::from(kj::mv(t));
|
cannam@49
|
880 }
|
cannam@49
|
881 };
|
cannam@49
|
882
|
cannam@49
|
883 } // namespace capnp
|
cannam@49
|
884
|
cannam@49
|
885 #endif // CAPNP_CAPABILITY_H_
|