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: HandleClientStreamEnd()26 void HandleClientStreamEnd() PW_UNLOCK_FUNCTION(rpc_lock()) { 27 MarkClientStreamCompleted(); 28 // TODO(pwbug/597): Ensure on_client_stream_end_ is properly guarded. 29 rpc_lock().unlock(); 30 31 #if PW_RPC_CLIENT_STREAM_END_CALLBACK 32 if (on_client_stream_end_) { 33 on_client_stream_end_(); 34 } 35 #endif // PW_RPC_CLIENT_STREAM_END_CALLBACK 36 } 37 38 protected: 39 constexpr ServerCall() = default; 40 ServerCall(ServerCall && other)41 ServerCall(ServerCall&& other) { *this = std::move(other); } 42 ~ServerCall()43 ~ServerCall() { 44 // Any errors are logged in Channel::Send. 45 CloseAndSendResponse(OkStatus()).IgnoreError(); 46 } 47 48 // Version of operator= used by the raw call classes. PW_LOCKS_EXCLUDED(rpc_lock ())49 ServerCall& operator=(ServerCall&& other) PW_LOCKS_EXCLUDED(rpc_lock()) { 50 LockGuard lock(rpc_lock()); 51 MoveServerCallFrom(other); 52 return *this; 53 } 54 55 void MoveServerCallFrom(ServerCall& other) 56 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()); 57 ServerCall(const CallContext & context,MethodType type)58 ServerCall(const CallContext& context, MethodType type) 59 : Call(context, type) {} 60 61 // set_on_client_stream_end is templated so that it can be conditionally 62 // disabled with a helpful static_assert message. 63 template <typename UnusedType = void> set_on_client_stream_end(Function<void ()> && on_client_stream_end)64 void set_on_client_stream_end( 65 [[maybe_unused]] Function<void()>&& on_client_stream_end) { 66 // TODO(pwbug/597): Ensure on_client_stream_end_ is properly guarded. 67 static_assert( 68 cfg::kClientStreamEndCallbackEnabled<UnusedType>, 69 "The client stream end callback is disabled, so " 70 "set_on_client_stream_end cannot be called. To enable the client end " 71 "callback, set PW_RPC_CLIENT_STREAM_END_CALLBACK to 1."); 72 #if PW_RPC_CLIENT_STREAM_END_CALLBACK 73 on_client_stream_end_ = std::move(on_client_stream_end); 74 #endif // PW_RPC_CLIENT_STREAM_END_CALLBACK 75 } 76 77 private: 78 #if PW_RPC_CLIENT_STREAM_END_CALLBACK 79 // Called when a client stream completes. 80 Function<void()> on_client_stream_end_; 81 #endif // PW_RPC_CLIENT_STREAM_END_CALLBACK 82 }; 83 84 } // namespace pw::rpc::internal 85