• 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 
15 // This file defines the ServerReaderWriter, ServerReader, and ServerWriter
16 // classes for the raw RPC interface. These classes are used for bidirectional,
17 // client, and server streaming RPCs.
18 #pragma once
19 
20 #include "pw_bytes/span.h"
21 #include "pw_rpc/channel.h"
22 #include "pw_rpc/internal/client_call.h"
23 #include "pw_rpc/writer.h"
24 
25 namespace pw::rpc {
26 
27 // Sends requests and handles responses for a bidirectional streaming RPC.
28 //
29 // These classes use private inheritance to hide the internal::Call API while
30 // allow direct use of its public and protected functions.
31 class RawClientReaderWriter : private internal::StreamResponseClientCall {
32  public:
33   constexpr RawClientReaderWriter() = default;
34 
35   RawClientReaderWriter(RawClientReaderWriter&&) = default;
36   RawClientReaderWriter& operator=(RawClientReaderWriter&&) = default;
37 
38   using internal::Call::active;
39   using internal::Call::channel_id;
40 
41   // Functions for setting the callbacks.
42   using internal::StreamResponseClientCall::set_on_completed;
43   using internal::StreamResponseClientCall::set_on_error;
44   using internal::StreamResponseClientCall::set_on_next;
45 
46   // Sends a stream request packet with the given raw payload.
47   using internal::Call::Write;
48 
49   // Notifies the server that no further client stream messages will be sent.
50   using internal::Call::CloseClientStream;
51 
52   // Cancels this RPC.
53   using internal::Call::Cancel;
54 
55   // Allow use as a generic RPC Writer.
56   using internal::Call::operator Writer&;
57   using internal::Call::operator const Writer&;
58 
59  protected:
60   friend class internal::StreamResponseClientCall;
61 
62   RawClientReaderWriter(internal::Endpoint& client,
63                         uint32_t channel_id,
64                         uint32_t service_id,
65                         uint32_t method_id,
66                         MethodType type = MethodType::kBidirectionalStreaming)
StreamResponseClientCall(client,channel_id,service_id,method_id,type)67       : StreamResponseClientCall(
68             client, channel_id, service_id, method_id, type) {}
69 };
70 
71 // Handles responses for a server streaming RPC.
72 class RawClientReader : private internal::StreamResponseClientCall {
73  public:
74   constexpr RawClientReader() = default;
75 
76   RawClientReader(RawClientReader&&) = default;
77   RawClientReader& operator=(RawClientReader&&) = default;
78 
79   using internal::StreamResponseClientCall::active;
80   using internal::StreamResponseClientCall::channel_id;
81 
82   using internal::StreamResponseClientCall::set_on_completed;
83   using internal::StreamResponseClientCall::set_on_error;
84   using internal::StreamResponseClientCall::set_on_next;
85 
86   using internal::Call::Cancel;
87 
88  private:
89   friend class internal::StreamResponseClientCall;
90 
RawClientReader(internal::Endpoint & client,uint32_t channel_id,uint32_t service_id,uint32_t method_id)91   RawClientReader(internal::Endpoint& client,
92                   uint32_t channel_id,
93                   uint32_t service_id,
94                   uint32_t method_id)
95       : StreamResponseClientCall(client,
96                                  channel_id,
97                                  service_id,
98                                  method_id,
99                                  MethodType::kServerStreaming) {}
100 };
101 
102 // Sends requests and handles the response for a client streaming RPC.
103 class RawClientWriter : private internal::UnaryResponseClientCall {
104  public:
105   constexpr RawClientWriter() = default;
106 
107   RawClientWriter(RawClientWriter&&) = default;
108   RawClientWriter& operator=(RawClientWriter&&) = default;
109 
110   using internal::UnaryResponseClientCall::active;
111   using internal::UnaryResponseClientCall::channel_id;
112 
113   using internal::UnaryResponseClientCall::set_on_completed;
114   using internal::UnaryResponseClientCall::set_on_error;
115 
116   using internal::Call::Cancel;
117   using internal::Call::CloseClientStream;
118   using internal::Call::Write;
119 
120   // Allow use as a generic RPC Writer.
121   using internal::Call::operator Writer&;
122   using internal::Call::operator const Writer&;
123 
124  private:
125   friend class internal::UnaryResponseClientCall;
126 
RawClientWriter(internal::Endpoint & client,uint32_t channel_id,uint32_t service_id,uint32_t method_id)127   RawClientWriter(internal::Endpoint& client,
128                   uint32_t channel_id,
129                   uint32_t service_id,
130                   uint32_t method_id)
131       : UnaryResponseClientCall(client,
132                                 channel_id,
133                                 service_id,
134                                 method_id,
135                                 MethodType::kClientStreaming) {}
136 };
137 
138 // Handles the response for to unary RPC.
139 class RawUnaryReceiver : private internal::UnaryResponseClientCall {
140  public:
141   constexpr RawUnaryReceiver() = default;
142 
143   RawUnaryReceiver(RawUnaryReceiver&&) = default;
144   RawUnaryReceiver& operator=(RawUnaryReceiver&&) = default;
145 
146   using internal::UnaryResponseClientCall::active;
147   using internal::UnaryResponseClientCall::channel_id;
148 
149   using internal::UnaryResponseClientCall::set_on_completed;
150   using internal::UnaryResponseClientCall::set_on_error;
151 
152   using internal::UnaryResponseClientCall::Cancel;
153 
154  private:
155   friend class internal::UnaryResponseClientCall;
156 
RawUnaryReceiver(internal::Endpoint & client,uint32_t channel_id,uint32_t service_id,uint32_t method_id)157   RawUnaryReceiver(internal::Endpoint& client,
158                    uint32_t channel_id,
159                    uint32_t service_id,
160                    uint32_t method_id)
161       : UnaryResponseClientCall(
162             client, channel_id, service_id, method_id, MethodType::kUnary) {}
163 };
164 
165 }  // namespace pw::rpc
166