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 virtual void Write(Response& response) = 0;
48
49 /// @brief Write the next outgoing message
50 /// @param response the next message to write
51 /// @param options the write options
52 /// @throws ugrpc::server::RpcError on an RPC error
53 /// @throws std::exception (internal) on error from middlewares
54 virtual void Write(Response& response, const grpc::WriteOptions& options) = 0;
55
56 /// @brief Write the next outgoing message
57 /// @param response the next message to write
58 /// @throws ugrpc::server::RpcError on an RPC error
59 /// @throws std::exception (internal) on error from middlewares
60 virtual void Write(Response&& response) = 0;
61
62 /// @brief Write the next outgoing message
63 /// @param response the next message to write
64 /// @param options the write options
65 /// @throws ugrpc::server::RpcError on an RPC error
66 /// @throws std::exception (internal) on error from middlewares
67 virtual void Write(Response&& response, const grpc::WriteOptions& options) = 0;
68};
69
70/// @brief Interface to both read and write messages.
71///
72/// If any method throws, further methods must not be called on the same stream.
73///
74/// This class allows the following concurrent calls:
75///
76/// - `Read`;
77/// - `Write`
78///
79/// and there can only be one Read and one Write in flight at a time.
80///
81/// If any method throws, further methods must not be called on the same stream.
82template <class Request, class Response>
83class ReaderWriter : public Reader<Request>, public Writer<Response> {};
84
85} // namespace ugrpc::server
86
87USERVER_NAMESPACE_END