我已经在libtorrent中实现了自定义存储接口,如帮助部分here中所述。
storage_interface工作正常,但我无法弄清楚为什么readv
只在下载torrent时被随机调用。从我的观点来看,每当我在readv
中调用handle->read_piece
时,都应调用覆盖虚拟函数piece_finished_alert
。它应该读取read_piece_alert的作品?
缓冲区在read_piece_alert
中提供,但未在readv
中收到通知。
所以问题是为什么它只是随机调用,为什么它不会在read_piece()
调用时被调用?我的storage_interface可能错了吗?
代码如下所示:
struct temp_storage : storage_interface
{
virtual int readv(file::iovec_t const* bufs, int num_bufs
, int piece, int offset, int flags, storage_error& ec)
{
// Only called on random pieces while downloading a larger torrent
std::map<int, std::vector<char> >::const_iterator i = m_file_data.find(piece);
if (i == m_file_data.end()) return 0;
int available = i->second.size() - offset;
if (available <= 0) return 0;
if (available > num_bufs) available = num_bufs;
memcpy(&bufs, &i->second[offset], available);
return available;
}
virtual int writev(file::iovec_t const* bufs, int num_bufs
, int piece, int offset, int flags, storage_error& ec)
{
std::vector<char>& data = m_file_data[piece];
if (data.size() < offset + num_bufs) data.resize(offset + num_bufs);
std::memcpy(&data[offset], bufs, num_bufs);
return num_bufs;
}
virtual bool has_any_file(storage_error& ec) { return false; }
virtual ...
virtual ...
}
初学
storage_interface* temp_storage_constructor(storage_params const& params)
{
printf("NEW INTERFACE\n");
return new temp_storage(*params.files);
}
p.storage = &temp_storage_constructor;
以下功能设置警报并在每个已完成的作品上调用read_piece
。
while(true) {
std::vector<alert*> alerts;
s.pop_alerts(&alerts);
for (alert* i : alerts)
{
switch (i->type()) {
case read_piece_alert::alert_type:
{
read_piece_alert* p = (read_piece_alert*)i;
if (p->ec) {
// read_piece failed
break;
}
// piece buffer, size is provided without readv
// notification after invoking read_piece in piece_finished_alert
break;
}
case piece_finished_alert::alert_type: {
piece_finished_alert* p = (piece_finished_alert*)i;
p->handle.read_piece(p->piece_index);
// Once the piece is finished, we read it to obtain the buffer in read_piece_alert.
break;
}
default:
break;
}
}
Sleep(100);
}
答案 0 :(得分:0)
我会回答我自己的问题。正如Arvid在评论中所说:readv
因缓存而未被调用。将settings_pack::use_read_cache
设置为false将始终调用readv
。