Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
frame.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
11#pragma once
12
22#include <cstdint>
23#include <span>
24#include <vector>
25
27{
35 constexpr size_t grpc_header_size = 5;
36
40 constexpr size_t default_max_message_size = 4 * 1024 * 1024;
41
50 {
51 bool compressed = false;
52 std::vector<uint8_t> data;
53
57 grpc_message() = default;
58
64 explicit grpc_message(std::vector<uint8_t> payload, bool is_compressed = false)
65 : compressed(is_compressed), data(std::move(payload)) {}
66
74 static auto parse(std::span<const uint8_t> input) -> Result<grpc_message>;
75
82 auto serialize() const -> std::vector<uint8_t>;
83
88 auto serialized_size() const -> size_t
89 {
90 return grpc_header_size + data.size();
91 }
92
97 auto empty() const -> bool { return data.empty(); }
98
103 auto size() const -> size_t { return data.size(); }
104 };
105
109 constexpr const char* grpc_content_type = "application/grpc";
110
114 constexpr const char* grpc_content_type_proto = "application/grpc+proto";
115
119 namespace trailer_names
120 {
121 constexpr const char* grpc_status = "grpc-status";
122 constexpr const char* grpc_message = "grpc-message";
123 constexpr const char* grpc_status_details = "grpc-status-details-bin";
124 }
125
129 namespace header_names
130 {
131 constexpr const char* te = "te";
132 constexpr const char* content_type = "content-type";
133 constexpr const char* grpc_encoding = "grpc-encoding";
134 constexpr const char* grpc_accept_encoding = "grpc-accept-encoding";
135 constexpr const char* grpc_timeout = "grpc-timeout";
136 constexpr const char* user_agent = "user-agent";
137 }
138
142 namespace compression
143 {
144 constexpr const char* identity = "identity";
145 constexpr const char* deflate = "deflate";
146 constexpr const char* gzip = "gzip";
147 }
148
162 auto parse_timeout(std::string_view timeout_str) -> uint64_t;
163
169 auto format_timeout(uint64_t timeout_ms) -> std::string;
170
171} // namespace kcenon::network::protocols::grpc
constexpr const char * grpc_accept_encoding
Definition frame.h:134
constexpr const char * grpc_status_details
Definition frame.h:123
gRPC protocol implementation
Definition client.h:34
constexpr size_t grpc_header_size
gRPC message header size (5 bytes)
Definition frame.h:35
constexpr size_t default_max_message_size
Maximum gRPC message size (default 4MB)
Definition frame.h:40
auto format_timeout(uint64_t timeout_ms) -> std::string
Format timeout as gRPC timeout string.
Definition frame.cpp:131
constexpr const char * grpc_content_type
gRPC content-type header value
Definition frame.h:109
auto parse_timeout(std::string_view timeout_str) -> uint64_t
Parse timeout string (e.g., "10S", "100m", "1000u")
Definition frame.cpp:88
constexpr const char * grpc_content_type_proto
gRPC content-type with proto encoding
Definition frame.h:114
Network-specific error and result type definitions.
gRPC message with compression flag and payload
Definition frame.h:50
grpc_message()=default
Default constructor.
static auto parse(std::span< const uint8_t > input) -> Result< grpc_message >
Parse gRPC message from raw bytes.
Definition frame.cpp:14
grpc_message(std::vector< uint8_t > payload, bool is_compressed=false)
Construct with data.
Definition frame.h:64
auto serialize() const -> std::vector< uint8_t >
Serialize message to bytes with length prefix.
Definition frame.cpp:67
auto size() const -> size_t
Get message size (payload only)
Definition frame.h:103
auto serialized_size() const -> size_t
Get total serialized size including header.
Definition frame.h:88
std::vector< uint8_t > data
Message payload.
Definition frame.h:52
bool compressed
Whether payload is compressed.
Definition frame.h:51
auto empty() const -> bool
Check if message is empty.
Definition frame.h:97
gRPC status with code, message, and optional details
Definition status.h:95