annotate win64-msvc/include/capnp/rpc-twoparty.h @ 132:42a73082be24

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