diff --git a/docs/index.html b/docs/index.html
index d6a711c49..7c4576e4a 100755
--- a/docs/index.html
+++ b/docs/index.html
@@ -156,6 +156,7 @@ class session: public boost::noncopyable
session(int listen_port, const std::string& fingerprint = std::string());
torrent_handle add_torrent(const torrent_info& t, const std::string& save_path);
+ void remove_torrent(const torrent_handle& h);
void set_http_settings(const http_settings& settings);
};
@@ -170,6 +171,11 @@ want to save the files. The save_path will be prepended to the director
structure in the torrent-file.
+
+remove_torrent() will close all peer connections associated with the torrent and tell
+the tracker that we've stopped participating in the swarm.
+
+
If the torrent you are trying to add already exists in the session (is either queued
for checking, being checked or downloading) add_torrent() will throw
@@ -476,7 +482,6 @@ Its declaration looks like this:
struct torrent_handle
{
torrent_handle();
- void abort();
torrent_status status() const;
void get_download_queue(std::vector<partial_piece_info>& queue);
@@ -490,13 +495,6 @@ perform any operation on it, unless you first assign it a valid handle. If you t
any operation they will simply return.
-
-
-abort() will close all peer connections associated with this torrent and tell
-the tracker that we've stopped participating in the swarm. This handle will become invalid
-shortly after this call has been made.
-
-
status()
diff --git a/include/libtorrent/debug.hpp b/include/libtorrent/debug.hpp
index dbb02ec9d..382adabfe 100755
--- a/include/libtorrent/debug.hpp
+++ b/include/libtorrent/debug.hpp
@@ -40,7 +40,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
-#if defined(TORRENT_VERBOSE_LOGGING)
// DEBUG API
struct logger
@@ -94,7 +93,6 @@ namespace libtorrent
std::ofstream m_file;
};
-#endif
}
diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp
index 646084fb5..98ab2b2a4 100755
--- a/include/libtorrent/peer_connection.hpp
+++ b/include/libtorrent/peer_connection.hpp
@@ -158,7 +158,7 @@ namespace libtorrent
const peer_id& get_peer_id() const { return m_peer_id; }
const std::vector& get_bitfield() const { return m_have_piece; }
-#if defined(TORRENT_VERBOSE_LOGGING)
+#ifndef NDEBUG
boost::shared_ptr m_logger;
#endif
@@ -228,7 +228,7 @@ namespace libtorrent
// something that generates data to be
// sent to this peer, we check this and
// if it's not added to the selector we
- // add it.
+ // add it. (this is done in send_buffer_updated())
bool m_added_to_selector;
// remote peer's id
diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp
index f0d4c7a2d..4beeb0d42 100755
--- a/include/libtorrent/session.hpp
+++ b/include/libtorrent/session.hpp
@@ -175,7 +175,7 @@ namespace libtorrent
volatile bool m_abort;
-#if defined(TORRENT_VERBOSE_LOGGING)
+#ifndef NDEBUG
boost::shared_ptr create_log(std::string name)
{
name = "libtorrent_log_" + name + ".log";
@@ -205,6 +205,7 @@ namespace libtorrent
torrent_handle add_torrent(
const torrent_info& ti
, const boost::filesystem::path& save_path);
+ void remove_torrent(const torrent_handle& h);
void set_http_settings(const http_settings& s);
diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp
index fe979cec5..e72d2f9f2 100755
--- a/include/libtorrent/torrent_handle.hpp
+++ b/include/libtorrent/torrent_handle.hpp
@@ -89,16 +89,18 @@ namespace libtorrent
friend class session;
torrent_handle(): m_ses(0) {}
- void abort();
-
void get_peer_info(std::vector& v);
torrent_status status() const;
void get_download_queue(std::vector& queue) const;
// TODO: add force reannounce
+ // TODO: add torrent_info getter
private:
+ // called by session::remove_torrent()
+ void abort() const;
+
torrent_handle(detail::session_impl* s,
detail::checker_impl* c,
const sha1_hash& h)
diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp
index b79236708..69495b7e8 100755
--- a/src/peer_connection.cpp
+++ b/src/peer_connection.cpp
@@ -318,7 +318,10 @@ bool libtorrent::peer_connection::dispatch_message()
r.length = read_int(&m_recv_buffer[9]);
m_requests.push_back(r);
- send_buffer_updated();
+ if (!m_choked)
+ {
+ send_buffer_updated();
+ }
#if defined(TORRENT_VERBOSE_LOGGING)
(*m_logger) << m_socket->sender().as_string() << " <== REQUEST [ piece: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n";
@@ -912,34 +915,29 @@ void libtorrent::peer_connection::send_data()
if (m_sending_piece.index() != r.piece)
{
- m_sending_piece.open(m_torrent->filesystem(), r.piece, piece_file::in);
+ m_sending_piece.open(
+ m_torrent->filesystem()
+ , r.piece
+ , piece_file::in
+ , r.start);
#ifndef NDEBUG
assert(m_torrent->filesystem()->verify_piece(m_sending_piece) && "internal error");
m_sending_piece.open(m_torrent->filesystem(), r.piece, piece_file::in);
#endif
}
+ const int send_buffer_offset = m_send_buffer.size();
const int packet_size = 4 + 5 + 4 + r.length;
- m_send_buffer.resize(packet_size);
- write_int(packet_size-4, &m_send_buffer[0]);
- m_send_buffer[4] = msg_piece;
- write_int(r.piece, &m_send_buffer[5]);
- write_int(r.start, &m_send_buffer[9]);
+ m_send_buffer.resize(send_buffer_offset + packet_size);
+ write_int(packet_size-4, &m_send_buffer[send_buffer_offset]);
+ m_send_buffer[send_buffer_offset+4] = msg_piece;
+ write_int(r.piece, &m_send_buffer[send_buffer_offset+5]);
+ write_int(r.start, &m_send_buffer[send_buffer_offset+9]);
- if (r.start > m_sending_piece.tell())
- {
- m_sending_piece.seek_forward(r.start - m_sending_piece.tell());
- }
- else if (r.start < m_sending_piece.tell())
- {
- int index = m_sending_piece.index();
- m_sending_piece.close();
- m_sending_piece.open(m_torrent->filesystem(), index, piece_file::in);
- m_sending_piece.seek_forward(r.start);
- }
+ assert(r.start == m_sending_piece.tell());
- m_sending_piece.read(&m_send_buffer[13], r.length);
+ m_sending_piece.read(&m_send_buffer[send_buffer_offset+13], r.length);
#if defined(TORRENT_VERBOSE_LOGGING)
- (*m_logger) << m_socket->sender().as_string() << " ==> PIECE [ idx: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n";
+ (*m_logger) << m_socket->sender().as_string() << " ==> PIECE [ piece: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n";
#endif
// let the torrent keep track of how much we have uploaded
m_torrent->uploaded_bytes(r.length);
@@ -947,7 +945,14 @@ void libtorrent::peer_connection::send_data()
else
{
#if defined(TORRENT_VERBOSE_LOGGING)
- (*m_logger) << m_socket->sender().as_string() << " *** WARNING [ illegal piece request idx: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n";
+ (*m_logger) << m_socket->sender().as_string()
+ << " *** WARNING [ illegal piece request idx: " << r.piece
+ << " | s: " << r.start
+ << " | l: " << r.length
+ << " | max_piece: " << m_have_piece.size()
+ << " | torrent: " << (m_torrent != 0)
+ << " | have: " << m_torrent->have_piece(r.piece)
+ << " ]\n";
#endif
}
m_requests.erase(m_requests.begin());
diff --git a/src/session.cpp b/src/session.cpp
index b28a5add8..0c6d8ab1c 100755
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -590,6 +590,13 @@ namespace libtorrent
return torrent_handle(&m_impl, &m_checker_impl, ti.info_hash());
}
+ void session::remove_torrent(const torrent_handle& h)
+ {
+ if (h.m_ses != &m_impl) return;
+ // TODO: move the code of abort() here instead of calling it
+ h.abort();
+ }
+
void session::set_http_settings(const http_settings& s)
{
boost::mutex::scoped_lock l(m_impl.m_mutex);
diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp
index 9ea8c0b2b..8779dc87e 100755
--- a/src/torrent_handle.cpp
+++ b/src/torrent_handle.cpp
@@ -184,7 +184,7 @@ namespace libtorrent
}
}
- void torrent_handle::abort()
+ void torrent_handle::abort() const
{
if (m_ses == 0) return;
assert(m_chk != 0);
@@ -195,7 +195,6 @@ namespace libtorrent
if (t != 0)
{
t->abort();
- m_ses = 0;
return;
}
}
@@ -207,12 +206,9 @@ namespace libtorrent
if (d != 0)
{
d->abort = true;
- m_ses = 0;
return;
}
}
-
- m_ses = 0;
}
}