Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
Ewout van Bekkum | 7f5b305 | 2021-11-11 17:35:23 -0800 | [diff] [blame] | 15 | // clang-format off |
| 16 | #include "pw_rpc/internal/log_config.h" // PW_LOG_* macros must be first. |
| 17 | |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 18 | #include "pw_rpc/client.h" |
Ewout van Bekkum | 7f5b305 | 2021-11-11 17:35:23 -0800 | [diff] [blame] | 19 | // clang-format on |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 20 | |
| 21 | #include "pw_log/log.h" |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 22 | #include "pw_rpc/internal/client_call.h" |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 23 | #include "pw_rpc/internal/packet.h" |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 24 | #include "pw_status/try.h" |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 25 | |
| 26 | namespace pw::rpc { |
| 27 | namespace { |
| 28 | |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 29 | using internal::Packet; |
| 30 | using internal::PacketType; |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | Status Client::ProcessPacket(ConstByteSpan data) { |
Dennis Kormalev | e730306 | 2022-04-01 15:48:39 +0000 | [diff] [blame] | 35 | PW_TRY_ASSIGN(Packet packet, Endpoint::ProcessPacket(data, Packet::kClient)); |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 36 | |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 37 | // Find an existing call for this RPC, if any. |
Wyatt Hepler | c8bdc7d | 2021-10-20 09:29:01 -0700 | [diff] [blame] | 38 | internal::rpc_lock().lock(); |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 39 | internal::ClientCall* call = |
| 40 | static_cast<internal::ClientCall*>(FindCall(packet)); |
| 41 | |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 42 | internal::Channel* channel = GetInternalChannel(packet.channel_id()); |
Wyatt Hepler | c8bdc7d | 2021-10-20 09:29:01 -0700 | [diff] [blame] | 43 | |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 44 | if (channel == nullptr) { |
Wyatt Hepler | c8bdc7d | 2021-10-20 09:29:01 -0700 | [diff] [blame] | 45 | internal::rpc_lock().unlock(); |
Wyatt Hepler | b1c4799 | 2022-01-06 11:05:50 -0800 | [diff] [blame] | 46 | PW_LOG_WARN("RPC client received a packet for an unregistered channel"); |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 47 | return Status::Unavailable(); |
Alexei Frolov | 062ed18 | 2020-09-28 16:23:06 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 50 | if (call == nullptr || call->id() != packet.call_id()) { |
Alexei Frolov | 86e05de | 2021-10-19 16:52:31 -0700 | [diff] [blame] | 51 | // The call for the packet does not exist. If the packet is a server stream |
| 52 | // message, notify the server so that it can kill the stream. Otherwise, |
| 53 | // silently drop the packet (as it would terminate the RPC anyway). |
| 54 | if (packet.type() == PacketType::SERVER_STREAM) { |
Wyatt Hepler | 3be460d | 2021-09-09 16:28:12 -0700 | [diff] [blame] | 55 | channel->Send(Packet::ClientError(packet, Status::FailedPrecondition())) |
| 56 | .IgnoreError(); |
Wyatt Hepler | b1c4799 | 2022-01-06 11:05:50 -0800 | [diff] [blame] | 57 | PW_LOG_WARN("RPC client received stream message for an unknown call"); |
Wyatt Hepler | 3be460d | 2021-09-09 16:28:12 -0700 | [diff] [blame] | 58 | } |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 59 | internal::rpc_lock().unlock(); |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 60 | return OkStatus(); // OK since the packet was handled |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | switch (packet.type()) { |
| 64 | case PacketType::RESPONSE: |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 65 | // RPCs without a server stream include a payload with the final packet. |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 66 | if (call->has_server_stream()) { |
| 67 | static_cast<internal::StreamResponseClientCall&>(*call).HandleCompleted( |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 68 | packet.status()); |
| 69 | } else { |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 70 | static_cast<internal::UnaryResponseClientCall&>(*call).HandleCompleted( |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 71 | packet.payload(), packet.status()); |
| 72 | } |
| 73 | break; |
Alexei Frolov | d727622 | 2020-10-01 12:41:59 -0700 | [diff] [blame] | 74 | case PacketType::SERVER_ERROR: |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 75 | call->HandleError(packet.status()); |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 76 | break; |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 77 | case PacketType::SERVER_STREAM: |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 78 | if (call->has_server_stream()) { |
| 79 | call->HandlePayload(packet.payload()); |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 80 | } else { |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 81 | // Report the error to the server so it can abort the RPC. |
| 82 | channel->Send(Packet::ClientError(packet, Status::InvalidArgument())) |
| 83 | .IgnoreError(); // Errors are logged in Channel::Send. |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 84 | call->HandleError(Status::InvalidArgument()); |
| 85 | PW_LOG_DEBUG("Received SERVER_STREAM for RPC without a server stream"); |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 86 | } |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 87 | break; |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 88 | default: |
Wyatt Hepler | c8bdc7d | 2021-10-20 09:29:01 -0700 | [diff] [blame] | 89 | internal::rpc_lock().unlock(); |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 90 | PW_LOG_WARN("pw_rpc client unable to handle packet of type %u", |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 91 | static_cast<unsigned>(packet.type())); |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 94 | return OkStatus(); // OK since the packet was handled |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } // namespace pw::rpc |