userver: /data/code/userver/libraries/grpc-proto-structs/include/userver/grpc-proto-structs/server/stream.hpp Source File
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
11USERVER_NAMESPACE_BEGIN
12
13namespace 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.
21template <typename Request>
22class Reader {
23public:
26
27 explicit Reader(ProtobufMessageReader& reader) : reader_{reader} {}
28
29 /// @brief Await and read the next incoming message.
30 ///
31 /// Read protobuf message corresponding to `Request` with @ref ugrpc::server::Reader::Read
32 /// and construct `Request` from it.
33 ///
34 /// @see @ref ugrpc::server::Reader::Read method for details.
35 bool Read(Request& request) {
36 RequestMessage message;
37 if (reader_.Read(message)) {
38 proto_structs::MessageToStruct(message, request);
39 return true;
40 }
41 return false;
42 }
43
44private:
45 ProtobufMessageReader& reader_;
46};
47
48/// @brief proto-struct based Writer adapter
49///
50/// This class is not thread-safe.
51///
52/// If any method throws, further methods must not be called on the same stream.
53/// @see @ref ugrpc::server::Writer.
54template <typename Response>
55class Writer {
56public:
59
60 explicit Writer(ProtobufMessageWriter& writer) : writer_{writer} {}
61
62 /// @{
63 /// @brief Write the next outgoing message.
64 ///
65 /// Convert response to corresponding protobuf message and pass it to @ref ugrpc::server::Writer::Write.
66 ///
67 /// @see @ref ugrpc::server::Writer::Write method for details.
68 void Write(Response& response) { writer_.Write(proto_structs::StructToMessage(response)); }
69
70 void Write(Response& response, const grpc::WriteOptions& options) {
71 writer_.Write(proto_structs::StructToMessage(response), options);
72 }
73
74 void Write(Response&& response) { writer_.Write(proto_structs::StructToMessage(std::move(response))); }
75
76 void Write(Response&& response, const grpc::WriteOptions& options) {
77 writer_.Write(proto_structs::StructToMessage(std::move(response)), options);
78 }
79 /// @}
80
81private:
82 ProtobufMessageWriter& writer_;
83};
84
85/// @brief proto-struct based ReaderWriter adapter
86///
87/// If any method throws, further methods must not be called on the same stream.
88///
89/// This class allows the following concurrent calls:
90///
91/// - `Read`;
92/// - `Write`
93///
94/// and there can only be one Read and one Write in flight at a time.
95///
96/// If any method throws, further methods must not be called on the same stream.
97/// @see @ref ugrpc::server::ReaderWriter.
98template <typename Request, typename Response>
99class ReaderWriter : public Reader<Request>, public Writer<Response> {
100public:
103
104 explicit ReaderWriter(ProtobufMessageReaderWriter& reader_writer)
105 : Reader<Request>{reader_writer}, Writer<Response>{reader_writer} {}
106};
107
108} // namespace grpc_proto_structs::server
109
110USERVER_NAMESPACE_END