annotate osx/include/capnp/capability.h @ 139:413e081fcc6f

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