annotate win64-msvc/include/capnp/ez-rpc.h @ 142:75bf92aa2d1f

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