userver: /data/code/userver/libraries/grpc-proto-structs/include/userver/grpc-proto-structs/client/stream.hpp Source File
Loading...
Searching...
No Matches
stream.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief Structs Reader/Writer stream wrappers over Vanilla streams.
5
6#include <userver/proto-structs/convert.hpp>
7#include <userver/ugrpc/client/stream.hpp>
8#include <userver/utils/box.hpp>
9
10#include <userver/grpc-proto-structs/client/stream_read_future.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace grpc_proto_structs::client {
15
16/// @brief proto-struct based Reader adapter.
17template <typename StructsResponse>
18class Reader final {
19public:
20 using ProtobufResponse = proto_structs::traits::CompatibleMessageType<StructsResponse>;
21 using ProtobufReader = ugrpc::client::Reader<ProtobufResponse>;
22
23 explicit Reader(ProtobufReader&& reader) : reader_{std::move(reader)} {}
24
25 Reader(Reader&&) = default;
26 Reader& operator=(Reader&&) = default;
27
28 /// @brief Await and read the next incoming message.
29 ///
30 /// Read protobuf message corresponding to Response with @ref ugrpc::client::Reader::Read
31 /// and construct response from it.
32 [[nodiscard]] std::optional<StructsResponse> Read() {
33 ProtobufResponse message;
34 if (reader_.Read(message)) {
35 StructsResponse response;
36 proto_structs::MessageToStruct(message, response);
37 return {response};
38 }
39 return std::nullopt;
40 ;
41 }
42
43 /// @brief Get call context, useful e.g. for accessing metadata.
44 ///
45 /// @see @ref ugrpc::client::Reader::GetContext.
46 ugrpc::client::CallContext& GetContext() { return reader_.GetContext(); }
47
48 /// @overload
49 ///
50 /// @see @ref ugrpc::client::Reader::GetContext.
51 const ugrpc::client::CallContext& GetContext() const { return reader_.GetContext(); }
52
53private:
54 ProtobufReader reader_;
55};
56
57/// @brief proto-struct based Writer adapter.
58template <typename StructsRequest, typename StructsResponse>
59class Writer final {
60public:
61 using ProtobufResponse = proto_structs::traits::CompatibleMessageType<StructsResponse>;
62 using ProtobufRequest = proto_structs::traits::CompatibleMessageType<StructsRequest>;
63
64 using ProtobufWriter = ugrpc::client::Writer<ProtobufRequest, ProtobufResponse>;
65
66 explicit Writer(ProtobufWriter&& writer) : writer_{std::move(writer)} {}
67
68 Writer(Writer&&) = default;
69 Writer& operator=(Writer&&) = default;
70
71 /// @brief Write the next outgoing message
72 [[nodiscard]] bool Write(const StructsRequest& request) {
73 return writer_.Write(proto_structs::StructToMessage(request));
74 }
75
76 /// @brief Write the next outgoing message
77 [[nodiscard]] bool Write(StructsRequest&& request) {
78 return writer_.Write(proto_structs::StructToMessage(std::move(request)));
79 }
80
81 /// @brief Write the next outgoing message and check result
82 void WriteAndCheck(const StructsRequest& request) {
83 writer_.WriteAndCheck(proto_structs::StructToMessage(request));
84 }
85
86 /// @brief Write the next outgoing message and check result
87 void WriteAndCheck(StructsRequest&& request) {
88 writer_.WriteAndCheck(proto_structs::StructToMessage(std::move(request)));
89 }
90
91 /// @brief Complete the RPC successfully
92 StructsResponse Finish() { return writer_.Finish(); }
93
94 /// @brief Get call context, useful e.g. for accessing metadata.
95 ugrpc::client::CallContext& GetContext() { return writer_.GetContext(); }
96
97 /// @overload
98 const ugrpc::client::CallContext& GetContext() const { return writer_.GetContext(); }
99
100private:
101 ProtobufWriter writer_;
102};
103
104/// @brief proto-struct based ReaderWriter adapter.
105template <typename StructsRequest, typename StructsResponse>
106class ReaderWriter final {
107public:
108 using ProtobufResponse = proto_structs::traits::CompatibleMessageType<StructsResponse>;
109 using ProtobufRequest = proto_structs::traits::CompatibleMessageType<StructsRequest>;
110
111 using ProtobufReaderWriter = ugrpc::client::ReaderWriter<ProtobufRequest, ProtobufResponse>;
112
113 explicit ReaderWriter(ProtobufReaderWriter&& reader_writer) : reader_writer_{std::move(reader_writer)} {}
114
115 ReaderWriter(ReaderWriter&&) = default;
116 ReaderWriter& operator=(ReaderWriter&&) = default;
117
118 /// @brief Await and read the next incoming message.
119 ///
120 /// @see @ref ugrpc::client::ReaderWriter::Read.
121 [[nodiscard]] std::optional<StructsResponse> Read() {
122 ProtobufResponse message;
123 if (reader_writer_.Read(message)) {
124 StructsResponse response;
125 proto_structs::MessageToStruct(message, response);
126 return {response};
127 }
128 return std::nullopt;
129 }
130
131 /// @brief Return future to read next incoming result.
132 StreamReadFuture<StructsResponse> ReadAsync() { return {reader_writer_.ReadAsync(*response_), *response_}; }
133
134 /// @brief Write the next outgoing message.
135 ///
136 /// @see @ref ugrpc::client::ReaderWriter::Write.
137 [[nodiscard]] bool Write(const StructsRequest& request) {
138 return reader_writer_.Write(proto_structs::StructToMessage(request));
139 }
140
141 /// @brief Write the next outgoing message.
142 ///
143 /// @see @ref ugrpc::client::ReaderWriter::Write.
144 [[nodiscard]] bool Write(StructsRequest&& request) {
145 return reader_writer_.Write(proto_structs::StructToMessage(std::move(request)));
146 }
147
148 /// @brief Write the next outgoing message and check result.
149 ///
150 /// @see @ref ugrpc::client::ReaderWriter::WriteAndCheck.
151 void WriteAndCheck(const StructsRequest& request) {
152 reader_writer_.WriteAndCheck(proto_structs::StructToMessage(request));
153 }
154
155 /// @brief Write the next outgoing message and check result.
156 ///
157 /// @see @ref ugrpc::client::ReaderWriter::WriteAndCheck.
158 void WriteAndCheck(StructsRequest&& request) {
159 reader_writer_.WriteAndCheck(proto_structs::StructToMessage(std::move(request)));
160 }
161
162 /// @brief Announce end-of-output to the server.
163 ///
164 /// @see @ref ugrpc::client::ReaderWriter::WritesDone.
165 [[nodiscard]] bool WritesDone() { return reader_writer_.WritesDone(); }
166
167 /// @brief Get call context, useful e.g. for accessing metadata.
168 ///
169 /// @see @ref ugrpc::client::ReaderWriter::GetContext.
170 ugrpc::client::CallContext& GetContext() { return reader_writer_.GetContext(); }
171
172 /// @overload
173 ///
174 /// @see @ref ugrpc::client::ReaderWriter::GetContext.
175 const ugrpc::client::CallContext& GetContext() const { return reader_writer_.GetContext(); }
176
177private:
178 ProtobufReaderWriter reader_writer_;
179 utils::Box<ProtobufResponse> response_;
180};
181
182} // namespace grpc_proto_structs::client
183
184USERVER_NAMESPACE_END