annotate win32-mingw/include/capnp/ez-rpc.h @ 80:0b60a66de5e2

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