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