cannam@62: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@62: // Licensed under the MIT License:
cannam@62: //
cannam@62: // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@62: // of this software and associated documentation files (the "Software"), to deal
cannam@62: // in the Software without restriction, including without limitation the rights
cannam@62: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@62: // copies of the Software, and to permit persons to whom the Software is
cannam@62: // furnished to do so, subject to the following conditions:
cannam@62: //
cannam@62: // The above copyright notice and this permission notice shall be included in
cannam@62: // all copies or substantial portions of the Software.
cannam@62: //
cannam@62: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@62: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@62: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@62: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@62: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@62: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@62: // THE SOFTWARE.
cannam@62: 
cannam@62: #ifndef CAPNP_RPC_H_
cannam@62: #define CAPNP_RPC_H_
cannam@62: 
cannam@62: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@62: #pragma GCC system_header
cannam@62: #endif
cannam@62: 
cannam@62: #include "capability.h"
cannam@62: #include "rpc-prelude.h"
cannam@62: 
cannam@62: namespace capnp {
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: class VatNetwork;
cannam@62: template <typename SturdyRefObjectId>
cannam@62: class SturdyRefRestorer;
cannam@62: 
cannam@62: template <typename VatId>
cannam@62: class BootstrapFactory: public _::BootstrapFactoryBase {
cannam@62:   // Interface that constructs per-client bootstrap interfaces. Use this if you want each client
cannam@62:   // who connects to see a different bootstrap interface based on their (authenticated) VatId.
cannam@62:   // This allows an application to bootstrap off of the authentication performed at the VatNetwork
cannam@62:   // level. (Typically VatId is some sort of public key.)
cannam@62:   //
cannam@62:   // This is only useful for multi-party networks. For TwoPartyVatNetwork, there's no reason to
cannam@62:   // use a BootstrapFactory; just specify a single bootstrap capability in this case.
cannam@62: 
cannam@62: public:
cannam@62:   virtual Capability::Client createFor(typename VatId::Reader clientId) = 0;
cannam@62:   // Create a bootstrap capability appropriate for exposing to the given client. VatNetwork will
cannam@62:   // have authenticated the client VatId before this is called.
cannam@62: 
cannam@62: private:
cannam@62:   Capability::Client baseCreateFor(AnyStruct::Reader clientId) override;
cannam@62: };
cannam@62: 
cannam@62: template <typename VatId>
cannam@62: class RpcSystem: public _::RpcSystemBase {
cannam@62:   // Represents the RPC system, which is the portal to objects available on the network.
cannam@62:   //
cannam@62:   // The RPC implementation sits on top of an implementation of `VatNetwork`.  The `VatNetwork`
cannam@62:   // determines how to form connections between vats -- specifically, two-way, private, reliable,
cannam@62:   // sequenced datagram connections.  The RPC implementation determines how to use such connections
cannam@62:   // to manage object references and make method calls.
cannam@62:   //
cannam@62:   // See `makeRpcServer()` and `makeRpcClient()` below for convenient syntax for setting up an
cannam@62:   // `RpcSystem` given a `VatNetwork`.
cannam@62:   //
cannam@62:   // See `ez-rpc.h` for an even simpler interface for setting up RPC in a typical two-party
cannam@62:   // client/server scenario.
cannam@62: 
cannam@62: public:
cannam@62:   template <typename ProvisionId, typename RecipientId,
cannam@62:             typename ThirdPartyCapId, typename JoinResult>
cannam@62:   RpcSystem(
cannam@62:       VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:       kj::Maybe<Capability::Client> bootstrapInterface,
cannam@62:       kj::Maybe<RealmGateway<>::Client> gateway = nullptr);
cannam@62: 
cannam@62:   template <typename ProvisionId, typename RecipientId,
cannam@62:             typename ThirdPartyCapId, typename JoinResult>
cannam@62:   RpcSystem(
cannam@62:       VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:       BootstrapFactory<VatId>& bootstrapFactory,
cannam@62:       kj::Maybe<RealmGateway<>::Client> gateway = nullptr);
cannam@62: 
cannam@62:   template <typename ProvisionId, typename RecipientId,
cannam@62:             typename ThirdPartyCapId, typename JoinResult,
cannam@62:             typename LocalSturdyRefObjectId>
cannam@62:   RpcSystem(
cannam@62:       VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:       SturdyRefRestorer<LocalSturdyRefObjectId>& restorer);
cannam@62: 
cannam@62:   RpcSystem(RpcSystem&& other) = default;
cannam@62: 
cannam@62:   Capability::Client bootstrap(typename VatId::Reader vatId);
cannam@62:   // Connect to the given vat and return its bootstrap interface.
cannam@62: 
cannam@62:   Capability::Client restore(typename VatId::Reader hostId, AnyPointer::Reader objectId)
cannam@62:       KJ_DEPRECATED("Please transition to using a bootstrap interface instead.");
cannam@62:   // ** DEPRECATED **
cannam@62:   //
cannam@62:   // Restores the given SturdyRef from the network and return the capability representing it.
cannam@62:   //
cannam@62:   // `hostId` identifies the host from which to request the ref, in the format specified by the
cannam@62:   // `VatNetwork` in use.  `objectId` is the object ID in whatever format is expected by said host.
cannam@62:   //
cannam@62:   // This method will be removed in a future version of Cap'n Proto. Instead, please transition
cannam@62:   // to using bootstrap(), which is equivalent to calling restore() with a null `objectId`.
cannam@62:   // You may emulate the old concept of object IDs by exporting a bootstrap interface which has
cannam@62:   // methods that can be used to obtain other capabilities by ID.
cannam@62: 
cannam@62:   void setFlowLimit(size_t words);
cannam@62:   // Sets the incoming call flow limit. If more than `words` worth of call messages have not yet
cannam@62:   // received responses, the RpcSystem will not read further messages from the stream. This can be
cannam@62:   // used as a crude way to prevent a resource exhaustion attack (or bug) in which a peer makes an
cannam@62:   // excessive number of simultaneous calls that consume the receiver's RAM.
cannam@62:   //
cannam@62:   // There are some caveats. When over the flow limit, all messages are blocked, including returns.
cannam@62:   // If the outstanding calls are themselves waiting on calls going in the opposite direction, the
cannam@62:   // flow limit may prevent those calls from completing, leading to deadlock. However, a
cannam@62:   // sufficiently high limit should make this unlikely.
cannam@62:   //
cannam@62:   // Note that a call's parameter size counts against the flow limit until the call returns, even
cannam@62:   // if the recipient calls releaseParams() to free the parameter memory early. This is because
cannam@62:   // releaseParams() may simply indicate that the parameters have been forwarded to another
cannam@62:   // machine, but are still in-memory there. For illustration, say that Alice made a call to Bob
cannam@62:   // who forwarded the call to Carol. Bob has imposed a flow limit on Alice. Alice's calls are
cannam@62:   // being forwarded to Carol, so Bob never keeps the parameters in-memory for more than a brief
cannam@62:   // period. However, the flow limit counts all calls that haven't returned, even if Bob has
cannam@62:   // already freed the memory they consumed. You might argue that the right solution here is
cannam@62:   // instead for Carol to impose her own flow limit on Bob. This has a serious problem, though:
cannam@62:   // Bob might be forwarding requests to Carol on behalf of many different parties, not just Alice.
cannam@62:   // If Alice can pump enough data to hit the Bob -> Carol flow limit, then those other parties
cannam@62:   // will be disrupted. Thus, we can only really impose the limit on the Alice -> Bob link, which
cannam@62:   // only affects Alice. We need that one flow limit to limit Alice's impact on the whole system,
cannam@62:   // so it has to count all in-flight calls.
cannam@62:   //
cannam@62:   // In Sandstorm, flow limits are imposed by the supervisor on calls coming out of a grain, in
cannam@62:   // order to prevent a grain from inundating the system with in-flight calls. In practice, the
cannam@62:   // main time this happens is when a grain is pushing a large file download and doesn't implement
cannam@62:   // proper cooperative flow control.
cannam@62: };
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     Capability::Client bootstrapInterface);
cannam@62: // Make an RPC server.  Typical usage (e.g. in a main() function):
cannam@62: //
cannam@62: //    MyEventLoop eventLoop;
cannam@62: //    kj::WaitScope waitScope(eventLoop);
cannam@62: //    MyNetwork network;
cannam@62: //    MyMainInterface::Client bootstrap = makeMain();
cannam@62: //    auto server = makeRpcServer(network, bootstrap);
cannam@62: //    kj::NEVER_DONE.wait(waitScope);  // run forever
cannam@62: //
cannam@62: // See also ez-rpc.h, which has simpler instructions for the common case of a two-party
cannam@62: // client-server RPC connection.
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult, typename RealmGatewayClient,
cannam@62:           typename InternalRef = _::InternalRefFromRealmGatewayClient<RealmGatewayClient>,
cannam@62:           typename ExternalRef = _::ExternalRefFromRealmGatewayClient<RealmGatewayClient>>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     Capability::Client bootstrapInterface, RealmGatewayClient gateway);
cannam@62: // Make an RPC server for a VatNetwork that resides in a different realm from the application.
cannam@62: // The given RealmGateway is used to translate SturdyRefs between the app's ("internal") format
cannam@62: // and the network's ("external") format.
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     BootstrapFactory<VatId>& bootstrapFactory);
cannam@62: // Make an RPC server that can serve different bootstrap interfaces to different clients via a
cannam@62: // BootstrapInterface.
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult, typename RealmGatewayClient,
cannam@62:           typename InternalRef = _::InternalRefFromRealmGatewayClient<RealmGatewayClient>,
cannam@62:           typename ExternalRef = _::ExternalRefFromRealmGatewayClient<RealmGatewayClient>>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     BootstrapFactory<VatId>& bootstrapFactory, RealmGatewayClient gateway);
cannam@62: // Make an RPC server that can serve different bootstrap interfaces to different clients via a
cannam@62: // BootstrapInterface and communicates with a different realm than the application is in via a
cannam@62: // RealmGateway.
cannam@62: 
cannam@62: template <typename VatId, typename LocalSturdyRefObjectId,
cannam@62:           typename ProvisionId, typename RecipientId, typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     SturdyRefRestorer<LocalSturdyRefObjectId>& restorer)
cannam@62:     KJ_DEPRECATED("Please transition to using a bootstrap interface instead.");
cannam@62: // ** DEPRECATED **
cannam@62: //
cannam@62: // Create an RPC server which exports multiple main interfaces by object ID. The `restorer` object
cannam@62: // can be used to look up objects by ID.
cannam@62: //
cannam@62: // Please transition to exporting only one interface, which is known as the "bootstrap" interface.
cannam@62: // For backwards-compatibility with old clients, continue to implement SturdyRefRestorer, but
cannam@62: // return the new bootstrap interface when the request object ID is null. When new clients connect
cannam@62: // and request the bootstrap interface, they will get that interface. Eventually, once all clients
cannam@62: // are updated to request only the bootstrap interface, stop implementing SturdyRefRestorer and
cannam@62: // switch to passing the bootstrap capability itself as the second parameter to `makeRpcServer()`.
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId,
cannam@62:           typename RecipientId, typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId> makeRpcClient(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network);
cannam@62: // Make an RPC client.  Typical usage (e.g. in a main() function):
cannam@62: //
cannam@62: //    MyEventLoop eventLoop;
cannam@62: //    kj::WaitScope waitScope(eventLoop);
cannam@62: //    MyNetwork network;
cannam@62: //    auto client = makeRpcClient(network);
cannam@62: //    MyCapability::Client cap = client.restore(hostId, objId).castAs<MyCapability>();
cannam@62: //    auto response = cap.fooRequest().send().wait(waitScope);
cannam@62: //    handleMyResponse(response);
cannam@62: //
cannam@62: // See also ez-rpc.h, which has simpler instructions for the common case of a two-party
cannam@62: // client-server RPC connection.
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult, typename RealmGatewayClient,
cannam@62:           typename InternalRef = _::InternalRefFromRealmGatewayClient<RealmGatewayClient>,
cannam@62:           typename ExternalRef = _::ExternalRefFromRealmGatewayClient<RealmGatewayClient>>
cannam@62: RpcSystem<VatId> makeRpcClient(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     RealmGatewayClient gateway);
cannam@62: // Make an RPC client for a VatNetwork that resides in a different realm from the application.
cannam@62: // The given RealmGateway is used to translate SturdyRefs between the app's ("internal") format
cannam@62: // and the network's ("external") format.
cannam@62: 
cannam@62: template <typename SturdyRefObjectId>
cannam@62: class SturdyRefRestorer: public _::SturdyRefRestorerBase {
cannam@62:   // ** DEPRECATED **
cannam@62:   //
cannam@62:   // In Cap'n Proto 0.4.x, applications could export multiple main interfaces identified by
cannam@62:   // object IDs. The callback used to map object IDs to objects was `SturdyRefRestorer`, as we
cannam@62:   // imagined this would eventually be used for restoring SturdyRefs as well. In practice, it was
cannam@62:   // never used for real SturdyRefs, only for exporting singleton objects under well-known names.
cannam@62:   //
cannam@62:   // The new preferred strategy is to export only a _single_ such interface, called the
cannam@62:   // "bootstrap interface". That interface can itself have methods for obtaining other objects, of
cannam@62:   // course, but that is up to the app. `SturdyRefRestorer` exists for backwards-compatibility.
cannam@62:   //
cannam@62:   // Hint:  Use SturdyRefRestorer<capnp::Text> to define a server that exports services under
cannam@62:   //   string names.
cannam@62: 
cannam@62: public:
cannam@62:   virtual Capability::Client restore(typename SturdyRefObjectId::Reader ref)
cannam@62:       KJ_DEPRECATED(
cannam@62:           "Please transition to using bootstrap interfaces instead of SturdyRefRestorer.") = 0;
cannam@62:   // Restore the given object, returning a capability representing it.
cannam@62: 
cannam@62: private:
cannam@62:   Capability::Client baseRestore(AnyPointer::Reader ref) override final;
cannam@62: };
cannam@62: 
cannam@62: // =======================================================================================
cannam@62: // VatNetwork
cannam@62: 
cannam@62: class OutgoingRpcMessage {
cannam@62:   // A message to be sent by a `VatNetwork`.
cannam@62: 
cannam@62: public:
cannam@62:   virtual AnyPointer::Builder getBody() = 0;
cannam@62:   // Get the message body, which the caller may fill in any way it wants.  (The standard RPC
cannam@62:   // implementation initializes it as a Message as defined in rpc.capnp.)
cannam@62: 
cannam@62:   virtual void send() = 0;
cannam@62:   // Send the message, or at least put it in a queue to be sent later.  Note that the builder
cannam@62:   // returned by `getBody()` remains valid at least until the `OutgoingRpcMessage` is destroyed.
cannam@62: };
cannam@62: 
cannam@62: class IncomingRpcMessage {
cannam@62:   // A message received from a `VatNetwork`.
cannam@62: 
cannam@62: public:
cannam@62:   virtual AnyPointer::Reader getBody() = 0;
cannam@62:   // Get the message body, to be interpreted by the caller.  (The standard RPC implementation
cannam@62:   // interprets it as a Message as defined in rpc.capnp.)
cannam@62: };
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: class VatNetwork: public _::VatNetworkBase {
cannam@62:   // Cap'n Proto RPC operates between vats, where a "vat" is some sort of host of objects.
cannam@62:   // Typically one Cap'n Proto process (in the Unix sense) is one vat.  The RPC system is what
cannam@62:   // allows calls between objects hosted in different vats.
cannam@62:   //
cannam@62:   // The RPC implementation sits on top of an implementation of `VatNetwork`.  The `VatNetwork`
cannam@62:   // determines how to form connections between vats -- specifically, two-way, private, reliable,
cannam@62:   // sequenced datagram connections.  The RPC implementation determines how to use such connections
cannam@62:   // to manage object references and make method calls.
cannam@62:   //
cannam@62:   // The most common implementation of VatNetwork is TwoPartyVatNetwork (rpc-twoparty.h).  Most
cannam@62:   // simple client-server apps will want to use it.  (You may even want to use the EZ RPC
cannam@62:   // interfaces in `ez-rpc.h` and avoid all of this.)
cannam@62:   //
cannam@62:   // TODO(someday):  Provide a standard implementation for the public internet.
cannam@62: 
cannam@62: public:
cannam@62:   class Connection;
cannam@62: 
cannam@62:   struct ConnectionAndProvisionId {
cannam@62:     // Result of connecting to a vat introduced by another vat.
cannam@62: 
cannam@62:     kj::Own<Connection> connection;
cannam@62:     // Connection to the new vat.
cannam@62: 
cannam@62:     kj::Own<OutgoingRpcMessage> firstMessage;
cannam@62:     // An already-allocated `OutgoingRpcMessage` associated with `connection`.  The RPC system will
cannam@62:     // construct this as an `Accept` message and send it.
cannam@62: 
cannam@62:     Orphan<ProvisionId> provisionId;
cannam@62:     // A `ProvisionId` already allocated inside `firstMessage`, which the RPC system will use to
cannam@62:     // build the `Accept` message.
cannam@62:   };
cannam@62: 
cannam@62:   class Connection: public _::VatNetworkBase::Connection {
cannam@62:     // A two-way RPC connection.
cannam@62:     //
cannam@62:     // This object may represent a connection that doesn't exist yet, but is expected to exist
cannam@62:     // in the future.  In this case, sent messages will automatically be queued and sent once the
cannam@62:     // connection is ready, so that the caller doesn't need to know the difference.
cannam@62: 
cannam@62:   public:
cannam@62:     // Level 0 features ----------------------------------------------
cannam@62: 
cannam@62:     virtual typename VatId::Reader getPeerVatId() = 0;
cannam@62:     // Returns the connected vat's authenticated VatId. It is the VatNetwork's responsibility to
cannam@62:     // authenticate this, so that the caller can be assured that they are really talking to the
cannam@62:     // identified vat and not an imposter.
cannam@62: 
cannam@62:     virtual kj::Own<OutgoingRpcMessage> newOutgoingMessage(uint firstSegmentWordSize) override = 0;
cannam@62:     // Allocate a new message to be sent on this connection.
cannam@62:     //
cannam@62:     // If `firstSegmentWordSize` is non-zero, it should be treated as a hint suggesting how large
cannam@62:     // to make the first segment.  This is entirely a hint and the connection may adjust it up or
cannam@62:     // down.  If it is zero, the connection should choose the size itself.
cannam@62: 
cannam@62:     virtual kj::Promise<kj::Maybe<kj::Own<IncomingRpcMessage>>> receiveIncomingMessage() override = 0;
cannam@62:     // Wait for a message to be received and return it.  If the read stream cleanly terminates,
cannam@62:     // return null.  If any other problem occurs, throw an exception.
cannam@62: 
cannam@62:     virtual kj::Promise<void> shutdown() override KJ_WARN_UNUSED_RESULT = 0;
cannam@62:     // Waits until all outgoing messages have been sent, then shuts down the outgoing stream. The
cannam@62:     // returned promise resolves after shutdown is complete.
cannam@62: 
cannam@62:   private:
cannam@62:     AnyStruct::Reader baseGetPeerVatId() override;
cannam@62:   };
cannam@62: 
cannam@62:   // Level 0 features ------------------------------------------------
cannam@62: 
cannam@62:   virtual kj::Maybe<kj::Own<Connection>> connect(typename VatId::Reader hostId) = 0;
cannam@62:   // Connect to a VatId.  Note that this method immediately returns a `Connection`, even
cannam@62:   // if the network connection has not yet been established.  Messages can be queued to this
cannam@62:   // connection and will be delivered once it is open.  The caller must attempt to read from the
cannam@62:   // connection to verify that it actually succeeded; the read will fail if the connection
cannam@62:   // couldn't be opened.  Some network implementations may actually start sending messages before
cannam@62:   // hearing back from the server at all, to avoid a round trip.
cannam@62:   //
cannam@62:   // Returns nullptr if `hostId` refers to the local host.
cannam@62: 
cannam@62:   virtual kj::Promise<kj::Own<Connection>> accept() = 0;
cannam@62:   // Wait for the next incoming connection and return it.
cannam@62: 
cannam@62:   // Level 4 features ------------------------------------------------
cannam@62:   // TODO(someday)
cannam@62: 
cannam@62: private:
cannam@62:   kj::Maybe<kj::Own<_::VatNetworkBase::Connection>>
cannam@62:       baseConnect(AnyStruct::Reader hostId) override final;
cannam@62:   kj::Promise<kj::Own<_::VatNetworkBase::Connection>> baseAccept() override final;
cannam@62: };
cannam@62: 
cannam@62: // =======================================================================================
cannam@62: // ***************************************************************************************
cannam@62: // Inline implementation details start here
cannam@62: // ***************************************************************************************
cannam@62: // =======================================================================================
cannam@62: 
cannam@62: template <typename VatId>
cannam@62: Capability::Client BootstrapFactory<VatId>::baseCreateFor(AnyStruct::Reader clientId) {
cannam@62:   return createFor(clientId.as<VatId>());
cannam@62: }
cannam@62: 
cannam@62: template <typename SturdyRef, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: kj::Maybe<kj::Own<_::VatNetworkBase::Connection>>
cannam@62:     VatNetwork<SturdyRef, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>::
cannam@62:     baseConnect(AnyStruct::Reader ref) {
cannam@62:   auto maybe = connect(ref.as<SturdyRef>());
cannam@62:   return maybe.map([](kj::Own<Connection>& conn) -> kj::Own<_::VatNetworkBase::Connection> {
cannam@62:     return kj::mv(conn);
cannam@62:   });
cannam@62: }
cannam@62: 
cannam@62: template <typename SturdyRef, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: kj::Promise<kj::Own<_::VatNetworkBase::Connection>>
cannam@62:     VatNetwork<SturdyRef, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>::baseAccept() {
cannam@62:   return accept().then(
cannam@62:       [](kj::Own<Connection>&& connection) -> kj::Own<_::VatNetworkBase::Connection> {
cannam@62:     return kj::mv(connection);
cannam@62:   });
cannam@62: }
cannam@62: 
cannam@62: template <typename SturdyRef, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: AnyStruct::Reader VatNetwork<
cannam@62:     SturdyRef, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>::
cannam@62:     Connection::baseGetPeerVatId() {
cannam@62:   return getPeerVatId();
cannam@62: }
cannam@62: 
cannam@62: template <typename SturdyRef>
cannam@62: Capability::Client SturdyRefRestorer<SturdyRef>::baseRestore(AnyPointer::Reader ref) {
cannam@62: #pragma GCC diagnostic push
cannam@62: #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
cannam@62:   return restore(ref.getAs<SturdyRef>());
cannam@62: #pragma GCC diagnostic pop
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId>
cannam@62: template <typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId>::RpcSystem(
cannam@62:       VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:       kj::Maybe<Capability::Client> bootstrap,
cannam@62:       kj::Maybe<RealmGateway<>::Client> gateway)
cannam@62:     : _::RpcSystemBase(network, kj::mv(bootstrap), kj::mv(gateway)) {}
cannam@62: 
cannam@62: template <typename VatId>
cannam@62: template <typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId>::RpcSystem(
cannam@62:       VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:       BootstrapFactory<VatId>& bootstrapFactory,
cannam@62:       kj::Maybe<RealmGateway<>::Client> gateway)
cannam@62:     : _::RpcSystemBase(network, bootstrapFactory, kj::mv(gateway)) {}
cannam@62: 
cannam@62: template <typename VatId>
cannam@62: template <typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult,
cannam@62:           typename LocalSturdyRefObjectId>
cannam@62: RpcSystem<VatId>::RpcSystem(
cannam@62:       VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:       SturdyRefRestorer<LocalSturdyRefObjectId>& restorer)
cannam@62:     : _::RpcSystemBase(network, restorer) {}
cannam@62: 
cannam@62: template <typename VatId>
cannam@62: Capability::Client RpcSystem<VatId>::bootstrap(typename VatId::Reader vatId) {
cannam@62:   return baseBootstrap(_::PointerHelpers<VatId>::getInternalReader(vatId));
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId>
cannam@62: Capability::Client RpcSystem<VatId>::restore(
cannam@62:     typename VatId::Reader hostId, AnyPointer::Reader objectId) {
cannam@62:   return baseRestore(_::PointerHelpers<VatId>::getInternalReader(hostId), objectId);
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId>
cannam@62: inline void RpcSystem<VatId>::setFlowLimit(size_t words) {
cannam@62:   baseSetFlowLimit(words);
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     Capability::Client bootstrapInterface) {
cannam@62:   return RpcSystem<VatId>(network, kj::mv(bootstrapInterface));
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult,
cannam@62:           typename RealmGatewayClient, typename InternalRef, typename ExternalRef>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     Capability::Client bootstrapInterface, RealmGatewayClient gateway) {
cannam@62:   return RpcSystem<VatId>(network, kj::mv(bootstrapInterface),
cannam@62:       gateway.template castAs<RealmGateway<>>());
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     BootstrapFactory<VatId>& bootstrapFactory) {
cannam@62:   return RpcSystem<VatId>(network, bootstrapFactory);
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@62:           typename ThirdPartyCapId, typename JoinResult,
cannam@62:           typename RealmGatewayClient, typename InternalRef, typename ExternalRef>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     BootstrapFactory<VatId>& bootstrapFactory, RealmGatewayClient gateway) {
cannam@62:   return RpcSystem<VatId>(network, bootstrapFactory, gateway.template castAs<RealmGateway<>>());
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId, typename LocalSturdyRefObjectId,
cannam@62:           typename ProvisionId, typename RecipientId, typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId> makeRpcServer(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     SturdyRefRestorer<LocalSturdyRefObjectId>& restorer) {
cannam@62:   return RpcSystem<VatId>(network, restorer);
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId,
cannam@62:           typename RecipientId, typename ThirdPartyCapId, typename JoinResult>
cannam@62: RpcSystem<VatId> makeRpcClient(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network) {
cannam@62:   return RpcSystem<VatId>(network, nullptr);
cannam@62: }
cannam@62: 
cannam@62: template <typename VatId, typename ProvisionId,
cannam@62:           typename RecipientId, typename ThirdPartyCapId, typename JoinResult,
cannam@62:           typename RealmGatewayClient, typename InternalRef, typename ExternalRef>
cannam@62: RpcSystem<VatId> makeRpcClient(
cannam@62:     VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@62:     RealmGatewayClient gateway) {
cannam@62:   return RpcSystem<VatId>(network, nullptr, gateway.template castAs<RealmGateway<>>());
cannam@62: }
cannam@62: 
cannam@62: }  // namespace capnp
cannam@62: 
cannam@62: #endif  // CAPNP_RPC_H_