Complete ring buffer and direct download support #26

This commit is contained in:
Scott E. Graves 2024-12-23 10:04:30 -06:00
parent a8f16ab89d
commit 63c1b83f18
2 changed files with 23 additions and 4 deletions

View File

@ -35,14 +35,21 @@ direct_open_file::direct_open_file(std::uint64_t chunk_size,
filesystem_item fsi, i_provider &provider) filesystem_item fsi, i_provider &provider)
: open_file_base(chunk_size, chunk_timeout, fsi, provider, true), : open_file_base(chunk_size, chunk_timeout, fsi, provider, true),
total_chunks_(static_cast<std::size_t>( total_chunks_(static_cast<std::size_t>(
utils::divide_with_ceiling(fsi.size, chunk_size))) {} utils::divide_with_ceiling(fsi.size, chunk_size))) {
event_system::instance().raise<download_begin>(fsi_.api_path, "",
api_error::download_stopped);
}
direct_open_file::~direct_open_file() { close(); } direct_open_file::~direct_open_file() { close(); }
auto direct_open_file::close() -> bool { auto direct_open_file::close() -> bool {
stop_requested_ = true; stop_requested_ = true;
last_progress_ = 0U; last_progress_ = 0U;
return open_file_base::close(); auto ret = open_file_base::close();
event_system::instance().raise<download_end>(fsi_.api_path, "",
api_error::download_stopped);
return ret;
} }
auto direct_open_file::read(std::size_t read_size, std::uint64_t read_offset, auto direct_open_file::read(std::size_t read_size, std::uint64_t read_offset,
@ -71,8 +78,8 @@ auto direct_open_file::read(std::size_t read_size, std::uint64_t read_offset,
auto progress = (static_cast<double>(read_offset + read_size) / auto progress = (static_cast<double>(read_offset + read_size) /
static_cast<double>(fsi_.size)) * static_cast<double>(fsi_.size)) *
100.0; 100.0;
event_system::instance().raise<download_progress>( event_system::instance().raise<download_progress>(fsi_.api_path, "",
fsi_.api_path, fsi_.source_path, progress); progress);
} }
return res; return res;

View File

@ -83,6 +83,7 @@ ring_buffer_open_file::ring_buffer_open_file(std::string buffer_directory,
reader_thread_ = reader_thread_ =
std::make_unique<std::thread>([this]() { background_reader_thread(); }); std::make_unique<std::thread>([this]() { background_reader_thread(); });
event_system::instance().raise<download_begin>(fsi_.api_path, source_path_);
} }
ring_buffer_open_file::~ring_buffer_open_file() { ring_buffer_open_file::~ring_buffer_open_file() {
@ -140,6 +141,9 @@ void ring_buffer_open_file::background_reader_thread() {
chunk_lock.lock(); chunk_lock.lock();
check_and_wait(); check_and_wait();
} }
event_system::instance().raise<download_end>(fsi_.api_path, source_path_,
api_error::download_stopped);
} }
auto ring_buffer_open_file::can_handle_file(std::uint64_t file_size, auto ring_buffer_open_file::can_handle_file(std::uint64_t file_size,
@ -202,6 +206,10 @@ auto ring_buffer_open_file::download_chunk(std::size_t chunk, bool skip_active)
chunk == (total_chunks_ - 1U) ? last_chunk_size_ : chunk_size_, chunk == (total_chunks_ - 1U) ? last_chunk_size_ : chunk_size_,
}; };
event_system::instance().raise<download_chunk_begin>(
fsi_.api_path, source_path_, chunk, get_read_state().size(),
get_read_state().count());
auto res{ auto res{
provider_.read_file_bytes(fsi_.api_path, data_size, data_offset, buffer, provider_.read_file_bytes(fsi_.api_path, data_size, data_offset, buffer,
stop_requested_), stop_requested_),
@ -228,6 +236,10 @@ auto ring_buffer_open_file::download_chunk(std::size_t chunk, bool skip_active)
: api_error::invalid_ring_buffer_position; : api_error::invalid_ring_buffer_position;
} }
event_system::instance().raise<download_chunk_end>(
fsi_.api_path, source_path_, chunk, get_read_state().size(),
get_read_state().count(), res);
active_downloads_.erase(chunk); active_downloads_.erase(chunk);
unlock_and_notify(); unlock_and_notify();