From 62e964e383a4fa5eddc9f5f8035d2cd2e9d00bef Mon Sep 17 00:00:00 2001 From: Igor Sapego Date: Thu, 26 Dec 2024 05:44:23 +0100 Subject: [PATCH 1/2] IGNITE-23598 Fix codec --- .../include/ignite/network/data_buffer.h | 8 +-- .../cpp/network/src/network/data_buffer.cpp | 21 ++++---- .../src/network/length_prefix_codec.cpp | 2 +- .../cpp/thin-client-test/CMakeLists.txt | 1 + .../src/network_codec_test.cpp | 49 +++++++++++++++++++ 5 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 modules/platforms/cpp/thin-client-test/src/network_codec_test.cpp diff --git a/modules/platforms/cpp/network/include/ignite/network/data_buffer.h b/modules/platforms/cpp/network/include/ignite/network/data_buffer.h index cf2a3d9f655bf..9817305c842b8 100644 --- a/modules/platforms/cpp/network/include/ignite/network/data_buffer.h +++ b/modules/platforms/cpp/network/include/ignite/network/data_buffer.h @@ -48,9 +48,9 @@ namespace ignite * * @param data Data. * @param pos Start of data. - * @param len Length. + * @param end End of the data. */ - DataBuffer(const impl::interop::SP_ConstInteropMemory& data, int32_t pos, int32_t len); + DataBuffer(const impl::interop::SP_ConstInteropMemory& data, int32_t pos, int32_t end); /** * Destructor. @@ -127,8 +127,8 @@ namespace ignite /** Position in current data. */ int32_t position; - /** Data length. */ - int32_t length; + /** End position. */ + int32_t endPos; /** Data */ impl::interop::SP_ConstInteropMemory data; diff --git a/modules/platforms/cpp/network/src/network/data_buffer.cpp b/modules/platforms/cpp/network/src/network/data_buffer.cpp index 9caf5ca972b2f..0bd76d189cf56 100644 --- a/modules/platforms/cpp/network/src/network/data_buffer.cpp +++ b/modules/platforms/cpp/network/src/network/data_buffer.cpp @@ -26,7 +26,7 @@ namespace ignite { DataBuffer::DataBuffer() : position(0), - length(0), + endPos(0), data() { // No-op. @@ -34,15 +34,15 @@ namespace ignite DataBuffer::DataBuffer(const impl::interop::SP_ConstInteropMemory& data0) : position(0), - length(data0.Get()->Length()), + endPos(data0.Get()->Length()), data(data0) { // No-op. } - DataBuffer::DataBuffer(const impl::interop::SP_ConstInteropMemory& data0, int32_t pos, int32_t len) : + DataBuffer::DataBuffer(const impl::interop::SP_ConstInteropMemory& data0, int32_t pos, int32_t end) : position(pos), - length(len), + endPos(end), data(data0) { // No-op. @@ -75,7 +75,7 @@ namespace ignite if (!data.IsValid()) return 0; - return length - position; + return endPos - position; } bool DataBuffer::IsEmpty() const @@ -94,11 +94,12 @@ namespace ignite void DataBuffer::Advance(int32_t val) { position += val; + endPos; } impl::interop::InteropInputStream DataBuffer::GetInputStream() const { - impl::interop::InteropInputStream stream = impl::interop::InteropInputStream(data.Get(), length); + impl::interop::InteropInputStream stream = impl::interop::InteropInputStream(data.Get(), endPos); stream.Position(position); return stream; @@ -109,11 +110,11 @@ namespace ignite if (IsEmpty()) return DataBuffer(); - impl::interop::SP_InteropMemory mem(new impl::interop::InteropUnpooledMemory(length)); - mem.Get()->Length(length); - std::memcpy(mem.Get()->Data(), data.Get()->Data() + position, length); + impl::interop::SP_InteropMemory mem(new impl::interop::InteropUnpooledMemory(endPos)); + mem.Get()->Length(endPos); + std::memcpy(mem.Get()->Data(), data.Get()->Data() + position, endPos); - return DataBuffer(mem, 0, length); + return DataBuffer(mem, 0, endPos); } void DataBuffer::Skip(int32_t bytes) diff --git a/modules/platforms/cpp/network/src/network/length_prefix_codec.cpp b/modules/platforms/cpp/network/src/network/length_prefix_codec.cpp index b1333fd99e972..e83e2e1622b6e 100644 --- a/modules/platforms/cpp/network/src/network/length_prefix_codec.cpp +++ b/modules/platforms/cpp/network/src/network/length_prefix_codec.cpp @@ -45,7 +45,7 @@ namespace ignite DataBuffer LengthPrefixCodec::Decode(DataBuffer& data) { - if (packet.IsValid() && packet.Get()->Length() == (PACKET_HEADER_SIZE + packetSize)) + if (packet.IsValid() && packetSize != -1 && packet.Get()->Length() == (PACKET_HEADER_SIZE + packetSize)) { packetSize = -1; packet.Get()->Length(0); diff --git a/modules/platforms/cpp/thin-client-test/CMakeLists.txt b/modules/platforms/cpp/thin-client-test/CMakeLists.txt index 3b2f095a223ac..58695c628bab4 100644 --- a/modules/platforms/cpp/thin-client-test/CMakeLists.txt +++ b/modules/platforms/cpp/thin-client-test/CMakeLists.txt @@ -38,6 +38,7 @@ set(SOURCES src/test_utils.cpp src/ignite_client_test.cpp src/interop_test.cpp + src/network_codec_test.cpp src/scan_query_test.cpp src/sql_fields_query_test.cpp src/auth_test.cpp diff --git a/modules/platforms/cpp/thin-client-test/src/network_codec_test.cpp b/modules/platforms/cpp/thin-client-test/src/network_codec_test.cpp new file mode 100644 index 0000000000000..651b53030734f --- /dev/null +++ b/modules/platforms/cpp/thin-client-test/src/network_codec_test.cpp @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +using namespace boost::unit_test; +using namespace ignite::network; + + +BOOST_AUTO_TEST_SUITE(NetworkCodecTestSuite) + +BOOST_AUTO_TEST_CASE(LengthPrefixCodec_3bytes) +{ + ignite::impl::interop::SP_InteropMemory mem(new ignite::impl::interop::InteropUnpooledMemory(8)); + ignite::impl::interop::InteropOutputStream stream(mem.Get()); + stream.WriteInt32(4); + stream.WriteInt32(123456789); + + DataBuffer in1(mem, 0, 3); + DataBuffer in2(mem, 3, 8); + + SP_Codec codec(new LengthPrefixCodec()); + + DataBuffer out1 = codec.Get()->Decode(in1); + BOOST_CHECK(out1.IsEmpty()); + + DataBuffer out2 = codec.Get()->Decode(in2); + BOOST_CHECK(!out2.IsEmpty()); + BOOST_CHECK_EQUAL(out2.GetSize(), 8); +} + +BOOST_AUTO_TEST_SUITE_END() From 2c1b2c2f94f58037d02778c1e8e92bd8d426a042 Mon Sep 17 00:00:00 2001 From: Igor Sapego Date: Thu, 26 Dec 2024 11:19:50 +0100 Subject: [PATCH 2/2] IGNITE-23598 Fix --- modules/platforms/cpp/network/src/network/data_buffer.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/platforms/cpp/network/src/network/data_buffer.cpp b/modules/platforms/cpp/network/src/network/data_buffer.cpp index 0bd76d189cf56..c3e281f87ff9c 100644 --- a/modules/platforms/cpp/network/src/network/data_buffer.cpp +++ b/modules/platforms/cpp/network/src/network/data_buffer.cpp @@ -94,7 +94,6 @@ namespace ignite void DataBuffer::Advance(int32_t val) { position += val; - endPos; } impl::interop::InteropInputStream DataBuffer::GetInputStream() const