rpc-prelude.h 4.71 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
21 22 23 24

// This file contains a bunch of internal declarations that must appear before rpc.h can start.
// We don't define these directly in rpc.h because it makes the file hard to read.

25
#pragma once
26

27
#if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
28 29 30
#pragma GCC system_header
#endif

31
#include "capability.h"
32
#include "persistent.capnp.h"
33 34 35 36 37 38 39 40 41 42 43 44

namespace capnp {

class OutgoingRpcMessage;
class IncomingRpcMessage;

template <typename SturdyRefHostId>
class RpcSystem;

namespace _ {  // private

class VatNetworkBase {
45
  // Non-template version of VatNetwork.  Ignore this class; see VatNetwork in rpc.h.
46 47 48 49 50 51 52

public:
  class Connection;

  struct ConnectionAndProvisionId {
    kj::Own<Connection> connection;
    kj::Own<OutgoingRpcMessage> firstMessage;
53
    Orphan<AnyPointer> provisionId;
54 55 56 57 58 59
  };

  class Connection {
  public:
    virtual kj::Own<OutgoingRpcMessage> newOutgoingMessage(uint firstSegmentWordSize) = 0;
    virtual kj::Promise<kj::Maybe<kj::Own<IncomingRpcMessage>>> receiveIncomingMessage() = 0;
Kenton Varda's avatar
Kenton Varda committed
60
    virtual kj::Promise<void> shutdown() = 0;
61
    virtual AnyStruct::Reader baseGetPeerVatId() = 0;
62
  };
63
  virtual kj::Maybe<kj::Own<Connection>> baseConnect(AnyStruct::Reader vatId) = 0;
64
  virtual kj::Promise<kj::Own<Connection>> baseAccept() = 0;
65 66 67 68
};

class SturdyRefRestorerBase {
public:
69
  virtual Capability::Client baseRestore(AnyPointer::Reader ref) = 0;
70 71
};

72 73 74 75 76 77
class BootstrapFactoryBase {
  // Non-template version of BootstrapFactory.  Ignore this class; see BootstrapFactory in rpc.h.
public:
  virtual Capability::Client baseCreateFor(AnyStruct::Reader clientId) = 0;
};

78
class RpcSystemBase {
79 80
  // Non-template version of RpcSystem.  Ignore this class; see RpcSystem in rpc.h.

81
public:
82 83
  RpcSystemBase(VatNetworkBase& network, kj::Maybe<Capability::Client> bootstrapInterface,
                kj::Maybe<RealmGateway<>::Client> gateway);
84 85
  RpcSystemBase(VatNetworkBase& network, BootstrapFactoryBase& bootstrapFactory,
                kj::Maybe<RealmGateway<>::Client> gateway);
86
  RpcSystemBase(VatNetworkBase& network, SturdyRefRestorerBase& restorer);
87 88 89 90 91 92 93
  RpcSystemBase(RpcSystemBase&& other) noexcept;
  ~RpcSystemBase() noexcept(false);

private:
  class Impl;
  kj::Own<Impl> impl;

94 95
  Capability::Client baseBootstrap(AnyStruct::Reader vatId);
  Capability::Client baseRestore(AnyStruct::Reader vatId, AnyPointer::Reader objectId);
96
  void baseSetFlowLimit(size_t words);
97 98 99 100 101

  template <typename>
  friend class capnp::RpcSystem;
};

102
template <typename T> struct InternalRefFromRealmGateway_;
103 104 105 106
template <typename InternalRef, typename ExternalRef, typename InternalOwner,
          typename ExternalOwner>
struct InternalRefFromRealmGateway_<RealmGateway<InternalRef, ExternalRef, InternalOwner,
                                    ExternalOwner>> {
107 108 109 110 111 112 113 114
  typedef InternalRef Type;
};
template <typename T>
using InternalRefFromRealmGateway = typename InternalRefFromRealmGateway_<T>::Type;
template <typename T>
using InternalRefFromRealmGatewayClient = InternalRefFromRealmGateway<typename T::Calls>;

template <typename T> struct ExternalRefFromRealmGateway_;
115 116 117 118
template <typename InternalRef, typename ExternalRef, typename InternalOwner,
          typename ExternalOwner>
struct ExternalRefFromRealmGateway_<RealmGateway<InternalRef, ExternalRef, InternalOwner,
                                    ExternalOwner>> {
119 120 121 122 123 124 125
  typedef ExternalRef Type;
};
template <typename T>
using ExternalRefFromRealmGateway = typename ExternalRefFromRealmGateway_<T>::Type;
template <typename T>
using ExternalRefFromRealmGatewayClient = ExternalRefFromRealmGateway<typename T::Calls>;

126 127
}  // namespace _ (private)
}  // namespace capnp