cannam@62: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@62: // Licensed under the MIT License: cannam@62: // cannam@62: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@62: // of this software and associated documentation files (the "Software"), to deal cannam@62: // in the Software without restriction, including without limitation the rights cannam@62: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@62: // copies of the Software, and to permit persons to whom the Software is cannam@62: // furnished to do so, subject to the following conditions: cannam@62: // cannam@62: // The above copyright notice and this permission notice shall be included in cannam@62: // all copies or substantial portions of the Software. cannam@62: // cannam@62: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@62: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@62: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@62: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@62: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@62: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@62: // THE SOFTWARE. cannam@62: cannam@62: #ifndef CAPNP_RPC_TWOPARTY_H_ cannam@62: #define CAPNP_RPC_TWOPARTY_H_ cannam@62: cannam@62: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) cannam@62: #pragma GCC system_header cannam@62: #endif cannam@62: cannam@62: #include "rpc.h" cannam@62: #include "message.h" cannam@62: #include cannam@62: #include cannam@62: cannam@62: namespace capnp { cannam@62: cannam@62: namespace rpc { cannam@62: namespace twoparty { cannam@62: typedef VatId SturdyRefHostId; // For backwards-compatibility with version 0.4. cannam@62: } cannam@62: } cannam@62: cannam@62: typedef VatNetwork cannam@62: TwoPartyVatNetworkBase; cannam@62: cannam@62: class TwoPartyVatNetwork: public TwoPartyVatNetworkBase, cannam@62: private TwoPartyVatNetworkBase::Connection { cannam@62: // A `VatNetwork` that consists of exactly two parties communicating over an arbitrary byte cannam@62: // stream. This is used to implement the common case of a client/server network. cannam@62: // cannam@62: // See `ez-rpc.h` for a simple interface for setting up two-party clients and servers. cannam@62: // Use `TwoPartyVatNetwork` only if you need the advanced features. cannam@62: cannam@62: public: cannam@62: TwoPartyVatNetwork(kj::AsyncIoStream& stream, rpc::twoparty::Side side, cannam@62: ReaderOptions receiveOptions = ReaderOptions()); cannam@62: KJ_DISALLOW_COPY(TwoPartyVatNetwork); cannam@62: cannam@62: kj::Promise onDisconnect() { return disconnectPromise.addBranch(); } cannam@62: // Returns a promise that resolves when the peer disconnects. cannam@62: cannam@62: rpc::twoparty::Side getSide() { return side; } cannam@62: cannam@62: // implements VatNetwork ----------------------------------------------------- cannam@62: cannam@62: kj::Maybe> connect( cannam@62: rpc::twoparty::VatId::Reader ref) override; cannam@62: kj::Promise> accept() override; cannam@62: cannam@62: private: cannam@62: class OutgoingMessageImpl; cannam@62: class IncomingMessageImpl; cannam@62: cannam@62: kj::AsyncIoStream& stream; cannam@62: rpc::twoparty::Side side; cannam@62: MallocMessageBuilder peerVatId; cannam@62: ReaderOptions receiveOptions; cannam@62: bool accepted = false; cannam@62: cannam@62: kj::Maybe> previousWrite; cannam@62: // Resolves when the previous write completes. This effectively serves as the write queue. cannam@62: // Becomes null when shutdown() is called. cannam@62: cannam@62: kj::Own>> acceptFulfiller; cannam@62: // Fulfiller for the promise returned by acceptConnectionAsRefHost() on the client side, or the cannam@62: // second call on the server side. Never fulfilled, because there is only one connection. cannam@62: cannam@62: kj::ForkedPromise disconnectPromise = nullptr; cannam@62: cannam@62: class FulfillerDisposer: public kj::Disposer { cannam@62: // Hack: TwoPartyVatNetwork is both a VatNetwork and a VatNetwork::Connection. When the RPC cannam@62: // system detects (or initiates) a disconnection, it drops its reference to the Connection. cannam@62: // When all references have been dropped, then we want disconnectPromise to be fulfilled. cannam@62: // So we hand out Owns with this disposer attached, so that we can detect when cannam@62: // they are dropped. cannam@62: cannam@62: public: cannam@62: mutable kj::Own> fulfiller; cannam@62: mutable uint refcount = 0; cannam@62: cannam@62: void disposeImpl(void* pointer) const override; cannam@62: }; cannam@62: FulfillerDisposer disconnectFulfiller; cannam@62: cannam@62: kj::Own asConnection(); cannam@62: // Returns a pointer to this with the disposer set to disconnectFulfiller. cannam@62: cannam@62: // implements Connection ----------------------------------------------------- cannam@62: cannam@62: rpc::twoparty::VatId::Reader getPeerVatId() override; cannam@62: kj::Own newOutgoingMessage(uint firstSegmentWordSize) override; cannam@62: kj::Promise>> receiveIncomingMessage() override; cannam@62: kj::Promise shutdown() override; cannam@62: }; cannam@62: cannam@62: class TwoPartyServer: private kj::TaskSet::ErrorHandler { cannam@62: // Convenience class which implements a simple server which accepts connections on a listener cannam@62: // socket and serices them as two-party connections. cannam@62: cannam@62: public: cannam@62: explicit TwoPartyServer(Capability::Client bootstrapInterface); cannam@62: cannam@62: void accept(kj::Own&& connection); cannam@62: // Accepts the connection for servicing. cannam@62: cannam@62: kj::Promise listen(kj::ConnectionReceiver& listener); cannam@62: // Listens for connections on the given listener. The returned promise never resolves unless an cannam@62: // exception is thrown while trying to accept. You may discard the returned promise to cancel cannam@62: // listening. cannam@62: cannam@62: private: cannam@62: Capability::Client bootstrapInterface; cannam@62: kj::TaskSet tasks; cannam@62: cannam@62: struct AcceptedConnection; cannam@62: cannam@62: void taskFailed(kj::Exception&& exception) override; cannam@62: }; cannam@62: cannam@62: class TwoPartyClient { cannam@62: // Convenience class which implements a simple client. cannam@62: cannam@62: public: cannam@62: explicit TwoPartyClient(kj::AsyncIoStream& connection); cannam@62: TwoPartyClient(kj::AsyncIoStream& connection, Capability::Client bootstrapInterface, cannam@62: rpc::twoparty::Side side = rpc::twoparty::Side::CLIENT); cannam@62: cannam@62: Capability::Client bootstrap(); cannam@62: // Get the server's bootstrap interface. cannam@62: cannam@62: inline kj::Promise onDisconnect() { return network.onDisconnect(); } cannam@62: cannam@62: private: cannam@62: TwoPartyVatNetwork network; cannam@62: RpcSystem rpcSystem; cannam@62: }; cannam@62: cannam@62: } // namespace capnp cannam@62: cannam@62: #endif // CAPNP_RPC_TWOPARTY_H_