userver
C++ Async Framework
Toggle main menu visibility
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
12
USERVER_NAMESPACE_BEGIN
13
14
namespace
grpc_proto_structs::client {
15
16
/// @brief proto-struct based Reader adapter.
17
template
<
typename
StructsResponse>
18
class
Reader final {
19
public
:
20
using
ProtobufResponse = proto_structs::traits::CompatibleMessageType<StructsResponse>;
21
using
ProtobufReader = ugrpc::client::Reader<ProtobufResponse>;
22
23
explicit
Reader(ProtobufReader&& reader)
24
: reader_{std::move(reader)}
25
{}
26
27
Reader(Reader&&) =
default
;
28
Reader& operator=(Reader&&) =
default
;
29
30
/// @brief Await and read the next incoming message.
31
///
32
/// Read protobuf message corresponding to Response with @ref ugrpc::client::Reader::Read
33
/// and construct response from it.
34
[[nodiscard]]
std
::
optional
<
StructsResponse
>
Read
() {
35
ProtobufResponse message;
36
if
(reader_.Read(message)) {
37
StructsResponse response;
38
proto_structs::MessageToStruct(message, response);
39
return
{response};
40
}
41
return
std::nullopt;
42
;
43
}
44
45
/// @brief Get call context, useful e.g. for accessing metadata.
46
///
47
/// @see @ref ugrpc::client::Reader::GetContext.
48
ugrpc
::
client
::
CallContext
&
GetContext
() {
return
reader_.GetContext(); }
49
50
/// @overload
51
///
52
/// @see @ref ugrpc::client::Reader::GetContext.
53
const
ugrpc
::
client
::
CallContext
&
GetContext
()
const
{
return
reader_.GetContext(); }
54
55
private
:
56
ProtobufReader reader_;
57
};
58
59
/// @brief proto-struct based Writer adapter.
60
template
<
typename
StructsRequest,
typename
StructsResponse>
61
class
Writer final {
62
public
:
63
using
ProtobufResponse = proto_structs::traits::CompatibleMessageType<StructsResponse>;
64
using
ProtobufRequest = proto_structs::traits::CompatibleMessageType<StructsRequest>;
65
66
using
ProtobufWriter = ugrpc::client::Writer<ProtobufRequest, ProtobufResponse>;
67
68
explicit
Writer(ProtobufWriter&& writer)
69
: writer_{std::move(writer)}
70
{}
71
72
Writer(Writer&&) =
default
;
73
Writer& operator=(Writer&&) =
default
;
74
75
/// @brief Write the next outgoing message.
76
/// @note This version may move some fields from the request.
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.
82
/// @note This version preserves the original request object by copying necessary data.
83
[[nodiscard]]
bool
WriteCopy
(
const
StructsRequest& request) {
84
return
writer_.Write(proto_structs::StructToMessage(request));
85
}
86
87
/// @brief Write the next outgoing message and check result.
88
/// @note This version may move some fields from the request.
89
void
WriteAndCheck
(StructsRequest&& request) {
90
writer_.WriteAndCheck(proto_structs::StructToMessage(std::move(request)));
91
}
92
93
/// @brief Write the next outgoing message and check result.
94
/// @note This version preserves the original request object by copying necessary data.
95
void
WriteCopyAndCheck
(
const
StructsRequest& request) {
96
writer_.WriteAndCheck(proto_structs::StructToMessage(request));
97
}
98
99
/// @brief Complete the RPC successfully
100
StructsResponse
Finish
() {
return
proto_structs::MessageToStruct<StructsResponse>(writer_.Finish()); }
101
102
/// @brief Get call context, useful e.g. for accessing metadata.
103
ugrpc
::
client
::
CallContext
&
GetContext
() {
return
writer_.GetContext(); }
104
105
/// @overload
106
const
ugrpc
::
client
::
CallContext
&
GetContext
()
const
{
return
writer_.GetContext(); }
107
108
private
:
109
ProtobufWriter writer_;
110
};
111
112
/// @brief proto-struct based ReaderWriter adapter.
113
template
<
typename
StructsRequest,
typename
StructsResponse>
114
class
ReaderWriter final {
115
public
:
116
using
ProtobufResponse = proto_structs::traits::CompatibleMessageType<StructsResponse>;
117
using
ProtobufRequest = proto_structs::traits::CompatibleMessageType<StructsRequest>;
118
119
using
ProtobufReaderWriter = ugrpc::client::ReaderWriter<ProtobufRequest, ProtobufResponse>;
120
121
explicit
ReaderWriter(ProtobufReaderWriter&& reader_writer)
122
: reader_writer_{std::move(reader_writer)}
123
{}
124
125
ReaderWriter(ReaderWriter&&) =
default
;
126
ReaderWriter& operator=(ReaderWriter&&) =
default
;
127
128
/// @brief Await and read the next incoming message.
129
///
130
/// @see @ref ugrpc::client::ReaderWriter::Read.
131
[[nodiscard]]
std
::
optional
<
StructsResponse
>
Read
() {
132
ProtobufResponse message;
133
if
(reader_writer_.Read(message)) {
134
StructsResponse response;
135
proto_structs::MessageToStruct(message, response);
136
return
{response};
137
}
138
return
std::nullopt;
139
}
140
141
/// @brief Return future to read next incoming result.
142
StreamReadFuture
<
StructsResponse
>
ReadAsync
() {
143
return
StreamReadFuture{reader_writer_.ReadAsync(*response_), *response_};
144
}
145
146
/// @brief Write the next outgoing message.
147
///
148
/// @see @ref ugrpc::client::ReaderWriter::Write.
149
[[nodiscard]]
bool
Write
(
const
StructsRequest& request) {
150
return
reader_writer_.Write(proto_structs::StructToMessage(request));
151
}
152
153
/// @brief Write the next outgoing message.
154
///
155
/// @see @ref ugrpc::client::ReaderWriter::Write.
156
[[nodiscard]]
bool
Write
(StructsRequest&& request) {
157
return
reader_writer_.Write(proto_structs::StructToMessage(std::move(request)));
158
}
159
160
/// @brief Write the next outgoing message and check result.
161
///
162
/// @see @ref ugrpc::client::ReaderWriter::WriteAndCheck.
163
void
WriteAndCheck
(
const
StructsRequest& request) {
164
reader_writer_.WriteAndCheck(proto_structs::StructToMessage(request));
165
}
166
167
/// @brief Write the next outgoing message and check result.
168
///
169
/// @see @ref ugrpc::client::ReaderWriter::WriteAndCheck.
170
void
WriteAndCheck
(StructsRequest&& request) {
171
reader_writer_.WriteAndCheck(proto_structs::StructToMessage(std::move(request)));
172
}
173
174
/// @brief Announce end-of-output to the server.
175
///
176
/// @see @ref ugrpc::client::ReaderWriter::WritesDone.
177
[[nodiscard]]
bool
WritesDone
() {
return
reader_writer_.WritesDone(); }
178
179
/// @brief Get call context, useful e.g. for accessing metadata.
180
///
181
/// @see @ref ugrpc::client::ReaderWriter::GetContext.
182
ugrpc
::
client
::
CallContext
&
GetContext
() {
return
reader_writer_.GetContext(); }
183
184
/// @overload
185
///
186
/// @see @ref ugrpc::client::ReaderWriter::GetContext.
187
const
ugrpc
::
client
::
CallContext
&
GetContext
()
const
{
return
reader_writer_.GetContext(); }
188
189
private
:
190
ProtobufReaderWriter reader_writer_;
191
utils::Box<ProtobufResponse> response_;
192
};
193
194
}
// namespace grpc_proto_structs::client
195
196
USERVER_NAMESPACE_END
grpc-proto-structs
include
userver
grpc-proto-structs
client
stream.hpp
Generated on
for userver by
Doxygen
1.17.0