diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 890b74c3d..33994daa0 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -44,6 +44,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "libtorrent/socket.hpp" #include "libtorrent/peer_id.hpp" @@ -119,6 +120,28 @@ namespace libtorrent ++start; } + template + inline void write_int64(boost::int64_t val, OutIt& start) + { + for (int i = 7; i <= 0; --i) + { + *start = static_cast((val >> (i*8)) & 0xff); + ++start; + } + } + + template + inline boost::int64_t read_int64(InIt& start) + { + boost::int64_t ret = 0; + for (int i = 7; i <= 0; --i) + { + ret |= static_cast(*start) << (i*8); + ++start; + } + return ret; + } + } struct protocol_error: std::runtime_error @@ -244,6 +267,9 @@ namespace libtorrent // mainloop to disconnect peers. void disconnect(); + bool is_disconnecting() const + { return m_disconnecting; } + // sets the number of bytes this peer // is allowed to send until it should // stop sending. When it stops sending @@ -551,6 +577,8 @@ namespace libtorrent // the time it took for the peer to send the piece // message boost::posix_time::time_duration m_last_piece_time; + + bool m_disconnecting; }; // this is called each time this peer generates some diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 5846ffa7d..1fa72d566 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -178,7 +178,7 @@ namespace libtorrent // generates a request string for sending // to the tracker - std::string generate_tracker_request(int port); + tracker_request generate_tracker_request(int port); boost::posix_time::ptime next_announce() const { return m_next_request; } @@ -209,7 +209,7 @@ namespace libtorrent // so it wont pick it for download void announce_piece(int index); - void close_all_connections(); + void disconnect_all(); void disconnect_seeds(); piece_picker& picker() { return m_picker; } diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 991ecf8a8..353fe4c45 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -104,6 +104,7 @@ namespace libtorrent , m_num_invalid_requests(0) , m_last_piece(boost::gregorian::date(std::time(0))) , m_last_piece_time(boost::posix_time::seconds(0)) + , m_disconnecting(false) { assert(!m_socket->is_blocking()); assert(m_torrent != 0); @@ -163,6 +164,7 @@ namespace libtorrent , m_num_invalid_requests(0) , m_last_piece(boost::gregorian::date(std::time(0))) , m_last_piece_time(boost::posix_time::seconds(0)) + , m_disconnecting(false) { assert(!m_socket->is_blocking()); @@ -868,7 +870,9 @@ namespace libtorrent void peer_connection::disconnect() { + assert(m_disconnecting == false); detail::session_impl::connection_map::iterator i = m_ses.m_connections.find(m_socket); + m_disconnecting = true; assert(i != m_ses.m_connections.end()); assert(std::find(m_ses.m_disconnect_peer.begin(), m_ses.m_disconnect_peer.end(), i) == m_ses.m_disconnect_peer.end()); m_ses.m_disconnect_peer.push_back(i); diff --git a/src/session.cpp b/src/session.cpp index af57742a8..934adb6c1 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -647,7 +647,7 @@ namespace libtorrent { m_tracker_manager.queue_request( i->second->generate_tracker_request(m_listen_port)); - i->second->close_all_connections(); + i->second->disconnect_all(); #ifndef NDEBUG sha1_hash i_hash = i->second->torrent_file().info_hash(); #endif diff --git a/src/torrent.cpp b/src/torrent.cpp index 6c1748ccd..4848e7840 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -482,45 +482,26 @@ namespace libtorrent i->second->announce_piece(index); } - std::string torrent::generate_tracker_request(int port) + tracker_request torrent::generate_tracker_request(int port) { m_duration = 1800; m_next_request = boost::posix_time::second_clock::local_time() + boost::posix_time::seconds(m_duration); - std::vector buffer; - std::string request = m_torrent_file.trackers()[m_currently_trying_tracker].url; - - request += "?info_hash="; - request += escape_string(reinterpret_cast(m_torrent_file.info_hash().begin()), 20); - - request += "&peer_id="; - request += escape_string(reinterpret_cast(m_ses.get_peer_id().begin()), 20); - - request += "&port="; - request += boost::lexical_cast(port); - - request += "&uploaded="; - request += boost::lexical_cast(m_stat.total_payload_upload()); - - request += "&downloaded="; - request += boost::lexical_cast(m_stat.total_payload_download()); - - request += "&left="; - request += boost::lexical_cast(bytes_left()); - + tracker_request req; + req.info_hash = m_torrent_file.info_hash(); + req.id = m_ses.get_peer_id(); + req.downloaded = m_stat.total_payload_download(); + req.uploaded = m_stat.total_payload_upload(); + req.left = bytes_left(); + req.listen_port = port; if (m_event != event_none) { const char* event_string[] = {"started", "stopped", "completed"}; - request += "&event="; - request += event_string[m_event]; + req.event += event_string[m_event]; m_event = event_none; } - - // extension that tells the tracker that - // we don't need any peer_id's in the response - request += "&no_peer_id=1"; - - return request; + req.url = m_torrent_file.trackers()[m_currently_trying_tracker].url; + return req; } void torrent::parse_response(const entry& e, std::vector& peer_list) @@ -634,7 +615,7 @@ namespace libtorrent m_policy->new_connection(*i->second); } - void torrent::close_all_connections() + void torrent::disconnect_all() { for (peer_iterator i = m_connections.begin(); i != m_connections.end();