Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 1 | .. _module-pw_rpc: |
Alexei Frolov | 26e3ae6 | 2020-05-04 17:06:17 -0700 | [diff] [blame] | 2 | |
| 3 | ------ |
| 4 | pw_rpc |
| 5 | ------ |
| 6 | The ``pw_rpc`` module provides a system for defining and invoking remote |
| 7 | procedure calls (RPCs) on a device. |
| 8 | |
Wyatt Hepler | 830d26d | 2021-02-17 09:07:43 -0800 | [diff] [blame] | 9 | This document discusses the ``pw_rpc`` protocol and its C++ implementation. |
| 10 | ``pw_rpc`` implementations for other languages are described in their own |
| 11 | documents: |
| 12 | |
| 13 | .. toctree:: |
| 14 | :maxdepth: 1 |
| 15 | |
| 16 | py/docs |
| 17 | |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 18 | .. admonition:: Try it out! |
| 19 | |
Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 20 | For a quick intro to ``pw_rpc``, see the |
Alexei Frolov | d3e5cb7 | 2021-01-08 13:08:45 -0800 | [diff] [blame] | 21 | :ref:`module-pw_hdlc-rpc-example` in the :ref:`module-pw_hdlc` module. |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 22 | |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 23 | .. attention:: |
Alexei Frolov | 26e3ae6 | 2020-05-04 17:06:17 -0700 | [diff] [blame] | 24 | |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 25 | This documentation is under construction. |
| 26 | |
| 27 | Creating an RPC |
| 28 | =============== |
| 29 | |
| 30 | 1. RPC service declaration |
| 31 | -------------------------- |
| 32 | Pigweed RPCs are declared in a protocol buffer service definition. |
| 33 | |
| 34 | * `Protocol Buffer service documentation |
| 35 | <https://developers.google.com/protocol-buffers/docs/proto3#services>`_ |
| 36 | * `gRPC service definition documentation |
| 37 | <https://grpc.io/docs/what-is-grpc/core-concepts/#service-definition>`_ |
| 38 | |
| 39 | .. code-block:: protobuf |
| 40 | |
| 41 | syntax = "proto3"; |
| 42 | |
| 43 | package foo.bar; |
| 44 | |
| 45 | message Request {} |
| 46 | |
| 47 | message Response { |
| 48 | int32 number = 1; |
| 49 | } |
| 50 | |
| 51 | service TheService { |
| 52 | rpc MethodOne(Request) returns (Response) {} |
| 53 | rpc MethodTwo(Request) returns (stream Response) {} |
| 54 | } |
| 55 | |
| 56 | This protocol buffer is declared in a ``BUILD.gn`` file as follows: |
| 57 | |
| 58 | .. code-block:: python |
| 59 | |
| 60 | import("//build_overrides/pigweed.gni") |
| 61 | import("$dir_pw_protobuf_compiler/proto.gni") |
| 62 | |
| 63 | pw_proto_library("the_service_proto") { |
| 64 | sources = [ "foo_bar/the_service.proto" ] |
| 65 | } |
| 66 | |
Wyatt Hepler | 830d26d | 2021-02-17 09:07:43 -0800 | [diff] [blame] | 67 | .. admonition:: proto2 or proto3 syntax? |
| 68 | |
| 69 | Always use proto3 syntax rather than proto2 for new protocol buffers. Proto2 |
| 70 | protobufs can be compiled for ``pw_rpc``, but they are not as well supported |
| 71 | as proto3. Specifically, ``pw_rpc`` lacks support for non-zero default values |
| 72 | in proto2. When using Nanopb with ``pw_rpc``, proto2 response protobufs with |
| 73 | non-zero field defaults should be manually initialized to the default struct. |
| 74 | |
| 75 | In the past, proto3 was sometimes avoided because it lacked support for field |
| 76 | presence detection. Fortunately, this has been fixed: proto3 now supports |
| 77 | ``optional`` fields, which are equivalent to proto2 ``optional`` fields. |
| 78 | |
| 79 | If you need to distinguish between a default-valued field and a missing field, |
| 80 | mark the field as ``optional``. The presence of the field can be detected |
| 81 | with a ``HasField(name)`` or ``has_<field>`` member, depending on the library. |
| 82 | |
| 83 | Optional fields have some overhead --- default-valued fields are included in |
| 84 | the encoded proto, and, if using Nanopb, the proto structs have a |
| 85 | ``has_<field>`` flag for each optional field. Use plain fields if field |
| 86 | presence detection is not needed. |
| 87 | |
| 88 | .. code-block:: protobuf |
| 89 | |
| 90 | syntax = "proto3"; |
| 91 | |
| 92 | message MyMessage { |
| 93 | // Leaving this field unset is equivalent to setting it to 0. |
| 94 | int32 number = 1; |
| 95 | |
| 96 | // Setting this field to 0 is different from leaving it unset. |
| 97 | optional int32 other_number = 2; |
| 98 | } |
| 99 | |
Wyatt Hepler | 8779bcd | 2020-11-25 07:25:16 -0800 | [diff] [blame] | 100 | 2. RPC code generation |
| 101 | ---------------------- |
| 102 | ``pw_rpc`` generates a C++ header file for each ``.proto`` file. This header is |
| 103 | generated in the build output directory. Its exact location varies by build |
| 104 | system and toolchain, but the C++ include path always matches the sources |
| 105 | declaration in the ``pw_proto_library``. The ``.proto`` extension is replaced |
| 106 | with an extension corresponding to the protobuf library in use. |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 107 | |
Wyatt Hepler | 8779bcd | 2020-11-25 07:25:16 -0800 | [diff] [blame] | 108 | ================== =============== =============== ============= |
| 109 | Protobuf libraries Build subtarget Protobuf header pw_rpc header |
| 110 | ================== =============== =============== ============= |
| 111 | Raw only .raw_rpc (none) .raw_rpc.pb.h |
| 112 | Nanopb or raw .nanopb_rpc .pb.h .rpc.pb.h |
| 113 | pw_protobuf or raw .pwpb_rpc .pwpb.h .rpc.pwpb.h |
| 114 | ================== =============== =============== ============= |
| 115 | |
| 116 | For example, the generated RPC header for ``"foo_bar/the_service.proto"`` is |
| 117 | ``"foo_bar/the_service.rpc.pb.h"`` for Nanopb or |
| 118 | ``"foo_bar/the_service.raw_rpc.pb.h"`` for raw RPCs. |
| 119 | |
| 120 | The generated header defines a base class for each RPC service declared in the |
| 121 | ``.proto`` file. A service named ``TheService`` in package ``foo.bar`` would |
| 122 | generate the following base class: |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 123 | |
| 124 | .. cpp:class:: template <typename Implementation> foo::bar::generated::TheService |
| 125 | |
Wyatt Hepler | 8779bcd | 2020-11-25 07:25:16 -0800 | [diff] [blame] | 126 | 3. RPC service definition |
| 127 | ------------------------- |
| 128 | The serivce class is implemented by inheriting from the generated RPC service |
| 129 | base class and defining a method for each RPC. The methods must match the name |
| 130 | and function signature for one of the supported protobuf implementations. |
| 131 | Services may mix and match protobuf implementations within one service. |
| 132 | |
| 133 | .. tip:: |
| 134 | |
| 135 | The generated code includes RPC service implementation stubs. You can |
| 136 | reference or copy and paste these to get started with implementing a service. |
| 137 | These stub classes are generated at the bottom of the pw_rpc proto header. |
| 138 | |
Wyatt Hepler | df38ed1 | 2021-03-24 08:42:48 -0700 | [diff] [blame] | 139 | To use the stubs, do the following: |
| 140 | |
| 141 | #. Locate the generated RPC header in the build directory. For example: |
| 142 | |
| 143 | .. code-block:: sh |
| 144 | |
| 145 | find out/ -name <proto_name>.rpc.pb.h |
| 146 | |
| 147 | #. Scroll to the bottom of the generated RPC header. |
| 148 | #. Copy the stub class declaration to a header file. |
| 149 | #. Copy the member function definitions to a source file. |
| 150 | #. Rename the class or change the namespace, if desired. |
| 151 | #. List these files in a build target with a dependency on the |
| 152 | ``pw_proto_library``. |
| 153 | |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 154 | A Nanopb implementation of this service would be as follows: |
| 155 | |
| 156 | .. code-block:: cpp |
| 157 | |
Wyatt Hepler | 8779bcd | 2020-11-25 07:25:16 -0800 | [diff] [blame] | 158 | #include "foo_bar/the_service.rpc.pb.h" |
| 159 | |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 160 | namespace foo::bar { |
| 161 | |
| 162 | class TheService : public generated::TheService<TheService> { |
| 163 | public: |
| 164 | pw::Status MethodOne(ServerContext& ctx, |
| 165 | const foo_bar_Request& request, |
| 166 | foo_bar_Response& response) { |
| 167 | // implementation |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 168 | return pw::OkStatus(); |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void MethodTwo(ServerContext& ctx, |
| 172 | const foo_bar_Request& request, |
| 173 | ServerWriter<foo_bar_Response>& response) { |
| 174 | // implementation |
| 175 | response.Write(foo_bar_Response{.number = 123}); |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | } // namespace foo::bar |
| 180 | |
| 181 | The Nanopb implementation would be declared in a ``BUILD.gn``: |
| 182 | |
| 183 | .. code-block:: python |
| 184 | |
| 185 | import("//build_overrides/pigweed.gni") |
| 186 | |
| 187 | import("$dir_pw_build/target_types.gni") |
| 188 | |
| 189 | pw_source_set("the_service") { |
| 190 | public_configs = [ ":public" ] |
| 191 | public = [ "public/foo_bar/service.h" ] |
Wyatt Hepler | 8779bcd | 2020-11-25 07:25:16 -0800 | [diff] [blame] | 192 | public_deps = [ ":the_service_proto.nanopb_rpc" ] |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | .. attention:: |
| 196 | |
| 197 | pw_rpc's generated classes will support using ``pw_protobuf`` or raw buffers |
| 198 | (no protobuf library) in the future. |
| 199 | |
Wyatt Hepler | 8779bcd | 2020-11-25 07:25:16 -0800 | [diff] [blame] | 200 | 4. Register the service with a server |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 201 | ------------------------------------- |
Alexei Frolov | d3e5cb7 | 2021-01-08 13:08:45 -0800 | [diff] [blame] | 202 | This example code sets up an RPC server with an :ref:`HDLC<module-pw_hdlc>` |
Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 203 | channel output and the example service. |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 204 | |
| 205 | .. code-block:: cpp |
| 206 | |
| 207 | // Set up the output channel for the pw_rpc server to use. This configures the |
| 208 | // pw_rpc server to use HDLC over UART; projects not using UART and HDLC must |
| 209 | // adapt this as necessary. |
| 210 | pw::stream::SysIoWriter writer; |
| 211 | pw::rpc::RpcChannelOutput<kMaxTransmissionUnit> hdlc_channel_output( |
Alexei Frolov | d3e5cb7 | 2021-01-08 13:08:45 -0800 | [diff] [blame] | 212 | writer, pw::hdlc::kDefaultRpcAddress, "HDLC output"); |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 213 | |
| 214 | pw::rpc::Channel channels[] = { |
| 215 | pw::rpc::Channel::Create<1>(&hdlc_channel_output)}; |
| 216 | |
| 217 | // Declare the pw_rpc server with the HDLC channel. |
| 218 | pw::rpc::Server server(channels); |
| 219 | |
| 220 | pw::rpc::TheService the_service; |
| 221 | |
| 222 | void RegisterServices() { |
| 223 | // Register the foo.bar.TheService example service. |
| 224 | server.Register(the_service); |
| 225 | |
| 226 | // Register other services |
| 227 | } |
| 228 | |
| 229 | int main() { |
| 230 | // Set up the server. |
| 231 | RegisterServices(); |
| 232 | |
| 233 | // Declare a buffer for decoding incoming HDLC frames. |
| 234 | std::array<std::byte, kMaxTransmissionUnit> input_buffer; |
| 235 | |
| 236 | PW_LOG_INFO("Starting pw_rpc server"); |
Alexei Frolov | d3e5cb7 | 2021-01-08 13:08:45 -0800 | [diff] [blame] | 237 | pw::hdlc::ReadAndProcessPackets( |
Wyatt Hepler | 455b492 | 2020-09-18 00:19:21 -0700 | [diff] [blame] | 238 | server, hdlc_channel_output, input_buffer); |
| 239 | } |
Wyatt Hepler | 948f547 | 2020-06-02 16:52:28 -0700 | [diff] [blame] | 240 | |
Alexei Frolov | 1c670a2 | 2021-04-09 10:18:17 -0700 | [diff] [blame] | 241 | Channels |
| 242 | ======== |
| 243 | ``pw_rpc`` sends all of its packets over channels. These are logical, |
| 244 | application-layer routes used to tell the RPC system where a packet should go. |
| 245 | |
| 246 | Channels over a client-server connection must all have a unique ID, which can be |
| 247 | assigned statically at compile time or dynamically. |
| 248 | |
| 249 | .. code-block:: cpp |
| 250 | |
| 251 | // Creating a channel with the static ID 3. |
| 252 | pw::rpc::Channel static_channel = pw::rpc::Channel::Create<3>(&output); |
| 253 | |
| 254 | // Grouping channel IDs within an enum can lead to clearer code. |
| 255 | enum ChannelId { |
| 256 | kUartChannel = 1, |
| 257 | kSpiChannel = 2, |
| 258 | }; |
| 259 | |
| 260 | // Creating a channel with a static ID defined within an enum. |
| 261 | pw::rpc::Channel another_static_channel = |
| 262 | pw::rpc::Channel::Create<ChannelId::kUartChannel>(&output); |
| 263 | |
| 264 | // Creating a channel with a dynamic ID (note that no output is provided; it |
| 265 | // will be set when the channel is used. |
| 266 | pw::rpc::Channel dynamic_channel; |
| 267 | |
Alexei Frolov | 567e670 | 2021-04-13 09:13:02 -0700 | [diff] [blame] | 268 | Sometimes, the ID and output of a channel are not known at compile time as they |
| 269 | depend on information stored on the physical device. To support this use case, a |
| 270 | dynamically-assignable channel can be configured once at runtime with an ID and |
| 271 | output. |
| 272 | |
| 273 | .. code-block:: cpp |
| 274 | |
| 275 | // Create a dynamic channel without a compile-time ID or output. |
| 276 | pw::rpc::Channel dynamic_channel; |
| 277 | |
| 278 | void Init() { |
| 279 | // Called during boot to pull the channel configuration from the system. |
| 280 | dynamic_channel.Configure(GetChannelId(), some_output); |
| 281 | } |
| 282 | |
| 283 | |
Alexei Frolov | 7c7a386 | 2020-07-16 15:36:02 -0700 | [diff] [blame] | 284 | Services |
| 285 | ======== |
| 286 | A service is a logical grouping of RPCs defined within a .proto file. ``pw_rpc`` |
| 287 | uses these .proto definitions to generate code for a base service, from which |
| 288 | user-defined RPCs are implemented. |
| 289 | |
| 290 | ``pw_rpc`` supports multiple protobuf libraries, and the generated code API |
| 291 | depends on which is used. |
| 292 | |
Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 293 | .. _module-pw_rpc-protobuf-library-apis: |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 294 | |
| 295 | Protobuf library APIs |
| 296 | ===================== |
| 297 | |
Alexei Frolov | 7c7a386 | 2020-07-16 15:36:02 -0700 | [diff] [blame] | 298 | .. toctree:: |
| 299 | :maxdepth: 1 |
| 300 | |
| 301 | nanopb/docs |
| 302 | |
| 303 | Testing a pw_rpc integration |
| 304 | ============================ |
| 305 | After setting up a ``pw_rpc`` server in your project, you can test that it is |
| 306 | working as intended by registering the provided ``EchoService``, defined in |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 307 | ``echo.proto``, which echoes back a message that it receives. |
Alexei Frolov | 7c7a386 | 2020-07-16 15:36:02 -0700 | [diff] [blame] | 308 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 309 | .. literalinclude:: echo.proto |
Alexei Frolov | 7c7a386 | 2020-07-16 15:36:02 -0700 | [diff] [blame] | 310 | :language: protobuf |
| 311 | :lines: 14- |
| 312 | |
| 313 | For example, in C++ with nanopb: |
| 314 | |
| 315 | .. code:: c++ |
| 316 | |
| 317 | #include "pw_rpc/server.h" |
| 318 | |
| 319 | // Include the apporpriate header for your protobuf library. |
| 320 | #include "pw_rpc/echo_service_nanopb.h" |
| 321 | |
| 322 | constexpr pw::rpc::Channel kChannels[] = { /* ... */ }; |
| 323 | static pw::rpc::Server server(kChannels); |
| 324 | |
| 325 | static pw::rpc::EchoService echo_service; |
| 326 | |
| 327 | void Init() { |
| 328 | server.RegisterService(&echo_service); |
| 329 | } |
| 330 | |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 331 | Protocol description |
| 332 | ==================== |
| 333 | Pigweed RPC servers and clients communicate using ``pw_rpc`` packets. These |
| 334 | packets are used to send requests and responses, control streams, cancel ongoing |
| 335 | RPCs, and report errors. |
| 336 | |
| 337 | Packet format |
| 338 | ------------- |
| 339 | Pigweed RPC packets consist of a type and a set of fields. The packets are |
| 340 | encoded as protocol buffers. The full packet format is described in |
Wyatt Hepler | ba325e4 | 2021-03-08 14:23:34 -0800 | [diff] [blame] | 341 | ``pw_rpc/pw_rpc/internal/packet.proto``. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 342 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 343 | .. literalinclude:: internal/packet.proto |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 344 | :language: protobuf |
| 345 | :lines: 14- |
| 346 | |
| 347 | The packet type and RPC type determine which fields are present in a Pigweed RPC |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 348 | packet. Each packet type is only sent by either the client or the server. |
| 349 | These tables describe the meaning of and fields included with each packet type. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 350 | |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 351 | Client-to-server packets |
| 352 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 353 | +-------------------+-------------------------------------+ |
| 354 | | packet type | description | |
| 355 | +===================+=====================================+ |
| 356 | | REQUEST | Invoke an RPC | |
| 357 | | | | |
| 358 | | | .. code-block:: text | |
| 359 | | | | |
| 360 | | | - channel_id | |
| 361 | | | - service_id | |
| 362 | | | - method_id | |
| 363 | | | - payload | |
| 364 | | | (unary & server streaming only) | |
| 365 | | | | |
| 366 | +-------------------+-------------------------------------+ |
| 367 | | CLIENT_STREAM | Message in a client stream | |
| 368 | | | | |
| 369 | | | .. code-block:: text | |
| 370 | | | | |
| 371 | | | - channel_id | |
| 372 | | | - service_id | |
| 373 | | | - method_id | |
| 374 | | | - payload | |
| 375 | | | | |
| 376 | +-------------------+-------------------------------------+ |
| 377 | | CLIENT_ERROR | Received unexpected packet | |
| 378 | | | | |
| 379 | | | .. code-block:: text | |
| 380 | | | | |
| 381 | | | - channel_id | |
| 382 | | | - service_id | |
| 383 | | | - method_id | |
| 384 | | | - status | |
| 385 | | | | |
| 386 | +-------------------+-------------------------------------+ |
| 387 | | CANCEL | Cancel an ongoing RPC | |
| 388 | | | | |
| 389 | | | .. code-block:: text | |
| 390 | | | | |
| 391 | | | - channel_id | |
| 392 | | | - service_id | |
| 393 | | | - method_id | |
| 394 | | | | |
| 395 | +-------------------+-------------------------------------+ |
| 396 | | CLIENT_STREAM_END | Client stream is complete | |
| 397 | | | | |
| 398 | | | .. code-block:: text | |
| 399 | | | | |
| 400 | | | - channel_id | |
| 401 | | | - service_id | |
| 402 | | | - method_id | |
| 403 | | | | |
| 404 | +-------------------+-------------------------------------+ |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 405 | |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 406 | **Errors** |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 407 | |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 408 | The client sends ``CLIENT_ERROR`` packets to a server when it receives a packet |
| 409 | it did not request. If the RPC is a streaming RPC, the server should abort it. |
| 410 | |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 411 | The status code indicates the type of error. The status code is logged, but all |
| 412 | status codes result in the same action by the server: aborting the RPC. |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 413 | |
| 414 | * ``NOT_FOUND`` -- Received a packet for a service method the client does not |
| 415 | recognize. |
| 416 | * ``FAILED_PRECONDITION`` -- Received a packet for a service method that the |
| 417 | client did not invoke. |
| 418 | |
| 419 | Server-to-client packets |
| 420 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 421 | +-------------------+-------------------------------------+ |
| 422 | | packet type | description | |
| 423 | +===================+=====================================+ |
| 424 | | RESPONSE | RPC response | |
| 425 | | | | |
| 426 | | | .. code-block:: text | |
| 427 | | | | |
| 428 | | | - channel_id | |
| 429 | | | - service_id | |
| 430 | | | - method_id | |
| 431 | | | - payload | |
| 432 | | | - status | |
| 433 | | | (unary & client streaming only) | |
| 434 | | | | |
| 435 | +-------------------+-------------------------------------+ |
| 436 | | SERVER_STREAM_END | Server stream and RPC finished | |
| 437 | | | | |
| 438 | | | .. code-block:: text | |
| 439 | | | | |
| 440 | | | - channel_id | |
| 441 | | | - service_id | |
| 442 | | | - method_id | |
| 443 | | | - status | |
| 444 | | | | |
| 445 | +-------------------+-------------------------------------+ |
| 446 | | SERVER_ERROR | Received unexpected packet | |
| 447 | | | | |
| 448 | | | .. code-block:: text | |
| 449 | | | | |
| 450 | | | - channel_id | |
| 451 | | | - service_id (if relevant) | |
| 452 | | | - method_id (if relevant) | |
| 453 | | | - status | |
| 454 | | | | |
| 455 | +-------------------+-------------------------------------+ |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 456 | |
| 457 | **Errors** |
| 458 | |
| 459 | The server sends ``SERVER_ERROR`` packets when it receives a packet it cannot |
| 460 | process. The client should abort any RPC for which it receives an error. The |
| 461 | status field indicates the type of error. |
| 462 | |
| 463 | * ``NOT_FOUND`` -- The requested service or method does not exist. |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 464 | * ``FAILED_PRECONDITION`` -- A client stream or cancel packet was sent for an |
| 465 | RPC that is not pending. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 466 | * ``RESOURCE_EXHAUSTED`` -- The request came on a new channel, but a channel |
| 467 | could not be allocated for it. |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame] | 468 | * ``INTERNAL`` -- The server was unable to respond to an RPC due to an |
| 469 | unrecoverable internal error. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 470 | |
| 471 | Inovking a service method |
| 472 | ------------------------- |
| 473 | Calling an RPC requires a specific sequence of packets. This section describes |
| 474 | the protocol for calling service methods of each type: unary, server streaming, |
| 475 | client streaming, and bidirectional streaming. |
| 476 | |
| 477 | Unary RPC |
| 478 | ^^^^^^^^^ |
| 479 | In a unary RPC, the client sends a single request and the server sends a single |
| 480 | response. |
| 481 | |
| 482 | .. seqdiag:: |
| 483 | :scale: 110 |
| 484 | |
| 485 | seqdiag { |
| 486 | default_note_color = aliceblue; |
| 487 | |
| 488 | client -> server [ |
| 489 | label = "request", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 490 | leftnote = "PacketType.REQUEST\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 491 | ]; |
| 492 | |
| 493 | client <- server [ |
| 494 | label = "response", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 495 | rightnote = "PacketType.RESPONSE\nchannel ID\nservice ID\nmethod ID\npayload\nstatus" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 496 | ]; |
| 497 | } |
| 498 | |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 499 | The client may attempt to cancel a unary RPC by sending a ``CANCEL`` packet. The |
| 500 | server sends no response to a cancelled RPC. If the server processes the unary |
| 501 | RPC synchronously (the handling thread sends the response), it may not be |
| 502 | possible to cancel the RPC. |
| 503 | |
| 504 | .. seqdiag:: |
| 505 | :scale: 110 |
| 506 | |
| 507 | seqdiag { |
| 508 | default_note_color = aliceblue; |
| 509 | |
| 510 | client -> server [ |
| 511 | label = "request", |
| 512 | leftnote = "PacketType.REQUEST\nchannel ID\nservice ID\nmethod ID\npayload" |
| 513 | ]; |
| 514 | |
| 515 | client -> server [ |
| 516 | noactivate, |
| 517 | label = "cancel", |
| 518 | leftnote = "PacketType.CANCEL\nchannel ID\nservice ID\nmethod ID" |
| 519 | ]; |
| 520 | } |
| 521 | |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 522 | Server streaming RPC |
| 523 | ^^^^^^^^^^^^^^^^^^^^ |
| 524 | In a server streaming RPC, the client sends a single request and the server |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 525 | sends any number of responses followed by a ``SERVER_STREAM_END`` packet. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 526 | |
| 527 | .. seqdiag:: |
| 528 | :scale: 110 |
| 529 | |
| 530 | seqdiag { |
| 531 | default_note_color = aliceblue; |
| 532 | |
| 533 | client -> server [ |
| 534 | label = "request", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 535 | leftnote = "PacketType.REQUEST\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 536 | ]; |
| 537 | |
| 538 | client <-- server [ |
| 539 | noactivate, |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 540 | label = "messages (zero or more)", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 541 | rightnote = "PacketType.RESPONSE\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 542 | ]; |
| 543 | |
| 544 | client <- server [ |
| 545 | label = "done", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 546 | rightnote = "PacketType.SERVER_STREAM_END\nchannel ID\nservice ID\nmethod ID\nstatus" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 547 | ]; |
| 548 | } |
| 549 | |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 550 | The client may terminate a server streaming RPC by sending a ``CANCEL`` packet. |
| 551 | The server sends no response. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 552 | |
| 553 | .. seqdiag:: |
| 554 | :scale: 110 |
| 555 | |
| 556 | seqdiag { |
| 557 | default_note_color = aliceblue; |
| 558 | |
| 559 | client -> server [ |
| 560 | label = "request", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 561 | leftnote = "PacketType.REQUEST\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 562 | ]; |
| 563 | |
| 564 | client <-- server [ |
| 565 | noactivate, |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 566 | label = "messages (zero or more)", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 567 | rightnote = "PacketType.RESPONSE\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 568 | ]; |
| 569 | |
| 570 | client -> server [ |
| 571 | noactivate, |
| 572 | label = "cancel", |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 573 | leftnote = "PacketType.CANCEL\nchannel ID\nservice ID\nmethod ID" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 574 | ]; |
| 575 | } |
| 576 | |
| 577 | Client streaming RPC |
| 578 | ^^^^^^^^^^^^^^^^^^^^ |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 579 | In a client streaming RPC, the client starts the RPC by sending a ``REQUEST`` |
| 580 | packet with no payload. It then sends any number of messages in |
| 581 | ``CLIENT_STREAM`` packets, followed by a ``CLIENT_STREAM_END``. The server sends |
| 582 | a single response to finish the RPC. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 583 | |
| 584 | .. seqdiag:: |
| 585 | :scale: 110 |
| 586 | |
| 587 | seqdiag { |
| 588 | default_note_color = aliceblue; |
| 589 | |
| 590 | client -> server [ |
| 591 | label = "start", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 592 | leftnote = "PacketType.REQUEST\nchannel ID\nservice ID\nmethod ID" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 593 | ]; |
| 594 | |
| 595 | client --> server [ |
| 596 | noactivate, |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 597 | label = "messages (zero or more)", |
| 598 | leftnote = "PacketType.CLIENT_STREAM\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 599 | ]; |
| 600 | |
| 601 | client -> server [ |
| 602 | noactivate, |
| 603 | label = "done", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 604 | leftnote = "PacketType.CLIENT_STREAM_END\nchannel ID\nservice ID\nmethod ID" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 605 | ]; |
| 606 | |
| 607 | client <- server [ |
| 608 | label = "response", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 609 | rightnote = "PacketType.RESPONSE\nchannel ID\nservice ID\nmethod ID\npayload\nstatus" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 610 | ]; |
| 611 | } |
| 612 | |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 613 | The server may finish the RPC at any time by sending its ``RESPONSE`` packet, |
| 614 | even if it has not yet received the ``CLIENT_STREAM_END`` packet. The client may |
| 615 | terminate the RPC at any time by sending a ``CANCEL`` packet. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 616 | |
| 617 | .. seqdiag:: |
| 618 | :scale: 110 |
| 619 | |
| 620 | seqdiag { |
| 621 | default_note_color = aliceblue; |
| 622 | |
| 623 | client -> server [ |
| 624 | label = "start", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 625 | leftnote = "PacketType.REQUEST\nchannel ID\nservice ID\nmethod ID" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 626 | ]; |
| 627 | |
| 628 | client --> server [ |
| 629 | noactivate, |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 630 | label = "messages (zero or more)", |
| 631 | leftnote = "PacketType.CLIENT_STREAM\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 632 | ]; |
| 633 | |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 634 | client -> server [ |
| 635 | noactivate, |
| 636 | label = "cancel", |
| 637 | rightnote = "PacketType.CANCEL\nchannel ID\nservice ID\nmethod ID" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 638 | ]; |
| 639 | } |
| 640 | |
| 641 | Bidirectional streaming RPC |
| 642 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 643 | In a bidirectional streaming RPC, the client sends any number of requests and |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 644 | the server sends any number of responses. The client invokes the RPC by sending |
| 645 | a ``REQUEST`` with no payload. It sends a ``CLIENT_STREAM_END`` packet when it |
| 646 | has finished sending requests. The server sends a ``SERVER_STREAM_END`` packet |
| 647 | to finish the RPC. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 648 | |
| 649 | .. seqdiag:: |
| 650 | :scale: 110 |
| 651 | |
| 652 | seqdiag { |
| 653 | default_note_color = aliceblue; |
| 654 | |
| 655 | client -> server [ |
| 656 | label = "start", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 657 | leftnote = "PacketType.REQUEST\nchannel ID\nservice ID\nmethod ID" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 658 | ]; |
| 659 | |
| 660 | client --> server [ |
| 661 | noactivate, |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 662 | label = "messages (zero or more)", |
| 663 | leftnote = "PacketType.CLIENT_STREAM\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 664 | ]; |
| 665 | |
| 666 | ... (messages in any order) ... |
| 667 | |
| 668 | client <-- server [ |
| 669 | noactivate, |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 670 | label = "messages (zero or more)", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 671 | rightnote = "PacketType.RESPONSE\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 672 | ]; |
| 673 | |
| 674 | client -> server [ |
| 675 | noactivate, |
| 676 | label = "done", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 677 | leftnote = "PacketType.CLIENT_STREAM_END\nchannel ID\nservice ID\nmethod ID" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 678 | ]; |
| 679 | |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 680 | client <- server [ |
| 681 | label = "done", |
Wyatt Hepler | 0f26235 | 2020-07-29 09:51:27 -0700 | [diff] [blame] | 682 | rightnote = "PacketType.SERVER_STREAM_END\nchannel ID\nservice ID\nmethod ID\nstatus" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 683 | ]; |
| 684 | } |
| 685 | |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 686 | The server may finish the RPC at any time by sending the ``SERVER_STREAM_END`` |
| 687 | packet, even if it has not received the ``CLIENT_STREAM_END`` packet. The client |
| 688 | may terminate the RPC at any time by sending a ``CANCEL`` packet. |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 689 | |
| 690 | .. seqdiag:: |
| 691 | :scale: 110 |
| 692 | |
| 693 | seqdiag { |
| 694 | default_note_color = aliceblue; |
| 695 | |
| 696 | client -> server [ |
| 697 | label = "start", |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 698 | leftnote = "PacketType.REQUEST\nchannel ID\nservice ID\nmethod ID" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 699 | ]; |
| 700 | |
| 701 | client --> server [ |
| 702 | noactivate, |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 703 | label = "messages (zero or more)", |
| 704 | leftnote = "PacketType.CLIENT_STREAM\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 705 | ]; |
| 706 | |
| 707 | client <-- server [ |
| 708 | noactivate, |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 709 | label = "messages (zero or more)", |
| 710 | rightnote = "PacketType.RESPONSE\nchannel ID\nservice ID\nmethod ID\npayload" |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 711 | ]; |
| 712 | |
| 713 | client -> server [ |
| 714 | noactivate, |
| 715 | label = "cancel", |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 716 | leftnote = "PacketType.CANCEL\nchannel ID\nservice ID\nmethod ID" ]; |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Wyatt Hepler | 948f547 | 2020-06-02 16:52:28 -0700 | [diff] [blame] | 719 | RPC server |
| 720 | ========== |
| 721 | Declare an instance of ``rpc::Server`` and register services with it. |
| 722 | |
| 723 | .. admonition:: TODO |
| 724 | |
| 725 | Document the public interface |
| 726 | |
Alexei Frolov | bf33d21 | 2020-09-15 17:13:45 -0700 | [diff] [blame] | 727 | Size report |
| 728 | ----------- |
| 729 | The following size report showcases the memory usage of the core RPC server. It |
| 730 | is configured with a single channel using a basic transport interface that |
| 731 | directly reads from and writes to ``pw_sys_io``. The transport has a 128-byte |
| 732 | packet buffer, which comprises the plurality of the example's RAM usage. This is |
| 733 | not a suitable transport for an actual product; a real implementation would have |
| 734 | additional overhead proportional to the complexity of the transport. |
| 735 | |
| 736 | .. include:: server_size |
| 737 | |
Wyatt Hepler | 948f547 | 2020-06-02 16:52:28 -0700 | [diff] [blame] | 738 | RPC server implementation |
| 739 | ------------------------- |
| 740 | |
| 741 | The Method class |
| 742 | ^^^^^^^^^^^^^^^^ |
| 743 | The RPC Server depends on the ``pw::rpc::internal::Method`` class. ``Method`` |
| 744 | serves as the bridge between the ``pw_rpc`` server library and the user-defined |
Wyatt Hepler | e95bd72 | 2020-11-23 07:49:47 -0800 | [diff] [blame] | 745 | RPC functions. Each supported protobuf implementation extends ``Method`` to |
| 746 | implement its request and response proto handling. The ``pw_rpc`` server |
| 747 | calls into the ``Method`` implementation through the base class's ``Invoke`` |
| 748 | function. |
Wyatt Hepler | 948f547 | 2020-06-02 16:52:28 -0700 | [diff] [blame] | 749 | |
Wyatt Hepler | e95bd72 | 2020-11-23 07:49:47 -0800 | [diff] [blame] | 750 | ``Method`` implementations store metadata about each method, including a |
| 751 | function pointer to the user-defined method implementation. They also provide |
| 752 | ``static constexpr`` functions for creating each type of method. ``Method`` |
| 753 | implementations must satisfy the ``MethodImplTester`` test class in |
| 754 | ``pw_rpc_private/method_impl_tester.h``. |
Wyatt Hepler | 948f547 | 2020-06-02 16:52:28 -0700 | [diff] [blame] | 755 | |
Wyatt Hepler | e95bd72 | 2020-11-23 07:49:47 -0800 | [diff] [blame] | 756 | See ``pw_rpc/internal/method.h`` for more details about ``Method``. |
Wyatt Hepler | 948f547 | 2020-06-02 16:52:28 -0700 | [diff] [blame] | 757 | |
| 758 | Packet flow |
| 759 | ^^^^^^^^^^^ |
| 760 | |
| 761 | Requests |
| 762 | ~~~~~~~~ |
| 763 | |
| 764 | .. blockdiag:: |
| 765 | |
| 766 | blockdiag { |
| 767 | packets [shape = beginpoint]; |
| 768 | |
| 769 | group { |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 770 | label = "pw_rpc library"; |
Wyatt Hepler | 948f547 | 2020-06-02 16:52:28 -0700 | [diff] [blame] | 771 | |
| 772 | server [label = "Server"]; |
Alexei Frolov | 9a4d6bf | 2020-08-04 10:33:26 -0700 | [diff] [blame] | 773 | service [label = "Service"]; |
Wyatt Hepler | 948f547 | 2020-06-02 16:52:28 -0700 | [diff] [blame] | 774 | method [label = "internal::Method"]; |
| 775 | } |
| 776 | |
| 777 | stubs [label = "generated services", shape = ellipse]; |
| 778 | user [label = "user-defined RPCs", shape = roundedbox]; |
| 779 | |
| 780 | packets -> server -> service -> method -> stubs -> user; |
| 781 | packets -> server [folded]; |
| 782 | method -> stubs [folded]; |
| 783 | } |
| 784 | |
| 785 | Responses |
| 786 | ~~~~~~~~~ |
| 787 | |
| 788 | .. blockdiag:: |
| 789 | |
| 790 | blockdiag { |
| 791 | user -> stubs [folded]; |
| 792 | |
| 793 | group { |
Wyatt Hepler | 067dd7e | 2020-07-14 19:34:32 -0700 | [diff] [blame] | 794 | label = "pw_rpc library"; |
Wyatt Hepler | 948f547 | 2020-06-02 16:52:28 -0700 | [diff] [blame] | 795 | |
| 796 | server [label = "Server"]; |
| 797 | method [label = "internal::Method"]; |
| 798 | channel [label = "Channel"]; |
| 799 | } |
| 800 | |
| 801 | stubs [label = "generated services", shape = ellipse]; |
| 802 | user [label = "user-defined RPCs", shape = roundedbox]; |
| 803 | packets [shape = beginpoint]; |
| 804 | |
| 805 | user -> stubs -> method [folded]; |
| 806 | method -> server -> channel; |
| 807 | channel -> packets [folded]; |
| 808 | } |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 809 | |
| 810 | RPC client |
| 811 | ========== |
| 812 | The RPC client is used to send requests to a server and manages the contexts of |
| 813 | ongoing RPCs. |
| 814 | |
| 815 | Setting up a client |
| 816 | ------------------- |
| 817 | The ``pw::rpc::Client`` class is instantiated with a list of channels that it |
| 818 | uses to communicate. These channels can be shared with a server, but multiple |
| 819 | clients cannot use the same channels. |
| 820 | |
| 821 | To send incoming RPC packets from the transport layer to be processed by a |
| 822 | client, the client's ``ProcessPacket`` function is called with the packet data. |
| 823 | |
| 824 | .. code:: c++ |
| 825 | |
| 826 | #include "pw_rpc/client.h" |
| 827 | |
| 828 | namespace { |
| 829 | |
| 830 | pw::rpc::Channel my_channels[] = { |
| 831 | pw::rpc::Channel::Create<1>(&my_channel_output)}; |
| 832 | pw::rpc::Client my_client(my_channels); |
| 833 | |
| 834 | } // namespace |
| 835 | |
| 836 | // Called when the transport layer receives an RPC packet. |
| 837 | void ProcessRpcPacket(ConstByteSpan packet) { |
| 838 | my_client.ProcessPacket(packet); |
| 839 | } |
| 840 | |
Alexei Frolov | 5a3a61c | 2020-10-01 18:51:41 -0700 | [diff] [blame] | 841 | .. _module-pw_rpc-making-calls: |
| 842 | |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 843 | Making RPC calls |
| 844 | ---------------- |
| 845 | RPC calls are not made directly through the client, but using one of its |
| 846 | registered channels instead. A service client class is generated from a .proto |
| 847 | file for each selected protobuf library, which is then used to send RPC requests |
| 848 | through a given channel. The API for this depends on the protobuf library; |
Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 849 | please refer to the |
| 850 | :ref:`appropriate documentation<module-pw_rpc-protobuf-library-apis>`. Multiple |
| 851 | service client implementations can exist simulatenously and share the same |
| 852 | ``Client`` class. |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 853 | |
| 854 | When a call is made, a ``pw::rpc::ClientCall`` object is returned to the caller. |
| 855 | This object tracks the ongoing RPC call, and can be used to manage it. An RPC |
| 856 | call is only active as long as its ``ClientCall`` object is alive. |
| 857 | |
| 858 | .. tip:: |
| 859 | Use ``std::move`` when passing around ``ClientCall`` objects to keep RPCs |
| 860 | alive. |
| 861 | |
Alexei Frolov | 2d737bc | 2021-04-27 23:03:09 -0700 | [diff] [blame] | 862 | Example |
| 863 | ^^^^^^^ |
| 864 | .. code-block:: c++ |
| 865 | |
| 866 | #include "pw_rpc/echo_service_nanopb.h" |
| 867 | |
| 868 | namespace { |
Alexei Frolov | 2b54ee6 | 2021-04-29 14:58:21 -0700 | [diff] [blame] | 869 | // Generated clients are namespaced with their proto library. |
| 870 | using pw::rpc::nanopb::EchoServiceClient; |
| 871 | |
| 872 | EchoServiceClient::EchoCall echo_call; |
Alexei Frolov | bebba90 | 2021-06-09 17:03:52 -0700 | [diff] [blame] | 873 | |
| 874 | // Callback invoked when a response is received. This is called synchronously |
| 875 | // from Client::ProcessPacket. |
| 876 | void EchoResponse(const pw_rpc_EchoMessage& response, |
| 877 | pw::Status status) { |
| 878 | if (status.ok()) { |
| 879 | PW_LOG_INFO("Received echo response: %s", response.msg); |
| 880 | } else { |
| 881 | PW_LOG_ERROR("Echo failed with status %d", |
| 882 | static_cast<int>(status.code())); |
| 883 | } |
| 884 | } |
Alexei Frolov | 2d737bc | 2021-04-27 23:03:09 -0700 | [diff] [blame] | 885 | |
| 886 | } // namespace |
| 887 | |
Alexei Frolov | bebba90 | 2021-06-09 17:03:52 -0700 | [diff] [blame] | 888 | |
Alexei Frolov | 2d737bc | 2021-04-27 23:03:09 -0700 | [diff] [blame] | 889 | void CallEcho(const char* message) { |
| 890 | pw_rpc_EchoMessage request = pw_rpc_EchoMessage_init_default; |
| 891 | pw::string::Copy(message, request.msg); |
| 892 | |
| 893 | // By assigning the returned ClientCall to the global echo_call, the RPC |
| 894 | // call is kept alive until it completes. When a response is received, it |
| 895 | // will be logged by the handler function and the call will complete. |
Alexei Frolov | bebba90 | 2021-06-09 17:03:52 -0700 | [diff] [blame] | 896 | echo_call = EchoServiceClient::Echo(my_channel, request, EchoResponse); |
Alexei Frolov | 2d737bc | 2021-04-27 23:03:09 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 899 | Client implementation details |
| 900 | ----------------------------- |
| 901 | |
| 902 | The ClientCall class |
| 903 | ^^^^^^^^^^^^^^^^^^^^ |
| 904 | ``ClientCall`` stores the context of an active RPC, and serves as the user's |
| 905 | interface to the RPC client. The core RPC library provides a base ``ClientCall`` |
| 906 | class with common functionality, which is then extended for RPC client |
| 907 | implementations tied to different protobuf libraries to provide convenient |
| 908 | interfaces for working with RPCs. |
| 909 | |
| 910 | The RPC server stores a list of all of active ``ClientCall`` objects. When an |
| 911 | incoming packet is recieved, it dispatches to one of its active calls, which |
| 912 | then decodes the payload and presents it to the user. |
Alexei Frolov | 3e28092 | 2021-04-12 14:53:06 -0700 | [diff] [blame] | 913 | |
| 914 | ClientServer |
| 915 | ============ |
| 916 | Sometimes, a device needs to both process RPCs as a server, as well as making |
| 917 | calls to another device as a client. To do this, both a client and server must |
| 918 | be set up, and incoming packets must be sent to both of them. |
| 919 | |
| 920 | Pigweed simplifies this setup by providing a ``ClientServer`` class which wraps |
| 921 | an RPC client and server with the same set of channels. |
| 922 | |
| 923 | .. code-block:: cpp |
| 924 | |
| 925 | pw::rpc::Channel channels[] = { |
| 926 | pw::rpc::Channel::Create<1>(&channel_output)}; |
| 927 | |
| 928 | // Creates both a client and a server. |
| 929 | pw::rpc::ClientServer client_server(channels); |
| 930 | |
| 931 | void ProcessRpcData(pw::ConstByteSpan packet) { |
| 932 | // Calls into both the client and the server, sending the packet to the |
| 933 | // appropriate one. |
| 934 | client_server.ProcessPacket(packet, output); |
| 935 | } |