annotate osx/include/capnp/ez-rpc.h @ 54:5f67a29f0fc7

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