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 userver/grpc-proto-structs/server/stream.hpp
4
/// @brief proto-struct based server streaming interfaces
5
6
#
include
<
utility
>
7
8
#
include
<
userver
/
proto
-
structs
/
convert
.
hpp
>
9
#
include
<
userver
/
ugrpc
/
server
/
stream
.
hpp
>
10
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
grpc_proto_structs::server {
14
15
/// @brief proto-struct based Reader adapter
16
///
17
/// This class is not thread-safe.
18
///
19
/// If any method throws, further methods must not be called on the same stream.
20
/// @see @ref ugrpc::server::Reader.
21
template
<
typename
Request>
22
class
Reader
{
23
public
:
24
using
RequestMessage
=
proto_structs
::
traits
::
CompatibleMessageType
<
Request
>;
25
using
ProtobufMessageReader
=
ugrpc
::
server
::
Reader
<
RequestMessage
>;
26
27
explicit
Reader(ProtobufMessageReader& reader)
28
: reader_{reader}
29
{}
30
31
/// @brief Await and read the next incoming message.
32
///
33
/// Read protobuf message corresponding to `Request` with @ref ugrpc::server::Reader::Read
34
/// and construct `Request` from it.
35
///
36
/// @see @ref ugrpc::server::Reader::Read method for details.
37
bool
Read
(Request& request) {
38
RequestMessage message;
39
if
(reader_.Read(message)) {
40
proto_structs::MessageToStruct(message, request);
41
return
true
;
42
}
43
return
false
;
44
}
45
46
private
:
47
ProtobufMessageReader& reader_;
48
};
49
50
/// @brief proto-struct based Writer adapter
51
///
52
/// This class is not thread-safe.
53
///
54
/// If any method throws, further methods must not be called on the same stream.
55
/// @see @ref ugrpc::server::Writer.
56
template
<
typename
Response>
57
class
Writer
{
58
public
:
59
using
ResponseMessage
=
proto_structs
::
traits
::
CompatibleMessageType
<
Response
>;
60
using
ProtobufMessageWriter
=
ugrpc
::
server
::
Writer
<
ResponseMessage
>;
61
62
explicit
Writer(ProtobufMessageWriter& writer)
63
: writer_{writer}
64
{}
65
66
/// @{
67
/// @brief Write the next outgoing message.
68
/// @note This version may move some fields from the request.
69
///
70
/// Convert response to corresponding protobuf message and pass it to @ref ugrpc::server::Writer::Write.
71
///
72
/// @see @ref ugrpc::server::Writer::Write method for details.
73
void
Write
(Response&& response) { writer_.Write(proto_structs::StructToMessage(std::move(response))); }
74
75
void
Write(Response&& response,
const
grpc::WriteOptions& options) {
76
writer_.Write(proto_structs::StructToMessage(std::move(response)), options);
77
}
78
/// @}
79
80
/// @{
81
/// @brief Write the next outgoing message.
82
/// @note This version preserves the original request object by copying necessary data.
83
///
84
/// Convert response to corresponding protobuf message and pass it to @ref ugrpc::server::Writer::Write.
85
///
86
/// @see @ref ugrpc::server::Writer::Write method for details.
87
void
WriteCopy
(
const
Response& response) { writer_.Write(proto_structs::StructToMessage(response)); }
88
89
void
WriteCopy(
const
Response& response,
const
grpc::WriteOptions& options) {
90
writer_.Write(proto_structs::StructToMessage(response), options);
91
}
92
/// @}
93
94
private
:
95
ProtobufMessageWriter& writer_;
96
};
97
98
/// @brief proto-struct based ReaderWriter adapter
99
///
100
/// If any method throws, further methods must not be called on the same stream.
101
///
102
/// This class allows the following concurrent calls:
103
///
104
/// - `Read`;
105
/// - `Write`
106
///
107
/// and there can only be one Read and one Write in flight at a time.
108
///
109
/// If any method throws, further methods must not be called on the same stream.
110
/// @see @ref ugrpc::server::ReaderWriter.
111
template
<
typename
Request,
typename
Response>
112
class
ReaderWriter
:
public
Reader
<Request>,
public
Writer
<Response> {
113
public
:
114
using
ProtobufMessageReaderWriter
=
ugrpc
::
server
::
ReaderWriter
<
115
typename
Reader
<
Request
>::
RequestMessage
,
116
typename
Writer
<
Response
>::
ResponseMessage
>;
117
118
explicit
ReaderWriter(ProtobufMessageReaderWriter& reader_writer)
119
:
Reader
<Request>{reader_writer},
120
Writer
<Response>{reader_writer}
121
{}
122
};
123
124
}
// namespace grpc_proto_structs::server
125
126
USERVER_NAMESPACE_END
grpc-proto-structs
include
userver
grpc-proto-structs
server
stream.hpp
Generated on
for userver by
Doxygen
1.17.0