diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index f6212a69e..4fbf7f478 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -59,6 +59,8 @@ namespace libtorrent struct logger; #endif + std::string escape_string(const char* str, int len); + struct tracker_alert: alert { tracker_alert(const torrent_handle& h diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index bff9363fb..cd944b380 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -1139,7 +1139,8 @@ namespace libtorrent // we send it data faster, otherwise, slower. // It will also depend on how much data the // client has sent us. This is the mean to - // maintain a 1:1 share ratio with all peers. + // maintain the share ratio given by m_ratio + // with all peers. int diff = share_diff(); diff --git a/src/policy.cpp b/src/policy.cpp index 35d1ce4ce..c523e55b0 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -210,7 +210,7 @@ namespace // (and we should consider it free). If the share diff is // negative, there's no free download to get from this peer. int diff = i->second->share_diff(); - if (i->second->is_peer_interested() || diff <= 0 || diff==std::numeric_limits::max()) + if (i->second->is_peer_interested() || diff <= 0) continue; assert(diff > 0); @@ -233,13 +233,10 @@ namespace if (free_upload <= 0) return free_upload; int num_peers = 0; int total_diff = 0; - for (torrent::peer_iterator i = start; i != end; ++i) { - int diff=i->second->share_diff(); - if(diff==std::numeric_limits::max()) continue; - total_diff += diff; - if (!i->second->is_peer_interested() || diff >= 0) continue; + total_diff += i->second->share_diff(); + if (!i->second->is_peer_interested() || i->second->share_diff() >= 0) continue; ++num_peers; } @@ -305,11 +302,8 @@ namespace libtorrent if (!c->is_peer_interested()) return &(*i); -// int diff = i->total_download() -// - i->total_upload(); - int diff = c->share_diff(); - if(diff==std::numeric_limits::max()) - diff=0; + int diff = i->total_download() + - i->total_upload(); int weight = static_cast(c->statistics().download_rate() * 10.f) + diff @@ -371,6 +365,7 @@ namespace libtorrent , old_disconnected_peer()) , m_peers.end()); + // if the share ratio is 0 (infinite) // m_available_free_upload isn't used // because it isn't necessary @@ -417,8 +412,26 @@ namespace libtorrent ++m_num_unchoked; } } - else if (m_max_uploads != -1) + else { + // choke peers that have leeched too much without giving anything back + for (std::vector::iterator i = m_peers.begin(); + i != m_peers.end(); + ++i) + { + peer_connection* c = i->connection; + if (c == 0) continue; + + int diff = i->connection->share_diff(); + if (diff < -free_upload_amount + && !c->is_choked()) + { + // if we have uploaded more than a piece for free, choke peer and + // wait until we catch up with our download. + c->send_choke(); + } + } + // make sure we don't have too many // unchoked peers while (m_num_unchoked > m_max_uploads) @@ -440,40 +453,11 @@ namespace libtorrent unchoke_one_peer(); } + // make sure we have enough // unchoked peers while (m_num_unchoked < m_max_uploads && unchoke_one_peer()); } - else - { - // choke peers that have leeched too much without giving anything back - for (std::vector::iterator i = m_peers.begin(); - i != m_peers.end(); - ++i) - { - peer_connection* c = i->connection; - if (c == 0) continue; - - int diff=c->share_diff(); - // no problem if diff returns std::numeric_limits::max() - - if (diff <= -free_upload_amount - && !c->is_choked()) - { - // if we have uploaded more than a piece for free, choke peer and - // wait until we catch up with our download. - c->send_choke(); - } - else if (diff > -free_upload_amount - && c->is_choked() /* && c->is_peer_interested()*/) - { - // we have catched up. We have now shared the same amount - // to eachother. Unchoke this peer. - c->send_unchoke(); - } - } - - } #ifndef NDEBUG check_invariant(); diff --git a/src/torrent.cpp b/src/torrent.cpp index fad170e6d..25e127b08 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -126,33 +126,6 @@ namespace return ret; } - std::string escape_string(const char* str, int len) - { - static const char special_chars[] = "$-_.+!*'(),"; - - std::stringstream ret; - ret << std::hex << std::setfill('0'); - for (int i = 0; i < len; ++i) - { - if (std::isalnum(static_cast(*str)) - || std::count( - special_chars - , special_chars+sizeof(special_chars)-1 - , *str)) - { - ret << *str; - } - else - { - ret << "%" - << std::setw(2) - << (int)static_cast(*str); - } - ++str; - } - return ret.str(); - } - struct find_peer_by_id { find_peer_by_id(const peer_id& i, const torrent* t): id(i), tor(t) {} @@ -207,6 +180,32 @@ namespace namespace libtorrent { + std::string escape_string(const char* str, int len) + { + static const char special_chars[] = "$-_.+!*'(),"; + + std::stringstream ret; + ret << std::hex << std::setfill('0'); + for (int i = 0; i < len; ++i) + { + if (std::isalnum(static_cast(*str)) + || std::count( + special_chars + , special_chars+sizeof(special_chars)-1 + , *str)) + { + ret << *str; + } + else + { + ret << "%" + << std::setw(2) + << (int)static_cast(*str); + } + ++str; + } + return ret.str(); + } torrent::torrent( detail::session_impl& ses