• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // clang-format off
16 #include "pw_rpc/internal/log_config.h"  // PW_LOG_* macros must be first.
17 
18 #include "pw_rpc/client.h"
19 // clang-format on
20 
21 #include "pw_log/log.h"
22 #include "pw_rpc/internal/client_call.h"
23 #include "pw_rpc/internal/packet.h"
24 #include "pw_status/try.h"
25 
26 namespace pw::rpc {
27 namespace {
28 
29 using internal::Packet;
30 using internal::pwpb::PacketType;
31 
32 }  // namespace
33 
ProcessPacket(ConstByteSpan data)34 Status Client::ProcessPacket(ConstByteSpan data) {
35   PW_TRY_ASSIGN(Packet packet, Endpoint::ProcessPacket(data, Packet::kClient));
36 
37   // Find an existing call for this RPC, if any.
38   internal::rpc_lock().lock();
39   IntrusiveList<internal::Call>::iterator call = FindCall(packet);
40 
41   internal::Channel* channel = GetInternalChannel(packet.channel_id());
42 
43   if (channel == nullptr) {
44     internal::rpc_lock().unlock();
45     PW_LOG_WARN("RPC client received a packet for an unregistered channel: %lu",
46                 static_cast<unsigned long>(packet.channel_id()));
47     return Status::Unavailable();
48   }
49 
50   if (call == calls_end()) {
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) {
55       channel->Send(Packet::ClientError(packet, Status::FailedPrecondition()))
56           .IgnoreError();
57       PW_LOG_WARN("RPC client received stream message for an unknown call");
58     }
59     internal::rpc_lock().unlock();
60     return OkStatus();  // OK since the packet was handled
61   }
62 
63   switch (packet.type()) {
64     case PacketType::RESPONSE:
65       // RPCs without a server stream include a payload with the final packet.
66       if (call->has_server_stream()) {
67         static_cast<internal::StreamResponseClientCall&>(*call).HandleCompleted(
68             packet.status());
69       } else {
70         static_cast<internal::UnaryResponseClientCall&>(*call).HandleCompleted(
71             packet.payload(), packet.status());
72       }
73       break;
74     case PacketType::SERVER_ERROR:
75       call->HandleError(packet.status());
76       break;
77     case PacketType::SERVER_STREAM:
78       if (call->has_server_stream()) {
79         call->HandlePayload(packet.payload());
80       } else {
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.
84         call->HandleError(Status::InvalidArgument());
85         PW_LOG_DEBUG("Received SERVER_STREAM for RPC without a server stream");
86       }
87       break;
88 
89     case PacketType::REQUEST:
90     case PacketType::CLIENT_STREAM:
91     case PacketType::CLIENT_ERROR:
92     case PacketType::CLIENT_REQUEST_COMPLETION:
93     default:
94       internal::rpc_lock().unlock();
95       PW_LOG_WARN("pw_rpc client unable to handle packet of type %u",
96                   static_cast<unsigned>(packet.type()));
97   }
98 
99   return OkStatus();  // OK since the packet was handled
100 }
101 
102 }  // namespace pw::rpc
103