lt sync 2543

This commit is contained in:
Marcos Pinto 2008-07-17 00:15:35 +00:00
parent 310b428b78
commit 85939e93ba
8 changed files with 88 additions and 82 deletions

View File

@ -124,8 +124,36 @@ namespace libtorrent
size_type total_download() const; size_type total_download() const;
size_type total_upload() const; size_type total_upload() const;
// the ip/port pair this peer is or was connected on tcp::endpoint ip() const { return tcp::endpoint(addr, port); }
tcp::endpoint ip; void set_ip(tcp::endpoint const& endp)
{ addr = endp.address(); port = endp.port(); }
// this is the accumulated amount of
// uploaded and downloaded data to this
// peer. It only accounts for what was
// shared during the last connection to
// this peer. i.e. These are only updated
// when the connection is closed. For the
// total amount of upload and download
// we'll have to add thes figures with the
// statistics from the peer_connection.
size_type prev_amount_upload;
size_type prev_amount_download;
// the ip address this peer is or was connected on
address addr;
// the time when this peer was optimistically unchoked
// the last time.
libtorrent::ptime last_optimistically_unchoked;
// the time when the peer connected to us
// or disconnected if it isn't connected right now
libtorrent::ptime connected;
// if the peer is connected now, this
// will refer to a valid peer_connection
peer_connection* connection;
#ifndef TORRENT_DISABLE_GEO_IP #ifndef TORRENT_DISABLE_GEO_IP
#ifndef NDEBUG #ifndef NDEBUG
@ -137,6 +165,9 @@ namespace libtorrent
std::pair<const int, int>* inet_as; std::pair<const int, int>* inet_as;
#endif #endif
// the port this peer is or was connected on
uint16_t port;
// the number of failed connection attempts // the number of failed connection attempts
// this peer has // this peer has
boost::uint8_t failcount; boost::uint8_t failcount;
@ -205,30 +236,6 @@ namespace libtorrent
// pinged by the DHT // pinged by the DHT
bool added_to_dht:1; bool added_to_dht:1;
#endif #endif
// if the peer is connected now, this
// will refer to a valid peer_connection
peer_connection* connection;
// this is the accumulated amount of
// uploaded and downloaded data to this
// peer. It only accounts for what was
// shared during the last connection to
// this peer. i.e. These are only updated
// when the connection is closed. For the
// total amount of upload and download
// we'll have to add thes figures with the
// statistics from the peer_connection.
size_type prev_amount_upload;
size_type prev_amount_download;
// the time when this peer was optimistically unchoked
// the last time.
libtorrent::ptime last_optimistically_unchoked;
// the time when the peer connected to us
// or disconnected if it isn't connected right now
libtorrent::ptime connected;
}; };
int num_peers() const { return m_peers.size(); } int num_peers() const { return m_peers.size(); }

View File

@ -1695,7 +1695,7 @@ namespace libtorrent
&& p.second.connection && p.second.connection
&& p.second.connection->pid() == m_id && p.second.connection->pid() == m_id
&& !p.second.connection->pid().is_all_zeros() && !p.second.connection->pid().is_all_zeros()
&& p.second.ip.address() == m_pc->remote().address(); && p.second.addr == m_pc->remote().address();
} }
peer_id const& m_id; peer_id const& m_id;

View File

@ -165,18 +165,15 @@ namespace libtorrent
std::wstring wpath(safe_convert(path.native_file_string())); std::wstring wpath(safe_convert(path.native_file_string()));
m_fd = ::_wopen( m_fd = ::_wopen(
wpath.c_str() wpath.c_str()
, map_open_mode(mode) , map_open_mode(mode));
, S_IREAD | S_IWRITE);
#elif defined _WIN32 #elif defined _WIN32
m_fd = ::_open( m_fd = ::_open(
utf8_native(path.native_file_string()).c_str() utf8_native(path.native_file_string()).c_str()
, map_open_mode(mode) , map_open_mode(mode));
, S_IREAD | S_IWRITE);
#else #else
m_fd = ::open( m_fd = ::open(
utf8_native(path.native_file_string()).c_str() utf8_native(path.native_file_string()).c_str()
, map_open_mode(mode) , map_open_mode(mode));
, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
#endif #endif
if (m_fd == -1) if (m_fd == -1)
{ {

View File

@ -1912,8 +1912,6 @@ namespace libtorrent
std::vector<int> const& peer_connection::allowed_fast() std::vector<int> const& peer_connection::allowed_fast()
{ {
INVARIANT_CHECK;
boost::shared_ptr<torrent> t = m_torrent.lock(); boost::shared_ptr<torrent> t = m_torrent.lock();
TORRENT_ASSERT(t); TORRENT_ASSERT(t);

View File

@ -145,7 +145,7 @@ namespace
{} {}
bool operator()(std::pair<const address, policy::peer> const& p) const bool operator()(std::pair<const address, policy::peer> const& p) const
{ return p.second.ip == m_ep; } { return p.second.addr == m_ep.address() && p.second.port == m_ep.port(); }
tcp::endpoint const& m_ep; tcp::endpoint const& m_ep;
}; };
@ -160,7 +160,7 @@ namespace
bool operator()(std::pair<const address, policy::peer> const& p) const bool operator()(std::pair<const address, policy::peer> const& p) const
{ {
return p.second.connection == &m_conn return p.second.connection == &m_conn
|| (p.second.ip == m_conn.remote() || (p.second.ip() == m_conn.remote()
&& p.second.type == policy::peer::connectable); && p.second.type == policy::peer::connectable);
} }
@ -347,7 +347,7 @@ namespace libtorrent
for (iterator i = m_peers.begin() for (iterator i = m_peers.begin()
, end(m_peers.end()); i != end;) , end(m_peers.end()); i != end;)
{ {
if ((ses.m_ip_filter.access(i->second.ip.address()) & ip_filter::blocked) == 0) if ((ses.m_ip_filter.access(i->second.addr) & ip_filter::blocked) == 0)
{ {
++i; ++i;
continue; continue;
@ -357,14 +357,14 @@ namespace libtorrent
{ {
i->second.connection->disconnect("peer banned by IP filter"); i->second.connection->disconnect("peer banned by IP filter");
if (ses.m_alerts.should_post<peer_blocked_alert>()) if (ses.m_alerts.should_post<peer_blocked_alert>())
ses.m_alerts.post_alert(peer_blocked_alert(i->second.ip.address())); ses.m_alerts.post_alert(peer_blocked_alert(i->second.addr));
TORRENT_ASSERT(i->second.connection == 0 TORRENT_ASSERT(i->second.connection == 0
|| i->second.connection->peer_info_struct() == 0); || i->second.connection->peer_info_struct() == 0);
} }
else else
{ {
if (ses.m_alerts.should_post<peer_blocked_alert>()) if (ses.m_alerts.should_post<peer_blocked_alert>())
ses.m_alerts.post_alert(peer_blocked_alert(i->second.ip.address())); ses.m_alerts.post_alert(peer_blocked_alert(i->second.addr));
} }
erase_peer(i++); erase_peer(i++);
} }
@ -396,7 +396,7 @@ namespace libtorrent
return false; return false;
aux::session_impl& ses = m_torrent->session(); aux::session_impl& ses = m_torrent->session();
if (ses.m_port_filter.access(p.ip.port()) & port_filter::blocked) if (ses.m_port_filter.access(p.port) & port_filter::blocked)
return false; return false;
return true; return true;
} }
@ -444,7 +444,7 @@ namespace libtorrent
// advertise support) // advertise support)
if (!pinged && !pe.added_to_dht) if (!pinged && !pe.added_to_dht)
{ {
udp::endpoint node(pe.ip.address(), pe.ip.port()); udp::endpoint node(pe.addr, pe.port);
m_torrent->session().add_dht_node(node); m_torrent->session().add_dht_node(node);
pe.added_to_dht = true; pe.added_to_dht = true;
pinged = true; pinged = true;
@ -487,8 +487,8 @@ namespace libtorrent
{ {
(*m_torrent->session().m_logger) << time_now_string() (*m_torrent->session().m_logger) << time_now_string()
<< " *** FOUND CONNECTION CANDIDATE [" << " *** FOUND CONNECTION CANDIDATE ["
" ip: " << candidate->second.ip << " ip: " << candidate->second.ip() <<
" d: " << cidr_distance(external_ip, candidate->second.ip.address()) << " d: " << cidr_distance(external_ip, candidate->second.addr) <<
" external: " << external_ip << " external: " << external_ip <<
" t: " << total_seconds(time_now() - candidate->second.connected) << " t: " << total_seconds(time_now() - candidate->second.connected) <<
" ]\n"; " ]\n";
@ -683,11 +683,11 @@ namespace libtorrent
TORRENT_ASSERT(p != 0); TORRENT_ASSERT(p != 0);
TORRENT_ASSERT(p->connection); TORRENT_ASSERT(p->connection);
if (p->ip.port() == port) return true; if (p->port == port) return true;
if (m_torrent->settings().allow_multiple_connections_per_ip) if (m_torrent->settings().allow_multiple_connections_per_ip)
{ {
tcp::endpoint remote(p->ip.address(), port); tcp::endpoint remote(p->addr, port);
std::pair<iterator, iterator> range = m_peers.equal_range(remote.address()); std::pair<iterator, iterator> range = m_peers.equal_range(remote.address());
iterator i = std::find_if(range.first, range.second iterator i = std::find_if(range.first, range.second
, match_peer_endpoint(remote)); , match_peer_endpoint(remote));
@ -704,10 +704,10 @@ namespace libtorrent
} }
else else
{ {
TORRENT_ASSERT(m_peers.count(p->ip.address()) == 1); TORRENT_ASSERT(m_peers.count(p->addr) == 1);
} }
bool was_conn_cand = is_connect_candidate(*p, m_torrent->is_finished()); bool was_conn_cand = is_connect_candidate(*p, m_torrent->is_finished());
p->ip.port(port); p->port = port;
p->source |= src; p->source |= src;
if (was_conn_cand != is_connect_candidate(*p, m_torrent->is_finished())) if (was_conn_cand != is_connect_candidate(*p, m_torrent->is_finished()))
@ -806,7 +806,7 @@ namespace libtorrent
i->second.type = peer::connectable; i->second.type = peer::connectable;
i->second.ip = remote; i->second.set_ip(remote);
i->second.source |= src; i->second.source |= src;
// if this peer has failed before, decrease the // if this peer has failed before, decrease the
@ -1102,14 +1102,14 @@ namespace libtorrent
#endif #endif
if (!m_torrent->settings().allow_multiple_connections_per_ip) if (!m_torrent->settings().allow_multiple_connections_per_ip)
{ {
TORRENT_ASSERT(m_peers.count(p.ip.address()) == 1); TORRENT_ASSERT(m_peers.count(p.addr) == 1);
} }
else else
{ {
TORRENT_ASSERT(unique_test.count(p.ip) == 0); TORRENT_ASSERT(unique_test.count(p.ip()) == 0);
unique_test.insert(p.ip); unique_test.insert(p.ip());
TORRENT_ASSERT(i->first == p.ip.address()); TORRENT_ASSERT(i->first == p.addr);
// TORRENT_ASSERT(p.connection == 0 || p.ip == p.connection->remote()); // TORRENT_ASSERT(p.connection == 0 || p.ip() == p.connection->remote());
} }
++total_connections; ++total_connections;
if (!p.connection) if (!p.connection)
@ -1186,10 +1186,16 @@ namespace libtorrent
#endif #endif
policy::peer::peer(const tcp::endpoint& ip_, peer::connection_type t, int src) policy::peer::peer(const tcp::endpoint& ip_, peer::connection_type t, int src)
: ip(ip_) : prev_amount_upload(0)
, prev_amount_download(0)
, addr(ip_.address())
, last_optimistically_unchoked(min_time())
, connected(min_time())
, connection(0)
#ifndef TORRENT_DISABLE_GEO_IP #ifndef TORRENT_DISABLE_GEO_IP
, inet_as(0) , inet_as(0)
#endif #endif
, port(ip_.port())
, failcount(0) , failcount(0)
, trust_points(0) , trust_points(0)
, source(src) , source(src)
@ -1206,11 +1212,6 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
, added_to_dht(false) , added_to_dht(false)
#endif #endif
, connection(0)
, prev_amount_upload(0)
, prev_amount_download(0)
, last_optimistically_unchoked(min_time())
, connected(min_time())
{ {
TORRENT_ASSERT((src & 0xff) == src); TORRENT_ASSERT((src & 0xff) == src);
TORRENT_ASSERT(connected < time_now()); TORRENT_ASSERT(connected < time_now());
@ -1251,8 +1252,8 @@ namespace libtorrent
return lhs.failcount < rhs.failcount; return lhs.failcount < rhs.failcount;
// Local peers should always be tried first // Local peers should always be tried first
bool lhs_local = is_local(lhs.ip.address()); bool lhs_local = is_local(lhs.addr);
bool rhs_local = is_local(rhs.ip.address()); bool rhs_local = is_local(rhs.addr);
if (lhs_local != rhs_local) return lhs_local > rhs_local; if (lhs_local != rhs_local) return lhs_local > rhs_local;
if (lhs.connected != rhs.connected) if (lhs.connected != rhs.connected)
@ -1267,8 +1268,8 @@ namespace libtorrent
if (lhs_as != rhs_as) return lhs_as > rhs_as; if (lhs_as != rhs_as) return lhs_as > rhs_as;
} }
#endif #endif
int lhs_distance = cidr_distance(external_ip, lhs.ip.address()); int lhs_distance = cidr_distance(external_ip, lhs.addr);
int rhs_distance = cidr_distance(external_ip, rhs.ip.address()); int rhs_distance = cidr_distance(external_ip, rhs.addr);
if (lhs_distance < rhs_distance) return true; if (lhs_distance < rhs_distance) return true;
return false; return false;
} }

View File

@ -219,6 +219,11 @@ namespace aux {
(*m_logger) << "sizeof(peer_connection): " << sizeof(peer_connection) << "\n"; (*m_logger) << "sizeof(peer_connection): " << sizeof(peer_connection) << "\n";
(*m_logger) << "sizeof(bt_peer_connection): " << sizeof(bt_peer_connection) << "\n"; (*m_logger) << "sizeof(bt_peer_connection): " << sizeof(bt_peer_connection) << "\n";
(*m_logger) << "sizeof(policy::peer): " << sizeof(policy::peer) << "\n"; (*m_logger) << "sizeof(policy::peer): " << sizeof(policy::peer) << "\n";
(*m_logger) << "sizeof(tcp::endpoint): " << sizeof(tcp::endpoint) << "\n";
(*m_logger) << "sizeof(address): " << sizeof(address) << "\n";
(*m_logger) << "sizeof(address_v4): " << sizeof(address_v4) << "\n";
(*m_logger) << "sizeof(address_v6): " << sizeof(address_v6) << "\n";
(*m_logger) << "sizeof(void*): " << sizeof(void*) << "\n";
#endif #endif
#ifdef TORRENT_STATS #ifdef TORRENT_STATS

View File

@ -206,7 +206,7 @@ namespace libtorrent { namespace
<< " | c: " << client << " | c: " << client
<< " | crc1: " << i->second.crc << " | crc1: " << i->second.crc
<< " | crc2: " << e.crc << " | crc2: " << e.crc
<< " | ip: " << p->ip << " ]\n"; << " | ip: " << p->ip() << " ]\n";
#endif #endif
p->banned = true; p->banned = true;
if (p->connection) p->connection->disconnect("banning peer for sending bad data"); if (p->connection) p->connection->disconnect("banning peer for sending bad data");
@ -230,7 +230,7 @@ namespace libtorrent { namespace
<< " | b: " << b.block_index << " | b: " << b.block_index
<< " | c: " << client << " | c: " << client
<< " | crc: " << e.crc << " | crc: " << e.crc
<< " | ip: " << p->ip << " ]\n"; << " | ip: " << p->ip() << " ]\n";
#endif #endif
} }
@ -268,7 +268,7 @@ namespace libtorrent { namespace
<< " | c: " << client << " | c: " << client
<< " | ok_crc: " << ok_crc << " | ok_crc: " << ok_crc
<< " | bad_crc: " << b.second.crc << " | bad_crc: " << b.second.crc
<< " | ip: " << p->ip << " ]\n"; << " | ip: " << p->ip() << " ]\n";
#endif #endif
p->banned = true; p->banned = true;
if (p->connection) p->connection->disconnect("banning peer for sending bad data"); if (p->connection) p->connection->disconnect("banning peer for sending bad data");

View File

@ -841,7 +841,7 @@ namespace libtorrent
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
TORRENT_ASSERT(!m_trackers.empty()); if (m_trackers.empty()) return;
restart_tracker_timer(time_now() + seconds(tracker_retry_delay_max)); restart_tracker_timer(time_now() + seconds(tracker_retry_delay_max));
@ -1476,7 +1476,7 @@ namespace libtorrent
peer_id pid; peer_id pid;
if (p->connection) pid = p->connection->pid(); if (p->connection) pid = p->connection->pid();
m_ses.m_alerts.post_alert(peer_ban_alert( m_ses.m_alerts.post_alert(peer_ban_alert(
get_handle(), p->ip, pid)); get_handle(), p->ip(), pid));
} }
// mark the peer as banned // mark the peer as banned
@ -1485,11 +1485,11 @@ namespace libtorrent
if (p->connection) if (p->connection)
{ {
#ifdef TORRENT_LOGGING #ifdef TORRENT_LOGGING
(*m_ses.m_logger) << time_now_string() << " *** BANNING PEER [ " << p->ip (*m_ses.m_logger) << time_now_string() << " *** BANNING PEER [ " << p->ip()
<< " ] 'too many corrupt pieces'\n"; << " ] 'too many corrupt pieces'\n";
#endif #endif
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
(*p->connection->m_logger) << "*** BANNING PEER [ " << p->ip (*p->connection->m_logger) << "*** BANNING PEER [ " << p->ip()
<< " ] 'too many corrupt pieces'\n"; << " ] 'too many corrupt pieces'\n";
#endif #endif
p->connection->disconnect("too many corrupt pieces, banning peer"); p->connection->disconnect("too many corrupt pieces, banning peer");
@ -2534,11 +2534,10 @@ namespace libtorrent
error_code ec; error_code ec;
if (i->second.banned) if (i->second.banned)
{ {
tcp::endpoint ip = i->second.ip;
entry peer(entry::dictionary_t); entry peer(entry::dictionary_t);
peer["ip"] = ip.address().to_string(ec); peer["ip"] = i->second.addr.to_string(ec);
if (ec) continue; if (ec) continue;
peer["port"] = ip.port(); peer["port"] = i->second.port;
banned_peer_list.push_back(peer); banned_peer_list.push_back(peer);
continue; continue;
} }
@ -2554,11 +2553,10 @@ namespace libtorrent
// don't save peers that doesn't work // don't save peers that doesn't work
if (i->second.failcount >= max_failcount) continue; if (i->second.failcount >= max_failcount) continue;
tcp::endpoint ip = i->second.ip;
entry peer(entry::dictionary_t); entry peer(entry::dictionary_t);
peer["ip"] = ip.address().to_string(ec); peer["ip"] = i->second.addr.to_string(ec);
if (ec) continue; if (ec) continue;
peer["port"] = ip.port(); peer["port"] = i->second.port;
peer_list.push_back(peer); peer_list.push_back(peer);
} }
} }
@ -2571,7 +2569,7 @@ namespace libtorrent
i != m_policy.end_peer(); ++i) i != m_policy.end_peer(); ++i)
{ {
peer_list_entry e; peer_list_entry e;
e.ip = i->second.ip; e.ip = i->second.ip();
e.flags = i->second.banned ? peer_list_entry::banned : 0; e.flags = i->second.banned ? peer_list_entry::banned : 0;
e.failcount = i->second.failcount; e.failcount = i->second.failcount;
e.source = i->second.source; e.source = i->second.source;
@ -2660,7 +2658,7 @@ namespace libtorrent
} }
else else
{ {
bi.peer = p->ip; bi.peer = p->ip();
bi.bytes_progress = complete ? bi.block_size : 0; bi.bytes_progress = complete ? bi.block_size : 0;
} }
} }
@ -2684,7 +2682,7 @@ namespace libtorrent
#ifndef NDEBUG #ifndef NDEBUG
// this asserts that we don't have duplicates in the policy's peer list // this asserts that we don't have duplicates in the policy's peer list
peer_iterator i_ = std::find_if(m_connections.begin(), m_connections.end() peer_iterator i_ = std::find_if(m_connections.begin(), m_connections.end()
, bind(&peer_connection::remote, _1) == peerinfo->ip); , bind(&peer_connection::remote, _1) == peerinfo->ip());
TORRENT_ASSERT(i_ == m_connections.end() TORRENT_ASSERT(i_ == m_connections.end()
|| dynamic_cast<bt_peer_connection*>(*i_) == 0); || dynamic_cast<bt_peer_connection*>(*i_) == 0);
#endif #endif
@ -2692,8 +2690,8 @@ namespace libtorrent
TORRENT_ASSERT(want_more_peers()); TORRENT_ASSERT(want_more_peers());
TORRENT_ASSERT(m_ses.num_connections() < m_ses.max_connections()); TORRENT_ASSERT(m_ses.num_connections() < m_ses.max_connections());
tcp::endpoint const& a(peerinfo->ip); tcp::endpoint a(peerinfo->ip());
TORRENT_ASSERT((m_ses.m_ip_filter.access(a.address()) & ip_filter::blocked) == 0); TORRENT_ASSERT((m_ses.m_ip_filter.access(peerinfo->addr) & ip_filter::blocked) == 0);
boost::shared_ptr<socket_type> s(new socket_type(m_ses.m_io_service)); boost::shared_ptr<socket_type> s(new socket_type(m_ses.m_io_service));