annotate win32-mingw/include/capnp/rpc-twoparty.h @ 135:38d1c0e7850b

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