annotate win32-mingw/include/capnp/rpc-twoparty.h @ 159:f4b37539fcc7

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