pw_rpc: Move server/client calls to separate files

- Move the server/client calls to improve code organization and make
  upcoming lock annotations simpler, since the Endpoint class is fully
  defined in the server/client call headers.
- No longer have the server respond to a cancel for an unknown call.
  There is no point in sending an error for that RPC to the client
  because the client cancelled it and won't know about that call by the
  time it gets the error. In general, error packets should only be sent
  when an RPC fails to start or needs to be terminated.
- Use PW_TRY_ASSIGN in the server and client.

Change-Id: I467d06ef7139a5698be0b30e9ed1bac3a625e02e
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/66201
Commit-Queue: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Reviewed-by: Alexei Frolov <frolv@google.com>
diff --git a/pw_rpc/client.cc b/pw_rpc/client.cc
index eb5c6d8..35856a8 100644
--- a/pw_rpc/client.cc
+++ b/pw_rpc/client.cc
@@ -15,6 +15,7 @@
 #include "pw_rpc/client.h"
 
 #include "pw_log/log.h"
+#include "pw_rpc/internal/client_call.h"
 #include "pw_rpc/internal/packet.h"
 #include "pw_status/try.h"
 
@@ -27,20 +28,21 @@
 }  // namespace
 
 Status Client::ProcessPacket(ConstByteSpan data) {
-  internal::Call* base;
-  Result<Packet> result = Endpoint::ProcessPacket(data, Packet::kClient, base);
-
-  PW_TRY(result.status());
-
+  PW_TRY_ASSIGN(Result<Packet> result,
+                Endpoint::ProcessPacket(data, Packet::kClient));
   Packet& packet = *result;
 
+  // Find an existing call for this RPC, if any.
+  internal::ClientCall* call =
+      static_cast<internal::ClientCall*>(FindCall(packet));
+
   internal::Channel* channel = GetInternalChannel(packet.channel_id());
   if (channel == nullptr) {
     PW_LOG_WARN("RPC client received a packet for an unregistered channel");
     return Status::Unavailable();
   }
 
-  if (base == nullptr || base->id() != packet.call_id()) {
+  if (call == nullptr || call->id() != packet.call_id()) {
     // The call for the packet does not exist. If the packet is a server stream
     // message, notify the server so that it can kill the stream. Otherwise,
     // silently drop the packet (as it would terminate the RPC anyway).
@@ -52,35 +54,33 @@
     return OkStatus();  // OK since the packet was handled
   }
 
-  internal::ClientCall& call = *static_cast<internal::ClientCall*>(base);
-
   switch (packet.type()) {
     case PacketType::RESPONSE:
       // RPCs without a server stream include a payload with the final packet.
-      if (call.has_server_stream()) {
-        static_cast<internal::StreamResponseClientCall&>(call).HandleCompleted(
+      if (call->has_server_stream()) {
+        static_cast<internal::StreamResponseClientCall&>(*call).HandleCompleted(
             packet.status());
       } else {
-        static_cast<internal::UnaryResponseClientCall&>(call).HandleCompleted(
+        static_cast<internal::UnaryResponseClientCall&>(*call).HandleCompleted(
             packet.payload(), packet.status());
       }
       break;
     case PacketType::SERVER_ERROR:
-      call.HandleError(packet.status());
+      call->HandleError(packet.status());
       break;
     case PacketType::SERVER_STREAM:
-      if (call.has_server_stream()) {
-        call.HandlePayload(packet.payload());
+      if (call->has_server_stream()) {
+        call->HandlePayload(packet.payload());
       } else {
         PW_LOG_DEBUG("Received SERVER_STREAM for RPC without a server stream");
-        call.HandleError(Status::InvalidArgument());
+        call->HandleError(Status::InvalidArgument());
         // Report the error to the server so it can abort the RPC.
         channel->Send(Packet::ClientError(packet, Status::InvalidArgument()))
             .IgnoreError();  // Errors are logged in Channel::Send.
       }
       break;
     default:
-      PW_LOG_WARN("pw_rpc server unable to handle packet of type %u",
+      PW_LOG_WARN("pw_rpc client unable to handle packet of type %u",
                   static_cast<unsigned>(packet.type()));
   }