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