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: 26 void HandleClientRequestedCompletion() PW_UNLOCK_FUNCTION(rpc_lock()); 27 28 protected: 29 constexpr ServerCall() = default; 30 ServerCall(ServerCall && other)31 ServerCall(ServerCall&& other) { *this = std::move(other); } 32 ~ServerCall()33 ~ServerCall() { DestroyServerCall(); } 34 35 // Version of operator= used by the raw call classes. PW_LOCKS_EXCLUDED(rpc_lock ())36 ServerCall& operator=(ServerCall&& other) PW_LOCKS_EXCLUDED(rpc_lock()) { 37 RpcLockGuard lock; 38 MoveServerCallFrom(other); 39 return *this; 40 } 41 42 void MoveServerCallFrom(ServerCall& other) 43 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()); 44 ServerCall(const LockedCallContext & context,CallProperties properties)45 ServerCall(const LockedCallContext& context, CallProperties properties) 46 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) 47 : Call(context, properties) {} 48 49 // set_on_completion_requested is templated so that it can be 50 // conditionally disabled with a helpful static_assert message. 51 template <typename UnusedType = void> set_on_completion_requested(Function<void ()> && on_client_requested_completion)52 void set_on_completion_requested( 53 [[maybe_unused]] Function<void()>&& on_client_requested_completion) 54 PW_LOCKS_EXCLUDED(rpc_lock()) { 55 static_assert(cfg::kClientStreamEndCallbackEnabled<UnusedType>, 56 "The client stream end callback is disabled, so " 57 "set_on_completion_requested cannot be called. To " 58 "enable the client end " 59 "callback, set PW_RPC_COMPLETION_REQUEST_CALLBACK to 1."); 60 #if PW_RPC_COMPLETION_REQUEST_CALLBACK 61 RpcLockGuard lock; 62 on_client_requested_completion_ = std::move(on_client_requested_completion); 63 #endif // PW_RPC_COMPLETION_REQUEST_CALLBACK 64 } 65 66 // Sets the provided on_client_requested_completion callback if 67 // PW_RPC_COMPLETION_REQUEST_CALLBACK is defined. Unlike 68 // set_on_completion_requested this API will not raise a static_assert 69 // message at compile time even when the macro is not defined. set_on_completion_requested_if_enabled(Function<void ()> && on_client_requested_completion)70 void set_on_completion_requested_if_enabled( 71 Function<void()>&& on_client_requested_completion) 72 PW_LOCKS_EXCLUDED(rpc_lock()) { 73 #if PW_RPC_COMPLETION_REQUEST_CALLBACK 74 RpcLockGuard lock; 75 on_client_requested_completion_ = std::move(on_client_requested_completion); 76 #else 77 on_client_requested_completion = nullptr; 78 #endif // PW_RPC_COMPLETION_REQUEST_CALLBACK 79 } 80 81 private: 82 #if PW_RPC_COMPLETION_REQUEST_CALLBACK 83 // Called when a client stream completes. 84 Function<void()> on_client_requested_completion_ PW_GUARDED_BY(rpc_lock()); 85 #endif // PW_RPC_COMPLETION_REQUEST_CALLBACK 86 }; 87 88 } // namespace pw::rpc::internal 89