Skip to content

Commit

Permalink
fix a small 'build fail' issue caused by the deprecation of ByteSize(…
Browse files Browse the repository at this point in the history
…) of protobuf message (#433)

* change Legacy ByteSize() API of protobuf message to ByteSize Long(), because ByteSize() was deprecated in new version's protobuf

* Replace ByteSize() by ByteSizeLong() just for 'Protobuf Message' of which version number strictly greater than v3.9.2.

Cherry-pick: 6a4da5b
  • Loading branch information
OjackstevensonO authored and chenshuo committed Apr 28, 2020
1 parent c8b2cbc commit bd50e5c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
24 changes: 22 additions & 2 deletions examples/protobuf/codec/codec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,34 @@ void ProtobufCodec::fillEmptyBuffer(Buffer* buf, const google::protobuf::Message
// code copied from MessageLite::SerializeToArray() and MessageLite::SerializePartialToArray().
GOOGLE_DCHECK(message.IsInitialized()) << InitializationErrorMessage("serialize", message);

int byte_size = message.ByteSize();
/**
* 'ByteSize()' of message is deprecated in Protocol Buffers v3.4.0 firstly.
* But, till to v3.11.0, it just getting start to be marked by '__attribute__((deprecated()))'.
* So, here, v3.9.2 is selected as maximum version using 'ByteSize()' to avoid
* potential effect for previous muduo code/projects as far as possible.
* Note: All information above just INFER from
* 1) https://github.com/protocolbuffers/protobuf/releases/tag/v3.4.0
* 2) MACRO in file 'include/google/protobuf/port_def.inc'.
* eg. '#define PROTOBUF_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))'.
* In addition, usage of 'ToIntSize()' comes from Impl of ByteSize() in new version's Protocol Buffers.
*/

#if GOOGLE_PROTOBUF_VERSION > 3009002
int byte_size = google::protobuf::internal::ToIntSize(message.ByteSizeLong());
#else
int byte_size = message.ByteSize();
#endif
buf->ensureWritableBytes(byte_size);

uint8_t* start = reinterpret_cast<uint8_t*>(buf->beginWrite());
uint8_t* end = message.SerializeWithCachedSizesToArray(start);
if (end - start != byte_size)
{
ByteSizeConsistencyError(byte_size, message.ByteSize(), static_cast<int>(end - start));
#if GOOGLE_PROTOBUF_VERSION > 3009002
ByteSizeConsistencyError(byte_size, google::protobuf::internal::ToIntSize(message.ByteSizeLong()), static_cast<int>(end - start));
#else
ByteSizeConsistencyError(byte_size, message.ByteSize(), static_cast<int>(end - start));
#endif
}
buf->hasWritten(byte_size);

Expand Down
21 changes: 19 additions & 2 deletions muduo/net/protobuf/ProtobufCodecLite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,31 @@ int ProtobufCodecLite::serializeToBuffer(const google::protobuf::Message& messag
// code copied from MessageLite::SerializeToArray() and MessageLite::SerializePartialToArray().
GOOGLE_DCHECK(message.IsInitialized()) << InitializationErrorMessage("serialize", message);

int byte_size = message.ByteSize();
/**
* 'ByteSize()' of message is deprecated in Protocol Buffers v3.4.0 firstly. But, till to v3.11.0, it just getting start to be marked by '__attribute__((deprecated()))'.
* So, here, v3.9.2 is selected as maximum version using 'ByteSize()' to avoid potential effect for previous muduo code/projects as far as possible.
* Note: All information above just INFER from
* 1) https://github.com/protocolbuffers/protobuf/releases/tag/v3.4.0
* 2) MACRO in file 'include/google/protobuf/port_def.inc'. eg. '#define PROTOBUF_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))'.
* In addition, usage of 'ToIntSize()' comes from Impl of ByteSize() in new version's Protocol Buffers.
*/

#if GOOGLE_PROTOBUF_VERSION > 3009002
int byte_size = google::protobuf::internal::ToIntSize(message.ByteSizeLong());
#else
int byte_size = message.ByteSize();
#endif
buf->ensureWritableBytes(byte_size + kChecksumLen);

uint8_t* start = reinterpret_cast<uint8_t*>(buf->beginWrite());
uint8_t* end = message.SerializeWithCachedSizesToArray(start);
if (end - start != byte_size)
{
ByteSizeConsistencyError(byte_size, message.ByteSize(), static_cast<int>(end - start));
#if GOOGLE_PROTOBUF_VERSION > 3009002
ByteSizeConsistencyError(byte_size, google::protobuf::internal::ToIntSize(message.ByteSizeLong()), static_cast<int>(end - start));
#else
ByteSizeConsistencyError(byte_size, message.ByteSize(), static_cast<int>(end - start));
#endif
}
buf->hasWritten(byte_size);
return byte_size;
Expand Down

0 comments on commit bd50e5c

Please sign in to comment.