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