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/server.cc b/pw_rpc/server.cc
index a96a78f..f1a352e 100644
--- a/pw_rpc/server.cc
+++ b/pw_rpc/server.cc
@@ -33,16 +33,13 @@
 
 Status Server::ProcessPacket(std::span<const byte> data,
                              ChannelOutput& interface) {
-  internal::Call* base;
-  Result<Packet> result = Endpoint::ProcessPacket(data, Packet::kServer, base);
-  internal::ServerCall* const call = static_cast<internal::ServerCall*>(base);
-
-  if (!result.ok()) {
-    return result.status();
-  }
-
+  PW_TRY_ASSIGN(Result<Packet> result,
+                Endpoint::ProcessPacket(data, Packet::kServer));
   Packet& packet = *result;
 
+  internal::ServerCall* const call =
+      static_cast<internal::ServerCall*>(FindCall(packet));
+
   // Verbose log for debugging.
   // PW_LOG_DEBUG("RPC server received packet type %u for %u:%08x/%08x",
   //              static_cast<unsigned>(packet.type()),
@@ -96,7 +93,9 @@
       }
       break;
     case PacketType::CANCEL:
-      HandleCancelPacket(packet, *channel, call);
+      if (call != nullptr && call->id() == packet.call_id()) {
+        call->HandleError(Status::Cancelled());
+      }
       break;
     case PacketType::CLIENT_STREAM_END:
       HandleClientStreamPacket(packet, *channel, call);
@@ -105,7 +104,8 @@
       PW_LOG_WARN("pw_rpc server unable to handle packet of type %u",
                   unsigned(packet.type()));
   }
-  return OkStatus();
+
+  return OkStatus();  // OK since the packet was handled
 }
 
 std::tuple<Service*, const internal::Method*> Server::FindMethod(
@@ -155,16 +155,4 @@
   }
 }
 
-void Server::HandleCancelPacket(const Packet& packet,
-                                internal::Channel& channel,
-                                internal::ServerCall* call) const {
-  if (call == nullptr || call->id() != packet.call_id()) {
-    channel.Send(Packet::ServerError(packet, Status::FailedPrecondition()))
-        .IgnoreError();  // TODO(pwbug/387): Handle Status properly
-    PW_LOG_DEBUG("Received CANCEL packet for method that is not pending");
-  } else {
-    call->HandleError(Status::Cancelled());
-  }
-}
-
 }  // namespace pw::rpc