• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
15 
16 #include "pw_function/function.h"
17 #include "pw_rpc/internal/call.h"
18 #include "pw_rpc/internal/config.h"
19 #include "pw_rpc/internal/lock.h"
20 
21 namespace pw::rpc::internal {
22 
23 // A Call object, as used by an RPC server.
24 class ServerCall : public Call {
25  public:
HandleClientRequestedCompletion()26   void HandleClientRequestedCompletion() PW_UNLOCK_FUNCTION(rpc_lock()) {
27     MarkStreamCompleted();
28 
29 #if PW_RPC_COMPLETION_REQUEST_CALLBACK
30     auto on_client_requested_completion_local =
31         std::move(on_client_requested_completion_);
32     CallbackStarted();
33     rpc_lock().unlock();
34 
35     if (on_client_requested_completion_local) {
36       on_client_requested_completion_local();
37     }
38 
39     rpc_lock().lock();
40     CallbackFinished();
41 #endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
42     rpc_lock().unlock();
43   }
44 
45  protected:
46   constexpr ServerCall() = default;
47 
ServerCall(ServerCall && other)48   ServerCall(ServerCall&& other) { *this = std::move(other); }
49 
~ServerCall()50   ~ServerCall() { DestroyServerCall(); }
51 
52   // Version of operator= used by the raw call classes.
PW_LOCKS_EXCLUDED(rpc_lock ())53   ServerCall& operator=(ServerCall&& other) PW_LOCKS_EXCLUDED(rpc_lock()) {
54     RpcLockGuard lock;
55     MoveServerCallFrom(other);
56     return *this;
57   }
58 
59   void MoveServerCallFrom(ServerCall& other)
60       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
61 
ServerCall(const LockedCallContext & context,CallProperties properties)62   ServerCall(const LockedCallContext& context, CallProperties properties)
63       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock())
64       : Call(context, properties) {}
65 
66   // set_on_completion_requested is templated so that it can be
67   // conditionally disabled with a helpful static_assert message.
68   template <typename UnusedType = void>
set_on_completion_requested(Function<void ()> && on_client_requested_completion)69   void set_on_completion_requested(
70       [[maybe_unused]] Function<void()>&& on_client_requested_completion)
71       PW_LOCKS_EXCLUDED(rpc_lock()) {
72     static_assert(cfg::kClientStreamEndCallbackEnabled<UnusedType>,
73                   "The client stream end callback is disabled, so "
74                   "set_on_completion_requested cannot be called. To "
75                   "enable the client end "
76                   "callback, set PW_RPC_COMPLETION_REQUEST_CALLBACK to 1.");
77 #if PW_RPC_COMPLETION_REQUEST_CALLBACK
78     RpcLockGuard lock;
79     on_client_requested_completion_ = std::move(on_client_requested_completion);
80 #endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
81   }
82 
83   // Sets the provided on_client_requested_completion callback if
84   // PW_RPC_COMPLETION_REQUEST_CALLBACK is defined. Unlike
85   // set_on_completion_requested this API will not raise a static_assert
86   // message at compile time even when the macro is not defined.
set_on_completion_requested_if_enabled(Function<void ()> && on_client_requested_completion)87   void set_on_completion_requested_if_enabled(
88       Function<void()>&& on_client_requested_completion)
89       PW_LOCKS_EXCLUDED(rpc_lock()) {
90 #if PW_RPC_COMPLETION_REQUEST_CALLBACK
91     RpcLockGuard lock;
92     on_client_requested_completion_ = std::move(on_client_requested_completion);
93 #else
94     on_client_requested_completion = nullptr;
95 #endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
96   }
97 
98  private:
99 #if PW_RPC_COMPLETION_REQUEST_CALLBACK
100   // Called when a client stream completes.
101   Function<void()> on_client_requested_completion_ PW_GUARDED_BY(rpc_lock());
102 #endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
103 };
104 
105 }  // namespace pw::rpc::internal
106