cannam@135: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@135: // Licensed under the MIT License: cannam@135: // cannam@135: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@135: // of this software and associated documentation files (the "Software"), to deal cannam@135: // in the Software without restriction, including without limitation the rights cannam@135: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@135: // copies of the Software, and to permit persons to whom the Software is cannam@135: // furnished to do so, subject to the following conditions: cannam@135: // cannam@135: // The above copyright notice and this permission notice shall be included in cannam@135: // all copies or substantial portions of the Software. cannam@135: // cannam@135: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@135: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@135: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@135: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@135: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@135: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@135: // THE SOFTWARE. cannam@135: cannam@135: #ifndef CAPNP_CAPABILITY_H_ cannam@135: #define CAPNP_CAPABILITY_H_ cannam@135: cannam@135: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) cannam@135: #pragma GCC system_header cannam@135: #endif cannam@135: cannam@135: #if CAPNP_LITE cannam@135: #error "RPC APIs, including this header, are not available in lite mode." cannam@135: #endif cannam@135: cannam@135: #include cannam@135: #include cannam@135: #include "any.h" cannam@135: #include "pointer-helpers.h" cannam@135: cannam@135: namespace capnp { cannam@135: cannam@135: template cannam@135: class Response; cannam@135: cannam@135: template cannam@135: class RemotePromise: public kj::Promise>, public T::Pipeline { cannam@135: // A Promise which supports pipelined calls. T is typically a struct type. T must declare cannam@135: // an inner "mix-in" type "Pipeline" which implements pipelining; RemotePromise simply cannam@135: // multiply-inherits that type along with Promise>. T::Pipeline must be movable, cannam@135: // but does not need to be copyable (i.e. just like Promise). cannam@135: // cannam@135: // The promise is for an owned pointer so that the RPC system can allocate the MessageReader cannam@135: // itself. cannam@135: cannam@135: public: cannam@135: inline RemotePromise(kj::Promise>&& promise, typename T::Pipeline&& pipeline) cannam@135: : kj::Promise>(kj::mv(promise)), cannam@135: T::Pipeline(kj::mv(pipeline)) {} cannam@135: inline RemotePromise(decltype(nullptr)) cannam@135: : kj::Promise>(nullptr), cannam@135: T::Pipeline(nullptr) {} cannam@135: KJ_DISALLOW_COPY(RemotePromise); cannam@135: RemotePromise(RemotePromise&& other) = default; cannam@135: RemotePromise& operator=(RemotePromise&& other) = default; cannam@135: }; cannam@135: cannam@135: class LocalClient; cannam@135: namespace _ { // private cannam@135: struct RawSchema; cannam@135: struct RawBrandedSchema; cannam@135: extern const RawSchema NULL_INTERFACE_SCHEMA; // defined in schema.c++ cannam@135: class CapabilityServerSetBase; cannam@135: } // namespace _ (private) cannam@135: cannam@135: struct Capability { cannam@135: // A capability without type-safe methods. Typed capability clients wrap `Client` and typed cannam@135: // capability servers subclass `Server` to dispatch to the regular, typed methods. cannam@135: cannam@135: class Client; cannam@135: class Server; cannam@135: cannam@135: struct _capnpPrivate { cannam@135: struct IsInterface; cannam@135: static constexpr uint64_t typeId = 0x3; cannam@135: static constexpr Kind kind = Kind::INTERFACE; cannam@135: static constexpr _::RawSchema const* schema = &_::NULL_INTERFACE_SCHEMA; cannam@135: cannam@135: static const _::RawBrandedSchema* const brand; cannam@135: // Can't quite declare this one inline without including generated-header-support.h. Avoiding cannam@135: // for now by declaring out-of-line. cannam@135: // TODO(cleanup): Split RawSchema stuff into its own header that can be included here, or cannam@135: // something. cannam@135: }; cannam@135: }; cannam@135: cannam@135: // ======================================================================================= cannam@135: // Capability clients cannam@135: cannam@135: class RequestHook; cannam@135: class ResponseHook; cannam@135: class PipelineHook; cannam@135: class ClientHook; cannam@135: cannam@135: template cannam@135: class Request: public Params::Builder { cannam@135: // A call that hasn't been sent yet. This class extends a Builder for the call's "Params" cannam@135: // structure with a method send() that actually sends it. cannam@135: // cannam@135: // Given a Cap'n Proto method `foo(a :A, b :B): C`, the generated client interface will have cannam@135: // a method `Request fooRequest()` (as well as a convenience method cannam@135: // `RemotePromise foo(A::Reader a, B::Reader b)`). cannam@135: cannam@135: public: cannam@135: inline Request(typename Params::Builder builder, kj::Own&& hook) cannam@135: : Params::Builder(builder), hook(kj::mv(hook)) {} cannam@135: inline Request(decltype(nullptr)): Params::Builder(nullptr) {} cannam@135: cannam@135: RemotePromise send() KJ_WARN_UNUSED_RESULT; cannam@135: // Send the call and return a promise for the results. cannam@135: cannam@135: private: cannam@135: kj::Own hook; cannam@135: cannam@135: friend class Capability::Client; cannam@135: friend struct DynamicCapability; cannam@135: template cannam@135: friend class CallContext; cannam@135: friend class RequestHook; cannam@135: }; cannam@135: cannam@135: template cannam@135: class Response: public Results::Reader { cannam@135: // A completed call. This class extends a Reader for the call's answer structure. The Response cannam@135: // is move-only -- once it goes out-of-scope, the underlying message will be freed. cannam@135: cannam@135: public: cannam@135: inline Response(typename Results::Reader reader, kj::Own&& hook) cannam@135: : Results::Reader(reader), hook(kj::mv(hook)) {} cannam@135: cannam@135: private: cannam@135: kj::Own hook; cannam@135: cannam@135: template cannam@135: friend class Request; cannam@135: friend class ResponseHook; cannam@135: }; cannam@135: cannam@135: class Capability::Client { cannam@135: // Base type for capability clients. cannam@135: cannam@135: public: cannam@135: typedef Capability Reads; cannam@135: typedef Capability Calls; cannam@135: cannam@135: Client(decltype(nullptr)); cannam@135: // If you need to declare a Client before you have anything to assign to it (perhaps because cannam@135: // the assignment is going to occur in an if/else scope), you can start by initializing it to cannam@135: // `nullptr`. The resulting client is not meant to be called and throws exceptions from all cannam@135: // methods. cannam@135: cannam@135: template ()>> cannam@135: Client(kj::Own&& server); cannam@135: // Make a client capability that wraps the given server capability. The server's methods will cannam@135: // only be executed in the given EventLoop, regardless of what thread calls the client's methods. cannam@135: cannam@135: template ()>> cannam@135: Client(kj::Promise&& promise); cannam@135: // Make a client from a promise for a future client. The resulting client queues calls until the cannam@135: // promise resolves. cannam@135: cannam@135: Client(kj::Exception&& exception); cannam@135: // Make a broken client that throws the given exception from all calls. cannam@135: cannam@135: Client(Client& other); cannam@135: Client& operator=(Client& other); cannam@135: // Copies by reference counting. Warning: This refcounting is not thread-safe. All copies of cannam@135: // the client must remain in one thread. cannam@135: cannam@135: Client(Client&&) = default; cannam@135: Client& operator=(Client&&) = default; cannam@135: // Move constructor avoids reference counting. cannam@135: cannam@135: explicit Client(kj::Own&& hook); cannam@135: // For use by the RPC implementation: Wrap a ClientHook. cannam@135: cannam@135: template cannam@135: typename T::Client castAs(); cannam@135: // Reinterpret the capability as implementing the given interface. Note that no error will occur cannam@135: // here if the capability does not actually implement this interface, but later method calls will cannam@135: // fail. It's up to the application to decide how indicate that additional interfaces are cannam@135: // supported. cannam@135: // cannam@135: // TODO(perf): GCC 4.8 / Clang 3.3: rvalue-qualified version for better performance. cannam@135: cannam@135: template cannam@135: typename T::Client castAs(InterfaceSchema schema); cannam@135: // Dynamic version. `T` must be `DynamicCapability`, and you must `#include `. cannam@135: cannam@135: kj::Promise whenResolved(); cannam@135: // If the capability is actually only a promise, the returned promise resolves once the cannam@135: // capability itself has resolved to its final destination (or propagates the exception if cannam@135: // the capability promise is rejected). This is mainly useful for error-checking in the case cannam@135: // where no calls are being made. There is no reason to wait for this before making calls; if cannam@135: // the capability does not resolve, the call results will propagate the error. cannam@135: cannam@135: Request typelessRequest( cannam@135: uint64_t interfaceId, uint16_t methodId, cannam@135: kj::Maybe sizeHint); cannam@135: // Make a request without knowing the types of the params or results. You specify the type ID cannam@135: // and method number manually. cannam@135: cannam@135: // TODO(someday): method(s) for Join cannam@135: cannam@135: protected: cannam@135: Client() = default; cannam@135: cannam@135: template cannam@135: Request newCall(uint64_t interfaceId, uint16_t methodId, cannam@135: kj::Maybe sizeHint); cannam@135: cannam@135: private: cannam@135: kj::Own hook; cannam@135: cannam@135: static kj::Own makeLocalClient(kj::Own&& server); cannam@135: cannam@135: template cannam@135: friend struct _::PointerHelpers; cannam@135: friend struct DynamicCapability; cannam@135: friend class Orphanage; cannam@135: friend struct DynamicStruct; cannam@135: friend struct DynamicList; cannam@135: template cannam@135: friend struct List; cannam@135: friend class _::CapabilityServerSetBase; cannam@135: friend class ClientHook; cannam@135: }; cannam@135: cannam@135: // ======================================================================================= cannam@135: // Capability servers cannam@135: cannam@135: class CallContextHook; cannam@135: cannam@135: template cannam@135: class CallContext: public kj::DisallowConstCopy { cannam@135: // Wrapper around CallContextHook with a specific return type. cannam@135: // cannam@135: // Methods of this class may only be called from within the server's event loop, not from other cannam@135: // threads. cannam@135: // cannam@135: // The CallContext becomes invalid as soon as the call reports completion. cannam@135: cannam@135: public: cannam@135: explicit CallContext(CallContextHook& hook); cannam@135: cannam@135: typename Params::Reader getParams(); cannam@135: // Get the params payload. cannam@135: cannam@135: void releaseParams(); cannam@135: // Release the params payload. getParams() will throw an exception after this is called. cannam@135: // Releasing the params may allow the RPC system to free up buffer space to handle other cannam@135: // requests. Long-running asynchronous methods should try to call this as early as is cannam@135: // convenient. cannam@135: cannam@135: typename Results::Builder getResults(kj::Maybe sizeHint = nullptr); cannam@135: typename Results::Builder initResults(kj::Maybe sizeHint = nullptr); cannam@135: void setResults(typename Results::Reader value); cannam@135: void adoptResults(Orphan&& value); cannam@135: Orphanage getResultsOrphanage(kj::Maybe sizeHint = nullptr); cannam@135: // Manipulate the results payload. The "Return" message (part of the RPC protocol) will cannam@135: // typically be allocated the first time one of these is called. Some RPC systems may cannam@135: // allocate these messages in a limited space (such as a shared memory segment), therefore the cannam@135: // application should delay calling these as long as is convenient to do so (but don't delay cannam@135: // if doing so would require extra copies later). cannam@135: // cannam@135: // `sizeHint` indicates a guess at the message size. This will usually be used to decide how cannam@135: // much space to allocate for the first message segment (don't worry: only space that is actually cannam@135: // used will be sent on the wire). If omitted, the system decides. The message root pointer cannam@135: // should not be included in the size. So, if you are simply going to copy some existing message cannam@135: // directly into the results, just call `.totalSize()` and pass that in. cannam@135: cannam@135: template cannam@135: kj::Promise tailCall(Request&& tailRequest); cannam@135: // Resolve the call by making a tail call. `tailRequest` is a request that has been filled in cannam@135: // but not yet sent. The context will send the call, then fill in the results with the result cannam@135: // of the call. If tailCall() is used, {get,init,set,adopt}Results (above) *must not* be called. cannam@135: // cannam@135: // The RPC implementation may be able to optimize a tail call to another machine such that the cannam@135: // results never actually pass through this machine. Even if no such optimization is possible, cannam@135: // `tailCall()` may allow pipelined calls to be forwarded optimistically to the new call site. cannam@135: // cannam@135: // In general, this should be the last thing a method implementation calls, and the promise cannam@135: // returned from `tailCall()` should then be returned by the method implementation. cannam@135: cannam@135: void allowCancellation(); cannam@135: // Indicate that it is OK for the RPC system to discard its Promise for this call's result if cannam@135: // the caller cancels the call, thereby transitively canceling any asynchronous operations the cannam@135: // call implementation was performing. This is not done by default because it could represent a cannam@135: // security risk: applications must be carefully written to ensure that they do not end up in cannam@135: // a bad state if an operation is canceled at an arbitrary point. However, for long-running cannam@135: // method calls that hold significant resources, prompt cancellation is often useful. cannam@135: // cannam@135: // Keep in mind that asynchronous cancellation cannot occur while the method is synchronously cannam@135: // executing on a local thread. The method must perform an asynchronous operation or call cannam@135: // `EventLoop::current().evalLater()` to yield control. cannam@135: // cannam@135: // Note: You might think that we should offer `onCancel()` and/or `isCanceled()` methods that cannam@135: // provide notification when the caller cancels the request without forcefully killing off the cannam@135: // promise chain. Unfortunately, this composes poorly with promise forking: the canceled cannam@135: // path may be just one branch of a fork of the result promise. The other branches still want cannam@135: // the call to continue. Promise forking is used within the Cap'n Proto implementation -- in cannam@135: // particular each pipelined call forks the result promise. So, if a caller made a pipelined cannam@135: // call and then dropped the original object, the call should not be canceled, but it would be cannam@135: // excessively complicated for the framework to avoid notififying of cancellation as long as cannam@135: // pipelined calls still exist. cannam@135: cannam@135: private: cannam@135: CallContextHook* hook; cannam@135: cannam@135: friend class Capability::Server; cannam@135: friend struct DynamicCapability; cannam@135: }; cannam@135: cannam@135: class Capability::Server { cannam@135: // Objects implementing a Cap'n Proto interface must subclass this. Typically, such objects cannam@135: // will instead subclass a typed Server interface which will take care of implementing cannam@135: // dispatchCall(). cannam@135: cannam@135: public: cannam@135: typedef Capability Serves; cannam@135: cannam@135: virtual kj::Promise dispatchCall(uint64_t interfaceId, uint16_t methodId, cannam@135: CallContext context) = 0; cannam@135: // Call the given method. `params` is the input struct, and should be released as soon as it cannam@135: // is no longer needed. `context` may be used to allocate the output struct and deal with cannam@135: // cancellation. cannam@135: cannam@135: // TODO(someday): Method which can optionally be overridden to implement Join when the object is cannam@135: // a proxy. cannam@135: cannam@135: protected: cannam@135: inline Capability::Client thisCap(); cannam@135: // Get a capability pointing to this object, much like the `this` keyword. cannam@135: // cannam@135: // The effect of this method is undefined if: cannam@135: // - No capability client has been created pointing to this object. (This is always the case in cannam@135: // the server's constructor.) cannam@135: // - The capability client pointing at this object has been destroyed. (This is always the case cannam@135: // in the server's destructor.) cannam@135: // - Multiple capability clients have been created around the same server (possible if the server cannam@135: // is refcounted, which is not recommended since the client itself provides refcounting). cannam@135: cannam@135: template cannam@135: CallContext internalGetTypedContext( cannam@135: CallContext typeless); cannam@135: kj::Promise internalUnimplemented(const char* actualInterfaceName, cannam@135: uint64_t requestedTypeId); cannam@135: kj::Promise internalUnimplemented(const char* interfaceName, cannam@135: uint64_t typeId, uint16_t methodId); cannam@135: kj::Promise internalUnimplemented(const char* interfaceName, const char* methodName, cannam@135: uint64_t typeId, uint16_t methodId); cannam@135: cannam@135: private: cannam@135: ClientHook* thisHook = nullptr; cannam@135: friend class LocalClient; cannam@135: }; cannam@135: cannam@135: // ======================================================================================= cannam@135: cannam@135: class ReaderCapabilityTable: private _::CapTableReader { cannam@135: // Class which imbues Readers with the ability to read capabilities. cannam@135: // cannam@135: // In Cap'n Proto format, the encoding of a capability pointer is simply an integer index into cannam@135: // an external table. Since these pointers fundamentally point outside the message, a cannam@135: // MessageReader by default has no idea what they point at, and therefore reading capabilities cannam@135: // from such a reader will throw exceptions. cannam@135: // cannam@135: // In order to be able to read capabilities, you must first attach a capability table, using cannam@135: // this class. By "imbuing" a Reader, you get a new Reader which will interpret capability cannam@135: // pointers by treating them as indexes into the ReaderCapabilityTable. cannam@135: // cannam@135: // Note that when using Cap'n Proto's RPC system, this is handled automatically. cannam@135: cannam@135: public: cannam@135: explicit ReaderCapabilityTable(kj::Array>> table); cannam@135: KJ_DISALLOW_COPY(ReaderCapabilityTable); cannam@135: cannam@135: template cannam@135: T imbue(T reader); cannam@135: // Return a reader equivalent to `reader` except that when reading capability-valued fields, cannam@135: // the capabilities are looked up in this table. cannam@135: cannam@135: private: cannam@135: kj::Array>> table; cannam@135: cannam@135: kj::Maybe> extractCap(uint index) override; cannam@135: }; cannam@135: cannam@135: class BuilderCapabilityTable: private _::CapTableBuilder { cannam@135: // Class which imbues Builders with the ability to read and write capabilities. cannam@135: // cannam@135: // This is much like ReaderCapabilityTable, except for builders. The table starts out empty, cannam@135: // but capabilities can be added to it over time. cannam@135: cannam@135: public: cannam@135: BuilderCapabilityTable(); cannam@135: KJ_DISALLOW_COPY(BuilderCapabilityTable); cannam@135: cannam@135: inline kj::ArrayPtr>> getTable() { return table; } cannam@135: cannam@135: template cannam@135: T imbue(T builder); cannam@135: // Return a builder equivalent to `builder` except that when reading capability-valued fields, cannam@135: // the capabilities are looked up in this table. cannam@135: cannam@135: private: cannam@135: kj::Vector>> table; cannam@135: cannam@135: kj::Maybe> extractCap(uint index) override; cannam@135: uint injectCap(kj::Own&& cap) override; cannam@135: void dropCap(uint index) override; cannam@135: }; cannam@135: cannam@135: // ======================================================================================= cannam@135: cannam@135: namespace _ { // private cannam@135: cannam@135: class CapabilityServerSetBase { cannam@135: public: cannam@135: Capability::Client addInternal(kj::Own&& server, void* ptr); cannam@135: kj::Promise getLocalServerInternal(Capability::Client& client); cannam@135: }; cannam@135: cannam@135: } // namespace _ (private) cannam@135: cannam@135: template cannam@135: class CapabilityServerSet: private _::CapabilityServerSetBase { cannam@135: // Allows a server to recognize its own capabilities when passed back to it, and obtain the cannam@135: // underlying Server objects associated with them. cannam@135: // cannam@135: // All objects in the set must have the same interface type T. The objects may implement various cannam@135: // interfaces derived from T (and in fact T can be `capnp::Capability` to accept all objects), cannam@135: // but note that if you compile with RTTI disabled then you will not be able to down-cast through cannam@135: // virtual inheritance, and all inheritance between server interfaces is virtual. So, with RTTI cannam@135: // disabled, you will likely need to set T to be the most-derived Cap'n Proto interface type, cannam@135: // and you server class will need to be directly derived from that, so that you can use cannam@135: // static_cast (or kj::downcast) to cast to it after calling getLocalServer(). (If you compile cannam@135: // with RTTI, then you can freely dynamic_cast and ignore this issue!) cannam@135: cannam@135: public: cannam@135: CapabilityServerSet() = default; cannam@135: KJ_DISALLOW_COPY(CapabilityServerSet); cannam@135: cannam@135: typename T::Client add(kj::Own&& server); cannam@135: // Create a new capability Client for the given Server and also add this server to the set. cannam@135: cannam@135: kj::Promise> getLocalServer(typename T::Client& client); cannam@135: // Given a Client pointing to a server previously passed to add(), return the corresponding cannam@135: // Server. This returns a promise because if the input client is itself a promise, this must cannam@135: // wait for it to resolve. Keep in mind that the server will be deleted when all clients are cannam@135: // gone, so the caller should make sure to keep the client alive (hence why this method only cannam@135: // accepts an lvalue input). cannam@135: }; cannam@135: cannam@135: // ======================================================================================= cannam@135: // Hook interfaces which must be implemented by the RPC system. Applications never call these cannam@135: // directly; the RPC system implements them and the types defined earlier in this file wrap them. cannam@135: cannam@135: class RequestHook { cannam@135: // Hook interface implemented by RPC system representing a request being built. cannam@135: cannam@135: public: cannam@135: virtual RemotePromise send() = 0; cannam@135: // Send the call and return a promise for the result. cannam@135: cannam@135: virtual const void* getBrand() = 0; cannam@135: // Returns a void* that identifies who made this request. This can be used by an RPC adapter to cannam@135: // discover when tail call is going to be sent over its own connection and therefore can be cannam@135: // optimized into a remote tail call. cannam@135: cannam@135: template cannam@135: inline static kj::Own from(Request&& request) { cannam@135: return kj::mv(request.hook); cannam@135: } cannam@135: }; cannam@135: cannam@135: class ResponseHook { cannam@135: // Hook interface implemented by RPC system representing a response. cannam@135: // cannam@135: // At present this class has no methods. It exists only for garbage collection -- when the cannam@135: // ResponseHook is destroyed, the results can be freed. cannam@135: cannam@135: public: cannam@135: virtual ~ResponseHook() noexcept(false); cannam@135: // Just here to make sure the type is dynamic. cannam@135: cannam@135: template cannam@135: inline static kj::Own from(Response&& response) { cannam@135: return kj::mv(response.hook); cannam@135: } cannam@135: }; cannam@135: cannam@135: // class PipelineHook is declared in any.h because it is needed there. cannam@135: cannam@135: class ClientHook { cannam@135: public: cannam@135: ClientHook(); cannam@135: cannam@135: virtual Request newCall( cannam@135: uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) = 0; cannam@135: // Start a new call, allowing the client to allocate request/response objects as it sees fit. cannam@135: // This version is used when calls are made from application code in the local process. cannam@135: cannam@135: struct VoidPromiseAndPipeline { cannam@135: kj::Promise promise; cannam@135: kj::Own pipeline; cannam@135: }; cannam@135: cannam@135: virtual VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, cannam@135: kj::Own&& context) = 0; cannam@135: // Call the object, but the caller controls allocation of the request/response objects. If the cannam@135: // callee insists on allocating these objects itself, it must make a copy. This version is used cannam@135: // when calls come in over the network via an RPC system. Note that even if the returned cannam@135: // `Promise` is discarded, the call may continue executing if any pipelined calls are cannam@135: // waiting for it. cannam@135: // cannam@135: // Since the caller of this method chooses the CallContext implementation, it is the caller's cannam@135: // responsibility to ensure that the returned promise is not canceled unless allowed via cannam@135: // the context's `allowCancellation()`. cannam@135: // cannam@135: // The call must not begin synchronously; the callee must arrange for the call to begin in a cannam@135: // later turn of the event loop. Otherwise, application code may call back and affect the cannam@135: // callee's state in an unexpected way. cannam@135: cannam@135: virtual kj::Maybe getResolved() = 0; cannam@135: // If this ClientHook is a promise that has already resolved, returns the inner, resolved version cannam@135: // of the capability. The caller may permanently replace this client with the resolved one if cannam@135: // desired. Returns null if the client isn't a promise or hasn't resolved yet -- use cannam@135: // `whenMoreResolved()` to distinguish between them. cannam@135: cannam@135: virtual kj::Maybe>> whenMoreResolved() = 0; cannam@135: // If this client is a settled reference (not a promise), return nullptr. Otherwise, return a cannam@135: // promise that eventually resolves to a new client that is closer to being the final, settled cannam@135: // client (i.e. the value eventually returned by `getResolved()`). Calling this repeatedly cannam@135: // should eventually produce a settled client. cannam@135: cannam@135: kj::Promise whenResolved(); cannam@135: // Repeatedly calls whenMoreResolved() until it returns nullptr. cannam@135: cannam@135: virtual kj::Own addRef() = 0; cannam@135: // Return a new reference to the same capability. cannam@135: cannam@135: virtual const void* getBrand() = 0; cannam@135: // Returns a void* that identifies who made this client. This can be used by an RPC adapter to cannam@135: // discover when a capability it needs to marshal is one that it created in the first place, and cannam@135: // therefore it can transfer the capability without proxying. cannam@135: cannam@135: static const uint NULL_CAPABILITY_BRAND; cannam@135: // Value is irrelevant; used for pointer. cannam@135: cannam@135: inline bool isNull() { return getBrand() == &NULL_CAPABILITY_BRAND; } cannam@135: // Returns true if the capability was created as a result of assigning a Client to null or by cannam@135: // reading a null pointer out of a Cap'n Proto message. cannam@135: cannam@135: virtual void* getLocalServer(_::CapabilityServerSetBase& capServerSet); cannam@135: // If this is a local capability created through `capServerSet`, return the underlying Server. cannam@135: // Otherwise, return nullptr. Default implementation (which everyone except LocalClient should cannam@135: // use) always returns nullptr. cannam@135: cannam@135: static kj::Own from(Capability::Client client) { return kj::mv(client.hook); } cannam@135: }; cannam@135: cannam@135: class CallContextHook { cannam@135: // Hook interface implemented by RPC system to manage a call on the server side. See cannam@135: // CallContext. cannam@135: cannam@135: public: cannam@135: virtual AnyPointer::Reader getParams() = 0; cannam@135: virtual void releaseParams() = 0; cannam@135: virtual AnyPointer::Builder getResults(kj::Maybe sizeHint) = 0; cannam@135: virtual kj::Promise tailCall(kj::Own&& request) = 0; cannam@135: virtual void allowCancellation() = 0; cannam@135: cannam@135: virtual kj::Promise onTailCall() = 0; cannam@135: // If `tailCall()` is called, resolves to the PipelineHook from the tail call. An cannam@135: // implementation of `ClientHook::call()` is allowed to call this at most once. cannam@135: cannam@135: virtual ClientHook::VoidPromiseAndPipeline directTailCall(kj::Own&& request) = 0; cannam@135: // Call this when you would otherwise call onTailCall() immediately followed by tailCall(). cannam@135: // Implementations of tailCall() should typically call directTailCall() and then fulfill the cannam@135: // promise fulfiller for onTailCall() with the returned pipeline. cannam@135: cannam@135: virtual kj::Own addRef() = 0; cannam@135: }; cannam@135: cannam@135: kj::Own newLocalPromiseClient(kj::Promise>&& promise); cannam@135: // Returns a ClientHook that queues up calls until `promise` resolves, then forwards them to cannam@135: // the new client. This hook's `getResolved()` and `whenMoreResolved()` methods will reflect the cannam@135: // redirection to the eventual replacement client. cannam@135: cannam@135: kj::Own newLocalPromisePipeline(kj::Promise>&& promise); cannam@135: // Returns a PipelineHook that queues up calls until `promise` resolves, then forwards them to cannam@135: // the new pipeline. cannam@135: cannam@135: kj::Own newBrokenCap(kj::StringPtr reason); cannam@135: kj::Own newBrokenCap(kj::Exception&& reason); cannam@135: // Helper function that creates a capability which simply throws exceptions when called. cannam@135: cannam@135: kj::Own newBrokenPipeline(kj::Exception&& reason); cannam@135: // Helper function that creates a pipeline which simply throws exceptions when called. cannam@135: cannam@135: Request newBrokenRequest( cannam@135: kj::Exception&& reason, kj::Maybe sizeHint); cannam@135: // Helper function that creates a Request object that simply throws exceptions when sent. cannam@135: cannam@135: // ======================================================================================= cannam@135: // Extend PointerHelpers for interfaces cannam@135: cannam@135: namespace _ { // private cannam@135: cannam@135: template cannam@135: struct PointerHelpers { cannam@135: static inline typename T::Client get(PointerReader reader) { cannam@135: return typename T::Client(reader.getCapability()); cannam@135: } cannam@135: static inline typename T::Client get(PointerBuilder builder) { cannam@135: return typename T::Client(builder.getCapability()); cannam@135: } cannam@135: static inline void set(PointerBuilder builder, typename T::Client&& value) { cannam@135: builder.setCapability(kj::mv(value.Capability::Client::hook)); cannam@135: } cannam@135: static inline void set(PointerBuilder builder, typename T::Client& value) { cannam@135: builder.setCapability(value.Capability::Client::hook->addRef()); cannam@135: } cannam@135: static inline void adopt(PointerBuilder builder, Orphan&& value) { cannam@135: builder.adopt(kj::mv(value.builder)); cannam@135: } cannam@135: static inline Orphan disown(PointerBuilder builder) { cannam@135: return Orphan(builder.disown()); cannam@135: } cannam@135: }; cannam@135: cannam@135: } // namespace _ (private) cannam@135: cannam@135: // ======================================================================================= cannam@135: // Extend List for interfaces cannam@135: cannam@135: template cannam@135: struct List { cannam@135: List() = delete; cannam@135: cannam@135: class Reader { cannam@135: public: cannam@135: typedef List Reads; cannam@135: cannam@135: Reader() = default; cannam@135: inline explicit Reader(_::ListReader reader): reader(reader) {} cannam@135: cannam@135: inline uint size() const { return reader.size() / ELEMENTS; } cannam@135: inline typename T::Client operator[](uint index) const { cannam@135: KJ_IREQUIRE(index < size()); cannam@135: return typename T::Client(reader.getPointerElement(index * ELEMENTS).getCapability()); cannam@135: } cannam@135: cannam@135: typedef _::IndexingIterator Iterator; cannam@135: inline Iterator begin() const { return Iterator(this, 0); } cannam@135: inline Iterator end() const { return Iterator(this, size()); } cannam@135: cannam@135: private: cannam@135: _::ListReader reader; cannam@135: template cannam@135: friend struct _::PointerHelpers; cannam@135: template cannam@135: friend struct List; cannam@135: friend class Orphanage; cannam@135: template cannam@135: friend struct ToDynamic_; cannam@135: }; cannam@135: cannam@135: class Builder { cannam@135: public: cannam@135: typedef List Builds; cannam@135: cannam@135: Builder() = delete; cannam@135: inline Builder(decltype(nullptr)) {} cannam@135: inline explicit Builder(_::ListBuilder builder): builder(builder) {} cannam@135: cannam@135: inline operator Reader() const { return Reader(builder.asReader()); } cannam@135: inline Reader asReader() const { return Reader(builder.asReader()); } cannam@135: cannam@135: inline uint size() const { return builder.size() / ELEMENTS; } cannam@135: inline typename T::Client operator[](uint index) { cannam@135: KJ_IREQUIRE(index < size()); cannam@135: return typename T::Client(builder.getPointerElement(index * ELEMENTS).getCapability()); cannam@135: } cannam@135: inline void set(uint index, typename T::Client value) { cannam@135: KJ_IREQUIRE(index < size()); cannam@135: builder.getPointerElement(index * ELEMENTS).setCapability(kj::mv(value.hook)); cannam@135: } cannam@135: inline void adopt(uint index, Orphan&& value) { cannam@135: KJ_IREQUIRE(index < size()); cannam@135: builder.getPointerElement(index * ELEMENTS).adopt(kj::mv(value)); cannam@135: } cannam@135: inline Orphan disown(uint index) { cannam@135: KJ_IREQUIRE(index < size()); cannam@135: return Orphan(builder.getPointerElement(index * ELEMENTS).disown()); cannam@135: } cannam@135: cannam@135: typedef _::IndexingIterator Iterator; cannam@135: inline Iterator begin() { return Iterator(this, 0); } cannam@135: inline Iterator end() { return Iterator(this, size()); } cannam@135: cannam@135: private: cannam@135: _::ListBuilder builder; cannam@135: friend class Orphanage; cannam@135: template cannam@135: friend struct ToDynamic_; cannam@135: }; cannam@135: cannam@135: private: cannam@135: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { cannam@135: return builder.initList(ElementSize::POINTER, size * ELEMENTS); cannam@135: } cannam@135: inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { cannam@135: return builder.getList(ElementSize::POINTER, defaultValue); cannam@135: } cannam@135: inline static _::ListReader getFromPointer( cannam@135: const _::PointerReader& reader, const word* defaultValue) { cannam@135: return reader.getList(ElementSize::POINTER, defaultValue); cannam@135: } cannam@135: cannam@135: template cannam@135: friend struct List; cannam@135: template cannam@135: friend struct _::PointerHelpers; cannam@135: }; cannam@135: cannam@135: // ======================================================================================= cannam@135: // Inline implementation details cannam@135: cannam@135: template cannam@135: RemotePromise Request::send() { cannam@135: auto typelessPromise = hook->send(); cannam@135: hook = nullptr; // prevent reuse cannam@135: cannam@135: // Convert the Promise to return the correct response type. cannam@135: // Explicitly upcast to kj::Promise to make clear that calling .then() doesn't invalidate the cannam@135: // Pipeline part of the RemotePromise. cannam@135: auto typedPromise = kj::implicitCast>&>(typelessPromise) cannam@135: .then([](Response&& response) -> Response { cannam@135: return Response(response.getAs(), kj::mv(response.hook)); cannam@135: }); cannam@135: cannam@135: // Wrap the typeless pipeline in a typed wrapper. cannam@135: typename Results::Pipeline typedPipeline( cannam@135: kj::mv(kj::implicitCast(typelessPromise))); cannam@135: cannam@135: return RemotePromise(kj::mv(typedPromise), kj::mv(typedPipeline)); cannam@135: } cannam@135: cannam@135: inline Capability::Client::Client(kj::Own&& hook): hook(kj::mv(hook)) {} cannam@135: template cannam@135: inline Capability::Client::Client(kj::Own&& server) cannam@135: : hook(makeLocalClient(kj::mv(server))) {} cannam@135: template cannam@135: inline Capability::Client::Client(kj::Promise&& promise) cannam@135: : hook(newLocalPromiseClient(promise.then([](T&& t) { return kj::mv(t.hook); }))) {} cannam@135: inline Capability::Client::Client(Client& other): hook(other.hook->addRef()) {} cannam@135: inline Capability::Client& Capability::Client::operator=(Client& other) { cannam@135: hook = other.hook->addRef(); cannam@135: return *this; cannam@135: } cannam@135: template cannam@135: inline typename T::Client Capability::Client::castAs() { cannam@135: return typename T::Client(hook->addRef()); cannam@135: } cannam@135: inline kj::Promise Capability::Client::whenResolved() { cannam@135: return hook->whenResolved(); cannam@135: } cannam@135: inline Request Capability::Client::typelessRequest( cannam@135: uint64_t interfaceId, uint16_t methodId, cannam@135: kj::Maybe sizeHint) { cannam@135: return newCall(interfaceId, methodId, sizeHint); cannam@135: } cannam@135: template cannam@135: inline Request Capability::Client::newCall( cannam@135: uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) { cannam@135: auto typeless = hook->newCall(interfaceId, methodId, sizeHint); cannam@135: return Request(typeless.template getAs(), kj::mv(typeless.hook)); cannam@135: } cannam@135: cannam@135: template cannam@135: inline CallContext::CallContext(CallContextHook& hook): hook(&hook) {} cannam@135: template cannam@135: inline typename Params::Reader CallContext::getParams() { cannam@135: return hook->getParams().template getAs(); cannam@135: } cannam@135: template cannam@135: inline void CallContext::releaseParams() { cannam@135: hook->releaseParams(); cannam@135: } cannam@135: template cannam@135: inline typename Results::Builder CallContext::getResults( cannam@135: kj::Maybe sizeHint) { cannam@135: // `template` keyword needed due to: http://llvm.org/bugs/show_bug.cgi?id=17401 cannam@135: return hook->getResults(sizeHint).template getAs(); cannam@135: } cannam@135: template cannam@135: inline typename Results::Builder CallContext::initResults( cannam@135: kj::Maybe sizeHint) { cannam@135: // `template` keyword needed due to: http://llvm.org/bugs/show_bug.cgi?id=17401 cannam@135: return hook->getResults(sizeHint).template initAs(); cannam@135: } cannam@135: template cannam@135: inline void CallContext::setResults(typename Results::Reader value) { cannam@135: hook->getResults(value.totalSize()).template setAs(value); cannam@135: } cannam@135: template cannam@135: inline void CallContext::adoptResults(Orphan&& value) { cannam@135: hook->getResults(nullptr).adopt(kj::mv(value)); cannam@135: } cannam@135: template cannam@135: inline Orphanage CallContext::getResultsOrphanage( cannam@135: kj::Maybe sizeHint) { cannam@135: return Orphanage::getForMessageContaining(hook->getResults(sizeHint)); cannam@135: } cannam@135: template cannam@135: template cannam@135: inline kj::Promise CallContext::tailCall( cannam@135: Request&& tailRequest) { cannam@135: return hook->tailCall(kj::mv(tailRequest.hook)); cannam@135: } cannam@135: template cannam@135: inline void CallContext::allowCancellation() { cannam@135: hook->allowCancellation(); cannam@135: } cannam@135: cannam@135: template cannam@135: CallContext Capability::Server::internalGetTypedContext( cannam@135: CallContext typeless) { cannam@135: return CallContext(*typeless.hook); cannam@135: } cannam@135: cannam@135: Capability::Client Capability::Server::thisCap() { cannam@135: return Client(thisHook->addRef()); cannam@135: } cannam@135: cannam@135: template cannam@135: T ReaderCapabilityTable::imbue(T reader) { cannam@135: return T(_::PointerHelpers>::getInternalReader(reader).imbue(this)); cannam@135: } cannam@135: cannam@135: template cannam@135: T BuilderCapabilityTable::imbue(T builder) { cannam@135: return T(_::PointerHelpers>::getInternalBuilder(kj::mv(builder)).imbue(this)); cannam@135: } cannam@135: cannam@135: template cannam@135: typename T::Client CapabilityServerSet::add(kj::Own&& server) { cannam@135: void* ptr = reinterpret_cast(server.get()); cannam@135: // Clang insists that `castAs` is a template-dependent member and therefore we need the cannam@135: // `template` keyword here, but AFAICT this is wrong: addImpl() is not a template. cannam@135: return addInternal(kj::mv(server), ptr).template castAs(); cannam@135: } cannam@135: cannam@135: template cannam@135: kj::Promise> CapabilityServerSet::getLocalServer( cannam@135: typename T::Client& client) { cannam@135: return getLocalServerInternal(client) cannam@135: .then([](void* server) -> kj::Maybe { cannam@135: if (server == nullptr) { cannam@135: return nullptr; cannam@135: } else { cannam@135: return *reinterpret_cast(server); cannam@135: } cannam@135: }); cannam@135: } cannam@135: cannam@135: template cannam@135: struct Orphanage::GetInnerReader { cannam@135: static inline kj::Own apply(typename T::Client t) { cannam@135: return ClientHook::from(kj::mv(t)); cannam@135: } cannam@135: }; cannam@135: cannam@135: } // namespace capnp cannam@135: cannam@135: #endif // CAPNP_CAPABILITY_H_