annotate win64-msvc/include/capnp/rpc-twoparty.h @ 74:2f2b27544483

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