diff --git a/src/brpc/policy/baidu_rpc_protocol.cpp b/src/brpc/policy/baidu_rpc_protocol.cpp index a15e2246d2..5fe37e9419 100644 --- a/src/brpc/policy/baidu_rpc_protocol.cpp +++ b/src/brpc/policy/baidu_rpc_protocol.cpp @@ -62,7 +62,7 @@ DEFINE_bool(baidu_std_protocol_deliver_timeout_ms, false, // 5. Not supported: chunk_info // Pack header into `buf' -inline void PackRpcHeader(char* rpc_header, int meta_size, int payload_size) { +inline void PackRpcHeader(char* rpc_header, uint32_t meta_size, int payload_size) { uint32_t* dummy = (uint32_t*)rpc_header; // suppress strict-alias warning *dummy = *(uint32_t*)"PRPC"; butil::RawPacker(rpc_header + 4) @@ -72,7 +72,7 @@ inline void PackRpcHeader(char* rpc_header, int meta_size, int payload_size) { static void SerializeRpcHeaderAndMeta( butil::IOBuf* out, const RpcMeta& meta, int payload_size) { - const int meta_size = meta.ByteSize(); + const uint32_t meta_size = GetProtobufByteSize(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 8f9855e112..5a31ae7230 100644 --- a/src/brpc/policy/hulu_pbrpc_protocol.cpp +++ b/src/brpc/policy/hulu_pbrpc_protocol.cpp @@ -142,7 +142,7 @@ class HuluRawUnpacker { const char* _stream; }; -inline void PackHuluHeader(char* hulu_header, int meta_size, int body_size) { +inline void PackHuluHeader(char* hulu_header, uint32_t meta_size, int body_size) { uint32_t* dummy = reinterpret_cast(hulu_header); // suppress strict-alias warning *dummy = *reinterpret_cast("HULU"); HuluRawPacker rp(hulu_header + 4); @@ -152,7 +152,7 @@ inline void PackHuluHeader(char* hulu_header, int meta_size, int body_size) { template static void SerializeHuluHeaderAndMeta( butil::IOBuf* out, const Meta& meta, int payload_size) { - const int meta_size = meta.ByteSize(); + const uint32_t meta_size = GetProtobufByteSize(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 e5f34b2ffe..38a749dc72 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 = pbreq.ByteSize(); + nshead.body_len = GetProtobufByteSize(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 b0769d0f8b..f6ca32c26c 100644 --- a/src/brpc/policy/sofa_pbrpc_protocol.cpp +++ b/src/brpc/policy/sofa_pbrpc_protocol.cpp @@ -129,7 +129,7 @@ class SofaRawUnpacker { const char* _stream; }; -inline void PackSofaHeader(char* sofa_header, int meta_size, int body_size) { +inline void PackSofaHeader(char* sofa_header, uint32_t meta_size, int body_size) { uint32_t* dummy = reinterpret_cast(sofa_header); // suppress strict-alias warning *dummy = *reinterpret_cast("SOFA"); @@ -139,7 +139,7 @@ inline void PackSofaHeader(char* sofa_header, int meta_size, int body_size) { static void SerializeSofaHeaderAndMeta( butil::IOBuf* out, const SofaRpcMeta& meta, int payload_size) { - const int meta_size = meta.ByteSize(); + const uint32_t meta_size = GetProtobufByteSize(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 df47032de6..cd0e0fd3b2 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 = fm.ByteSize(); + const uint32_t meta_length = GetProtobufByteSize(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 b988a3963b..0492d0b5f7 100755 --- a/src/brpc/protocol.h +++ b/src/brpc/protocol.h @@ -56,6 +56,18 @@ 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 +// use template to avoid include `google/protobuf/message.h` +template +inline uint32_t GetProtobufByteSize(const T& message) { +#if GOOGLE_PROTOBUF_VERSION >= 3010000 + return message.ByteSizeLong(); +#else + return static_cast(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.