From 068adcd9a989ebf9e791a5f974500e568aeb9061 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 12 May 2008 06:03:31 +0000 Subject: [PATCH] indentation fix --- src/bt_peer_connection.cpp | 79 ++++++++++++++++++------------------- src/peer_connection.cpp | 79 +++++++++++++++++-------------------- src/session_impl.cpp | 12 +----- src/torrent.cpp | 9 ++--- src/ut_pex.cpp | 2 +- src/web_peer_connection.cpp | 16 ++++---- 6 files changed, 88 insertions(+), 109 deletions(-) diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 23b304a07..415a88b52 100755 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -800,7 +800,7 @@ namespace libtorrent TORRENT_ASSERT(received > 0); if (packet_size() != 1) { - disconnect("'choke' message size != 1"); + disconnect("'choke' message size != 1", 2); return; } m_statistics.received_bytes(0, received); @@ -835,7 +835,7 @@ namespace libtorrent TORRENT_ASSERT(received > 0); if (packet_size() != 1) { - disconnect("'unchoke' message size != 1"); + disconnect("'unchoke' message size != 1", 2); return; } m_statistics.received_bytes(0, received); @@ -855,7 +855,7 @@ namespace libtorrent TORRENT_ASSERT(received > 0); if (packet_size() != 1) { - disconnect("'interested' message size != 1"); + disconnect("'interested' message size != 1", 2); return; } m_statistics.received_bytes(0, received); @@ -875,7 +875,7 @@ namespace libtorrent TORRENT_ASSERT(received > 0); if (packet_size() != 1) { - disconnect("'not interested' message size != 1"); + disconnect("'not interested' message size != 1", 2); return; } m_statistics.received_bytes(0, received); @@ -895,7 +895,7 @@ namespace libtorrent TORRENT_ASSERT(received > 0); if (packet_size() != 5) { - disconnect("'have' message size != 5"); + disconnect("'have' message size != 5", 2); return; } m_statistics.received_bytes(0, received); @@ -927,7 +927,7 @@ namespace libtorrent if (t->valid_metadata() && packet_size() - 1 != ((int)get_bitfield().size() + 7) / 8) { - disconnect("bitfield with invalid size"); + disconnect("bitfield with invalid size", 2); return; } @@ -964,7 +964,7 @@ namespace libtorrent TORRENT_ASSERT(received > 0); if (packet_size() != 13) { - disconnect("'request' message size != 13"); + disconnect("'request' message size != 13", 2); return; } m_statistics.received_bytes(0, received); @@ -1046,7 +1046,7 @@ namespace libtorrent TORRENT_ASSERT(received > 0); if (packet_size() != 13) { - disconnect("'cancel' message size != 13"); + disconnect("'cancel' message size != 13", 2); return; } m_statistics.received_bytes(0, received); @@ -1073,14 +1073,14 @@ namespace libtorrent if (!m_supports_dht_port) { - disconnect("got 'dht_port' message from peer that doesn't support it"); + disconnect("got 'dht_port' message from peer that doesn't support it", 2); return; } TORRENT_ASSERT(received > 0); if (packet_size() != 3) { - disconnect("'dht_port' message size != 3"); + disconnect("'dht_port' message size != 3", 2); return; } m_statistics.received_bytes(0, received); @@ -1100,7 +1100,7 @@ namespace libtorrent if (!m_supports_fast) { - disconnect("got 'suggest_piece' without FAST excension support"); + disconnect("got 'suggest_piece' without FAST excension support", 2); return; } @@ -1120,7 +1120,7 @@ namespace libtorrent if (!m_supports_fast) { - disconnect("got 'have_all' without FAST extension support"); + disconnect("got 'have_all' without FAST extension support", 2); return; } m_statistics.received_bytes(0, received); @@ -1133,7 +1133,7 @@ namespace libtorrent if (!m_supports_fast) { - disconnect("got 'have_none' without FAST extension support"); + disconnect("got 'have_none' without FAST extension support", 2); return; } m_statistics.received_bytes(0, received); @@ -1146,7 +1146,7 @@ namespace libtorrent if (!m_supports_fast) { - disconnect("got 'reject_request' without FAST extension support"); + disconnect("got 'reject_request' without FAST extension support", 2); return; } @@ -1170,7 +1170,7 @@ namespace libtorrent if (!m_supports_fast) { - disconnect("got 'allowed_fast' without FAST extension support"); + disconnect("got 'allowed_fast' without FAST extension support", 2); return; } @@ -1195,13 +1195,13 @@ namespace libtorrent m_statistics.received_bytes(0, received); if (packet_size() < 2) { - disconnect("'extended' message smaller than 2 bytes"); + disconnect("'extended' message smaller than 2 bytes", 2); return; } if (associated_torrent().expired()) { - disconnect("'extended' message sent before proper handshake"); + disconnect("'extended' message sent before proper handshake", 2); return; } @@ -1231,7 +1231,7 @@ namespace libtorrent std::stringstream msg; msg << "unknown extended message id: " << extended_id; - disconnect(msg.str().c_str()); + disconnect(msg.str().c_str(), 2); return; } @@ -1352,7 +1352,7 @@ namespace libtorrent std::stringstream msg; msg << "unkown message id: " << packet_type << " size: " << packet_size(); - disconnect(msg.str().c_str()); + disconnect(msg.str().c_str(), 2); return packet_finished(); } @@ -1804,7 +1804,7 @@ namespace libtorrent if (recv_buffer.left() < 20) { if (packet_finished()) - disconnect("sync hash not found"); + disconnect("sync hash not found", 2); return; } @@ -1830,7 +1830,7 @@ namespace libtorrent m_sync_bytes_read += bytes_processed; if (m_sync_bytes_read >= 512) { - disconnect("sync hash not found within 532 bytes"); + disconnect("sync hash not found within 532 bytes", 2); return; } @@ -1915,7 +1915,7 @@ namespace libtorrent if (!m_RC4_handler.get()) { - disconnect("invalid streamkey identifier (info hash) in encrypted handshake"); + disconnect("invalid streamkey identifier (info hash) in encrypted handshake", 2); return; } @@ -1927,7 +1927,7 @@ namespace libtorrent const char sh_vc[] = {0,0,0,0, 0,0,0,0}; if (!std::equal(sh_vc, sh_vc+8, recv_buffer.begin + 20)) { - disconnect("unable to verify constant"); + disconnect("unable to verify constant", 2); return; } @@ -1949,7 +1949,7 @@ namespace libtorrent if (recv_buffer.left() < 8) { if (packet_finished()) - disconnect("sync verification constant not found"); + disconnect("sync verification constant not found", 2); return; } @@ -1974,7 +1974,7 @@ namespace libtorrent m_sync_bytes_read += bytes_processed; if (m_sync_bytes_read >= 512) { - disconnect("sync verification constant not found within 520 bytes"); + disconnect("sync verification constant not found within 520 bytes", 2); return; } @@ -2037,7 +2037,7 @@ namespace libtorrent case pe_settings::plaintext: if (!(crypto_field & 0x01)) { - disconnect("plaintext not provided"); + disconnect("plaintext not provided", 1); return; } crypto_select = 0x01; @@ -2045,7 +2045,7 @@ namespace libtorrent case pe_settings::rc4: if (!(crypto_field & 0x02)) { - disconnect("rc4 not provided"); + disconnect("rc4 not provided", 1); return; } crypto_select = 0x02; @@ -2067,7 +2067,7 @@ namespace libtorrent } if (!crypto_select) { - disconnect("rc4/plaintext not provided"); + disconnect("rc4/plaintext not provided", 1); return; } break; @@ -2085,7 +2085,7 @@ namespace libtorrent { if (allowed_enc_level == pe_settings::plaintext) { - disconnect("rc4 selected by peer when not provided"); + disconnect("rc4 selected by peer when not provided", 2); return; } m_rc4_encrypted = true; @@ -2094,14 +2094,14 @@ namespace libtorrent { if (allowed_enc_level == pe_settings::rc4) { - disconnect("plaintext selected by peer when not provided"); + disconnect("plaintext selected by peer when not provided", 2); return; } m_rc4_encrypted = false; } else { - disconnect("unsupported crypto method selected by peer"); + disconnect("unsupported crypto method selected by peer", 2); return; } } @@ -2109,7 +2109,7 @@ namespace libtorrent int len_pad = detail::read_int16(recv_buffer.begin); if (len_pad < 0 || len_pad > 512) { - disconnect("invalid pad length"); + disconnect("invalid pad length", 2); return; } @@ -2147,7 +2147,7 @@ namespace libtorrent if (len_ia < 0) { - disconnect("invalid len_ia in handshake"); + disconnect("invalid len_ia in handshake", 2); return; } @@ -2279,7 +2279,7 @@ namespace libtorrent TORRENT_ASSERT((!is_local() && m_encrypted) || is_local()); #endif // #ifndef TORRENT_DISABLE_ENCRYPTION - disconnect("incorrect protocol identifier"); + disconnect("incorrect protocol identifier", 2); return; } @@ -2363,7 +2363,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING (*m_logger) << " received invalid info_hash\n"; #endif - disconnect("invalid info-hash in handshake"); + disconnect("invalid info-hash in handshake", 2); return; } @@ -2452,8 +2452,7 @@ namespace libtorrent if (pid == m_ses.get_peer_id()) { - set_failed(); - disconnect("closing connection to ourself"); + disconnect("closing connection to ourself", 1); return; } @@ -2469,8 +2468,7 @@ namespace libtorrent // since it most likely is ourself then if (pid == m_ses.get_peer_id()) { - set_failed(); - disconnect("closing connection to ourself"); + disconnect("closing connection to ourself", 1); return; } @@ -2546,7 +2544,7 @@ namespace libtorrent // packet too large std::stringstream msg; msg << "packet > 1 MB (" << (unsigned int)packet_size << " bytes)"; - disconnect(msg.str().c_str()); + disconnect(msg.str().c_str(), 2); return; } @@ -2591,7 +2589,6 @@ namespace libtorrent // SEND DATA // -------------------------- - // throws exception when the client should be disconnected void bt_peer_connection::on_sent(error_code const& error , std::size_t bytes_transferred) { diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 142d3b5a2..4acd4fca8 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -439,7 +439,7 @@ namespace libtorrent // if this is a web seed. we don't have a peer_info struct if (m_peer_info) m_peer_info->seed = true; // if we're a seed too, disconnect - if (t->is_finished()) + if (t->is_finished() && m_ses.settings().close_redundant_connections) { disconnect("seed to seed connection redundant"); return; @@ -697,7 +697,7 @@ namespace libtorrent (*m_logger) << " " << i->second->torrent_file().info_hash() << "\n"; } #endif - disconnect("got invalid info-hash"); + disconnect("got invalid info-hash", 2); return; } @@ -1065,7 +1065,7 @@ namespace libtorrent // if we got an invalid message, abort if (index >= int(m_have_piece.size()) || index < 0) { - disconnect("got 'have'-message with higher index than the number of pieces"); + disconnect("got 'have'-message with higher index than the number of pieces", 2); return; } @@ -1159,7 +1159,7 @@ namespace libtorrent msg << "got bitfield with invalid size: " << (bitfield.size() / 8) << "bytes. expected: " << (m_have_piece.size() / 8) << " bytes"; - disconnect(msg.str().c_str()); + disconnect(msg.str().c_str(), 2); return; } @@ -1471,7 +1471,7 @@ namespace libtorrent "start: " << p.start << " | " "length: " << p.length << " ]\n"; #endif - disconnect("got invalid piece packet"); + disconnect("got invalid piece packet", 2); return; } @@ -2156,24 +2156,38 @@ namespace libtorrent (*m_ses.m_logger) << time_now_string() << " CONNECTION TIMED OUT: " << m_remote.address().to_string() << "\n"; #endif - set_failed(); - disconnect("timed out"); + disconnect("timed out: connect", 1); } - void peer_connection::disconnect(char const* message) + // the error argument defaults to 0, which means deliberate disconnect + // 1 means unexpected disconnect/error + // 2 protocol error (client sent something invalid) + void peer_connection::disconnect(char const* message, int error) { session_impl::mutex_t::scoped_lock l(m_ses.m_mutex); #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING - (*m_logger) << "*** CONNECTION FAILED " << message << "\n"; + switch (error) + { + case 0: + (*m_logger) << "*** CONNECTION CLOSED " << message << "\n"; + break; + case 1: + (*m_logger) << "*** CONNECTION FAILED " << message << "\n"; + break; + case 2: + (*m_logger) << "*** PEER ERROR " << message << "\n"; + break; + } #endif // we cannot do this in a constructor TORRENT_ASSERT(m_in_constructor == false); + if (error > 0) m_failed = true; + if (m_disconnecting) return; boost::intrusive_ptr me(this); INVARIANT_CHECK; - if (m_disconnecting) return; if (m_connecting && m_connection_ticket >= 0) { m_ses.m_half_open.done(m_connection_ticket); @@ -2182,13 +2196,18 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); - if (message && m_ses.m_alerts.should_post(alert::debug)) + if (message) { - m_ses.m_alerts.post_alert( - peer_error_alert( - remote() - , pid() - , message)); + if (error > 1 && m_ses.m_alerts.should_post(alert::info)) + { + m_ses.m_alerts.post_alert( + peer_error_alert(remote(), pid(), message)); + } + else if (error <= 1 && m_ses.m_alerts.should_post(alert::debug)) + { + m_ses.m_alerts.post_alert( + peer_disconnected_alert(remote(), pid(), message)); + } } if (t) @@ -2392,7 +2411,7 @@ namespace libtorrent if (disk_buffer_size > 16 * 1024) { - disconnect("invalid piece size"); + disconnect("invalid piece size", 2); return false; } @@ -3034,7 +3053,6 @@ namespace libtorrent (*m_logger) << time_now_string() << " **ERROR**: " << error.message() << "[in peer_connection::on_receive_data]\n"; #endif - set_failed(); on_receive(error, bytes_transferred); disconnect(error.message().c_str()); return; @@ -3127,27 +3145,6 @@ namespace libtorrent setup_receive(); } - /* - catch (file_error& e) - { - session_impl::mutex_t::scoped_lock l(m_ses.m_mutex); - - boost::shared_ptr t = m_torrent.lock(); - if (!t) - { - disconnect(e.what()); - return; - } - - if (t->alerts().should_post(alert::fatal)) - { - t->alerts().post_alert( - file_error_alert(t->get_handle() - , std::string("torrent paused: ") + e.what())); - } - t->pause(); - } -*/ bool peer_connection::can_write() const { @@ -3250,8 +3247,7 @@ namespace libtorrent (*m_ses.m_logger) << time_now_string() << " CONNECTION FAILED: " << m_remote.address().to_string() << ": " << e.message() << "\n"; #endif - set_failed(); - disconnect(e.message().c_str()); + disconnect(e.message().c_str(), 1); return; } @@ -3306,7 +3302,6 @@ namespace libtorrent #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING (*m_logger) << "**ERROR**: " << error.message() << " [in peer_connection::on_send_data]\n"; #endif - set_failed(); disconnect(error.message().c_str()); return; } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index e3b669094..5f6fb90a9 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1165,20 +1165,10 @@ namespace aux { peer_connection& c = *j->get(); if (c.has_timed_out()) { - if (m_alerts.should_post(alert::debug)) - { - m_alerts.post_alert( - peer_error_alert( - c.remote() - , c.pid() - , "connection timed out")); - } #if defined(TORRENT_VERBOSE_LOGGING) (*c.m_logger) << "*** CONNECTION TIMED OUT\n"; #endif - - c.set_failed(); - c.disconnect("timed out"); + c.disconnect("timed out: inactive", true); continue; } diff --git a/src/torrent.cpp b/src/torrent.cpp index 22dd9ccee..39b4df523 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -2137,9 +2137,7 @@ namespace libtorrent #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING (*m_ses.m_logger) << " ** HOSTNAME LOOKUP FAILED!**: " << e.what() << "\n"; #endif - - // TODO: post an error alert! - c->disconnect(e.what()); + c->disconnect(e.what(), 1); } #endif } @@ -2920,7 +2918,7 @@ namespace libtorrent } } std::for_each(seeds.begin(), seeds.end() - , bind(&peer_connection::disconnect, _1, "torrent finished, disconnecting seed")); + , bind(&peer_connection::disconnect, _1, "torrent finished, disconnecting seed", 0)); TORRENT_ASSERT(m_storage); // we need to keep the object alive during this operation @@ -3605,8 +3603,7 @@ namespace libtorrent #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING (*p->m_logger) << "**ERROR**: " << e.what() << "\n"; #endif - p->set_failed(); - p->disconnect(e.what()); + p->disconnect(e.what(), 1); } #endif } diff --git a/src/ut_pex.cpp b/src/ut_pex.cpp index d27186a1b..a98fc9508 100644 --- a/src/ut_pex.cpp +++ b/src/ut_pex.cpp @@ -113,7 +113,7 @@ namespace libtorrent { namespace for (torrent::peer_iterator i = m_torrent.begin() , end(m_torrent.end()); i != end; ++i) { - peer_connection* peer = *i; + peer_connection* peer = *i; if (!send_peer(*peer)) continue; tcp::endpoint const& remote = peer->remote(); diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index 444e9c13c..3b9a19a7d 100755 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -349,7 +349,7 @@ namespace libtorrent if (error) { - disconnect("failed to parse HTTP response"); + disconnect("failed to parse HTTP response", 2); return; } @@ -380,7 +380,7 @@ namespace libtorrent m_ses.m_alerts.post_alert(url_seed_alert(t->get_handle(), url() , error_msg)); } - disconnect(error_msg.c_str()); + disconnect(error_msg.c_str(), 1); return; } if (!m_parser.header_finished()) break; @@ -406,7 +406,7 @@ namespace libtorrent { // we should not try this server again. t->remove_url_seed(m_url); - disconnect("got HTTP redirection status without location header"); + disconnect("got HTTP redirection status without location header", 2); return; } @@ -430,7 +430,7 @@ namespace libtorrent std::stringstream msg; msg << "got invalid HTTP redirection location (\"" << location << "\") " "expected it to end with: " << path; - disconnect(msg.str().c_str()); + disconnect(msg.str().c_str(), 2); return; } location.resize(i); @@ -475,7 +475,7 @@ namespace libtorrent t->remove_url_seed(m_url); std::stringstream msg; msg << "invalid range in HTTP response: " << range_str.str(); - disconnect(msg.str().c_str()); + disconnect(msg.str().c_str(), 2); return; } // the http range is inclusive @@ -489,7 +489,7 @@ namespace libtorrent { // we should not try this server again. t->remove_url_seed(m_url); - disconnect("no content-length in HTTP response"); + disconnect("no content-length in HTTP response", 2); return; } } @@ -501,7 +501,7 @@ namespace libtorrent if (m_requests.empty() || m_file_requests.empty()) { - disconnect("unexpected HTTP response"); + disconnect("unexpected HTTP response", 2); return; } @@ -534,7 +534,7 @@ namespace libtorrent { // this means the end of the incoming request ends _before_ the // first expected byte (fs + m_piece.size()) - disconnect("invalid range in HTTP response"); + disconnect("invalid range in HTTP response", 2); return; }