new storage
This commit is contained in:
parent
a1356219da
commit
329e610ca6
|
@ -151,6 +151,9 @@ int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
using namespace libtorrent;
|
using namespace libtorrent;
|
||||||
|
|
||||||
|
// TEMPORARY
|
||||||
|
boost::filesystem::path::default_name_check(boost::filesystem::no_check);
|
||||||
|
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
{
|
{
|
||||||
std::cerr << "usage: ./client_test torrent-files ...\n"
|
std::cerr << "usage: ./client_test torrent-files ...\n"
|
||||||
|
|
|
@ -90,7 +90,7 @@ namespace libtorrent
|
||||||
enum open_mode { in, out };
|
enum open_mode { in, out };
|
||||||
|
|
||||||
// opens a piece with the given index from storage s
|
// opens a piece with the given index from storage s
|
||||||
void open(storage* s, int index, open_mode m, int seek_offset = 0);
|
void open(storage* s, int index, open_mode m, int seek_offset = 0, bool lock_ = true);
|
||||||
void close()
|
void close()
|
||||||
{
|
{
|
||||||
//std::cout << std::clock() << "close " << m_piece_index << "\n";
|
//std::cout << std::clock() << "close " << m_piece_index << "\n";
|
||||||
|
@ -99,18 +99,21 @@ namespace libtorrent
|
||||||
m_storage = 0;
|
m_storage = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(const char* buf, int size);
|
void write(const char* buf, int size, bool lock_ = true);
|
||||||
int read(char* buf, int size);
|
int read(char* buf, int size, bool lock_ = true);
|
||||||
void seek_forward(int step);
|
void seek_forward(int step, bool lock_ = true);
|
||||||
|
|
||||||
// tells the position in the file
|
// tells the position in the file
|
||||||
int tell() const { return m_piece_offset; }
|
int tell() const { return m_piece_offset; }
|
||||||
int left() const { return m_piece_size - m_piece_offset; }
|
int left() const { return m_piece_size - m_piece_offset; }
|
||||||
|
|
||||||
int index() const { return m_piece_index; }
|
int index() const { return m_piece_index; }
|
||||||
|
void lock(bool lock_ = true);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
void reopen();
|
||||||
|
|
||||||
// the file itself
|
// the file itself
|
||||||
std::fstream m_file;
|
std::fstream m_file;
|
||||||
|
|
||||||
|
@ -123,6 +126,9 @@ namespace libtorrent
|
||||||
// file we're currently reading from/writing to
|
// file we're currently reading from/writing to
|
||||||
std::vector<file>::const_iterator m_file_iter;
|
std::vector<file>::const_iterator m_file_iter;
|
||||||
|
|
||||||
|
// the global position
|
||||||
|
entry::integer_type m_position;
|
||||||
|
|
||||||
// the position we're at in the current file
|
// the position we're at in the current file
|
||||||
std::size_t m_file_offset;
|
std::size_t m_file_offset;
|
||||||
|
|
||||||
|
@ -142,6 +148,7 @@ namespace libtorrent
|
||||||
class storage
|
class storage
|
||||||
{
|
{
|
||||||
friend class piece_file;
|
friend class piece_file;
|
||||||
|
friend class piece_sorter;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void initialize_pieces(torrent* t,
|
void initialize_pieces(torrent* t,
|
||||||
|
@ -158,6 +165,9 @@ namespace libtorrent
|
||||||
|
|
||||||
const std::vector<bool>& pieces() const { return m_have_piece; }
|
const std::vector<bool>& pieces() const { return m_have_piece; }
|
||||||
|
|
||||||
|
entry::integer_type piece_storage(int piece);
|
||||||
|
void allocate_pieces(int num);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
// total number of bytes left to be downloaded
|
// total number of bytes left to be downloaded
|
||||||
|
@ -171,8 +181,44 @@ namespace libtorrent
|
||||||
|
|
||||||
const torrent_info* m_torrent_file;
|
const torrent_info* m_torrent_file;
|
||||||
|
|
||||||
|
// allocated pieces in file
|
||||||
|
std::vector<entry::integer_type> m_allocated_pieces;
|
||||||
|
// unallocated blocks at the end of files
|
||||||
|
std::vector<entry::integer_type> m_free_blocks;
|
||||||
|
// allocated blocks whose checksum doesn't match
|
||||||
|
std::vector<entry::integer_type> m_free_pieces;
|
||||||
|
|
||||||
|
// index here is a slot number in the file
|
||||||
|
// -1 : the slot is unallocated
|
||||||
|
// -2 : the slot is allocated but not assigned to a piece
|
||||||
|
// * : the slot is assigned to this piece
|
||||||
|
std::vector<int> m_slot_to_piece;
|
||||||
|
|
||||||
|
// synchronization
|
||||||
|
boost::mutex m_locked_pieces_monitor;
|
||||||
|
boost::condition m_unlocked_pieces;
|
||||||
|
std::vector<bool> m_locked_pieces;
|
||||||
|
|
||||||
|
boost::recursive_mutex m_mutex;
|
||||||
|
|
||||||
|
torrent* m_torrent;
|
||||||
|
};
|
||||||
|
|
||||||
|
class piece_sorter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void operator()();
|
||||||
|
|
||||||
|
private:
|
||||||
|
typedef std::vector<int> pieces_type;
|
||||||
|
|
||||||
|
storage* m_storage;
|
||||||
|
boost::mutex m_monitor;
|
||||||
|
boost::condition m_more_pieces;
|
||||||
|
pieces_type m_pieces;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // TORRENT_STORAGE_HPP_INCLUDED
|
#endif // TORRENT_STORAGE_HPP_INCLUDED
|
||||||
|
|
||||||
|
|
|
@ -91,8 +91,9 @@ namespace libtorrent
|
||||||
// tracker request
|
// tracker request
|
||||||
bool should_request() const throw()
|
bool should_request() const throw()
|
||||||
{
|
{
|
||||||
boost::posix_time::time_duration d = m_next_request - boost::posix_time::second_clock::local_time();
|
// boost::posix_time::time_duration d = m_next_request - boost::posix_time::second_clock::local_time();
|
||||||
return d.is_negative();
|
// return d.is_negative();
|
||||||
|
return m_next_request < boost::posix_time::second_clock::local_time();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool failed() const throw() { return !m_failed.empty(); }
|
bool failed() const throw() { return !m_failed.empty(); }
|
||||||
|
@ -221,11 +222,12 @@ namespace libtorrent
|
||||||
logger* spawn_logger(const char* title);
|
logger* spawn_logger(const char* title);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
virtual void debug_log(const std::string& line);
|
virtual void debug_log(const std::string& line);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
void try_next_tracker();
|
void try_next_tracker();
|
||||||
|
|
||||||
enum event_id
|
enum event_id
|
||||||
|
|
|
@ -81,8 +81,8 @@ libtorrent::peer_connection::peer_connection(
|
||||||
, m_timeout(120)
|
, m_timeout(120)
|
||||||
, m_packet_size(1)
|
, m_packet_size(1)
|
||||||
, m_recv_pos(0)
|
, m_recv_pos(0)
|
||||||
, m_last_receive(std::time(0))
|
, m_last_receive(boost::gregorian::date(std::time(0)))
|
||||||
, m_last_sent(std::time(0))
|
, m_last_sent(boost::gregorian::date(std::time(0)))
|
||||||
, m_selector(sel)
|
, m_selector(sel)
|
||||||
, m_socket(s)
|
, m_socket(s)
|
||||||
, m_torrent(t)
|
, m_torrent(t)
|
||||||
|
@ -125,8 +125,8 @@ libtorrent::peer_connection::peer_connection(
|
||||||
, m_timeout(120)
|
, m_timeout(120)
|
||||||
, m_packet_size(1)
|
, m_packet_size(1)
|
||||||
, m_recv_pos(0)
|
, m_recv_pos(0)
|
||||||
, m_last_receive(std::time(0))
|
, m_last_receive(boost::gregorian::date(std::time(0)))
|
||||||
, m_last_sent(std::time(0))
|
, m_last_sent(boost::gregorian::date(std::time(0)))
|
||||||
, m_selector(sel)
|
, m_selector(sel)
|
||||||
, m_socket(s)
|
, m_socket(s)
|
||||||
, m_torrent(0)
|
, m_torrent(0)
|
||||||
|
|
1078
src/storage.cpp
1078
src/storage.cpp
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue