annotate win64-msvc/include/capnp/rpc-twoparty.h @ 48:9530b331f8c1

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