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

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