diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 73f27c29c..3828c3df1 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -1751,7 +1751,6 @@ namespace libtorrent std::string myip = root.dict_find_string_value("yourip"); if (!myip.empty()) { - // TODO: don't trust this blindly if (myip.size() == address_v4::bytes_type().size()) { address_v4::bytes_type bytes; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 0d87909b2..b9a961bb4 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -3983,7 +3983,6 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); assert(t); - // TODO: we should probably just send a HAVE_ALL here for (int i = 0; i < int(m_have_piece.size()); ++i) { if (m_have_piece[i] || !t->have_piece(i)) continue; diff --git a/src/torrent.cpp b/src/torrent.cpp index 3918d2174..21f3d648c 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -5964,6 +5964,19 @@ namespace libtorrent return true; } + bool connecting_time_compare(peer_connection const* lhs, peer_connection const* rhs) + { + bool lhs_connecting = lhs->is_connecting() && !lhs->is_disconnecting(); + bool rhs_connecting = rhs->is_connecting() && !rhs->is_disconnecting(); + if (lhs_connecting > rhs_connecting) return false; + if (lhs_connecting < rhs_connecting) return true; + + // a lower value of connected_time means it's been waiting + // longer. This is a less-than comparison, so if lhs has + // waited longer than rhs, we should return false. + return lhs->connected_time() >= rhs->connected_time(); + } + bool torrent::attach_peer(peer_connection* p) { // INVARIANT_CHECK; @@ -6083,13 +6096,12 @@ namespace libtorrent // find any peer that's connecting (i.e. a half-open TCP connection) // that's also not disconnecting - // TODO: 1 ideally, we would disconnect the oldest connection - // i.e. the one that has waited the longest to connect. - std::set::iterator i = std::find_if(begin(), end() - , boost::bind(&peer_connection::is_connecting, _1) - && !boost::bind(&peer_connection::is_disconnecting, _1)); + // disconnect the peer that's been wating to establish a connection + // the longest + std::set::iterator i = std::max_element(begin(), end() + , &connecting_time_compare); - if (i == end()) + if (i == end() || !(*i)->is_connecting() || (*i)->is_disconnecting()) { // this seems odd, but we might as well handle it p->disconnect(errors::too_many_connections);