From 587f3caae6156af860968e3e91eef00fdeeda1e5 Mon Sep 17 00:00:00 2001 From: yangzhg Date: Wed, 30 Mar 2022 09:43:59 +0800 Subject: [PATCH] use method instead of macro --- src/brpc/policy/baidu_rpc_protocol.cpp | 2 +- src/brpc/policy/hulu_pbrpc_protocol.cpp | 2 +- src/brpc/policy/public_pbrpc_protocol.cpp | 2 +- src/brpc/policy/sofa_pbrpc_protocol.cpp | 2 +- src/brpc/policy/streaming_rpc_protocol.cpp | 2 +- src/brpc/protocol.h | 9 ++++++--- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/brpc/policy/baidu_rpc_protocol.cpp b/src/brpc/policy/baidu_rpc_protocol.cpp index ad83a85ec0..93775a9e7c 100644 --- a/src/brpc/policy/baidu_rpc_protocol.cpp +++ b/src/brpc/policy/baidu_rpc_protocol.cpp @@ -69,7 +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) { - const uint32_t meta_size = PROTOBUF_BYTE_SIZE(meta); + const uint32_t meta_size = get_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); diff --git a/src/brpc/policy/hulu_pbrpc_protocol.cpp b/src/brpc/policy/hulu_pbrpc_protocol.cpp index aa04b3106d..d789a152b0 100644 --- a/src/brpc/policy/hulu_pbrpc_protocol.cpp +++ b/src/brpc/policy/hulu_pbrpc_protocol.cpp @@ -152,7 +152,7 @@ inline void PackHuluHeader(char* hulu_header, uint32_t meta_size, int body_size) template static void SerializeHuluHeaderAndMeta( butil::IOBuf* out, const Meta& meta, int payload_size) { - const uint32_t meta_size = PROTOBUF_BYTE_SIZE(meta); + const uint32_t meta_size = get_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); diff --git a/src/brpc/policy/public_pbrpc_protocol.cpp b/src/brpc/policy/public_pbrpc_protocol.cpp index 1aba5fa0f5..79e84bc6de 100644 --- a/src/brpc/policy/public_pbrpc_protocol.cpp +++ b/src/brpc/policy/public_pbrpc_protocol.cpp @@ -266,7 +266,7 @@ void PackPublicPbrpcRequest(butil::IOBuf* buf, nshead.magic_num = NSHEAD_MAGICNUM; snprintf(nshead.provider, sizeof(nshead.provider), "%s", PROVIDER); nshead.version = NSHEAD_VERSION; - nshead.body_len = PROTOBUF_BYTE_SIZE(pbreq); + nshead.body_len = get_protobuf_byte_size(pbreq); buf->append(&nshead, sizeof(nshead)); Span* span = ControllerPrivateAccessor(controller).span(); diff --git a/src/brpc/policy/sofa_pbrpc_protocol.cpp b/src/brpc/policy/sofa_pbrpc_protocol.cpp index 5f6125751c..2176b74c44 100644 --- a/src/brpc/policy/sofa_pbrpc_protocol.cpp +++ b/src/brpc/policy/sofa_pbrpc_protocol.cpp @@ -139,7 +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) { - const uint32_t meta_size = PROTOBUF_BYTE_SIZE(meta); + const uint32_t meta_size = get_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); diff --git a/src/brpc/policy/streaming_rpc_protocol.cpp b/src/brpc/policy/streaming_rpc_protocol.cpp index 2097b6f83d..8f88edc168 100644 --- a/src/brpc/policy/streaming_rpc_protocol.cpp +++ b/src/brpc/policy/streaming_rpc_protocol.cpp @@ -43,7 +43,7 @@ void PackStreamMessage(butil::IOBuf* out, const StreamFrameMeta &fm, const butil::IOBuf *data) { const uint32_t data_length = data ? data->length() : 0; - const uint32_t meta_length = PROTOBUF_BYTE_SIZE(fm); + const uint32_t meta_length = get_protobuf_byte_size(fm); char head[12]; uint32_t* dummy = (uint32_t*)head; // suppresses strict-alias warning *(uint32_t*)dummy = *(const uint32_t*)"STRM"; diff --git a/src/brpc/protocol.h b/src/brpc/protocol.h index f54dd5de4b..193516f328 100755 --- a/src/brpc/protocol.h +++ b/src/brpc/protocol.h @@ -58,12 +58,15 @@ DECLARE_bool(log_error_text); // Get the serialized byte size of the protobuf message, // different versions of protobuf have different methods - +// use template to avoid include `google/protobuf/message.h` +template +inline unit32_t get_protobuf_byte_size(const T& message) { #if GOOGLE_PROTOBUF_VERSION >= 3010000 -#define PROTOBUF_BYTE_SIZE(message) ((message).ByteSizeLong()) + return message.ByteSizeLong(); #else -#define PROTOBUF_BYTE_SIZE(message) (static_cast((message).ByteSize())) + return static_cast((message).ByteSize()); #endif +} // 3 steps to add a new Protocol: // Step1: Add a new ProtocolType in src/brpc/options.proto