annotate win64-msvc/include/capnp/capability.h @ 58:eab3b14ddc95

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