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