userver: userver/ugrpc/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/ugrpc/server/stream.hpp
4/// @brief Server streaming interfaces
5
6#include <grpcpp/server_context.h>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace ugrpc::server {
11
12/// @brief Interface to read client's requests.
13///
14/// This class is not thread-safe.
15///
16/// If any method throws, further methods must not be called on the same stream.
17template <class Request>
18class Reader {
19public:
20 /// @cond
21 virtual ~Reader() = default;
22 /// @endcond
23
24 /// @brief Await and read the next incoming message
25 /// @param request where to put the request on success
26 /// @returns `true` on success, `false` on end-of-input
27 /// @throws ugrpc::server::RpcError on an RPC error
28 [[nodiscard]] virtual bool Read(Request& request) = 0;
29};
30
31/// @brief Interface to write server's responses.
32///
33/// This class is not thread-safe.
34///
35/// If any method throws, further methods must not be called on the same stream.
36template <class Response>
37class Writer {
38public:
39 /// @cond
40 virtual ~Writer() = default;
41 /// @endcond
42
43 /// @brief Write the next outgoing message
44 /// @param response the next message to write
45 /// @throws ugrpc::server::RpcError on an RPC error
46 /// @throws std::exception (internal) on error from middlewares
47 ///
48 /// This method uses move-only semantics for safety:
49 /// - Middlewares may modify responses during writes, making reuse dangerous
50 /// - After the call, `response` is in moved-from state and must not be reused
51 /// - Explicit std::move ensures ownership transfer and prevents accidental reuse
52 virtual void Write(Response&& response) = 0;
53
54 /// @brief Write the next outgoing message
55 /// @param response the next message to write
56 /// @param options the write options
57 /// @throws ugrpc::server::RpcError on an RPC error
58 /// @throws std::exception (internal) on error from middlewares
59 ///
60 /// This method uses move-only semantics for safety:
61 /// - Middlewares may modify responses during writes, making reuse dangerous
62 /// - After the call, `response` is in moved-from state and must not be reused
63 /// - Explicit std::move ensures ownership transfer and prevents accidental reuse
64 virtual void Write(Response&& response, const grpc::WriteOptions& options) = 0;
65};
66
67/// @brief Interface to both read and write messages.
68///
69/// If any method throws, further methods must not be called on the same stream.
70///
71/// This class allows the following concurrent calls:
72///
73/// - `Read`;
74/// - `Write`
75///
76/// and there can only be one Read and one Write in flight at a time.
77///
78/// If any method throws, further methods must not be called on the same stream.
79template <class Request, class Response>
80class ReaderWriter : public Reader<Request>, public Writer<Response> {};
81
82} // namespace ugrpc::server
83
84USERVER_NAMESPACE_END