annotate osx/include/capnp/rpc-twoparty.h @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 0994c39f1e94
children
rev   line source
cannam@62 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@62 2 // Licensed under the MIT License:
cannam@62 3 //
cannam@62 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@62 5 // of this software and associated documentation files (the "Software"), to deal
cannam@62 6 // in the Software without restriction, including without limitation the rights
cannam@62 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@62 8 // copies of the Software, and to permit persons to whom the Software is
cannam@62 9 // furnished to do so, subject to the following conditions:
cannam@62 10 //
cannam@62 11 // The above copyright notice and this permission notice shall be included in
cannam@62 12 // all copies or substantial portions of the Software.
cannam@62 13 //
cannam@62 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@62 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@62 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@62 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@62 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@62 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@62 20 // THE SOFTWARE.
cannam@62 21
cannam@62 22 #ifndef CAPNP_RPC_TWOPARTY_H_
cannam@62 23 #define CAPNP_RPC_TWOPARTY_H_
cannam@62 24
cannam@62 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@62 26 #pragma GCC system_header
cannam@62 27 #endif
cannam@62 28
cannam@62 29 #include "rpc.h"
cannam@62 30 #include "message.h"
cannam@62 31 #include <kj/async-io.h>
cannam@62 32 #include <capnp/rpc-twoparty.capnp.h>
cannam@62 33
cannam@62 34 namespace capnp {
cannam@62 35
cannam@62 36 namespace rpc {
cannam@62 37 namespace twoparty {
cannam@62 38 typedef VatId SturdyRefHostId; // For backwards-compatibility with version 0.4.
cannam@62 39 }
cannam@62 40 }
cannam@62 41
cannam@62 42 typedef VatNetwork<rpc::twoparty::VatId, rpc::twoparty::ProvisionId,
cannam@62 43 rpc::twoparty::RecipientId, rpc::twoparty::ThirdPartyCapId, rpc::twoparty::JoinResult>
cannam@62 44 TwoPartyVatNetworkBase;
cannam@62 45
cannam@62 46 class TwoPartyVatNetwork: public TwoPartyVatNetworkBase,
cannam@62 47 private TwoPartyVatNetworkBase::Connection {
cannam@62 48 // A `VatNetwork` that consists of exactly two parties communicating over an arbitrary byte
cannam@62 49 // stream. This is used to implement the common case of a client/server network.
cannam@62 50 //
cannam@62 51 // See `ez-rpc.h` for a simple interface for setting up two-party clients and servers.
cannam@62 52 // Use `TwoPartyVatNetwork` only if you need the advanced features.
cannam@62 53
cannam@62 54 public:
cannam@62 55 TwoPartyVatNetwork(kj::AsyncIoStream& stream, rpc::twoparty::Side side,
cannam@62 56 ReaderOptions receiveOptions = ReaderOptions());
cannam@62 57 KJ_DISALLOW_COPY(TwoPartyVatNetwork);
cannam@62 58
cannam@62 59 kj::Promise<void> onDisconnect() { return disconnectPromise.addBranch(); }
cannam@62 60 // Returns a promise that resolves when the peer disconnects.
cannam@62 61
cannam@62 62 rpc::twoparty::Side getSide() { return side; }
cannam@62 63
cannam@62 64 // implements VatNetwork -----------------------------------------------------
cannam@62 65
cannam@62 66 kj::Maybe<kj::Own<TwoPartyVatNetworkBase::Connection>> connect(
cannam@62 67 rpc::twoparty::VatId::Reader ref) override;
cannam@62 68 kj::Promise<kj::Own<TwoPartyVatNetworkBase::Connection>> accept() override;
cannam@62 69
cannam@62 70 private:
cannam@62 71 class OutgoingMessageImpl;
cannam@62 72 class IncomingMessageImpl;
cannam@62 73
cannam@62 74 kj::AsyncIoStream& stream;
cannam@62 75 rpc::twoparty::Side side;
cannam@62 76 MallocMessageBuilder peerVatId;
cannam@62 77 ReaderOptions receiveOptions;
cannam@62 78 bool accepted = false;
cannam@62 79
cannam@62 80 kj::Maybe<kj::Promise<void>> previousWrite;
cannam@62 81 // Resolves when the previous write completes. This effectively serves as the write queue.
cannam@62 82 // Becomes null when shutdown() is called.
cannam@62 83
cannam@62 84 kj::Own<kj::PromiseFulfiller<kj::Own<TwoPartyVatNetworkBase::Connection>>> acceptFulfiller;
cannam@62 85 // Fulfiller for the promise returned by acceptConnectionAsRefHost() on the client side, or the
cannam@62 86 // second call on the server side. Never fulfilled, because there is only one connection.
cannam@62 87
cannam@62 88 kj::ForkedPromise<void> disconnectPromise = nullptr;
cannam@62 89
cannam@62 90 class FulfillerDisposer: public kj::Disposer {
cannam@62 91 // Hack: TwoPartyVatNetwork is both a VatNetwork and a VatNetwork::Connection. When the RPC
cannam@62 92 // system detects (or initiates) a disconnection, it drops its reference to the Connection.
cannam@62 93 // When all references have been dropped, then we want disconnectPromise to be fulfilled.
cannam@62 94 // So we hand out Own<Connection>s with this disposer attached, so that we can detect when
cannam@62 95 // they are dropped.
cannam@62 96
cannam@62 97 public:
cannam@62 98 mutable kj::Own<kj::PromiseFulfiller<void>> fulfiller;
cannam@62 99 mutable uint refcount = 0;
cannam@62 100
cannam@62 101 void disposeImpl(void* pointer) const override;
cannam@62 102 };
cannam@62 103 FulfillerDisposer disconnectFulfiller;
cannam@62 104
cannam@62 105 kj::Own<TwoPartyVatNetworkBase::Connection> asConnection();
cannam@62 106 // Returns a pointer to this with the disposer set to disconnectFulfiller.
cannam@62 107
cannam@62 108 // implements Connection -----------------------------------------------------
cannam@62 109
cannam@62 110 rpc::twoparty::VatId::Reader getPeerVatId() override;
cannam@62 111 kj::Own<OutgoingRpcMessage> newOutgoingMessage(uint firstSegmentWordSize) override;
cannam@62 112 kj::Promise<kj::Maybe<kj::Own<IncomingRpcMessage>>> receiveIncomingMessage() override;
cannam@62 113 kj::Promise<void> shutdown() override;
cannam@62 114 };
cannam@62 115
cannam@62 116 class TwoPartyServer: private kj::TaskSet::ErrorHandler {
cannam@62 117 // Convenience class which implements a simple server which accepts connections on a listener
cannam@62 118 // socket and serices them as two-party connections.
cannam@62 119
cannam@62 120 public:
cannam@62 121 explicit TwoPartyServer(Capability::Client bootstrapInterface);
cannam@62 122
cannam@62 123 void accept(kj::Own<kj::AsyncIoStream>&& connection);
cannam@62 124 // Accepts the connection for servicing.
cannam@62 125
cannam@62 126 kj::Promise<void> listen(kj::ConnectionReceiver& listener);
cannam@62 127 // Listens for connections on the given listener. The returned promise never resolves unless an
cannam@62 128 // exception is thrown while trying to accept. You may discard the returned promise to cancel
cannam@62 129 // listening.
cannam@62 130
cannam@62 131 private:
cannam@62 132 Capability::Client bootstrapInterface;
cannam@62 133 kj::TaskSet tasks;
cannam@62 134
cannam@62 135 struct AcceptedConnection;
cannam@62 136
cannam@62 137 void taskFailed(kj::Exception&& exception) override;
cannam@62 138 };
cannam@62 139
cannam@62 140 class TwoPartyClient {
cannam@62 141 // Convenience class which implements a simple client.
cannam@62 142
cannam@62 143 public:
cannam@62 144 explicit TwoPartyClient(kj::AsyncIoStream& connection);
cannam@62 145 TwoPartyClient(kj::AsyncIoStream& connection, Capability::Client bootstrapInterface,
cannam@62 146 rpc::twoparty::Side side = rpc::twoparty::Side::CLIENT);
cannam@62 147
cannam@62 148 Capability::Client bootstrap();
cannam@62 149 // Get the server's bootstrap interface.
cannam@62 150
cannam@62 151 inline kj::Promise<void> onDisconnect() { return network.onDisconnect(); }
cannam@62 152
cannam@62 153 private:
cannam@62 154 TwoPartyVatNetwork network;
cannam@62 155 RpcSystem<rpc::twoparty::VatId> rpcSystem;
cannam@62 156 };
cannam@62 157
cannam@62 158 } // namespace capnp
cannam@62 159
cannam@62 160 #endif // CAPNP_RPC_TWOPARTY_H_