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