Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
25
26#include <chrono>
27#include <functional>
28#include <memory>
29#include <optional>
30#include <string>
31#include <vector>
32
34{
38 using grpc_metadata = std::vector<std::pair<std::string, std::string>>;
39
45 {
47 std::chrono::milliseconds default_timeout{30000};
48
50 bool use_tls = true;
51
53 std::string root_certificates;
54
56 std::optional<std::string> client_certificate;
57
59 std::optional<std::string> client_key;
60
63
65 std::chrono::milliseconds keepalive_time{0};
66
68 std::chrono::milliseconds keepalive_timeout{20000};
69
71 uint32_t max_retry_attempts = 3;
72 };
73
79 {
81 std::optional<std::chrono::system_clock::time_point> deadline;
82
85
87 bool wait_for_ready = false;
88
91
96 template<typename Duration>
97 void set_timeout(Duration timeout)
98 {
99 deadline = std::chrono::system_clock::now() +
100 std::chrono::duration_cast<std::chrono::system_clock::duration>(timeout);
101 }
102 };
103
115 {
116 public:
122 explicit grpc_client(const std::string& target,
123 const grpc_channel_config& config = {});
124
129
130 // Non-copyable
131 grpc_client(const grpc_client&) = delete;
133
134 // Movable
136 grpc_client& operator=(grpc_client&&) noexcept;
137
142 auto connect() -> VoidResult;
143
147 auto disconnect() -> void;
148
153 auto is_connected() const -> bool;
154
160 auto wait_for_connected(std::chrono::milliseconds timeout) -> bool;
161
166 auto target() const -> const std::string&;
167
184 auto call_raw(const std::string& method,
185 const std::vector<uint8_t>& request,
186 const call_options& options = {}) -> Result<grpc_message>;
187
195 auto call_raw_async(const std::string& method,
196 const std::vector<uint8_t>& request,
197 std::function<void(Result<grpc_message>)> callback,
198 const call_options& options = {}) -> void;
199
205 {
206 public:
211 virtual auto read() -> Result<grpc_message> = 0;
212
217 virtual auto has_more() const -> bool = 0;
218
223 virtual auto finish() -> grpc_status = 0;
224
225 virtual ~server_stream_reader() = default;
226 };
227
235 auto server_stream_raw(const std::string& method,
236 const std::vector<uint8_t>& request,
237 const call_options& options = {})
239
245 {
246 public:
252 virtual auto write(const std::vector<uint8_t>& message) -> VoidResult = 0;
253
258 virtual auto writes_done() -> VoidResult = 0;
259
264 virtual auto finish() -> Result<grpc_message> = 0;
265
266 virtual ~client_stream_writer() = default;
267 };
268
275 auto client_stream_raw(const std::string& method,
276 const call_options& options = {})
277 -> Result<std::unique_ptr<client_stream_writer>>;
278
284 {
285 public:
291 virtual auto write(const std::vector<uint8_t>& message) -> VoidResult = 0;
292
297 virtual auto read() -> Result<grpc_message> = 0;
298
303 virtual auto writes_done() -> VoidResult = 0;
304
309 virtual auto finish() -> grpc_status = 0;
310
311 virtual ~bidi_stream() = default;
312 };
313
320 auto bidi_stream_raw(const std::string& method,
321 const call_options& options = {})
322 -> Result<std::unique_ptr<bidi_stream>>;
323
324 private:
325 class impl;
326 std::unique_ptr<impl> impl_;
327 };
328
329} // namespace kcenon::network::protocols::grpc
virtual auto read() -> Result< grpc_message >=0
Read next message from stream.
virtual auto write(const std::vector< uint8_t > &message) -> VoidResult=0
Write message to stream.
virtual auto writes_done() -> VoidResult=0
Signal that writing is done.
virtual auto finish() -> grpc_status=0
Finish the call and get final status.
virtual auto write(const std::vector< uint8_t > &message) -> VoidResult=0
Write message to stream.
virtual auto writes_done() -> VoidResult=0
Signal that writing is done.
virtual auto finish() -> Result< grpc_message >=0
Finish the call and get response.
virtual auto finish() -> grpc_status=0
Get final status after stream ends.
virtual auto has_more() const -> bool=0
Check if stream has more messages.
virtual auto read() -> Result< grpc_message >=0
Read next message from stream.
gRPC client for making RPC calls
Definition client.h:115
auto is_connected() const -> bool
Check if connected.
Definition client.cpp:1650
grpc_client & operator=(const grpc_client &)=delete
auto connect() -> VoidResult
Connect to the server.
Definition client.cpp:1640
grpc_client(const grpc_client &)=delete
auto client_stream_raw(const std::string &method, const call_options &options={}) -> Result< std::unique_ptr< client_stream_writer > >
Start a client streaming RPC call.
Definition client.cpp:1688
auto target() const -> const std::string &
Get the target address.
Definition client.cpp:1660
auto call_raw_async(const std::string &method, const std::vector< uint8_t > &request, std::function< void(Result< grpc_message >)> callback, const call_options &options={}) -> void
Make an async unary RPC call.
Definition client.cpp:1672
auto wait_for_connected(std::chrono::milliseconds timeout) -> bool
Wait for connection to be ready.
Definition client.cpp:1655
auto disconnect() -> void
Disconnect from the server.
Definition client.cpp:1645
auto call_raw(const std::string &method, const std::vector< uint8_t > &request, const call_options &options={}) -> Result< grpc_message >
Make a unary RPC call.
Definition client.cpp:1665
auto server_stream_raw(const std::string &method, const std::vector< uint8_t > &request, const call_options &options={}) -> Result< std::unique_ptr< server_stream_reader > >
Start a server streaming RPC call.
Definition client.cpp:1680
auto bidi_stream_raw(const std::string &method, const call_options &options={}) -> Result< std::unique_ptr< bidi_stream > >
Start a bidirectional streaming RPC call.
Definition client.cpp:1695
grpc_client(const std::string &target, const grpc_channel_config &config={})
Construct gRPC client.
Definition client.cpp:1629
tracing_config config
Definition exporters.cpp:29
gRPC message framing and serialization.
gRPC protocol implementation
Definition client.h:34
constexpr size_t default_max_message_size
Maximum gRPC message size (default 4MB)
Definition frame.h:40
std::vector< std::pair< std::string, std::string > > grpc_metadata
Metadata key-value pair for gRPC requests/responses.
Definition client.h:38
Network-specific error and result type definitions.
gRPC status codes and error representation.
Options for individual RPC calls.
Definition client.h:79
std::string compression_algorithm
Compression algorithm to use.
Definition client.h:90
grpc_metadata metadata
Metadata to send with the request.
Definition client.h:84
bool wait_for_ready
Whether to wait for the server to be ready.
Definition client.h:87
std::optional< std::chrono::system_clock::time_point > deadline
Deadline for this call.
Definition client.h:81
void set_timeout(Duration timeout)
Set deadline from timeout duration.
Definition client.h:97
std::string root_certificates
Root certificates for TLS (PEM format)
Definition client.h:53
uint32_t max_retry_attempts
Maximum number of retry attempts.
Definition client.h:71
std::optional< std::string > client_key
Client private key for mutual TLS (PEM format)
Definition client.h:59
std::chrono::milliseconds keepalive_time
Keepalive time in milliseconds (0 = disabled)
Definition client.h:65
std::optional< std::string > client_certificate
Client certificate for mutual TLS (PEM format)
Definition client.h:56
std::chrono::milliseconds default_timeout
Default timeout for RPC calls.
Definition client.h:47
std::chrono::milliseconds keepalive_timeout
Keepalive timeout in milliseconds.
Definition client.h:68
size_t max_message_size
Maximum message size in bytes.
Definition client.h:62
gRPC status with code, message, and optional details
Definition status.h:95