Skip to content

Commit

Permalink
Set next transfer read size based on previous read length
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Dec 11, 2024
1 parent db44b54 commit 0c8c652
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
5 changes: 2 additions & 3 deletions cpp/controllers/abstract_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion cpp/controllers/abstract_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class AbstractController : public PhaseHandler
offset = 0;
}

bool UpdateTransferSize();
void UpdateTransferSize(int);
void UpdateOffsetAndLength();

void LogTrace(const string &s) const
Expand Down
7 changes: 4 additions & 3 deletions cpp/controllers/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ void Controller::Send()
return;
}

const bool pending_data = UpdateTransferSize();
const bool pending_data = GetRemainingLength();

if (pending_data && IsDataIn() && !TransferToHost()) {
return;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -474,7 +474,8 @@ bool Controller::TransferToHost()
assert(!CommandMetaData::Instance().GetCdbMetaData(static_cast<scsi_command>(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());
Expand Down
6 changes: 3 additions & 3 deletions cpp/test/abstract_controller_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 0c8c652

Please sign in to comment.