annotate osx/include/capnp/rpc.h @ 139:413e081fcc6f

Rebuild MAD with 64-bit FPM
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 30 Nov 2016 20:59:17 +0000
parents 41e769c91eca
children 0994c39f1e94
rev   line source
cannam@134 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@134 2 // Licensed under the MIT License:
cannam@134 3 //
cannam@134 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@134 5 // of this software and associated documentation files (the "Software"), to deal
cannam@134 6 // in the Software without restriction, including without limitation the rights
cannam@134 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@134 8 // copies of the Software, and to permit persons to whom the Software is
cannam@134 9 // furnished to do so, subject to the following conditions:
cannam@134 10 //
cannam@134 11 // The above copyright notice and this permission notice shall be included in
cannam@134 12 // all copies or substantial portions of the Software.
cannam@134 13 //
cannam@134 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@134 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@134 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@134 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@134 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@134 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@134 20 // THE SOFTWARE.
cannam@134 21
cannam@134 22 #ifndef CAPNP_RPC_H_
cannam@134 23 #define CAPNP_RPC_H_
cannam@134 24
cannam@134 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@134 26 #pragma GCC system_header
cannam@134 27 #endif
cannam@134 28
cannam@134 29 #include "capability.h"
cannam@134 30 #include "rpc-prelude.h"
cannam@134 31
cannam@134 32 namespace capnp {
cannam@134 33
cannam@134 34 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 35 typename ThirdPartyCapId, typename JoinResult>
cannam@134 36 class VatNetwork;
cannam@134 37 template <typename SturdyRefObjectId>
cannam@134 38 class SturdyRefRestorer;
cannam@134 39
cannam@134 40 template <typename VatId>
cannam@134 41 class BootstrapFactory: public _::BootstrapFactoryBase {
cannam@134 42 // Interface that constructs per-client bootstrap interfaces. Use this if you want each client
cannam@134 43 // who connects to see a different bootstrap interface based on their (authenticated) VatId.
cannam@134 44 // This allows an application to bootstrap off of the authentication performed at the VatNetwork
cannam@134 45 // level. (Typically VatId is some sort of public key.)
cannam@134 46 //
cannam@134 47 // This is only useful for multi-party networks. For TwoPartyVatNetwork, there's no reason to
cannam@134 48 // use a BootstrapFactory; just specify a single bootstrap capability in this case.
cannam@134 49
cannam@134 50 public:
cannam@134 51 virtual Capability::Client createFor(typename VatId::Reader clientId) = 0;
cannam@134 52 // Create a bootstrap capability appropriate for exposing to the given client. VatNetwork will
cannam@134 53 // have authenticated the client VatId before this is called.
cannam@134 54
cannam@134 55 private:
cannam@134 56 Capability::Client baseCreateFor(AnyStruct::Reader clientId) override;
cannam@134 57 };
cannam@134 58
cannam@134 59 template <typename VatId>
cannam@134 60 class RpcSystem: public _::RpcSystemBase {
cannam@134 61 // Represents the RPC system, which is the portal to objects available on the network.
cannam@134 62 //
cannam@134 63 // The RPC implementation sits on top of an implementation of `VatNetwork`. The `VatNetwork`
cannam@134 64 // determines how to form connections between vats -- specifically, two-way, private, reliable,
cannam@134 65 // sequenced datagram connections. The RPC implementation determines how to use such connections
cannam@134 66 // to manage object references and make method calls.
cannam@134 67 //
cannam@134 68 // See `makeRpcServer()` and `makeRpcClient()` below for convenient syntax for setting up an
cannam@134 69 // `RpcSystem` given a `VatNetwork`.
cannam@134 70 //
cannam@134 71 // See `ez-rpc.h` for an even simpler interface for setting up RPC in a typical two-party
cannam@134 72 // client/server scenario.
cannam@134 73
cannam@134 74 public:
cannam@134 75 template <typename ProvisionId, typename RecipientId,
cannam@134 76 typename ThirdPartyCapId, typename JoinResult>
cannam@134 77 RpcSystem(
cannam@134 78 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 79 kj::Maybe<Capability::Client> bootstrapInterface,
cannam@134 80 kj::Maybe<RealmGateway<>::Client> gateway = nullptr);
cannam@134 81
cannam@134 82 template <typename ProvisionId, typename RecipientId,
cannam@134 83 typename ThirdPartyCapId, typename JoinResult>
cannam@134 84 RpcSystem(
cannam@134 85 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 86 BootstrapFactory<VatId>& bootstrapFactory,
cannam@134 87 kj::Maybe<RealmGateway<>::Client> gateway = nullptr);
cannam@134 88
cannam@134 89 template <typename ProvisionId, typename RecipientId,
cannam@134 90 typename ThirdPartyCapId, typename JoinResult,
cannam@134 91 typename LocalSturdyRefObjectId>
cannam@134 92 RpcSystem(
cannam@134 93 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 94 SturdyRefRestorer<LocalSturdyRefObjectId>& restorer);
cannam@134 95
cannam@134 96 RpcSystem(RpcSystem&& other) = default;
cannam@134 97
cannam@134 98 Capability::Client bootstrap(typename VatId::Reader vatId);
cannam@134 99 // Connect to the given vat and return its bootstrap interface.
cannam@134 100
cannam@134 101 Capability::Client restore(typename VatId::Reader hostId, AnyPointer::Reader objectId)
cannam@134 102 KJ_DEPRECATED("Please transition to using a bootstrap interface instead.");
cannam@134 103 // ** DEPRECATED **
cannam@134 104 //
cannam@134 105 // Restores the given SturdyRef from the network and return the capability representing it.
cannam@134 106 //
cannam@134 107 // `hostId` identifies the host from which to request the ref, in the format specified by the
cannam@134 108 // `VatNetwork` in use. `objectId` is the object ID in whatever format is expected by said host.
cannam@134 109 //
cannam@134 110 // This method will be removed in a future version of Cap'n Proto. Instead, please transition
cannam@134 111 // to using bootstrap(), which is equivalent to calling restore() with a null `objectId`.
cannam@134 112 // You may emulate the old concept of object IDs by exporting a bootstrap interface which has
cannam@134 113 // methods that can be used to obtain other capabilities by ID.
cannam@134 114
cannam@134 115 void setFlowLimit(size_t words);
cannam@134 116 // Sets the incoming call flow limit. If more than `words` worth of call messages have not yet
cannam@134 117 // received responses, the RpcSystem will not read further messages from the stream. This can be
cannam@134 118 // used as a crude way to prevent a resource exhaustion attack (or bug) in which a peer makes an
cannam@134 119 // excessive number of simultaneous calls that consume the receiver's RAM.
cannam@134 120 //
cannam@134 121 // There are some caveats. When over the flow limit, all messages are blocked, including returns.
cannam@134 122 // If the outstanding calls are themselves waiting on calls going in the opposite direction, the
cannam@134 123 // flow limit may prevent those calls from completing, leading to deadlock. However, a
cannam@134 124 // sufficiently high limit should make this unlikely.
cannam@134 125 //
cannam@134 126 // Note that a call's parameter size counts against the flow limit until the call returns, even
cannam@134 127 // if the recipient calls releaseParams() to free the parameter memory early. This is because
cannam@134 128 // releaseParams() may simply indicate that the parameters have been forwarded to another
cannam@134 129 // machine, but are still in-memory there. For illustration, say that Alice made a call to Bob
cannam@134 130 // who forwarded the call to Carol. Bob has imposed a flow limit on Alice. Alice's calls are
cannam@134 131 // being forwarded to Carol, so Bob never keeps the parameters in-memory for more than a brief
cannam@134 132 // period. However, the flow limit counts all calls that haven't returned, even if Bob has
cannam@134 133 // already freed the memory they consumed. You might argue that the right solution here is
cannam@134 134 // instead for Carol to impose her own flow limit on Bob. This has a serious problem, though:
cannam@134 135 // Bob might be forwarding requests to Carol on behalf of many different parties, not just Alice.
cannam@134 136 // If Alice can pump enough data to hit the Bob -> Carol flow limit, then those other parties
cannam@134 137 // will be disrupted. Thus, we can only really impose the limit on the Alice -> Bob link, which
cannam@134 138 // only affects Alice. We need that one flow limit to limit Alice's impact on the whole system,
cannam@134 139 // so it has to count all in-flight calls.
cannam@134 140 //
cannam@134 141 // In Sandstorm, flow limits are imposed by the supervisor on calls coming out of a grain, in
cannam@134 142 // order to prevent a grain from inundating the system with in-flight calls. In practice, the
cannam@134 143 // main time this happens is when a grain is pushing a large file download and doesn't implement
cannam@134 144 // proper cooperative flow control.
cannam@134 145 };
cannam@134 146
cannam@134 147 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 148 typename ThirdPartyCapId, typename JoinResult>
cannam@134 149 RpcSystem<VatId> makeRpcServer(
cannam@134 150 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 151 Capability::Client bootstrapInterface);
cannam@134 152 // Make an RPC server. Typical usage (e.g. in a main() function):
cannam@134 153 //
cannam@134 154 // MyEventLoop eventLoop;
cannam@134 155 // kj::WaitScope waitScope(eventLoop);
cannam@134 156 // MyNetwork network;
cannam@134 157 // MyMainInterface::Client bootstrap = makeMain();
cannam@134 158 // auto server = makeRpcServer(network, bootstrap);
cannam@134 159 // kj::NEVER_DONE.wait(waitScope); // run forever
cannam@134 160 //
cannam@134 161 // See also ez-rpc.h, which has simpler instructions for the common case of a two-party
cannam@134 162 // client-server RPC connection.
cannam@134 163
cannam@134 164 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 165 typename ThirdPartyCapId, typename JoinResult, typename RealmGatewayClient,
cannam@134 166 typename InternalRef = _::InternalRefFromRealmGatewayClient<RealmGatewayClient>,
cannam@134 167 typename ExternalRef = _::ExternalRefFromRealmGatewayClient<RealmGatewayClient>>
cannam@134 168 RpcSystem<VatId> makeRpcServer(
cannam@134 169 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 170 Capability::Client bootstrapInterface, RealmGatewayClient gateway);
cannam@134 171 // Make an RPC server for a VatNetwork that resides in a different realm from the application.
cannam@134 172 // The given RealmGateway is used to translate SturdyRefs between the app's ("internal") format
cannam@134 173 // and the network's ("external") format.
cannam@134 174
cannam@134 175 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 176 typename ThirdPartyCapId, typename JoinResult>
cannam@134 177 RpcSystem<VatId> makeRpcServer(
cannam@134 178 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 179 BootstrapFactory<VatId>& bootstrapFactory);
cannam@134 180 // Make an RPC server that can serve different bootstrap interfaces to different clients via a
cannam@134 181 // BootstrapInterface.
cannam@134 182
cannam@134 183 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 184 typename ThirdPartyCapId, typename JoinResult, typename RealmGatewayClient,
cannam@134 185 typename InternalRef = _::InternalRefFromRealmGatewayClient<RealmGatewayClient>,
cannam@134 186 typename ExternalRef = _::ExternalRefFromRealmGatewayClient<RealmGatewayClient>>
cannam@134 187 RpcSystem<VatId> makeRpcServer(
cannam@134 188 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 189 BootstrapFactory<VatId>& bootstrapFactory, RealmGatewayClient gateway);
cannam@134 190 // Make an RPC server that can serve different bootstrap interfaces to different clients via a
cannam@134 191 // BootstrapInterface and communicates with a different realm than the application is in via a
cannam@134 192 // RealmGateway.
cannam@134 193
cannam@134 194 template <typename VatId, typename LocalSturdyRefObjectId,
cannam@134 195 typename ProvisionId, typename RecipientId, typename ThirdPartyCapId, typename JoinResult>
cannam@134 196 RpcSystem<VatId> makeRpcServer(
cannam@134 197 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 198 SturdyRefRestorer<LocalSturdyRefObjectId>& restorer)
cannam@134 199 KJ_DEPRECATED("Please transition to using a bootstrap interface instead.");
cannam@134 200 // ** DEPRECATED **
cannam@134 201 //
cannam@134 202 // Create an RPC server which exports multiple main interfaces by object ID. The `restorer` object
cannam@134 203 // can be used to look up objects by ID.
cannam@134 204 //
cannam@134 205 // Please transition to exporting only one interface, which is known as the "bootstrap" interface.
cannam@134 206 // For backwards-compatibility with old clients, continue to implement SturdyRefRestorer, but
cannam@134 207 // return the new bootstrap interface when the request object ID is null. When new clients connect
cannam@134 208 // and request the bootstrap interface, they will get that interface. Eventually, once all clients
cannam@134 209 // are updated to request only the bootstrap interface, stop implementing SturdyRefRestorer and
cannam@134 210 // switch to passing the bootstrap capability itself as the second parameter to `makeRpcServer()`.
cannam@134 211
cannam@134 212 template <typename VatId, typename ProvisionId,
cannam@134 213 typename RecipientId, typename ThirdPartyCapId, typename JoinResult>
cannam@134 214 RpcSystem<VatId> makeRpcClient(
cannam@134 215 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network);
cannam@134 216 // Make an RPC client. Typical usage (e.g. in a main() function):
cannam@134 217 //
cannam@134 218 // MyEventLoop eventLoop;
cannam@134 219 // kj::WaitScope waitScope(eventLoop);
cannam@134 220 // MyNetwork network;
cannam@134 221 // auto client = makeRpcClient(network);
cannam@134 222 // MyCapability::Client cap = client.restore(hostId, objId).castAs<MyCapability>();
cannam@134 223 // auto response = cap.fooRequest().send().wait(waitScope);
cannam@134 224 // handleMyResponse(response);
cannam@134 225 //
cannam@134 226 // See also ez-rpc.h, which has simpler instructions for the common case of a two-party
cannam@134 227 // client-server RPC connection.
cannam@134 228
cannam@134 229 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 230 typename ThirdPartyCapId, typename JoinResult, typename RealmGatewayClient,
cannam@134 231 typename InternalRef = _::InternalRefFromRealmGatewayClient<RealmGatewayClient>,
cannam@134 232 typename ExternalRef = _::ExternalRefFromRealmGatewayClient<RealmGatewayClient>>
cannam@134 233 RpcSystem<VatId> makeRpcClient(
cannam@134 234 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 235 RealmGatewayClient gateway);
cannam@134 236 // Make an RPC client for a VatNetwork that resides in a different realm from the application.
cannam@134 237 // The given RealmGateway is used to translate SturdyRefs between the app's ("internal") format
cannam@134 238 // and the network's ("external") format.
cannam@134 239
cannam@134 240 template <typename SturdyRefObjectId>
cannam@134 241 class SturdyRefRestorer: public _::SturdyRefRestorerBase {
cannam@134 242 // ** DEPRECATED **
cannam@134 243 //
cannam@134 244 // In Cap'n Proto 0.4.x, applications could export multiple main interfaces identified by
cannam@134 245 // object IDs. The callback used to map object IDs to objects was `SturdyRefRestorer`, as we
cannam@134 246 // imagined this would eventually be used for restoring SturdyRefs as well. In practice, it was
cannam@134 247 // never used for real SturdyRefs, only for exporting singleton objects under well-known names.
cannam@134 248 //
cannam@134 249 // The new preferred strategy is to export only a _single_ such interface, called the
cannam@134 250 // "bootstrap interface". That interface can itself have methods for obtaining other objects, of
cannam@134 251 // course, but that is up to the app. `SturdyRefRestorer` exists for backwards-compatibility.
cannam@134 252 //
cannam@134 253 // Hint: Use SturdyRefRestorer<capnp::Text> to define a server that exports services under
cannam@134 254 // string names.
cannam@134 255
cannam@134 256 public:
cannam@134 257 virtual Capability::Client restore(typename SturdyRefObjectId::Reader ref)
cannam@134 258 KJ_DEPRECATED(
cannam@134 259 "Please transition to using bootstrap interfaces instead of SturdyRefRestorer.") = 0;
cannam@134 260 // Restore the given object, returning a capability representing it.
cannam@134 261
cannam@134 262 private:
cannam@134 263 Capability::Client baseRestore(AnyPointer::Reader ref) override final;
cannam@134 264 };
cannam@134 265
cannam@134 266 // =======================================================================================
cannam@134 267 // VatNetwork
cannam@134 268
cannam@134 269 class OutgoingRpcMessage {
cannam@134 270 // A message to be sent by a `VatNetwork`.
cannam@134 271
cannam@134 272 public:
cannam@134 273 virtual AnyPointer::Builder getBody() = 0;
cannam@134 274 // Get the message body, which the caller may fill in any way it wants. (The standard RPC
cannam@134 275 // implementation initializes it as a Message as defined in rpc.capnp.)
cannam@134 276
cannam@134 277 virtual void send() = 0;
cannam@134 278 // Send the message, or at least put it in a queue to be sent later. Note that the builder
cannam@134 279 // returned by `getBody()` remains valid at least until the `OutgoingRpcMessage` is destroyed.
cannam@134 280 };
cannam@134 281
cannam@134 282 class IncomingRpcMessage {
cannam@134 283 // A message received from a `VatNetwork`.
cannam@134 284
cannam@134 285 public:
cannam@134 286 virtual AnyPointer::Reader getBody() = 0;
cannam@134 287 // Get the message body, to be interpreted by the caller. (The standard RPC implementation
cannam@134 288 // interprets it as a Message as defined in rpc.capnp.)
cannam@134 289 };
cannam@134 290
cannam@134 291 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 292 typename ThirdPartyCapId, typename JoinResult>
cannam@134 293 class VatNetwork: public _::VatNetworkBase {
cannam@134 294 // Cap'n Proto RPC operates between vats, where a "vat" is some sort of host of objects.
cannam@134 295 // Typically one Cap'n Proto process (in the Unix sense) is one vat. The RPC system is what
cannam@134 296 // allows calls between objects hosted in different vats.
cannam@134 297 //
cannam@134 298 // The RPC implementation sits on top of an implementation of `VatNetwork`. The `VatNetwork`
cannam@134 299 // determines how to form connections between vats -- specifically, two-way, private, reliable,
cannam@134 300 // sequenced datagram connections. The RPC implementation determines how to use such connections
cannam@134 301 // to manage object references and make method calls.
cannam@134 302 //
cannam@134 303 // The most common implementation of VatNetwork is TwoPartyVatNetwork (rpc-twoparty.h). Most
cannam@134 304 // simple client-server apps will want to use it. (You may even want to use the EZ RPC
cannam@134 305 // interfaces in `ez-rpc.h` and avoid all of this.)
cannam@134 306 //
cannam@134 307 // TODO(someday): Provide a standard implementation for the public internet.
cannam@134 308
cannam@134 309 public:
cannam@134 310 class Connection;
cannam@134 311
cannam@134 312 struct ConnectionAndProvisionId {
cannam@134 313 // Result of connecting to a vat introduced by another vat.
cannam@134 314
cannam@134 315 kj::Own<Connection> connection;
cannam@134 316 // Connection to the new vat.
cannam@134 317
cannam@134 318 kj::Own<OutgoingRpcMessage> firstMessage;
cannam@134 319 // An already-allocated `OutgoingRpcMessage` associated with `connection`. The RPC system will
cannam@134 320 // construct this as an `Accept` message and send it.
cannam@134 321
cannam@134 322 Orphan<ProvisionId> provisionId;
cannam@134 323 // A `ProvisionId` already allocated inside `firstMessage`, which the RPC system will use to
cannam@134 324 // build the `Accept` message.
cannam@134 325 };
cannam@134 326
cannam@134 327 class Connection: public _::VatNetworkBase::Connection {
cannam@134 328 // A two-way RPC connection.
cannam@134 329 //
cannam@134 330 // This object may represent a connection that doesn't exist yet, but is expected to exist
cannam@134 331 // in the future. In this case, sent messages will automatically be queued and sent once the
cannam@134 332 // connection is ready, so that the caller doesn't need to know the difference.
cannam@134 333
cannam@134 334 public:
cannam@134 335 // Level 0 features ----------------------------------------------
cannam@134 336
cannam@134 337 virtual typename VatId::Reader getPeerVatId() = 0;
cannam@134 338 // Returns the connected vat's authenticated VatId. It is the VatNetwork's responsibility to
cannam@134 339 // authenticate this, so that the caller can be assured that they are really talking to the
cannam@134 340 // identified vat and not an imposter.
cannam@134 341
cannam@134 342 virtual kj::Own<OutgoingRpcMessage> newOutgoingMessage(uint firstSegmentWordSize) override = 0;
cannam@134 343 // Allocate a new message to be sent on this connection.
cannam@134 344 //
cannam@134 345 // If `firstSegmentWordSize` is non-zero, it should be treated as a hint suggesting how large
cannam@134 346 // to make the first segment. This is entirely a hint and the connection may adjust it up or
cannam@134 347 // down. If it is zero, the connection should choose the size itself.
cannam@134 348
cannam@134 349 virtual kj::Promise<kj::Maybe<kj::Own<IncomingRpcMessage>>> receiveIncomingMessage() override = 0;
cannam@134 350 // Wait for a message to be received and return it. If the read stream cleanly terminates,
cannam@134 351 // return null. If any other problem occurs, throw an exception.
cannam@134 352
cannam@134 353 virtual kj::Promise<void> shutdown() override KJ_WARN_UNUSED_RESULT = 0;
cannam@134 354 // Waits until all outgoing messages have been sent, then shuts down the outgoing stream. The
cannam@134 355 // returned promise resolves after shutdown is complete.
cannam@134 356
cannam@134 357 private:
cannam@134 358 AnyStruct::Reader baseGetPeerVatId() override;
cannam@134 359 };
cannam@134 360
cannam@134 361 // Level 0 features ------------------------------------------------
cannam@134 362
cannam@134 363 virtual kj::Maybe<kj::Own<Connection>> connect(typename VatId::Reader hostId) = 0;
cannam@134 364 // Connect to a VatId. Note that this method immediately returns a `Connection`, even
cannam@134 365 // if the network connection has not yet been established. Messages can be queued to this
cannam@134 366 // connection and will be delivered once it is open. The caller must attempt to read from the
cannam@134 367 // connection to verify that it actually succeeded; the read will fail if the connection
cannam@134 368 // couldn't be opened. Some network implementations may actually start sending messages before
cannam@134 369 // hearing back from the server at all, to avoid a round trip.
cannam@134 370 //
cannam@134 371 // Returns nullptr if `hostId` refers to the local host.
cannam@134 372
cannam@134 373 virtual kj::Promise<kj::Own<Connection>> accept() = 0;
cannam@134 374 // Wait for the next incoming connection and return it.
cannam@134 375
cannam@134 376 // Level 4 features ------------------------------------------------
cannam@134 377 // TODO(someday)
cannam@134 378
cannam@134 379 private:
cannam@134 380 kj::Maybe<kj::Own<_::VatNetworkBase::Connection>>
cannam@134 381 baseConnect(AnyStruct::Reader hostId) override final;
cannam@134 382 kj::Promise<kj::Own<_::VatNetworkBase::Connection>> baseAccept() override final;
cannam@134 383 };
cannam@134 384
cannam@134 385 // =======================================================================================
cannam@134 386 // ***************************************************************************************
cannam@134 387 // Inline implementation details start here
cannam@134 388 // ***************************************************************************************
cannam@134 389 // =======================================================================================
cannam@134 390
cannam@134 391 template <typename VatId>
cannam@134 392 Capability::Client BootstrapFactory<VatId>::baseCreateFor(AnyStruct::Reader clientId) {
cannam@134 393 return createFor(clientId.as<VatId>());
cannam@134 394 }
cannam@134 395
cannam@134 396 template <typename SturdyRef, typename ProvisionId, typename RecipientId,
cannam@134 397 typename ThirdPartyCapId, typename JoinResult>
cannam@134 398 kj::Maybe<kj::Own<_::VatNetworkBase::Connection>>
cannam@134 399 VatNetwork<SturdyRef, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>::
cannam@134 400 baseConnect(AnyStruct::Reader ref) {
cannam@134 401 auto maybe = connect(ref.as<SturdyRef>());
cannam@134 402 return maybe.map([](kj::Own<Connection>& conn) -> kj::Own<_::VatNetworkBase::Connection> {
cannam@134 403 return kj::mv(conn);
cannam@134 404 });
cannam@134 405 }
cannam@134 406
cannam@134 407 template <typename SturdyRef, typename ProvisionId, typename RecipientId,
cannam@134 408 typename ThirdPartyCapId, typename JoinResult>
cannam@134 409 kj::Promise<kj::Own<_::VatNetworkBase::Connection>>
cannam@134 410 VatNetwork<SturdyRef, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>::baseAccept() {
cannam@134 411 return accept().then(
cannam@134 412 [](kj::Own<Connection>&& connection) -> kj::Own<_::VatNetworkBase::Connection> {
cannam@134 413 return kj::mv(connection);
cannam@134 414 });
cannam@134 415 }
cannam@134 416
cannam@134 417 template <typename SturdyRef, typename ProvisionId, typename RecipientId,
cannam@134 418 typename ThirdPartyCapId, typename JoinResult>
cannam@134 419 AnyStruct::Reader VatNetwork<
cannam@134 420 SturdyRef, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>::
cannam@134 421 Connection::baseGetPeerVatId() {
cannam@134 422 return getPeerVatId();
cannam@134 423 }
cannam@134 424
cannam@134 425 template <typename SturdyRef>
cannam@134 426 Capability::Client SturdyRefRestorer<SturdyRef>::baseRestore(AnyPointer::Reader ref) {
cannam@134 427 #pragma GCC diagnostic push
cannam@134 428 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
cannam@134 429 return restore(ref.getAs<SturdyRef>());
cannam@134 430 #pragma GCC diagnostic pop
cannam@134 431 }
cannam@134 432
cannam@134 433 template <typename VatId>
cannam@134 434 template <typename ProvisionId, typename RecipientId,
cannam@134 435 typename ThirdPartyCapId, typename JoinResult>
cannam@134 436 RpcSystem<VatId>::RpcSystem(
cannam@134 437 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 438 kj::Maybe<Capability::Client> bootstrap,
cannam@134 439 kj::Maybe<RealmGateway<>::Client> gateway)
cannam@134 440 : _::RpcSystemBase(network, kj::mv(bootstrap), kj::mv(gateway)) {}
cannam@134 441
cannam@134 442 template <typename VatId>
cannam@134 443 template <typename ProvisionId, typename RecipientId,
cannam@134 444 typename ThirdPartyCapId, typename JoinResult>
cannam@134 445 RpcSystem<VatId>::RpcSystem(
cannam@134 446 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 447 BootstrapFactory<VatId>& bootstrapFactory,
cannam@134 448 kj::Maybe<RealmGateway<>::Client> gateway)
cannam@134 449 : _::RpcSystemBase(network, bootstrapFactory, kj::mv(gateway)) {}
cannam@134 450
cannam@134 451 template <typename VatId>
cannam@134 452 template <typename ProvisionId, typename RecipientId,
cannam@134 453 typename ThirdPartyCapId, typename JoinResult,
cannam@134 454 typename LocalSturdyRefObjectId>
cannam@134 455 RpcSystem<VatId>::RpcSystem(
cannam@134 456 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 457 SturdyRefRestorer<LocalSturdyRefObjectId>& restorer)
cannam@134 458 : _::RpcSystemBase(network, restorer) {}
cannam@134 459
cannam@134 460 template <typename VatId>
cannam@134 461 Capability::Client RpcSystem<VatId>::bootstrap(typename VatId::Reader vatId) {
cannam@134 462 return baseBootstrap(_::PointerHelpers<VatId>::getInternalReader(vatId));
cannam@134 463 }
cannam@134 464
cannam@134 465 template <typename VatId>
cannam@134 466 Capability::Client RpcSystem<VatId>::restore(
cannam@134 467 typename VatId::Reader hostId, AnyPointer::Reader objectId) {
cannam@134 468 return baseRestore(_::PointerHelpers<VatId>::getInternalReader(hostId), objectId);
cannam@134 469 }
cannam@134 470
cannam@134 471 template <typename VatId>
cannam@134 472 inline void RpcSystem<VatId>::setFlowLimit(size_t words) {
cannam@134 473 baseSetFlowLimit(words);
cannam@134 474 }
cannam@134 475
cannam@134 476 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 477 typename ThirdPartyCapId, typename JoinResult>
cannam@134 478 RpcSystem<VatId> makeRpcServer(
cannam@134 479 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 480 Capability::Client bootstrapInterface) {
cannam@134 481 return RpcSystem<VatId>(network, kj::mv(bootstrapInterface));
cannam@134 482 }
cannam@134 483
cannam@134 484 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 485 typename ThirdPartyCapId, typename JoinResult,
cannam@134 486 typename RealmGatewayClient, typename InternalRef, typename ExternalRef>
cannam@134 487 RpcSystem<VatId> makeRpcServer(
cannam@134 488 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 489 Capability::Client bootstrapInterface, RealmGatewayClient gateway) {
cannam@134 490 return RpcSystem<VatId>(network, kj::mv(bootstrapInterface),
cannam@134 491 gateway.template castAs<RealmGateway<>>());
cannam@134 492 }
cannam@134 493
cannam@134 494 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 495 typename ThirdPartyCapId, typename JoinResult>
cannam@134 496 RpcSystem<VatId> makeRpcServer(
cannam@134 497 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 498 BootstrapFactory<VatId>& bootstrapFactory) {
cannam@134 499 return RpcSystem<VatId>(network, bootstrapFactory);
cannam@134 500 }
cannam@134 501
cannam@134 502 template <typename VatId, typename ProvisionId, typename RecipientId,
cannam@134 503 typename ThirdPartyCapId, typename JoinResult,
cannam@134 504 typename RealmGatewayClient, typename InternalRef, typename ExternalRef>
cannam@134 505 RpcSystem<VatId> makeRpcServer(
cannam@134 506 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 507 BootstrapFactory<VatId>& bootstrapFactory, RealmGatewayClient gateway) {
cannam@134 508 return RpcSystem<VatId>(network, bootstrapFactory, gateway.template castAs<RealmGateway<>>());
cannam@134 509 }
cannam@134 510
cannam@134 511 template <typename VatId, typename LocalSturdyRefObjectId,
cannam@134 512 typename ProvisionId, typename RecipientId, typename ThirdPartyCapId, typename JoinResult>
cannam@134 513 RpcSystem<VatId> makeRpcServer(
cannam@134 514 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 515 SturdyRefRestorer<LocalSturdyRefObjectId>& restorer) {
cannam@134 516 return RpcSystem<VatId>(network, restorer);
cannam@134 517 }
cannam@134 518
cannam@134 519 template <typename VatId, typename ProvisionId,
cannam@134 520 typename RecipientId, typename ThirdPartyCapId, typename JoinResult>
cannam@134 521 RpcSystem<VatId> makeRpcClient(
cannam@134 522 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network) {
cannam@134 523 return RpcSystem<VatId>(network, nullptr);
cannam@134 524 }
cannam@134 525
cannam@134 526 template <typename VatId, typename ProvisionId,
cannam@134 527 typename RecipientId, typename ThirdPartyCapId, typename JoinResult,
cannam@134 528 typename RealmGatewayClient, typename InternalRef, typename ExternalRef>
cannam@134 529 RpcSystem<VatId> makeRpcClient(
cannam@134 530 VatNetwork<VatId, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>& network,
cannam@134 531 RealmGatewayClient gateway) {
cannam@134 532 return RpcSystem<VatId>(network, nullptr, gateway.template castAs<RealmGateway<>>());
cannam@134 533 }
cannam@134 534
cannam@134 535 } // namespace capnp
cannam@134 536
cannam@134 537 #endif // CAPNP_RPC_H_