annotate win32-mingw/include/capnp/ez-rpc.h @ 159:f4b37539fcc7

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