Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from synchronous to async read #127

Draft
wants to merge 1 commit into
base: Omega
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/SFTPSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ CSFTPSession::CSFTPSession(const kodi::addon::VFSUrl& url)
Disconnect();

m_LastActive = std::chrono::high_resolution_clock::now();
read_request_identifier = 0;
}

CSFTPSession::~CSFTPSession()
Expand Down Expand Up @@ -296,7 +297,36 @@ int CSFTPSession::Read(sftp_file handle, void* buffer, size_t length)
{
std::unique_lock<std::recursive_mutex> lock(m_lock);
m_LastActive = std::chrono::high_resolution_clock::now();
int result = sftp_read(handle, buffer, length);

// Start read request if no other read was already queued
if (!read_request_identifier) {
read_request_length = length;
read_request_identifier = sftp_async_read_begin(handle, read_request_length);
}

// Retrieve read from libssh while handling size differences of Kodi-requested and already sftp-requested buffers
size_t result = -1;
if (length >= read_request_length) {
result = sftp_async_read(handle, buffer, length, read_request_identifier);
} else {
void* temp_buf = malloc(read_request_length);
if (!temp_buf) {
kodi::Log(ADDON_LOG_ERROR, "SFTPSession::Read - Failed to allocate a temporary buffer with size %d", read_request_length);
return -1;
}
result = sftp_async_read(handle, temp_buf, read_request_length, read_request_identifier);
if (result > length) {
Seek(handle, GetPosition(handle) - (result - length));
kodi::Log(ADDON_LOG_INFO, "SFTPSession::Read - this read requested less bytes than before, requiring inefficient copying of buffers. Last read: %d, this read: %d", result, length);
}
result = std::min(result, length);
memcpy(buffer, temp_buf, result);
free(temp_buf);
}

// Already request the next chunk, assume the length will be the same
read_request_length = std::min(length, read_request_length);
read_request_identifier = sftp_async_read_begin(handle, read_request_length);
return result;
}

Expand Down
2 changes: 2 additions & 0 deletions src/SFTPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class CSFTPSession
bool m_connected;
ssh_session m_session;
sftp_session m_sftp_session;
int read_request_identifier;
size_t read_request_length;
std::chrono::high_resolution_clock::time_point m_LastActive;
};

Expand Down