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