annotate win32-mingw/include/capnp/capability.h @ 50:37d53a7e8262

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