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