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