This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add message encoding/decoding utility to FML. #6017
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright 2018 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/fml/message.h" | ||
|
|
||
| #include "flutter/fml/logging.h" | ||
|
|
||
| namespace fml { | ||
|
|
||
| Message::Message() = default; | ||
|
|
||
| Message::~Message() = default; | ||
|
|
||
| static uint32_t NextPowerOfTwoSize(uint32_t x) { | ||
| if (x == 0) { | ||
| return 1; | ||
| } | ||
|
|
||
| --x; | ||
|
|
||
| x |= x >> 1; | ||
| x |= x >> 2; | ||
| x |= x >> 4; | ||
| x |= x >> 8; | ||
| x |= x >> 16; | ||
|
|
||
| return x + 1; | ||
| } | ||
|
|
||
| const uint8_t* Message::GetBuffer() const { | ||
| return buffer_; | ||
| } | ||
|
|
||
| size_t Message::GetBufferSize() const { | ||
| return buffer_length_; | ||
| } | ||
|
|
||
| size_t Message::GetDataLength() const { | ||
| return data_length_; | ||
| } | ||
|
|
||
| size_t Message::GetSizeRead() const { | ||
| return size_read_; | ||
| } | ||
|
|
||
| bool Message::Reserve(size_t size) { | ||
| if (buffer_length_ >= size) { | ||
| return true; | ||
| } | ||
| return Resize(NextPowerOfTwoSize(size)); | ||
| } | ||
|
|
||
| bool Message::Resize(size_t size) { | ||
| if (buffer_ == nullptr) { | ||
| // This is the initial resize where we have no previous buffer. | ||
| FML_DCHECK(buffer_length_ == 0); | ||
|
|
||
| void* buffer = ::malloc(size); | ||
| const bool success = buffer != nullptr; | ||
|
|
||
| if (success) { | ||
| buffer_ = static_cast<uint8_t*>(buffer); | ||
| buffer_length_ = size; | ||
| } | ||
|
|
||
| return success; | ||
| } | ||
|
|
||
| FML_DCHECK(size > buffer_length_); | ||
|
|
||
| void* resized = ::realloc(buffer_, size); | ||
|
|
||
| const bool success = resized != nullptr; | ||
|
|
||
| // In case of failure, the input buffer to realloc is still valid. | ||
| if (success) { | ||
| buffer_ = static_cast<uint8_t*>(resized); | ||
| buffer_length_ = size; | ||
| } | ||
|
|
||
| return success; | ||
| } | ||
|
|
||
| uint8_t* Message::PrepareEncode(size_t size) { | ||
| if (!Reserve(data_length_ + size)) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| auto old_length = data_length_; | ||
| data_length_ += size; | ||
| return buffer_ + old_length; | ||
| } | ||
|
|
||
| uint8_t* Message::PrepareDecode(size_t size) { | ||
| if ((size + size_read_) > buffer_length_) { | ||
| return nullptr; | ||
| } | ||
| auto buffer = buffer_ + size_read_; | ||
| size_read_ += size; | ||
| return buffer; | ||
| } | ||
|
|
||
| void Message::ResetRead() { | ||
| size_read_ = 0; | ||
| } | ||
|
|
||
| } // namespace fml | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| // Copyright 2018 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_FML_MESSAGE_H_ | ||
| #define FLUTTER_FML_MESSAGE_H_ | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <memory> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include "flutter/fml/compiler_specific.h" | ||
| #include "flutter/fml/macros.h" | ||
|
|
||
| namespace fml { | ||
|
|
||
| #define FML_SERIALIZE(message, value) \ | ||
| if (!message.Encode(value)) { \ | ||
| return false; \ | ||
| } | ||
|
|
||
| #define FML_SERIALIZE_TRAITS(message, value, traits) \ | ||
| if (!message.Encode<traits>(value)) { \ | ||
| return false; \ | ||
| } | ||
|
|
||
| #define FML_DESERIALIZE(message, value) \ | ||
| if (!message.Decode(value)) { \ | ||
| return false; \ | ||
| } | ||
|
|
||
| #define FML_DESERIALIZE_TRAITS(message, value, traits) \ | ||
| if (!message.Decode<traits>(value)) { \ | ||
| return false; \ | ||
| } | ||
|
|
||
| class Message; | ||
|
|
||
| class MessageSerializable { | ||
| public: | ||
| virtual ~MessageSerializable() = default; | ||
|
|
||
| virtual bool Serialize(Message& message) const = 0; | ||
|
|
||
| virtual bool Deserialize(Message& message) = 0; | ||
|
|
||
| virtual size_t GetSerializableTag() const { return 0; }; | ||
| }; | ||
|
|
||
| // The traits passed to the encode/decode calls that accept traits should be | ||
| // something like the following. | ||
| // | ||
| // class MessageSerializableTraits { | ||
| // static size_t GetSerializableTag(const T&); | ||
| // static std::unique_ptr<T> CreateForSerializableTag(size_t tag); | ||
| // }; | ||
|
|
||
| template <class T> | ||
| struct Serializable : public std::integral_constant< | ||
| bool, | ||
| std::is_trivially_copyable<T>::value || | ||
| std::is_base_of<MessageSerializable, T>::value> { | ||
| }; | ||
|
|
||
| // Utility class to encode and decode |Serializable| types to and from a buffer. | ||
| // Elements have to be read back into the same order they were written. | ||
| class Message { | ||
| public: | ||
| Message(); | ||
|
|
||
| ~Message(); | ||
|
|
||
| const uint8_t* GetBuffer() const; | ||
|
|
||
| size_t GetBufferSize() const; | ||
|
|
||
| size_t GetDataLength() const; | ||
|
|
||
| size_t GetSizeRead() const; | ||
|
|
||
| void ResetRead(); | ||
|
|
||
| // Encoders. | ||
|
|
||
| template <typename T, | ||
| typename = std::enable_if_t<std::is_trivially_copyable<T>::value>> | ||
| FML_WARN_UNUSED_RESULT bool Encode(const T& value) { | ||
| if (auto buffer = PrepareEncode(sizeof(T))) { | ||
| ::memcpy(buffer, &value, sizeof(T)); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| FML_WARN_UNUSED_RESULT bool Encode(const MessageSerializable& value) { | ||
| return value.Serialize(*this); | ||
| } | ||
|
|
||
| template <typename Traits, | ||
| typename T, | ||
| typename = std::enable_if_t< | ||
| std::is_base_of<MessageSerializable, T>::value>> | ||
| FML_WARN_UNUSED_RESULT bool Encode(const std::unique_ptr<T>& value) { | ||
| // Encode if null. | ||
| if (!Encode(static_cast<bool>(value))) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!value) { | ||
| return true; | ||
| } | ||
|
|
||
| // Encode the type. | ||
| if (!Encode(Traits::GetSerializableTag(*value.get()))) { | ||
| return false; | ||
| } | ||
|
|
||
| // Encode the value. | ||
| if (!Encode(*value.get())) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Decoders. | ||
|
|
||
| template <typename T, | ||
| typename = std::enable_if_t<std::is_trivially_copyable<T>::value>> | ||
| FML_WARN_UNUSED_RESULT bool Decode(T& value) { | ||
| if (auto buffer = PrepareDecode(sizeof(T))) { | ||
| ::memcpy(&value, buffer, sizeof(T)); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| FML_WARN_UNUSED_RESULT bool Decode(MessageSerializable& value) { | ||
| return value.Deserialize(*this); | ||
| } | ||
|
|
||
| template <typename Traits, | ||
| typename T, | ||
| typename = std::enable_if_t< | ||
| std::is_base_of<MessageSerializable, T>::value>> | ||
| FML_WARN_UNUSED_RESULT bool Decode(std::unique_ptr<T>& value) { | ||
| // Decode if null. | ||
| bool is_null = false; | ||
| if (!Decode(is_null)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (is_null) { | ||
| return true; | ||
| } | ||
|
|
||
| // Decode type. | ||
| size_t tag = 0; | ||
| if (!Decode(tag)) { | ||
| return false; | ||
| } | ||
|
|
||
| std::unique_ptr<T> new_value = Traits::CreateForSerializableTag(tag); | ||
| if (!new_value) { | ||
| return false; | ||
| } | ||
|
|
||
| // Decode value. | ||
| if (!Decode(*new_value.get())) { | ||
| return false; | ||
| } | ||
|
|
||
| std::swap(value, new_value); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private: | ||
| uint8_t* buffer_ = nullptr; | ||
| size_t buffer_length_ = 0; | ||
| size_t data_length_ = 0; | ||
| size_t size_read_ = 0; | ||
|
|
||
| FML_WARN_UNUSED_RESULT | ||
| bool Reserve(size_t size); | ||
|
|
||
| FML_WARN_UNUSED_RESULT | ||
| bool Resize(size_t size); | ||
|
|
||
| FML_WARN_UNUSED_RESULT | ||
| uint8_t* PrepareEncode(size_t size); | ||
|
|
||
| FML_WARN_UNUSED_RESULT | ||
| uint8_t* PrepareDecode(size_t size); | ||
|
|
||
| FML_DISALLOW_COPY_AND_ASSIGN(Message); | ||
| }; | ||
|
|
||
| } // namespace fml | ||
|
|
||
| #endif // FLUTTER_FML_MESSAGE_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can
buffer_be changed to astd::vector<uint8_t>instead of implementing resize logic here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not figure out how to deal with allocation errors on malloc and realloc when using the std::vector container. This implementation handles those edge cases and the FML_WARN_UNUSED_RESULT makes it hard to ignore those errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vector::resize will throw on an allocation failure. Also, many allocators will always report success, followed by a segfault when the caller tries to use unavailable memory.
Either way is fine though