annotate win32-mingw/include/capnp/rpc-twoparty.h @ 50:37d53a7e8262

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