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
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
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
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
48
ugrpc::client::CallContext
&
GetContext
() {
return
reader_.GetContext(); }
49
53
const
ugrpc::client::CallContext
&
GetContext
()
const
{
return
reader_.GetContext(); }
54
55
private
:
56
ProtobufReader reader_;
57
};
58
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
77
[[nodiscard]]
bool
Write
(StructsRequest&& request) {
78
return
writer_.Write(
proto_structs::StructToMessage
(std::move(request)));
79
}
80
83
[[nodiscard]]
bool
WriteCopy
(
const
StructsRequest& request) {
84
return
writer_.Write(
proto_structs::StructToMessage
(request));
85
}
86
89
void
WriteAndCheck
(StructsRequest&& request) {
90
writer_.WriteAndCheck(
proto_structs::StructToMessage
(std::move(request)));
91
}
92
95
void
WriteCopyAndCheck
(
const
StructsRequest& request) {
96
writer_.WriteAndCheck(
proto_structs::StructToMessage
(request));
97
}
98
100
StructsResponse
Finish
() {
return
proto_structs::MessageToStruct<StructsResponse>
(writer_.Finish()); }
101
103
ugrpc::client::CallContext
&
GetContext
() {
return
writer_.GetContext(); }
104
106
const
ugrpc::client::CallContext
&
GetContext
()
const
{
return
writer_.GetContext(); }
107
108
private
:
109
ProtobufWriter writer_;
110
};
111
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
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
142
StreamReadFuture<StructsResponse>
ReadAsync
() {
143
return
StreamReadFuture
{reader_writer_.ReadAsync(*response_), *response_};
144
}
145
149
[[nodiscard]]
bool
Write
(
const
StructsRequest& request) {
150
return
reader_writer_.Write(
proto_structs::StructToMessage
(request));
151
}
152
156
[[nodiscard]]
bool
Write
(StructsRequest&& request) {
157
return
reader_writer_.Write(
proto_structs::StructToMessage
(std::move(request)));
158
}
159
163
void
WriteAndCheck
(
const
StructsRequest& request) {
164
reader_writer_.WriteAndCheck(
proto_structs::StructToMessage
(request));
165
}
166
170
void
WriteAndCheck
(StructsRequest&& request) {
171
reader_writer_.WriteAndCheck(
proto_structs::StructToMessage
(std::move(request)));
172
}
173
177
[[nodiscard]]
bool
WritesDone
() {
return
reader_writer_.WritesDone(); }
178
182
ugrpc::client::CallContext
&
GetContext
() {
return
reader_writer_.GetContext(); }
183
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