better exception handling and more invariant checks

This commit is contained in:
Arvid Norberg 2008-01-19 19:00:54 +00:00
parent 365b8c07c6
commit 8b455be1d9
3 changed files with 29 additions and 14 deletions

View File

@ -396,6 +396,7 @@ namespace libtorrent
peer_connection::~peer_connection()
{
// INVARIANT_CHECK;
TORRENT_ASSERT(!m_in_constructor);
TORRENT_ASSERT(m_disconnecting);
#ifdef TORRENT_VERBOSE_LOGGING
@ -627,6 +628,7 @@ namespace libtorrent
// check to make sure we don't have another connection with the same
// info_hash and peer_id. If we do. close this connection.
t->attach_peer(this);
if (m_disconnecting) return;
m_torrent = wpt;
TORRENT_ASSERT(!m_torrent.expired());
@ -2048,16 +2050,19 @@ namespace libtorrent
#if defined(TORRENT_VERBOSE_LOGGING)
(*m_logger) << "*** CONNECTION FAILED " << message << "\n";
#endif
// we cannot create an intrusive pointer to ourselves, since we
// might be calling disconnect from the constructor (which would
// delete ourselves before the constructor returns)
// boost::intrusive_ptr<peer_connection> me(this);
INVARIANT_CHECK;
if (m_disconnecting) return;
m_disconnecting = true;
if (m_connecting)
if (m_connecting && m_connection_ticket >= 0)
{
m_ses.m_half_open.done(m_connection_ticket);
m_ses.m_io_service.post(boost::bind(&close_socket_ignore_error, m_socket));
m_connection_ticket = -1;
}
boost::shared_ptr<torrent> t = m_torrent.lock();
@ -2092,7 +2097,11 @@ namespace libtorrent
m_torrent.reset();
}
boost::shared_ptr<socket_type> sock = m_socket;
m_disconnecting = true;
m_ses.close_connection(this, message);
m_ses.m_io_service.post(boost::bind(&close_socket_ignore_error, sock));
}
void peer_connection::set_upload_limit(int limit)
@ -3004,6 +3013,18 @@ namespace libtorrent
#ifndef NDEBUG
void peer_connection::check_invariant() const
{
if (m_disconnecting)
{
for (aux::session_impl::torrent_map::const_iterator i = m_ses.m_torrents.begin()
, end(m_ses.m_torrents.end()); i != end; ++i)
TORRENT_ASSERT(!i->second->has_peer((peer_connection*)this));
TORRENT_ASSERT(!m_torrent.lock());
}
else if (!m_in_constructor)
{
TORRENT_ASSERT(m_ses.has_peer((peer_connection*)this));
}
for (int i = 0; i < 2; ++i)
{
// this peer is in the bandwidth history iff max_assignable < limit
@ -3253,7 +3274,6 @@ namespace libtorrent
bool peer_connection::is_seed() const
{
INVARIANT_CHECK;
// if m_num_pieces == 0, we probably don't have the
// metadata yet.
return m_num_pieces == (int)m_have_piece.size() && m_num_pieces > 0;

View File

@ -2475,6 +2475,7 @@ namespace detail
boost::shared_ptr<torrent> t = (*i)->associated_torrent().lock();
peer_connection* p = i->get();
TORRENT_ASSERT(!p->is_disconnecting());
if (!p->is_choked()) ++unchokes;
if (p->peer_info_struct()
&& p->peer_info_struct()->optimistically_unchoked)

View File

@ -2195,8 +2195,6 @@ namespace libtorrent
throw protocol_error("reached connection limit");
}
TORRENT_ASSERT(m_connections.find(p) == m_connections.end());
peer_iterator ci = m_connections.insert(p).first;
try
{
// if new_connection throws, we have to remove the
@ -2210,13 +2208,8 @@ namespace libtorrent
if (pp) p->add_extension(pp);
}
#endif
TORRENT_ASSERT(m_connections.find(p) == ci);
TORRENT_ASSERT(*ci == p);
if (!m_policy.new_connection(**ci))
{
m_connections.erase(ci);
if (!m_policy.new_connection(*p))
return;
}
}
catch (std::exception& e)
{
@ -2224,9 +2217,10 @@ namespace libtorrent
(*m_ses.m_logger) << time_now_string() << " CLOSING CONNECTION "
<< p->remote() << " policy::new_connection threw: " << e.what() << "\n";
#endif
m_connections.erase(ci);
throw;
}
TORRENT_ASSERT(m_connections.find(p) == m_connections.end());
peer_iterator ci = m_connections.insert(p).first;
TORRENT_ASSERT(p->remote() == p->get_socket()->remote_endpoint());
#ifndef NDEBUG