annotate osx/include/capnp/rpc-twoparty.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_TWOPARTY_H_
cannam@134 23 #define CAPNP_RPC_TWOPARTY_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 "rpc.h"
cannam@134 30 #include "message.h"
cannam@134 31 #include <kj/async-io.h>
cannam@134 32 #include <capnp/rpc-twoparty.capnp.h>
cannam@134 33
cannam@134 34 namespace capnp {
cannam@134 35
cannam@134 36 namespace rpc {
cannam@134 37 namespace twoparty {
cannam@134 38 typedef VatId SturdyRefHostId; // For backwards-compatibility with version 0.4.
cannam@134 39 }
cannam@134 40 }
cannam@134 41
cannam@134 42 typedef VatNetwork<rpc::twoparty::VatId, rpc::twoparty::ProvisionId,
cannam@134 43 rpc::twoparty::RecipientId, rpc::twoparty::ThirdPartyCapId, rpc::twoparty::JoinResult>
cannam@134 44 TwoPartyVatNetworkBase;
cannam@134 45
cannam@134 46 class TwoPartyVatNetwork: public TwoPartyVatNetworkBase,
cannam@134 47 private TwoPartyVatNetworkBase::Connection {
cannam@134 48 // A `VatNetwork` that consists of exactly two parties communicating over an arbitrary byte
cannam@134 49 // stream. This is used to implement the common case of a client/server network.
cannam@134 50 //
cannam@134 51 // See `ez-rpc.h` for a simple interface for setting up two-party clients and servers.
cannam@134 52 // Use `TwoPartyVatNetwork` only if you need the advanced features.
cannam@134 53
cannam@134 54 public:
cannam@134 55 TwoPartyVatNetwork(kj::AsyncIoStream& stream, rpc::twoparty::Side side,
cannam@134 56 ReaderOptions receiveOptions = ReaderOptions());
cannam@134 57 KJ_DISALLOW_COPY(TwoPartyVatNetwork);
cannam@134 58
cannam@134 59 kj::Promise<void> onDisconnect() { return disconnectPromise.addBranch(); }
cannam@134 60 // Returns a promise that resolves when the peer disconnects.
cannam@134 61
cannam@134 62 rpc::twoparty::Side getSide() { return side; }
cannam@134 63
cannam@134 64 // implements VatNetwork -----------------------------------------------------
cannam@134 65
cannam@134 66 kj::Maybe<kj::Own<TwoPartyVatNetworkBase::Connection>> connect(
cannam@134 67 rpc::twoparty::VatId::Reader ref) override;
cannam@134 68 kj::Promise<kj::Own<TwoPartyVatNetworkBase::Connection>> accept() override;
cannam@134 69
cannam@134 70 private:
cannam@134 71 class OutgoingMessageImpl;
cannam@134 72 class IncomingMessageImpl;
cannam@134 73
cannam@134 74 kj::AsyncIoStream& stream;
cannam@134 75 rpc::twoparty::Side side;
cannam@134 76 MallocMessageBuilder peerVatId;
cannam@134 77 ReaderOptions receiveOptions;
cannam@134 78 bool accepted = false;
cannam@134 79
cannam@134 80 kj::Maybe<kj::Promise<void>> previousWrite;
cannam@134 81 // Resolves when the previous write completes. This effectively serves as the write queue.
cannam@134 82 // Becomes null when shutdown() is called.
cannam@134 83
cannam@134 84 kj::Own<kj::PromiseFulfiller<kj::Own<TwoPartyVatNetworkBase::Connection>>> acceptFulfiller;
cannam@134 85 // Fulfiller for the promise returned by acceptConnectionAsRefHost() on the client side, or the
cannam@134 86 // second call on the server side. Never fulfilled, because there is only one connection.
cannam@134 87
cannam@134 88 kj::ForkedPromise<void> disconnectPromise = nullptr;
cannam@134 89
cannam@134 90 class FulfillerDisposer: public kj::Disposer {
cannam@134 91 // Hack: TwoPartyVatNetwork is both a VatNetwork and a VatNetwork::Connection. When the RPC
cannam@134 92 // system detects (or initiates) a disconnection, it drops its reference to the Connection.
cannam@134 93 // When all references have been dropped, then we want disconnectPromise to be fulfilled.
cannam@134 94 // So we hand out Own<Connection>s with this disposer attached, so that we can detect when
cannam@134 95 // they are dropped.
cannam@134 96
cannam@134 97 public:
cannam@134 98 mutable kj::Own<kj::PromiseFulfiller<void>> fulfiller;
cannam@134 99 mutable uint refcount = 0;
cannam@134 100
cannam@134 101 void disposeImpl(void* pointer) const override;
cannam@134 102 };
cannam@134 103 FulfillerDisposer disconnectFulfiller;
cannam@134 104
cannam@134 105 kj::Own<TwoPartyVatNetworkBase::Connection> asConnection();
cannam@134 106 // Returns a pointer to this with the disposer set to disconnectFulfiller.
cannam@134 107
cannam@134 108 // implements Connection -----------------------------------------------------
cannam@134 109
cannam@134 110 rpc::twoparty::VatId::Reader getPeerVatId() override;
cannam@134 111 kj::Own<OutgoingRpcMessage> newOutgoingMessage(uint firstSegmentWordSize) override;
cannam@134 112 kj::Promise<kj::Maybe<kj::Own<IncomingRpcMessage>>> receiveIncomingMessage() override;
cannam@134 113 kj::Promise<void> shutdown() override;
cannam@134 114 };
cannam@134 115
cannam@134 116 class TwoPartyServer: private kj::TaskSet::ErrorHandler {
cannam@134 117 // Convenience class which implements a simple server which accepts connections on a listener
cannam@134 118 // socket and serices them as two-party connections.
cannam@134 119
cannam@134 120 public:
cannam@134 121 explicit TwoPartyServer(Capability::Client bootstrapInterface);
cannam@134 122
cannam@134 123 void accept(kj::Own<kj::AsyncIoStream>&& connection);
cannam@134 124 // Accepts the connection for servicing.
cannam@134 125
cannam@134 126 kj::Promise<void> listen(kj::ConnectionReceiver& listener);
cannam@134 127 // Listens for connections on the given listener. The returned promise never resolves unless an
cannam@134 128 // exception is thrown while trying to accept. You may discard the returned promise to cancel
cannam@134 129 // listening.
cannam@134 130
cannam@134 131 private:
cannam@134 132 Capability::Client bootstrapInterface;
cannam@134 133 kj::TaskSet tasks;
cannam@134 134
cannam@134 135 struct AcceptedConnection;
cannam@134 136
cannam@134 137 void taskFailed(kj::Exception&& exception) override;
cannam@134 138 };
cannam@134 139
cannam@134 140 class TwoPartyClient {
cannam@134 141 // Convenience class which implements a simple client.
cannam@134 142
cannam@134 143 public:
cannam@134 144 explicit TwoPartyClient(kj::AsyncIoStream& connection);
cannam@134 145 TwoPartyClient(kj::AsyncIoStream& connection, Capability::Client bootstrapInterface,
cannam@134 146 rpc::twoparty::Side side = rpc::twoparty::Side::CLIENT);
cannam@134 147
cannam@134 148 Capability::Client bootstrap();
cannam@134 149 // Get the server's bootstrap interface.
cannam@134 150
cannam@134 151 inline kj::Promise<void> onDisconnect() { return network.onDisconnect(); }
cannam@134 152
cannam@134 153 private:
cannam@134 154 TwoPartyVatNetwork network;
cannam@134 155 RpcSystem<rpc::twoparty::VatId> rpcSystem;
cannam@134 156 };
cannam@134 157
cannam@134 158 } // namespace capnp
cannam@134 159
cannam@134 160 #endif // CAPNP_RPC_TWOPARTY_H_