xref: /aosp_15_r20/external/pigweed/pw_rpc/server_call.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 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/internal/server_call.h"
16 
17 #include "pw_log/log.h"
18 
19 namespace pw::rpc::internal {
20 
HandleClientRequestedCompletion()21 void ServerCall::HandleClientRequestedCompletion()
22     PW_UNLOCK_FUNCTION(rpc_lock()) {
23   MarkStreamCompleted();
24 
25 #if PW_RPC_COMPLETION_REQUEST_CALLBACK
26   auto on_client_requested_completion_local =
27       std::move(on_client_requested_completion_);
28   CallbackStarted();
29   rpc_lock().unlock();
30 
31   if (on_client_requested_completion_local) {
32     on_client_requested_completion_local();
33   }
34 
35   rpc_lock().lock();
36   CallbackFinished();
37 #else
38   PW_LOG_WARN(
39       "Received completion request packet for %u:%08x/%08x, but completion "
40       "request callbacks are disabled (PW_RPC_COMPLETION_REQUEST_CALLBACK is "
41       "0). The client call may be waiting for an action that the server cannot "
42       "complete. The server should be compiled with completion callbacks to "
43       "support services that require them.",
44       static_cast<unsigned>(channel_id_locked()),
45       static_cast<unsigned>(service_id()),
46       static_cast<unsigned>(method_id()));
47 #endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
48   rpc_lock().unlock();
49 }
50 
MoveServerCallFrom(ServerCall & other)51 void ServerCall::MoveServerCallFrom(ServerCall& other) {
52   WaitUntilReadyForMove(*this, other);
53 
54   // If this call is active, finish it first.
55   if (active_locked()) {
56     CloseAndSendResponseLocked(OkStatus()).IgnoreError();
57   }
58 
59   MoveFrom(other);
60 
61 #if PW_RPC_COMPLETION_REQUEST_CALLBACK
62   on_client_requested_completion_ =
63       std::move(other.on_client_requested_completion_);
64 #endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
65 }
66 
67 }  // namespace pw::rpc::internal
68