Skip to content

Commit

Permalink
add macro
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhg committed Mar 29, 2022
1 parent fb59c1d commit 0c6135f
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 25 deletions.
6 changes: 1 addition & 5 deletions src/brpc/policy/baidu_rpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ inline void PackRpcHeader(char* rpc_header, uint32_t meta_size, int payload_size

static void SerializeRpcHeaderAndMeta(
butil::IOBuf* out, const RpcMeta& meta, int payload_size) {
#if GOOGLE_PROTOBUF_VERSION >= 3010000
const uint32_t meta_size = meta.ByteSizeLong();
#else
const int meta_size = meta.ByteSize();
#endif
const uint32_t meta_size = PROTOBUF_BYTE_SIZE(meta);
if (meta_size <= 244) { // most common cases
char header_and_meta[12 + meta_size];
PackRpcHeader(header_and_meta, meta_size, payload_size);
Expand Down
6 changes: 1 addition & 5 deletions src/brpc/policy/hulu_pbrpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ inline void PackHuluHeader(char* hulu_header, uint32_t meta_size, int body_size)
template <typename Meta>
static void SerializeHuluHeaderAndMeta(
butil::IOBuf* out, const Meta& meta, int payload_size) {
#if GOOGLE_PROTOBUF_VERSION >= 3010000
const uint32_t meta_size = meta.ByteSizeLong();
#else
const int meta_size = meta.ByteSize();
#endif
const uint32_t meta_size = PROTOBUF_BYTE_SIZE(meta);
if (meta_size <= 244) { // most common cases
char header_and_meta[12 + meta_size];
PackHuluHeader(header_and_meta, meta_size, payload_size);
Expand Down
6 changes: 1 addition & 5 deletions src/brpc/policy/public_pbrpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,7 @@ void PackPublicPbrpcRequest(butil::IOBuf* buf,
nshead.magic_num = NSHEAD_MAGICNUM;
snprintf(nshead.provider, sizeof(nshead.provider), "%s", PROVIDER);
nshead.version = NSHEAD_VERSION;
#if GOOGLE_PROTOBUF_VERSION >= 3010000
nshead.body_len = pbreq.ByteSizeLong();
#else
nshead.body_len = pbreq.ByteSize();
#endif
nshead.body_len = PROTOBUF_BYTE_SIZE(pbreq);
buf->append(&nshead, sizeof(nshead));

Span* span = ControllerPrivateAccessor(controller).span();
Expand Down
6 changes: 1 addition & 5 deletions src/brpc/policy/sofa_pbrpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ inline void PackSofaHeader(char* sofa_header, uint32_t meta_size, int body_size)

static void SerializeSofaHeaderAndMeta(
butil::IOBuf* out, const SofaRpcMeta& meta, int payload_size) {
#if GOOGLE_PROTOBUF_VERSION >= 3010000
const uint32_t meta_size = meta.ByteSizeLong();
#else
const int meta_size = meta.ByteSize();
#endif
const uint32_t meta_size = PROTOBUF_BYTE_SIZE(meta);
if (meta_size <= 232) { // most common cases
char header_and_meta[24 + meta_size];
PackSofaHeader(header_and_meta, meta_size, payload_size);
Expand Down
6 changes: 1 addition & 5 deletions src/brpc/policy/streaming_rpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ void PackStreamMessage(butil::IOBuf* out,
const StreamFrameMeta &fm,
const butil::IOBuf *data) {
const uint32_t data_length = data ? data->length() : 0;
#if GOOGLE_PROTOBUF_VERSION >= 3010000
const uint32_t meta_length = fm.ByteSizeLong();
#else
const uint32_t meta_length = fm.ByteSize();
#endif
const uint32_t meta_length = PROTOBUF_BYTE_SIZE(fm);
char head[12];
uint32_t* dummy = (uint32_t*)head; // suppresses strict-alias warning
*(uint32_t*)dummy = *(const uint32_t*)"STRM";
Expand Down
9 changes: 9 additions & 0 deletions src/brpc/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ class InputMessageBase;
DECLARE_uint64(max_body_size);
DECLARE_bool(log_error_text);

// Get the serialized byte size of the protobuf message,
// different versions of protobuf have different methods

#if GOOGLE_PROTOBUF_VERSION >= 3010000
#define PROTOBUF_BYTE_SIZE(message) ((message).ByteSizeLong())
#else
#define PROTOBUF_BYTE_SIZE(message) (static_cast<uint32_t>((message).ByteSize()))
#endif

// 3 steps to add a new Protocol:
// Step1: Add a new ProtocolType in src/brpc/options.proto
// as identifier of the Protocol.
Expand Down

0 comments on commit 0c6135f

Please sign in to comment.