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_EZ_RPC_H_ cannam@148: #define CAPNP_EZ_RPC_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: cannam@148: struct sockaddr; cannam@148: cannam@148: namespace kj { class AsyncIoProvider; class LowLevelAsyncIoProvider; } cannam@148: cannam@148: namespace capnp { cannam@148: cannam@148: class EzRpcContext; cannam@148: cannam@148: class EzRpcClient { cannam@148: // Super-simple interface for setting up a Cap'n Proto RPC client. Example: cannam@148: // cannam@148: // # Cap'n Proto schema cannam@148: // interface Adder { cannam@148: // add @0 (left :Int32, right :Int32) -> (value :Int32); cannam@148: // } cannam@148: // cannam@148: // // C++ client cannam@148: // int main() { cannam@148: // capnp::EzRpcClient client("localhost:3456"); cannam@148: // Adder::Client adder = client.getMain(); cannam@148: // auto request = adder.addRequest(); cannam@148: // request.setLeft(12); cannam@148: // request.setRight(34); cannam@148: // auto response = request.send().wait(client.getWaitScope()); cannam@148: // assert(response.getValue() == 46); cannam@148: // return 0; cannam@148: // } cannam@148: // cannam@148: // // C++ server cannam@148: // class AdderImpl final: public Adder::Server { cannam@148: // public: cannam@148: // kj::Promise add(AddContext context) override { cannam@148: // auto params = context.getParams(); cannam@148: // context.getResults().setValue(params.getLeft() + params.getRight()); cannam@148: // return kj::READY_NOW; cannam@148: // } cannam@148: // }; cannam@148: // cannam@148: // int main() { cannam@148: // capnp::EzRpcServer server(kj::heap(), "*:3456"); cannam@148: // kj::NEVER_DONE.wait(server.getWaitScope()); cannam@148: // } cannam@148: // cannam@148: // This interface is easy, but it hides a lot of useful features available from the lower-level cannam@148: // classes: cannam@148: // - The server can only export a small set of public, singleton capabilities under well-known cannam@148: // string names. This is fine for transient services where no state needs to be kept between cannam@148: // connections, but hides the power of Cap'n Proto when it comes to long-lived resources. cannam@148: // - EzRpcClient/EzRpcServer automatically set up a `kj::EventLoop` and make it current for the cannam@148: // thread. Only one `kj::EventLoop` can exist per thread, so you cannot use these interfaces cannam@148: // if you wish to set up your own event loop. (However, you can safely create multiple cannam@148: // EzRpcClient / EzRpcServer objects in a single thread; they will make sure to make no more cannam@148: // than one EventLoop.) cannam@148: // - These classes only support simple two-party connections, not multilateral VatNetworks. cannam@148: // - These classes only support communication over a raw, unencrypted socket. If you want to cannam@148: // build on an abstract stream (perhaps one which supports encryption), you must use the cannam@148: // lower-level interfaces. cannam@148: // cannam@148: // Some of these restrictions will probably be lifted in future versions, but some things will cannam@148: // always require using the low-level interfaces directly. If you are interested in working cannam@148: // at a lower level, start by looking at these interfaces: cannam@148: // - `kj::setupAsyncIo()` in `kj/async-io.h`. cannam@148: // - `RpcSystem` in `capnp/rpc.h`. cannam@148: // - `TwoPartyVatNetwork` in `capnp/rpc-twoparty.h`. cannam@148: cannam@148: public: cannam@148: explicit EzRpcClient(kj::StringPtr serverAddress, uint defaultPort = 0, cannam@148: ReaderOptions readerOpts = ReaderOptions()); cannam@148: // Construct a new EzRpcClient and connect to the given address. The connection is formed in cannam@148: // the background -- if it fails, calls to capabilities returned by importCap() will fail with an cannam@148: // appropriate exception. cannam@148: // cannam@148: // `defaultPort` is the IP port number to use if `serverAddress` does not include it explicitly. cannam@148: // If unspecified, the port is required in `serverAddress`. cannam@148: // cannam@148: // The address is parsed by `kj::Network` in `kj/async-io.h`. See that interface for more info cannam@148: // on the address format, but basically it's what you'd expect. cannam@148: // cannam@148: // `readerOpts` is the ReaderOptions structure used to read each incoming message on the cannam@148: // connection. Setting this may be necessary if you need to receive very large individual cannam@148: // messages or messages. However, it is recommended that you instead think about how to change cannam@148: // your protocol to send large data blobs in multiple small chunks -- this is much better for cannam@148: // both security and performance. See `ReaderOptions` in `message.h` for more details. cannam@148: cannam@148: EzRpcClient(const struct sockaddr* serverAddress, uint addrSize, cannam@148: ReaderOptions readerOpts = ReaderOptions()); cannam@148: // Like the above constructor, but connects to an already-resolved socket address. Any address cannam@148: // format supported by `kj::Network` in `kj/async-io.h` is accepted. cannam@148: cannam@148: explicit EzRpcClient(int socketFd, ReaderOptions readerOpts = ReaderOptions()); cannam@148: // Create a client on top of an already-connected socket. cannam@148: // `readerOpts` acts as in the first constructor. cannam@148: cannam@148: ~EzRpcClient() noexcept(false); cannam@148: cannam@148: template cannam@148: typename Type::Client getMain(); cannam@148: Capability::Client getMain(); cannam@148: // Get the server's main (aka "bootstrap") interface. cannam@148: cannam@148: template cannam@148: typename Type::Client importCap(kj::StringPtr name) cannam@148: KJ_DEPRECATED("Change your server to export a main interface, then use getMain() instead."); cannam@148: Capability::Client importCap(kj::StringPtr name) cannam@148: KJ_DEPRECATED("Change your server to export a main interface, then use getMain() instead."); cannam@148: // ** DEPRECATED ** cannam@148: // cannam@148: // Ask the sever for the capability with the given name. You may specify a type to automatically cannam@148: // down-cast to that type. It is up to you to specify the correct expected type. cannam@148: // cannam@148: // Named interfaces are deprecated. The new preferred usage pattern is for the server to export cannam@148: // a "main" interface which itself has methods for getting any other interfaces. cannam@148: cannam@148: kj::WaitScope& getWaitScope(); cannam@148: // Get the `WaitScope` for the client's `EventLoop`, which allows you to synchronously wait on cannam@148: // promises. cannam@148: cannam@148: kj::AsyncIoProvider& getIoProvider(); cannam@148: // Get the underlying AsyncIoProvider set up by the RPC system. This is useful if you want cannam@148: // to do some non-RPC I/O in asynchronous fashion. cannam@148: cannam@148: kj::LowLevelAsyncIoProvider& getLowLevelIoProvider(); cannam@148: // Get the underlying LowLevelAsyncIoProvider set up by the RPC system. This is useful if you cannam@148: // want to do some non-RPC I/O in asynchronous fashion. cannam@148: cannam@148: private: cannam@148: struct Impl; cannam@148: kj::Own impl; cannam@148: }; cannam@148: cannam@148: class EzRpcServer { cannam@148: // The server counterpart to `EzRpcClient`. See `EzRpcClient` for an example. cannam@148: cannam@148: public: cannam@148: explicit EzRpcServer(Capability::Client mainInterface, kj::StringPtr bindAddress, cannam@148: uint defaultPort = 0, ReaderOptions readerOpts = ReaderOptions()); cannam@148: // Construct a new `EzRpcServer` that binds to the given address. An address of "*" means to cannam@148: // bind to all local addresses. cannam@148: // cannam@148: // `defaultPort` is the IP port number to use if `serverAddress` does not include it explicitly. cannam@148: // If unspecified, a port is chosen automatically, and you must call getPort() to find out what cannam@148: // it is. cannam@148: // cannam@148: // The address is parsed by `kj::Network` in `kj/async-io.h`. See that interface for more info cannam@148: // on the address format, but basically it's what you'd expect. cannam@148: // cannam@148: // The server might not begin listening immediately, especially if `bindAddress` needs to be cannam@148: // resolved. If you need to wait until the server is definitely up, wait on the promise returned cannam@148: // by `getPort()`. cannam@148: // cannam@148: // `readerOpts` is the ReaderOptions structure used to read each incoming message on the cannam@148: // connection. Setting this may be necessary if you need to receive very large individual cannam@148: // messages or messages. However, it is recommended that you instead think about how to change cannam@148: // your protocol to send large data blobs in multiple small chunks -- this is much better for cannam@148: // both security and performance. See `ReaderOptions` in `message.h` for more details. cannam@148: cannam@148: EzRpcServer(Capability::Client mainInterface, struct sockaddr* bindAddress, uint addrSize, cannam@148: ReaderOptions readerOpts = ReaderOptions()); cannam@148: // Like the above constructor, but binds to an already-resolved socket address. Any address cannam@148: // format supported by `kj::Network` in `kj/async-io.h` is accepted. cannam@148: cannam@148: EzRpcServer(Capability::Client mainInterface, int socketFd, uint port, cannam@148: ReaderOptions readerOpts = ReaderOptions()); cannam@148: // Create a server on top of an already-listening socket (i.e. one on which accept() may be cannam@148: // called). `port` is returned by `getPort()` -- it serves no other purpose. cannam@148: // `readerOpts` acts as in the other two above constructors. cannam@148: cannam@148: explicit EzRpcServer(kj::StringPtr bindAddress, uint defaultPort = 0, cannam@148: ReaderOptions readerOpts = ReaderOptions()) cannam@148: KJ_DEPRECATED("Please specify a main interface for your server."); cannam@148: EzRpcServer(struct sockaddr* bindAddress, uint addrSize, cannam@148: ReaderOptions readerOpts = ReaderOptions()) cannam@148: KJ_DEPRECATED("Please specify a main interface for your server."); cannam@148: EzRpcServer(int socketFd, uint port, ReaderOptions readerOpts = ReaderOptions()) cannam@148: KJ_DEPRECATED("Please specify a main interface for your server."); cannam@148: cannam@148: ~EzRpcServer() noexcept(false); cannam@148: cannam@148: void exportCap(kj::StringPtr name, Capability::Client cap); cannam@148: // Export a capability publicly under the given name, so that clients can import it. cannam@148: // cannam@148: // Keep in mind that you can implicitly convert `kj::Own&&` to cannam@148: // `Capability::Client`, so it's typical to pass something like cannam@148: // `kj::heap()` as the second parameter. cannam@148: cannam@148: kj::Promise getPort(); cannam@148: // Get the IP port number on which this server is listening. This promise won't resolve until cannam@148: // the server is actually listening. If the address was not an IP address (e.g. it was a Unix cannam@148: // domain socket) then getPort() resolves to zero. cannam@148: cannam@148: kj::WaitScope& getWaitScope(); cannam@148: // Get the `WaitScope` for the client's `EventLoop`, which allows you to synchronously wait on cannam@148: // promises. cannam@148: cannam@148: kj::AsyncIoProvider& getIoProvider(); cannam@148: // Get the underlying AsyncIoProvider set up by the RPC system. This is useful if you want cannam@148: // to do some non-RPC I/O in asynchronous fashion. cannam@148: cannam@148: kj::LowLevelAsyncIoProvider& getLowLevelIoProvider(); cannam@148: // Get the underlying LowLevelAsyncIoProvider set up by the RPC system. This is useful if you cannam@148: // want to do some non-RPC I/O in asynchronous fashion. cannam@148: cannam@148: private: cannam@148: struct Impl; cannam@148: kj::Own impl; cannam@148: }; cannam@148: cannam@148: // ======================================================================================= cannam@148: // inline implementation details cannam@148: cannam@148: template cannam@148: inline typename Type::Client EzRpcClient::getMain() { cannam@148: return getMain().castAs(); cannam@148: } cannam@148: cannam@148: template cannam@148: inline typename Type::Client EzRpcClient::importCap(kj::StringPtr name) { cannam@148: return importCap(name).castAs(); cannam@148: } cannam@148: cannam@148: } // namespace capnp cannam@148: cannam@148: #endif // CAPNP_EZ_RPC_H_