diff --git a/docs/index.html b/docs/index.html index eeb21d5f9..245be8037 100755 --- a/docs/index.html +++ b/docs/index.html @@ -292,7 +292,7 @@ public: dictionary_type& dict(); const dictionary_type& dict() const; - void print(std::ostream& os, int indent) const; + void print(std::ostream& os, int indent = 0) const; }; diff --git a/examples/dump_torrent.cpp b/examples/dump_torrent.cpp index 467265f2a..5c382d17c 100755 --- a/examples/dump_torrent.cpp +++ b/examples/dump_torrent.cpp @@ -60,7 +60,14 @@ int main(int argc, char* argv[]) entry e = bdecode(std::istream_iterator(in), std::istream_iterator()); torrent_info t(e); + + + std::cout << "\n\n----- raw info -----\n\n"; + e.print(std::cout); + + // print info about torrent + std::cout << "\n\n----- torrent file info -----\n\n"; std::cout << "trackers:\n"; for (std::vector::const_iterator i = t.trackers().begin(); i != t.trackers().end(); @@ -76,7 +83,8 @@ int main(int argc, char* argv[]) i != t.end_files(); ++i) { - std::cout << " " << std::setw(11) << i->size << " " << i->path << " " << i->filename << "\n"; + std::cout << " " << std::setw(11) << i->size + << " " << i->path << " " << i->filename << "\n"; } } diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index 911417e6e..2f69879c7 100755 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -188,7 +188,7 @@ namespace libtorrent return *reinterpret_cast(data); } - void print(std::ostream& os, int indent) const; + void print(std::ostream& os, int indent = 0) const; private: diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 6ce3ec054..d5dfc0544 100755 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -152,7 +152,7 @@ namespace libtorrent #if defined(TORRENT_VERBOSE_LOGGING) boost::shared_ptr create_log(std::string name) { - name += ".log"; + name = "libtorrent_log_" + name + ".log"; // current options are file_logger and cout_logger return boost::shared_ptr(new file_logger(name.c_str())); } diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index c16db9b5a..32dae91be 100755 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -86,7 +86,7 @@ namespace libtorrent enum open_mode { in, out }; // opens a piece with the given index from storage s - void open(storage* s, int index, open_mode m); + void open(storage* s, int index, open_mode m, int seek_offset = 0); void close() { //std::cout << std::clock() << "close " << m_piece_index << "\n"; diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index 08a79b86b..db56d2878 100755 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -109,6 +109,7 @@ namespace libtorrent entry::integer_type piece_length() const { return m_piece_length; } std::size_t num_pieces() const { return m_piece_hash.size(); } const sha1_hash& info_hash() const { return m_info_hash; } + const std::string& name() const { return m_name; } void print(std::ostream& os) const; entry::integer_type piece_size(unsigned int index) const @@ -149,6 +150,8 @@ namespace libtorrent // the hash that identifies this torrent sha1_hash m_info_hash; + + std::string m_name; }; } diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 24d5b745e..53cba8912 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -369,16 +369,12 @@ bool libtorrent::peer_connection::dispatch_message() return false; } -// if (m_receiving_piece.index() < 0 || m_receiving_piece.index() != index) -// { - m_receiving_piece.open(m_torrent->filesystem(), index, piece_file::out); -// } + m_receiving_piece.open(m_torrent->filesystem(), index, piece_file::out, offset); #if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== PIECE [ piece: " << index << " | s: " << offset << " | l: " << len << " ]\n"; #endif - m_receiving_piece.seek_forward(offset); m_receiving_piece.write(&m_recv_buffer[9], len); m_torrent->downloaded_bytes(len); diff --git a/src/session.cpp b/src/session.cpp index 0a8e1422f..6d8c7d523 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -564,8 +564,12 @@ namespace libtorrent const unsigned char* c = p.begin(); while (c != p.end() && *c != 0) { - if (!std::isprint(*c)) return std::string(); - ret += *c; + if (std::isprint(*c)) + ret += *c; + else if (*c <= 9) + ret += '0'+ *c; + else + return std::string(); ++c; } if (c == p.end()) return std::string(); diff --git a/src/stat.cpp b/src/stat.cpp index 6021a6614..50c6ef908 100755 --- a/src/stat.cpp +++ b/src/stat.cpp @@ -30,6 +30,11 @@ POSSIBILITY OF SUCH DAMAGE. */ +// TODO: Use two algorithms to estimate transfer rate. +// one (simple) for transfer rates that are >= 1 packet +// per second and one (low pass-filter) for rates < 1 +// packet per second. + #include #include "libtorrent/stat.hpp" diff --git a/src/storage.cpp b/src/storage.cpp index 3857b7ea3..52a0fa5d0 100755 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -57,7 +57,7 @@ using namespace libtorrent; // accepted as an argument, this way we may avoid opening a // file in vain if we're about to seek forward anyway -void libtorrent::piece_file::open(storage* s, int index, open_mode o) +void libtorrent::piece_file::open(storage* s, int index, open_mode o, int seek_offset) { open_mode old_mode = m_mode; storage* old_storage = m_storage; @@ -71,11 +71,14 @@ void libtorrent::piece_file::open(storage* s, int index, open_mode o) assert(index < m_storage->m_torrent_file->num_pieces() && "internal error"); - m_piece_offset = 0; - int piece_byte_offset = index * m_storage->m_torrent_file->piece_length(); + m_piece_offset = seek_offset; + int piece_byte_offset = index * m_storage->m_torrent_file->piece_length() + + m_piece_offset; entry::integer_type file_byte_offset = 0; - for (m_file_iter = m_storage->m_torrent_file->begin_files(); m_file_iter != m_storage->m_torrent_file->end_files(); ++m_file_iter) + for (m_file_iter = m_storage->m_torrent_file->begin_files(); + m_file_iter != m_storage->m_torrent_file->end_files(); + ++m_file_iter) { if (file_byte_offset + m_file_iter->size > piece_byte_offset) break; file_byte_offset += m_file_iter->size; @@ -86,6 +89,7 @@ void libtorrent::piece_file::open(storage* s, int index, open_mode o) if ((m_mode == out && !(m_file_mode & std::ios_base::out)) || old_file_iter != m_file_iter || !m_file.is_open() + || m_file.fail() || old_storage != m_storage) { std::ios_base::openmode m; @@ -108,15 +112,18 @@ void libtorrent::piece_file::open(storage* s, int index, open_mode o) assert(!m_file.fail()); } } + assert(!m_file.fail()); m_file_offset = piece_byte_offset - file_byte_offset; if (m_mode == in) m_file.seekg(m_file_offset, std::ios_base::beg); else m_file.seekp(m_file_offset, std::ios_base::beg); +#ifndef NDEBUG int gpos = m_file.tellg(); int ppos = m_file.tellp(); assert(m_mode == out || m_file_offset == gpos && "internal error"); assert(m_mode == in || m_file_offset == ppos && "internal error"); +#endif } int libtorrent::piece_file::read(char* buf, int size) @@ -125,6 +132,8 @@ int libtorrent::piece_file::read(char* buf, int size) // std::cout << std::clock() << "read " << m_piece_index << "\n"; assert(m_mode == in); + assert(!m_file.fail()); + assert(m_file.is_open()); int left_to_read = size; // make sure we don't read more than what belongs to this piece @@ -172,7 +181,10 @@ int libtorrent::piece_file::read(char* buf, int size) } } while (left_to_read > 0); - assert(m_file_offset == (long)m_file.tellg() && "internal error"); +#ifndef NDEBUG + int gpos = m_file.tellg(); + assert(m_file_offset == gpos && "internal error"); +#endif return read_total; } diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 75f39e577..deba6b622 100755 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -149,7 +149,7 @@ namespace libtorrent // extract file name (or the directory name if it's a multifile libtorrent) i = info.dict().find("name"); if (i == info.dict().end()) throw invalid_torrent_file(); - std::string filename = i->second.string(); + m_name = i->second.string(); // extract file list i = info.dict().find("files"); @@ -161,12 +161,12 @@ namespace libtorrent if (i == info.dict().end()) throw invalid_torrent_file(); m_files.push_back(file()); - m_files.back().filename = filename; + m_files.back().filename = m_name; m_files.back().size = i->second.integer(); } else { - extract_files(i->second.list(), m_files, filename); + extract_files(i->second.list(), m_files, m_name); } // calculate total size of all pieces