Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
server.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 <string_view>
32#include <utility>
33#include <vector>
34
36{
40 using grpc_metadata = std::vector<std::pair<std::string, std::string>>;
46 {
49
52
54 std::chrono::milliseconds keepalive_time{7200000}; // 2 hours
55
57 std::chrono::milliseconds keepalive_timeout{20000};
58
60 std::chrono::milliseconds max_connection_idle{0}; // 0 = unlimited
61
63 std::chrono::milliseconds max_connection_age{0}; // 0 = unlimited
64
66 size_t num_threads = 0;
67 };
68
77 {
78 public:
83 virtual auto client_metadata() const -> const grpc_metadata& = 0;
84
90 virtual auto add_trailing_metadata(const std::string& key,
91 const std::string& value) -> void = 0;
92
97 virtual auto set_trailing_metadata(grpc_metadata metadata) -> void = 0;
98
103 virtual auto is_cancelled() const -> bool = 0;
104
109 virtual auto deadline() const
110 -> std::optional<std::chrono::system_clock::time_point> = 0;
111
116 virtual auto peer() const -> std::string = 0;
117
122 virtual auto auth_context() const -> std::string = 0;
123
124 virtual ~server_context() = default;
125 };
126
134 using unary_handler = std::function<
135 std::pair<grpc_status, std::vector<uint8_t>>(
136 server_context& ctx,
137 const std::vector<uint8_t>& request)>;
138
143 {
144 public:
150 virtual auto write(const std::vector<uint8_t>& message) -> VoidResult = 0;
151
152 virtual ~server_writer() = default;
153 };
154
163 using server_streaming_handler = std::function<
165 server_context& ctx,
166 const std::vector<uint8_t>& request,
167 server_writer& writer)>;
168
173 {
174 public:
179 virtual auto read() -> Result<std::vector<uint8_t>> = 0;
180
185 virtual auto has_more() const -> bool = 0;
186
187 virtual ~server_reader() = default;
188 };
189
197 using client_streaming_handler = std::function<
198 std::pair<grpc_status, std::vector<uint8_t>>(
199 server_context& ctx,
200 server_reader& reader)>;
201
206 {
207 public:
208 virtual auto read() -> Result<std::vector<uint8_t>> = 0;
209 virtual auto write(const std::vector<uint8_t>& message) -> VoidResult = 0;
210 virtual auto has_more() const -> bool = 0;
211
212 virtual ~server_reader_writer() = default;
213 };
214
222 using bidi_streaming_handler = std::function<
224 server_context& ctx,
225 server_reader_writer& stream)>;
226
236 {
237 public:
242 virtual auto service_name() const -> std::string_view = 0;
243
244 virtual ~grpc_service() = default;
245 };
246
273 {
274 public:
279 explicit grpc_server(const grpc_server_config& config = {});
280
285
286 // Non-copyable
287 grpc_server(const grpc_server&) = delete;
289
290 // Movable
292 grpc_server& operator=(grpc_server&&) noexcept;
293
299 auto start(uint16_t port) -> VoidResult;
300
309 auto start_tls(uint16_t port,
310 const std::string& cert_path,
311 const std::string& key_path,
312 const std::string& ca_path = "") -> VoidResult;
313
317 auto stop() -> void;
318
322 auto wait() -> void;
323
328 auto is_running() const -> bool;
329
334 auto port() const -> uint16_t;
335
341 auto register_service(grpc_service* service) -> VoidResult;
342
349 auto register_unary_method(const std::string& full_method_name,
350 unary_handler handler) -> VoidResult;
351
358 auto register_server_streaming_method(const std::string& full_method_name,
360
367 auto register_client_streaming_method(const std::string& full_method_name,
369
376 auto register_bidi_streaming_method(const std::string& full_method_name,
378
379 private:
380 class impl;
381 std::unique_ptr<impl> impl_;
382 };
383
384} // namespace kcenon::network::protocols::grpc
gRPC server for handling RPC requests
Definition server.h:273
grpc_server(const grpc_server &)=delete
grpc_server & operator=(const grpc_server &)=delete
Base class for gRPC service implementations.
Definition server.h:236
virtual auto service_name() const -> std::string_view=0
Get the full service name (e.g., "package.ServiceName")
Context for handling a single RPC request.
Definition server.h:77
virtual auto client_metadata() const -> const grpc_metadata &=0
Get client metadata.
virtual auto peer() const -> std::string=0
Get peer address.
virtual auto is_cancelled() const -> bool=0
Check if the request has been cancelled.
virtual auto auth_context() const -> std::string=0
Get authentication context (for TLS)
virtual auto set_trailing_metadata(grpc_metadata metadata) -> void=0
Set trailing metadata.
virtual auto deadline() const -> std::optional< std::chrono::system_clock::time_point >=0
Get request deadline.
virtual auto add_trailing_metadata(const std::string &key, const std::string &value) -> void=0
Add trailing metadata.
Reader/writer interface for bidirectional streaming.
Definition server.h:206
virtual auto write(const std::vector< uint8_t > &message) -> VoidResult=0
virtual auto read() -> Result< std::vector< uint8_t > >=0
Reader interface for client streaming.
Definition server.h:173
virtual auto has_more() const -> bool=0
Check if more messages are available.
virtual auto read() -> Result< std::vector< uint8_t > >=0
Read next message from stream.
Writer interface for server streaming.
Definition server.h:143
virtual auto write(const std::vector< uint8_t > &message) -> VoidResult=0
Write a message to the stream.
tracing_config config
Definition exporters.cpp:29
gRPC message framing and serialization.
gRPC protocol implementation
Definition client.h:34
std::function< std::pair< grpc_status, std::vector< uint8_t > >( server_context &ctx, const std::vector< uint8_t > &request)> unary_handler
Handler function type for unary RPC.
Definition server.h:134
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
std::function< grpc_status( server_context &ctx, const std::vector< uint8_t > &request, server_writer &writer)> server_streaming_handler
Handler function type for server streaming RPC.
Definition server.h:163
std::function< grpc_status( server_context &ctx, server_reader_writer &stream)> bidi_streaming_handler
Handler function type for bidirectional streaming RPC.
Definition server.h:222
std::function< std::pair< grpc_status, std::vector< uint8_t > >( server_context &ctx, server_reader &reader)> client_streaming_handler
Handler function type for client streaming RPC.
Definition server.h:197
Network-specific error and result type definitions.
gRPC status codes and error representation.
std::chrono::milliseconds max_connection_age
Maximum connection age.
Definition server.h:63
std::chrono::milliseconds keepalive_time
Keepalive time (time between keepalive pings)
Definition server.h:54
std::chrono::milliseconds keepalive_timeout
Keepalive timeout.
Definition server.h:57
std::chrono::milliseconds max_connection_idle
Maximum connection idle time.
Definition server.h:60
size_t max_concurrent_streams
Maximum number of concurrent streams per connection.
Definition server.h:48
size_t num_threads
Number of worker threads (0 = auto-detect)
Definition server.h:66
size_t max_message_size
Maximum message size in bytes.
Definition server.h:51
gRPC status with code, message, and optional details
Definition status.h:95