annotate osx/include/capnp/ez-rpc.h @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 45360b968bf4
children
rev   line source
cannam@147 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@147 2 // Licensed under the MIT License:
cannam@147 3 //
cannam@147 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@147 5 // of this software and associated documentation files (the "Software"), to deal
cannam@147 6 // in the Software without restriction, including without limitation the rights
cannam@147 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@147 8 // copies of the Software, and to permit persons to whom the Software is
cannam@147 9 // furnished to do so, subject to the following conditions:
cannam@147 10 //
cannam@147 11 // The above copyright notice and this permission notice shall be included in
cannam@147 12 // all copies or substantial portions of the Software.
cannam@147 13 //
cannam@147 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@147 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@147 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@147 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@147 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@147 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@147 20 // THE SOFTWARE.
cannam@147 21
cannam@147 22 #ifndef CAPNP_EZ_RPC_H_
cannam@147 23 #define CAPNP_EZ_RPC_H_
cannam@147 24
cannam@147 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@147 26 #pragma GCC system_header
cannam@147 27 #endif
cannam@147 28
cannam@147 29 #include "rpc.h"
cannam@147 30 #include "message.h"
cannam@147 31
cannam@147 32 struct sockaddr;
cannam@147 33
cannam@147 34 namespace kj { class AsyncIoProvider; class LowLevelAsyncIoProvider; }
cannam@147 35
cannam@147 36 namespace capnp {
cannam@147 37
cannam@147 38 class EzRpcContext;
cannam@147 39
cannam@147 40 class EzRpcClient {
cannam@147 41 // Super-simple interface for setting up a Cap'n Proto RPC client. Example:
cannam@147 42 //
cannam@147 43 // # Cap'n Proto schema
cannam@147 44 // interface Adder {
cannam@147 45 // add @0 (left :Int32, right :Int32) -> (value :Int32);
cannam@147 46 // }
cannam@147 47 //
cannam@147 48 // // C++ client
cannam@147 49 // int main() {
cannam@147 50 // capnp::EzRpcClient client("localhost:3456");
cannam@147 51 // Adder::Client adder = client.getMain<Adder>();
cannam@147 52 // auto request = adder.addRequest();
cannam@147 53 // request.setLeft(12);
cannam@147 54 // request.setRight(34);
cannam@147 55 // auto response = request.send().wait(client.getWaitScope());
cannam@147 56 // assert(response.getValue() == 46);
cannam@147 57 // return 0;
cannam@147 58 // }
cannam@147 59 //
cannam@147 60 // // C++ server
cannam@147 61 // class AdderImpl final: public Adder::Server {
cannam@147 62 // public:
cannam@147 63 // kj::Promise<void> add(AddContext context) override {
cannam@147 64 // auto params = context.getParams();
cannam@147 65 // context.getResults().setValue(params.getLeft() + params.getRight());
cannam@147 66 // return kj::READY_NOW;
cannam@147 67 // }
cannam@147 68 // };
cannam@147 69 //
cannam@147 70 // int main() {
cannam@147 71 // capnp::EzRpcServer server(kj::heap<AdderImpl>(), "*:3456");
cannam@147 72 // kj::NEVER_DONE.wait(server.getWaitScope());
cannam@147 73 // }
cannam@147 74 //
cannam@147 75 // This interface is easy, but it hides a lot of useful features available from the lower-level
cannam@147 76 // classes:
cannam@147 77 // - The server can only export a small set of public, singleton capabilities under well-known
cannam@147 78 // string names. This is fine for transient services where no state needs to be kept between
cannam@147 79 // connections, but hides the power of Cap'n Proto when it comes to long-lived resources.
cannam@147 80 // - EzRpcClient/EzRpcServer automatically set up a `kj::EventLoop` and make it current for the
cannam@147 81 // thread. Only one `kj::EventLoop` can exist per thread, so you cannot use these interfaces
cannam@147 82 // if you wish to set up your own event loop. (However, you can safely create multiple
cannam@147 83 // EzRpcClient / EzRpcServer objects in a single thread; they will make sure to make no more
cannam@147 84 // than one EventLoop.)
cannam@147 85 // - These classes only support simple two-party connections, not multilateral VatNetworks.
cannam@147 86 // - These classes only support communication over a raw, unencrypted socket. If you want to
cannam@147 87 // build on an abstract stream (perhaps one which supports encryption), you must use the
cannam@147 88 // lower-level interfaces.
cannam@147 89 //
cannam@147 90 // Some of these restrictions will probably be lifted in future versions, but some things will
cannam@147 91 // always require using the low-level interfaces directly. If you are interested in working
cannam@147 92 // at a lower level, start by looking at these interfaces:
cannam@147 93 // - `kj::setupAsyncIo()` in `kj/async-io.h`.
cannam@147 94 // - `RpcSystem` in `capnp/rpc.h`.
cannam@147 95 // - `TwoPartyVatNetwork` in `capnp/rpc-twoparty.h`.
cannam@147 96
cannam@147 97 public:
cannam@147 98 explicit EzRpcClient(kj::StringPtr serverAddress, uint defaultPort = 0,
cannam@147 99 ReaderOptions readerOpts = ReaderOptions());
cannam@147 100 // Construct a new EzRpcClient and connect to the given address. The connection is formed in
cannam@147 101 // the background -- if it fails, calls to capabilities returned by importCap() will fail with an
cannam@147 102 // appropriate exception.
cannam@147 103 //
cannam@147 104 // `defaultPort` is the IP port number to use if `serverAddress` does not include it explicitly.
cannam@147 105 // If unspecified, the port is required in `serverAddress`.
cannam@147 106 //
cannam@147 107 // The address is parsed by `kj::Network` in `kj/async-io.h`. See that interface for more info
cannam@147 108 // on the address format, but basically it's what you'd expect.
cannam@147 109 //
cannam@147 110 // `readerOpts` is the ReaderOptions structure used to read each incoming message on the
cannam@147 111 // connection. Setting this may be necessary if you need to receive very large individual
cannam@147 112 // messages or messages. However, it is recommended that you instead think about how to change
cannam@147 113 // your protocol to send large data blobs in multiple small chunks -- this is much better for
cannam@147 114 // both security and performance. See `ReaderOptions` in `message.h` for more details.
cannam@147 115
cannam@147 116 EzRpcClient(const struct sockaddr* serverAddress, uint addrSize,
cannam@147 117 ReaderOptions readerOpts = ReaderOptions());
cannam@147 118 // Like the above constructor, but connects to an already-resolved socket address. Any address
cannam@147 119 // format supported by `kj::Network` in `kj/async-io.h` is accepted.
cannam@147 120
cannam@147 121 explicit EzRpcClient(int socketFd, ReaderOptions readerOpts = ReaderOptions());
cannam@147 122 // Create a client on top of an already-connected socket.
cannam@147 123 // `readerOpts` acts as in the first constructor.
cannam@147 124
cannam@147 125 ~EzRpcClient() noexcept(false);
cannam@147 126
cannam@147 127 template <typename Type>
cannam@147 128 typename Type::Client getMain();
cannam@147 129 Capability::Client getMain();
cannam@147 130 // Get the server's main (aka "bootstrap") interface.
cannam@147 131
cannam@147 132 template <typename Type>
cannam@147 133 typename Type::Client importCap(kj::StringPtr name)
cannam@147 134 KJ_DEPRECATED("Change your server to export a main interface, then use getMain() instead.");
cannam@147 135 Capability::Client importCap(kj::StringPtr name)
cannam@147 136 KJ_DEPRECATED("Change your server to export a main interface, then use getMain() instead.");
cannam@147 137 // ** DEPRECATED **
cannam@147 138 //
cannam@147 139 // Ask the sever for the capability with the given name. You may specify a type to automatically
cannam@147 140 // down-cast to that type. It is up to you to specify the correct expected type.
cannam@147 141 //
cannam@147 142 // Named interfaces are deprecated. The new preferred usage pattern is for the server to export
cannam@147 143 // a "main" interface which itself has methods for getting any other interfaces.
cannam@147 144
cannam@147 145 kj::WaitScope& getWaitScope();
cannam@147 146 // Get the `WaitScope` for the client's `EventLoop`, which allows you to synchronously wait on
cannam@147 147 // promises.
cannam@147 148
cannam@147 149 kj::AsyncIoProvider& getIoProvider();
cannam@147 150 // Get the underlying AsyncIoProvider set up by the RPC system. This is useful if you want
cannam@147 151 // to do some non-RPC I/O in asynchronous fashion.
cannam@147 152
cannam@147 153 kj::LowLevelAsyncIoProvider& getLowLevelIoProvider();
cannam@147 154 // Get the underlying LowLevelAsyncIoProvider set up by the RPC system. This is useful if you
cannam@147 155 // want to do some non-RPC I/O in asynchronous fashion.
cannam@147 156
cannam@147 157 private:
cannam@147 158 struct Impl;
cannam@147 159 kj::Own<Impl> impl;
cannam@147 160 };
cannam@147 161
cannam@147 162 class EzRpcServer {
cannam@147 163 // The server counterpart to `EzRpcClient`. See `EzRpcClient` for an example.
cannam@147 164
cannam@147 165 public:
cannam@147 166 explicit EzRpcServer(Capability::Client mainInterface, kj::StringPtr bindAddress,
cannam@147 167 uint defaultPort = 0, ReaderOptions readerOpts = ReaderOptions());
cannam@147 168 // Construct a new `EzRpcServer` that binds to the given address. An address of "*" means to
cannam@147 169 // bind to all local addresses.
cannam@147 170 //
cannam@147 171 // `defaultPort` is the IP port number to use if `serverAddress` does not include it explicitly.
cannam@147 172 // If unspecified, a port is chosen automatically, and you must call getPort() to find out what
cannam@147 173 // it is.
cannam@147 174 //
cannam@147 175 // The address is parsed by `kj::Network` in `kj/async-io.h`. See that interface for more info
cannam@147 176 // on the address format, but basically it's what you'd expect.
cannam@147 177 //
cannam@147 178 // The server might not begin listening immediately, especially if `bindAddress` needs to be
cannam@147 179 // resolved. If you need to wait until the server is definitely up, wait on the promise returned
cannam@147 180 // by `getPort()`.
cannam@147 181 //
cannam@147 182 // `readerOpts` is the ReaderOptions structure used to read each incoming message on the
cannam@147 183 // connection. Setting this may be necessary if you need to receive very large individual
cannam@147 184 // messages or messages. However, it is recommended that you instead think about how to change
cannam@147 185 // your protocol to send large data blobs in multiple small chunks -- this is much better for
cannam@147 186 // both security and performance. See `ReaderOptions` in `message.h` for more details.
cannam@147 187
cannam@147 188 EzRpcServer(Capability::Client mainInterface, struct sockaddr* bindAddress, uint addrSize,
cannam@147 189 ReaderOptions readerOpts = ReaderOptions());
cannam@147 190 // Like the above constructor, but binds to an already-resolved socket address. Any address
cannam@147 191 // format supported by `kj::Network` in `kj/async-io.h` is accepted.
cannam@147 192
cannam@147 193 EzRpcServer(Capability::Client mainInterface, int socketFd, uint port,
cannam@147 194 ReaderOptions readerOpts = ReaderOptions());
cannam@147 195 // Create a server on top of an already-listening socket (i.e. one on which accept() may be
cannam@147 196 // called). `port` is returned by `getPort()` -- it serves no other purpose.
cannam@147 197 // `readerOpts` acts as in the other two above constructors.
cannam@147 198
cannam@147 199 explicit EzRpcServer(kj::StringPtr bindAddress, uint defaultPort = 0,
cannam@147 200 ReaderOptions readerOpts = ReaderOptions())
cannam@147 201 KJ_DEPRECATED("Please specify a main interface for your server.");
cannam@147 202 EzRpcServer(struct sockaddr* bindAddress, uint addrSize,
cannam@147 203 ReaderOptions readerOpts = ReaderOptions())
cannam@147 204 KJ_DEPRECATED("Please specify a main interface for your server.");
cannam@147 205 EzRpcServer(int socketFd, uint port, ReaderOptions readerOpts = ReaderOptions())
cannam@147 206 KJ_DEPRECATED("Please specify a main interface for your server.");
cannam@147 207
cannam@147 208 ~EzRpcServer() noexcept(false);
cannam@147 209
cannam@147 210 void exportCap(kj::StringPtr name, Capability::Client cap);
cannam@147 211 // Export a capability publicly under the given name, so that clients can import it.
cannam@147 212 //
cannam@147 213 // Keep in mind that you can implicitly convert `kj::Own<MyType::Server>&&` to
cannam@147 214 // `Capability::Client`, so it's typical to pass something like
cannam@147 215 // `kj::heap<MyImplementation>(<constructor params>)` as the second parameter.
cannam@147 216
cannam@147 217 kj::Promise<uint> getPort();
cannam@147 218 // Get the IP port number on which this server is listening. This promise won't resolve until
cannam@147 219 // the server is actually listening. If the address was not an IP address (e.g. it was a Unix
cannam@147 220 // domain socket) then getPort() resolves to zero.
cannam@147 221
cannam@147 222 kj::WaitScope& getWaitScope();
cannam@147 223 // Get the `WaitScope` for the client's `EventLoop`, which allows you to synchronously wait on
cannam@147 224 // promises.
cannam@147 225
cannam@147 226 kj::AsyncIoProvider& getIoProvider();
cannam@147 227 // Get the underlying AsyncIoProvider set up by the RPC system. This is useful if you want
cannam@147 228 // to do some non-RPC I/O in asynchronous fashion.
cannam@147 229
cannam@147 230 kj::LowLevelAsyncIoProvider& getLowLevelIoProvider();
cannam@147 231 // Get the underlying LowLevelAsyncIoProvider set up by the RPC system. This is useful if you
cannam@147 232 // want to do some non-RPC I/O in asynchronous fashion.
cannam@147 233
cannam@147 234 private:
cannam@147 235 struct Impl;
cannam@147 236 kj::Own<Impl> impl;
cannam@147 237 };
cannam@147 238
cannam@147 239 // =======================================================================================
cannam@147 240 // inline implementation details
cannam@147 241
cannam@147 242 template <typename Type>
cannam@147 243 inline typename Type::Client EzRpcClient::getMain() {
cannam@147 244 return getMain().castAs<Type>();
cannam@147 245 }
cannam@147 246
cannam@147 247 template <typename Type>
cannam@147 248 inline typename Type::Client EzRpcClient::importCap(kj::StringPtr name) {
cannam@147 249 return importCap(name).castAs<Type>();
cannam@147 250 }
cannam@147 251
cannam@147 252 } // namespace capnp
cannam@147 253
cannam@147 254 #endif // CAPNP_EZ_RPC_H_