aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/public/pw_rpc/internal/server_call.h
blob: f6cccb0f979e10eb9bec90a5bab748244489acf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#pragma once

#include "pw_function/function.h"
#include "pw_rpc/internal/call.h"
#include "pw_rpc/internal/config.h"
#include "pw_rpc/internal/lock.h"

namespace pw::rpc::internal {

// A Call object, as used by an RPC server.
class ServerCall : public Call {
 public:
  void HandleClientRequestedCompletion() PW_UNLOCK_FUNCTION(rpc_lock()) {
    MarkStreamCompleted();

#if PW_RPC_COMPLETION_REQUEST_CALLBACK
    auto on_client_requested_completion_local =
        std::move(on_client_requested_completion_);
    CallbackStarted();
    rpc_lock().unlock();

    if (on_client_requested_completion_local) {
      on_client_requested_completion_local();
    }

    rpc_lock().lock();
    CallbackFinished();
#endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
    rpc_lock().unlock();
  }

 protected:
  constexpr ServerCall() = default;

  ServerCall(ServerCall&& other) { *this = std::move(other); }

  ~ServerCall() { DestroyServerCall(); }

  // Version of operator= used by the raw call classes.
  ServerCall& operator=(ServerCall&& other) PW_LOCKS_EXCLUDED(rpc_lock()) {
    RpcLockGuard lock;
    MoveServerCallFrom(other);
    return *this;
  }

  void MoveServerCallFrom(ServerCall& other)
      PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());

  ServerCall(const LockedCallContext& context, CallProperties properties)
      PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock())
      : Call(context, properties) {}

  // set_on_completion_requested is templated so that it can be
  // conditionally disabled with a helpful static_assert message.
  template <typename UnusedType = void>
  void set_on_completion_requested(
      [[maybe_unused]] Function<void()>&& on_client_requested_completion)
      PW_LOCKS_EXCLUDED(rpc_lock()) {
    static_assert(cfg::kClientStreamEndCallbackEnabled<UnusedType>,
                  "The client stream end callback is disabled, so "
                  "set_on_completion_requested cannot be called. To "
                  "enable the client end "
                  "callback, set PW_RPC_REQUEST_COMPLETION_CALLBACK to 1.");
#if PW_RPC_COMPLETION_REQUEST_CALLBACK
    RpcLockGuard lock;
    on_client_requested_completion_ = std::move(on_client_requested_completion);
#endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
  }

  // Sets the provided on_client_requested_completion callback if
  // PW_RPC_COMPLETION_REQUEST_CALLBACK is defined. Unlike
  // set_on_completion_requested this API will not raise a static_assert
  // message at compile time even when the macro is not defined.
  void set_on_completion_requested_if_enabled(
      Function<void()>&& on_client_requested_completion)
      PW_LOCKS_EXCLUDED(rpc_lock()) {
#if PW_RPC_COMPLETION_REQUEST_CALLBACK
    RpcLockGuard lock;
    on_client_requested_completion_ = std::move(on_client_requested_completion);
#else
    on_client_requested_completion = nullptr;
#endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
  }

 private:
#if PW_RPC_COMPLETION_REQUEST_CALLBACK
  // Called when a client stream completes.
  Function<void()> on_client_requested_completion_ PW_GUARDED_BY(rpc_lock());
#endif  // PW_RPC_COMPLETION_REQUEST_CALLBACK
};

}  // namespace pw::rpc::internal