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
25SerializeToByteBuffer(const ::google::protobuf::Message& message, std::size_t block_size = kDefaultSerializeBlockSize);
26
27/// @brief Parse a Protobuf message from the wire format.
28/// @param buffer the buffer that might be tempered with during deserialization
29/// @param message will contain the parsing result on success
30/// @returns `true` on success, `false` if @a buffer does not contain a valid
31/// message, according to the derived type of @a message
32bool ParseFromByteBuffer(grpc::ByteBuffer&& buffer, ::google::protobuf::Message& message);
33
34} // namespace ugrpc
35
36USERVER_NAMESPACE_END