annotate osx/include/capnp/capability.h @ 169:223a55898ab9 tip default

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