annotate win32-mingw/include/capnp/rpc.h @ 64:eccd51b72864

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