From c930e026240c2c255b77ad8b2322aeea9ab92ea3 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Sat, 7 Nov 2020 23:09:45 +0330 Subject: [PATCH] LibIPC: Add support for passing around ByteBuffers and HashMap It should be noted that using a shared buffer should still be preferred over passing a raw ByteBuffer over the wire. --- Libraries/LibIPC/Decoder.cpp | 19 +++++++++++++++++++ Libraries/LibIPC/Decoder.h | 21 +++++++++++++++++++++ Libraries/LibIPC/Encoder.cpp | 8 ++++++++ Libraries/LibIPC/Encoder.h | 11 +++++++++++ 4 files changed, 59 insertions(+) diff --git a/Libraries/LibIPC/Decoder.cpp b/Libraries/LibIPC/Decoder.cpp index 6854c8f0169..c76f9a73302 100644 --- a/Libraries/LibIPC/Decoder.cpp +++ b/Libraries/LibIPC/Decoder.cpp @@ -112,6 +112,25 @@ bool Decoder::decode(String& value) return !m_stream.handle_any_error(); } +bool Decoder::decode(ByteBuffer& value) +{ + i32 length = 0; + m_stream >> length; + if (m_stream.handle_any_error()) + return false; + if (length < 0) { + value = {}; + return true; + } + if (length == 0) { + value = ByteBuffer::create_uninitialized(0); + return true; + } + value = ByteBuffer::create_uninitialized(length); + m_stream >> value.bytes(); + return !m_stream.handle_any_error(); +} + bool Decoder::decode(URL& value) { String string; diff --git a/Libraries/LibIPC/Decoder.h b/Libraries/LibIPC/Decoder.h index 37c3d6a848f..0b5d39041eb 100644 --- a/Libraries/LibIPC/Decoder.h +++ b/Libraries/LibIPC/Decoder.h @@ -60,8 +60,29 @@ public: bool decode(i64&); bool decode(float&); bool decode(String&); + bool decode(ByteBuffer&); bool decode(URL&); bool decode(Dictionary&); + template + bool decode(HashMap& hashmap) + { + u32 size; + if (!decode(size) || size > NumericLimits::max()) + return false; + + for (size_t i = 0; i < size; ++i) { + K key; + if (!decode(key)) + return false; + + V value; + if (!decode(value)) + return false; + + hashmap.set(move(key), move(value)); + } + return true; + } template bool decode(T& value) diff --git a/Libraries/LibIPC/Encoder.cpp b/Libraries/LibIPC/Encoder.cpp index af93c9918e7..c0eb8d670ef 100644 --- a/Libraries/LibIPC/Encoder.cpp +++ b/Libraries/LibIPC/Encoder.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -141,6 +142,13 @@ Encoder& Encoder::operator<<(const String& value) return *this << value.view(); } +Encoder& Encoder::operator<<(const ByteBuffer& value) +{ + *this << static_cast(value.size()); + m_buffer.append(value.data(), value.size()); + return *this; +} + Encoder& Encoder::operator<<(const URL& value) { return *this << value.to_string(); diff --git a/Libraries/LibIPC/Encoder.h b/Libraries/LibIPC/Encoder.h index b5869c5a82e..2af6d0fbca3 100644 --- a/Libraries/LibIPC/Encoder.h +++ b/Libraries/LibIPC/Encoder.h @@ -58,8 +58,19 @@ public: Encoder& operator<<(const char*); Encoder& operator<<(const StringView&); Encoder& operator<<(const String&); + Encoder& operator<<(const ByteBuffer&); Encoder& operator<<(const URL&); Encoder& operator<<(const Dictionary&); + template + Encoder& operator<<(const HashMap& hashmap) + { + *this << (u32)hashmap.size(); + for (auto it : hashmap) { + *this << it.key; + *this << it.value; + } + return *this; + } template Encoder& operator<<(const Vector& vector)