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