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 | |
| 15 | #include "pw_rpc/client.h" |
| 16 | |
| 17 | #include "pw_log/log.h" |
| 18 | #include "pw_rpc/internal/packet.h" |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 19 | #include "pw_status/try.h" |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 20 | |
| 21 | namespace pw::rpc { |
| 22 | namespace { |
| 23 | |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 24 | using internal::Packet; |
| 25 | using internal::PacketType; |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | Status Client::ProcessPacket(ConstByteSpan data) { |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 30 | internal::Call* base; |
| 31 | Result<Packet> result = Endpoint::ProcessPacket(data, Packet::kClient, base); |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 32 | |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 33 | PW_TRY(result.status()); |
Alexei Frolov | 3f2d008 | 2020-10-04 22:30:39 -0700 | [diff] [blame] | 34 | |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 35 | Packet& packet = *result; |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 36 | |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 37 | internal::Channel* channel = GetInternalChannel(packet.channel_id()); |
| 38 | if (channel == nullptr) { |
Alexei Frolov | 062ed18 | 2020-09-28 16:23:06 -0700 | [diff] [blame] | 39 | PW_LOG_WARN("RPC client received a packet for an unregistered channel"); |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 40 | return Status::Unavailable(); |
Alexei Frolov | 062ed18 | 2020-09-28 16:23:06 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Alexei Frolov | 86e05de | 2021-10-19 16:52:31 -0700 | [diff] [blame] | 43 | if (base == nullptr || base->id() != packet.call_id()) { |
| 44 | // The call for the packet does not exist. If the packet is a server stream |
| 45 | // message, notify the server so that it can kill the stream. Otherwise, |
| 46 | // silently drop the packet (as it would terminate the RPC anyway). |
| 47 | if (packet.type() == PacketType::SERVER_STREAM) { |
| 48 | PW_LOG_WARN("RPC client received stream message for an unknown call"); |
Wyatt Hepler | 3be460d | 2021-09-09 16:28:12 -0700 | [diff] [blame] | 49 | channel->Send(Packet::ClientError(packet, Status::FailedPrecondition())) |
| 50 | .IgnoreError(); |
| 51 | } |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 52 | return OkStatus(); // OK since the packet was handled |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 55 | internal::ClientCall& call = *static_cast<internal::ClientCall*>(base); |
| 56 | |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 57 | switch (packet.type()) { |
| 58 | case PacketType::RESPONSE: |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 59 | // RPCs without a server stream include a payload with the final packet. |
| 60 | if (call.has_server_stream()) { |
| 61 | static_cast<internal::StreamResponseClientCall&>(call).HandleCompleted( |
| 62 | packet.status()); |
| 63 | } else { |
| 64 | static_cast<internal::UnaryResponseClientCall&>(call).HandleCompleted( |
| 65 | packet.payload(), packet.status()); |
| 66 | } |
| 67 | break; |
Alexei Frolov | d727622 | 2020-10-01 12:41:59 -0700 | [diff] [blame] | 68 | case PacketType::SERVER_ERROR: |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 69 | call.HandleError(packet.status()); |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 70 | break; |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 71 | case PacketType::SERVER_STREAM: |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 72 | if (call.has_server_stream()) { |
| 73 | call.HandlePayload(packet.payload()); |
| 74 | } else { |
| 75 | PW_LOG_DEBUG("Received SERVER_STREAM for RPC without a server stream"); |
| 76 | call.HandleError(Status::InvalidArgument()); |
| 77 | // Report the error to the server so it can abort the RPC. |
| 78 | channel->Send(Packet::ClientError(packet, Status::InvalidArgument())) |
| 79 | .IgnoreError(); // Errors are logged in Channel::Send. |
| 80 | } |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 81 | break; |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 82 | default: |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 83 | PW_LOG_WARN("pw_rpc server unable to handle packet of type %u", |
| 84 | static_cast<unsigned>(packet.type())); |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 87 | return OkStatus(); // OK since the packet was handled |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | } // namespace pw::rpc |