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