userver: userver/ugrpc/byte_buffer_utils.hpp Source File
Loading...
Searching...
No Matches
byte_buffer_utils.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/byte_buffer_utils.hpp
4/// @brief Helper functions for working with `grpc::ByteBuffer`
5
6#include <cstddef>
7
8#include <google/protobuf/message.h>
9#include <grpcpp/support/byte_buffer.h>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace ugrpc {
14
15/// @see @ref SerializeToByteBuffer
16inline constexpr std::size_t kDefaultSerializeBlockSize = 4096;
17
18/// @brief Serialize a Protobuf message to the wire format.
19/// @param message the message to serialize
20/// @param block_size can be used for performance tuning, too small chunk size
21/// results in extra allocations, too large chunk size results in wasting memory
22/// @throws std::runtime_error on serialization errors (supposedly only happens
23/// on extremely rare allocation failures or proto reflection malfunctioning).
24grpc::ByteBuffer SerializeToByteBuffer(
25 const ::google::protobuf::Message& message,
26 std::size_t block_size = kDefaultSerializeBlockSize);
27
28/// @brief Parse a Protobuf message from the wire format.
29/// @param buffer the buffer that might be tempered with during deserialization
30/// @param message will contain the parsing result on success
31/// @returns `true` on success, `false` if @a buffer does not contain a valid
32/// message, according to the derived type of @a message
33bool ParseFromByteBuffer(grpc::ByteBuffer&& buffer,
34 ::google::protobuf::Message& message);
35
36} // namespace ugrpc
37
38USERVER_NAMESPACE_END