xref: /aosp_15_r20/external/pigweed/pw_rpc/public/pw_rpc/writer.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 <cstdint>
17 
18 #include "pw_bytes/span.h"
19 #include "pw_function/function.h"
20 #include "pw_rpc/internal/lock.h"
21 #include "pw_status/status.h"
22 #include "pw_status/status_with_size.h"
23 
24 namespace pw::rpc {
25 namespace internal {
26 class Call;
27 }
28 
29 // The Writer class allows writing requests or responses to a streaming RPC.
30 // ClientWriter, ClientReaderWriter, ServerWriter, and ServerReaderWriter
31 // classes can be used as a generic Writer.
32 class Writer {
33  public:
34   Writer(const Writer&) = delete;
35   Writer(Writer&&) = delete;
36 
37   Writer& operator=(const Writer&) = delete;
38   Writer& operator=(Writer&&) = delete;
39 
40   bool active() const;
41   uint32_t channel_id() const;
42 
43   Status Write(ConstByteSpan payload) PW_LOCKS_EXCLUDED(internal::rpc_lock());
44   Status Write(const Function<StatusWithSize(ByteSpan)>& callback)
45       PW_LOCKS_EXCLUDED(internal::rpc_lock());
46 
47  private:
48   // Only allow Call to inherit from Writer. This guarantees that Writers can
49   // always safely downcast to Call.
50   friend class internal::Call;
51 
52   // Writers cannot be created directly. They may only be used as a reference to
53   // an existing call object.
54   constexpr Writer() = default;
55 };
56 
57 }  // namespace pw::rpc
58