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