Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::protocols::grpc::grpc_message Struct Reference

gRPC message with compression flag and payload More...

#include <frame.h>

Collaboration diagram for kcenon::network::protocols::grpc::grpc_message:
Collaboration graph

Public Member Functions

 grpc_message ()=default
 Default constructor.
 
 grpc_message (std::vector< uint8_t > payload, bool is_compressed=false)
 Construct with data.
 
auto serialize () const -> std::vector< uint8_t >
 Serialize message to bytes with length prefix.
 
auto serialized_size () const -> size_t
 Get total serialized size including header.
 
auto empty () const -> bool
 Check if message is empty.
 
auto size () const -> size_t
 Get message size (payload only)
 

Static Public Member Functions

static auto parse (std::span< const uint8_t > input) -> Result< grpc_message >
 Parse gRPC message from raw bytes.
 

Public Attributes

bool compressed = false
 Whether payload is compressed.
 
std::vector< uint8_t > data
 Message payload.
 

Detailed Description

gRPC message with compression flag and payload

Represents a gRPC message as transmitted over HTTP/2. Format: | Compressed-Flag (1 byte) | Message-Length (4 bytes) | Message |

Examples
grpc_service_example.cpp.

Definition at line 49 of file frame.h.

Constructor & Destructor Documentation

◆ grpc_message() [1/2]

kcenon::network::protocols::grpc::grpc_message::grpc_message ( )
default

Default constructor.

◆ grpc_message() [2/2]

kcenon::network::protocols::grpc::grpc_message::grpc_message ( std::vector< uint8_t > payload,
bool is_compressed = false )
inlineexplicit

Construct with data.

Parameters
payloadMessage data
is_compressedWhether data is compressed

Definition at line 64 of file frame.h.

65 : compressed(is_compressed), data(std::move(payload)) {}
std::vector< uint8_t > data
Message payload.
Definition frame.h:52
bool compressed
Whether payload is compressed.
Definition frame.h:51

Member Function Documentation

◆ empty()

auto kcenon::network::protocols::grpc::grpc_message::empty ( ) const -> bool
inline

Check if message is empty.

Returns
True if data is empty

Definition at line 97 of file frame.h.

97{ return data.empty(); }

References data.

◆ parse()

auto kcenon::network::protocols::grpc::grpc_message::parse ( std::span< const uint8_t > input) -> Result<grpc_message>
static

Parse gRPC message from raw bytes.

Parameters
inputRaw byte data (header + payload)
Returns
Result containing parsed message or error

Parses a gRPC length-prefixed message from the input buffer.

Definition at line 14 of file frame.cpp.

15{
16 // Check minimum header size
17 if (input.size() < grpc_header_size)
18 {
21 "Input too small for gRPC message header",
22 "grpc::frame",
23 "Expected at least 5 bytes, got " + std::to_string(input.size()));
24 }
25
26 // Parse compressed flag (1 byte)
27 bool compressed = (input[0] != 0);
28
29 // Parse message length (4 bytes, big-endian)
30 uint32_t length = (static_cast<uint32_t>(input[1]) << 24) |
31 (static_cast<uint32_t>(input[2]) << 16) |
32 (static_cast<uint32_t>(input[3]) << 8) |
33 (static_cast<uint32_t>(input[4]));
34
35 // Validate message length
36 if (length > default_max_message_size)
37 {
40 "gRPC message exceeds maximum size",
41 "grpc::frame",
42 "Max: " + std::to_string(default_max_message_size) +
43 ", Got: " + std::to_string(length));
44 }
45
46 // Check if we have enough data
47 size_t total_size = grpc_header_size + length;
48 if (input.size() < total_size)
49 {
52 "Input too small for gRPC message payload",
53 "grpc::frame",
54 "Expected " + std::to_string(total_size) +
55 " bytes, got " + std::to_string(input.size()));
56 }
57
58 // Extract message data
59 grpc_message msg;
60 msg.compressed = compressed;
61 msg.data.assign(input.begin() + grpc_header_size,
62 input.begin() + grpc_header_size + length);
63
64 return ok(std::move(msg));
65}
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
@ ok
Not an error; returned on success.
grpc_message()=default
Default constructor.

References compressed, data, kcenon::network::protocols::grpc::default_max_message_size, kcenon::network::error, kcenon::network::protocols::grpc::grpc_header_size, kcenon::network::error_codes::common_errors::invalid_argument, and kcenon::network::protocols::grpc::ok.

Referenced by kcenon::network::protocols::grpc::grpc_client::impl::call_raw(), kcenon::network::protocols::grpc::client_stream_writer_impl::on_complete(), kcenon::network::protocols::grpc::bidi_stream_impl::read(), and kcenon::network::protocols::grpc::server_stream_reader_impl::read().

Here is the caller graph for this function:

◆ serialize()

auto kcenon::network::protocols::grpc::grpc_message::serialize ( ) const -> std::vector<uint8_t>

Serialize message to bytes with length prefix.

Returns
Byte vector containing header + payload

Serializes the message with a 5-byte header prefix.

Definition at line 67 of file frame.cpp.

68{
69 std::vector<uint8_t> result;
70 result.reserve(grpc_header_size + data.size());
71
72 // Compressed flag (1 byte)
73 result.push_back(compressed ? 1 : 0);
74
75 // Message length (4 bytes, big-endian)
76 uint32_t length = static_cast<uint32_t>(data.size());
77 result.push_back(static_cast<uint8_t>((length >> 24) & 0xFF));
78 result.push_back(static_cast<uint8_t>((length >> 16) & 0xFF));
79 result.push_back(static_cast<uint8_t>((length >> 8) & 0xFF));
80 result.push_back(static_cast<uint8_t>(length & 0xFF));
81
82 // Message data
83 result.insert(result.end(), data.begin(), data.end());
84
85 return result;
86}

References compressed, data, and kcenon::network::protocols::grpc::grpc_header_size.

Referenced by kcenon::network::protocols::grpc::grpc_client::impl::call_raw(), kcenon::network::protocols::grpc::grpc_client::impl::server_stream_raw(), kcenon::network::protocols::grpc::bidi_stream_impl::write(), and kcenon::network::protocols::grpc::client_stream_writer_impl::write().

Here is the caller graph for this function:

◆ serialized_size()

auto kcenon::network::protocols::grpc::grpc_message::serialized_size ( ) const -> size_t
inline

Get total serialized size including header.

Returns
Total size in bytes

Definition at line 88 of file frame.h.

89 {
90 return grpc_header_size + data.size();
91 }

References data, and kcenon::network::protocols::grpc::grpc_header_size.

◆ size()

auto kcenon::network::protocols::grpc::grpc_message::size ( ) const -> size_t
inline

Get message size (payload only)

Returns
Payload size in bytes

Definition at line 103 of file frame.h.

103{ return data.size(); }

References data.

Member Data Documentation

◆ compressed

bool kcenon::network::protocols::grpc::grpc_message::compressed = false

Whether payload is compressed.

Definition at line 51 of file frame.h.

Referenced by parse(), and serialize().

◆ data

std::vector<uint8_t> kcenon::network::protocols::grpc::grpc_message::data

The documentation for this struct was generated from the following files: