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