From 0c8c65299bc6df94413cf6e3d7b48c838f281b01 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Wed, 11 Dec 2024 16:54:50 +0100 Subject: [PATCH] Set next transfer read size based on previous read length --- cpp/controllers/abstract_controller.cpp | 5 ++--- cpp/controllers/abstract_controller.h | 2 +- cpp/controllers/controller.cpp | 7 ++++--- cpp/test/abstract_controller_test.cpp | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cpp/controllers/abstract_controller.cpp b/cpp/controllers/abstract_controller.cpp index 79094217..131860e9 100644 --- a/cpp/controllers/abstract_controller.cpp +++ b/cpp/controllers/abstract_controller.cpp @@ -78,13 +78,12 @@ void AbstractController::SetTransferSize(int length, int size) chunk_size = length < size ? length : size; } -bool AbstractController::UpdateTransferSize() +void AbstractController::UpdateTransferSize(int length) { - remaining_length -= chunk_size; + remaining_length -= length < chunk_size ? length : chunk_size; if (remaining_length < chunk_size) { chunk_size = remaining_length; } - return remaining_length > 0; } void AbstractController::UpdateOffsetAndLength() diff --git a/cpp/controllers/abstract_controller.h b/cpp/controllers/abstract_controller.h index 588cac37..20bde4ac 100644 --- a/cpp/controllers/abstract_controller.h +++ b/cpp/controllers/abstract_controller.h @@ -131,7 +131,7 @@ class AbstractController : public PhaseHandler offset = 0; } - bool UpdateTransferSize(); + void UpdateTransferSize(int); void UpdateOffsetAndLength(); void LogTrace(const string &s) const diff --git a/cpp/controllers/controller.cpp b/cpp/controllers/controller.cpp index 29e82feb..431695e8 100644 --- a/cpp/controllers/controller.cpp +++ b/cpp/controllers/controller.cpp @@ -355,7 +355,7 @@ void Controller::Send() return; } - const bool pending_data = UpdateTransferSize(); + const bool pending_data = GetRemainingLength(); if (pending_data && IsDataIn() && !TransferToHost()) { return; @@ -427,7 +427,7 @@ void Controller::Receive() const int current_remaining_length = GetRemainingLength(); const int current_chunk_size = GetChunkSize(); - const bool pending_data = UpdateTransferSize(); + const bool pending_data = current_remaining_length; // Processing after receiving data switch (GetPhase()) { @@ -474,7 +474,8 @@ bool Controller::TransferToHost() assert(!CommandMetaData::Instance().GetCdbMetaData(static_cast(GetCdb()[0])).has_data_out); try { - SetCurrentLength(GetDeviceForLun(GetEffectiveLun())->ReadData(GetBuffer())); + const int length = GetDeviceForLun(GetEffectiveLun())->ReadData(GetBuffer()); + UpdateTransferSize(length); } catch (const scsi_exception &e) { Error(e.get_sense_key(), e.get_asc()); diff --git a/cpp/test/abstract_controller_test.cpp b/cpp/test/abstract_controller_test.cpp index d825ad3d..be85eb13 100644 --- a/cpp/test/abstract_controller_test.cpp +++ b/cpp/test/abstract_controller_test.cpp @@ -101,15 +101,15 @@ TEST(AbstractControllerTest, TransferSize) EXPECT_EQ(3, controller.GetTotalLength()); EXPECT_EQ(3, controller.GetRemainingLength()); EXPECT_EQ(1, controller.GetChunkSize()); - EXPECT_TRUE(controller.UpdateTransferSize()); + controller.UpdateTransferSize(1); EXPECT_EQ(3, controller.GetTotalLength()); EXPECT_EQ(2, controller.GetRemainingLength()); EXPECT_EQ(1, controller.GetChunkSize()); - EXPECT_TRUE(controller.UpdateTransferSize()); + controller.UpdateTransferSize(1); EXPECT_EQ(3, controller.GetTotalLength()); EXPECT_EQ(1, controller.GetRemainingLength()); EXPECT_EQ(1, controller.GetChunkSize()); - EXPECT_FALSE(controller.UpdateTransferSize()); + controller.UpdateTransferSize(1); EXPECT_EQ(3, controller.GetTotalLength()); EXPECT_EQ(0, controller.GetRemainingLength()); EXPECT_EQ(0, controller.GetChunkSize());