From 072d3b0f48c1e1f8e04fbba6d82e686dde53ba99 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 26 Nov 2009 21:40:10 +0000 Subject: [PATCH] moved some alert functions from the header to the cpp file --- include/libtorrent/alert_types.hpp | 264 ++++----------------------- src/alert.cpp | 278 +++++++++++++++++++++++++++++ 2 files changed, 313 insertions(+), 229 deletions(-) diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 8d277b9e8..eea2d43fc 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -39,9 +39,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/identify_client.hpp" -#include "libtorrent/socket_io.hpp" +//#include "libtorrent/socket_io.hpp" #include "libtorrent/address.hpp" -#include "libtorrent/escape_string.hpp" // to_hex namespace libtorrent { @@ -51,17 +50,7 @@ namespace libtorrent : handle(h) {} - virtual std::string message() const - { - if (!handle.is_valid()) return " - "; - if (handle.name().empty()) - { - char msg[41]; - to_hex((char const*)&handle.info_hash()[0], 20, msg); - return msg; - } - return handle.name(); - } + virtual std::string message() const; torrent_handle handle; }; @@ -77,12 +66,7 @@ namespace libtorrent const static int static_category = alert::peer_notification; virtual int category() const { return static_category; } - virtual std::string message() const - { - error_code ec; - return torrent_alert::message() + " peer (" + ip.address().to_string(ec) - + ", " + identify_client(pid) + ")"; - } + virtual std::string message() const; tcp::endpoint ip; peer_id pid; @@ -98,10 +82,7 @@ namespace libtorrent const static int static_category = alert::tracker_notification; virtual int category() const { return static_category; } - virtual std::string message() const - { - return torrent_alert::message() + " (" + url + ")"; - } + virtual std::string message() const; std::string url; }; @@ -127,13 +108,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(read_piece_alert); const static int static_category = alert::storage_notification; - virtual std::string message() const - { - char msg[200]; - snprintf(msg, sizeof(msg), "%s: piece %s %u", torrent_alert::message().c_str() - , buffer ? "successful" : "failed", piece); - return msg; - } + virtual std::string message() const; boost::shared_array buffer; int piece; @@ -151,13 +126,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(file_completed_alert); const static int static_category = alert::progress_notification; - virtual std::string message() const - { - char msg[200 + TORRENT_MAX_PATH]; - snprintf(msg, sizeof(msg), "%s: file %d finished downloading" - , torrent_alert::message().c_str(), index); - return msg; - } + virtual std::string message() const; int index; }; @@ -175,13 +144,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(file_renamed_alert); const static int static_category = alert::storage_notification; - virtual std::string message() const - { - char msg[200 + TORRENT_MAX_PATH * 2]; - snprintf(msg, sizeof(msg), "%s: file %d renamed to %s", torrent_alert::message().c_str() - , index, name.c_str()); - return msg; - } + virtual std::string message() const; std::string name; int index; @@ -201,13 +164,7 @@ namespace libtorrent const static int static_category = alert::storage_notification; - virtual std::string message() const - { - char ret[200 + TORRENT_MAX_PATH * 2]; - snprintf(ret, sizeof(ret), "%s: failed to rename file %d: %s" - , torrent_alert::message().c_str(), index, error.message().c_str()); - return ret; - } + virtual std::string message() const; int index; error_code error; @@ -234,20 +191,7 @@ namespace libtorrent const static int static_category = alert::performance_warning; - virtual std::string message() const - { - static char const* warning_str[] = - { - "max outstanding disk writes reached", - "max outstanding piece requests reached", - "upload limit too low (download rate will suffer)", - "download limit too low (upload rate will suffer)", - "send buffer watermark too low (upload rate will suffer)" - }; - - return torrent_alert::message() + ": performance warning: " - + warning_str[warning_code]; - } + virtual std::string message() const; performance_warning_t warning_code; }; @@ -266,16 +210,7 @@ namespace libtorrent const static int static_category = alert::status_notification; - virtual std::string message() const - { - static char const* state_str[] = - {"checking (q)", "checking", "dl metadata" - , "downloading", "finished", "seeding", "allocating" - , "checking (r)"}; - - return torrent_alert::message() + ": state changed to: " - + state_str[state]; - } + virtual std::string message() const; torrent_status::state_t state; torrent_status::state_t prev_state; @@ -312,14 +247,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(tracker_error_alert); const static int static_category = alert::tracker_notification | alert::error_notification; - virtual std::string message() const - { - char ret[400]; - snprintf(ret, sizeof(ret), "%s (%d) %s (%d)" - , torrent_alert::message().c_str(), status_code - , msg.c_str(), times_in_row); - return ret; - } + virtual std::string message() const; int times_in_row; int status_code; @@ -338,10 +266,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(tracker_warning_alert); const static int static_category = alert::tracker_notification | alert::error_notification; - virtual std::string message() const - { - return tracker_alert::message() + " warning: " + msg; - } + virtual std::string message() const; std::string msg; }; @@ -359,13 +284,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(scrape_reply_alert); - virtual std::string message() const - { - char ret[400]; - snprintf(ret, sizeof(ret), "%s scrape reply: %u %u" - , torrent_alert::message().c_str(), incomplete, complete); - return ret; - } + virtual std::string message() const; int incomplete; int complete; @@ -390,8 +309,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(scrape_failed_alert); const static int static_category = alert::tracker_notification | alert::error_notification; - virtual std::string message() const - { return tracker_alert::message() + " scrape failed: " + msg; } + virtual std::string message() const; std::string msg; }; @@ -407,13 +325,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(tracker_reply_alert); - virtual std::string message() const - { - char ret[400]; - snprintf(ret, sizeof(ret), "%s received peers: %u" - , torrent_alert::message().c_str(), num_peers); - return ret; - } + virtual std::string message() const; int num_peers; }; @@ -428,13 +340,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(dht_reply_alert); - virtual std::string message() const - { - char ret[400]; - snprintf(ret, sizeof(ret), "%s received DHT peers: %u" - , torrent_alert::message().c_str(), num_peers); - return ret; - } + virtual std::string message() const; int num_peers; }; @@ -449,11 +355,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(tracker_announce_alert); - virtual std::string message() const - { - const static char* event_str[] = {"none", "completed", "started", "stopped"}; - return tracker_alert::message() + " sending announce (" + event_str[event] + ")"; - } + virtual std::string message() const; int event; }; @@ -470,13 +372,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(hash_failed_alert); const static int static_category = alert::status_notification; - virtual std::string message() const - { - char ret[400]; - snprintf(ret, sizeof(ret), "%s hash for piece %u failed" - , torrent_alert::message().c_str(), piece_index); - return ret; - } + virtual std::string message() const; int piece_index; }; @@ -490,8 +386,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(peer_ban_alert); - virtual std::string message() const - { return peer_alert::message() + " banned peer"; } + virtual std::string message() const; }; struct TORRENT_EXPORT peer_unsnubbed_alert: peer_alert @@ -503,8 +398,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(peer_unsnubbed_alert); - virtual std::string message() const - { return peer_alert::message() + " peer unsnubbed"; } + virtual std::string message() const; }; struct TORRENT_EXPORT peer_snubbed_alert: peer_alert @@ -516,8 +410,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(peer_snubbed_alert); - virtual std::string message() const - { return peer_alert::message() + " peer snubbed"; } + virtual std::string message() const; }; struct TORRENT_EXPORT peer_error_alert: peer_alert @@ -597,13 +490,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(invalid_request_alert); - virtual std::string message() const - { - char ret[200]; - snprintf(ret, sizeof(ret), "%s peer sent an invalid piece request (piece: %u start: %u len: %u)" - , torrent_alert::message().c_str(), request.piece, request.start, request.length); - return ret; - } + virtual std::string message() const; peer_request request; }; @@ -634,13 +521,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(piece_finished_alert); const static int static_category = alert::progress_notification; - virtual std::string message() const - { - char ret[200]; - snprintf(ret, sizeof(ret), "%s piece: %u finished downloading" - , torrent_alert::message().c_str(), piece_index); - return ret; - } + virtual std::string message() const; int piece_index; }; @@ -658,13 +539,7 @@ namespace libtorrent const static int static_category = alert::progress_notification | alert::peer_notification; - virtual std::string message() const - { - char ret[200]; - snprintf(ret, sizeof(ret), "%s peer dropped block ( piece: %u block: %u)" - , torrent_alert::message().c_str(), piece_index, block_index); - return ret; - } + virtual std::string message() const; int block_index; int piece_index; @@ -683,13 +558,7 @@ namespace libtorrent const static int static_category = alert::progress_notification | alert::peer_notification; - virtual std::string message() const - { - char ret[200]; - snprintf(ret, sizeof(ret), "%s peer timed out request ( piece: %u block: %u)" - , torrent_alert::message().c_str(), piece_index, block_index); - return ret; - } + virtual std::string message() const; int block_index; int piece_index; @@ -707,13 +576,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(block_finished_alert); const static int static_category = alert::progress_notification; - virtual std::string message() const - { - char ret[200]; - snprintf(ret, sizeof(ret), "%s block finished downloading (piece: %u block: %u)" - , torrent_alert::message().c_str(), piece_index, block_index); - return ret; - } + virtual std::string message() const; int block_index; int piece_index; @@ -732,13 +595,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(block_downloading_alert); const static int static_category = alert::progress_notification; - virtual std::string message() const - { - char ret[200]; - snprintf(ret, sizeof(ret), "%s requested block (piece: %u block: %u) %s" - , torrent_alert::message().c_str(), piece_index, block_index, peer_speedmsg); - return ret; - } + virtual std::string message() const; char const* peer_speedmsg; int block_index; @@ -756,13 +613,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(unwanted_block_alert); - virtual std::string message() const - { - char ret[200]; - snprintf(ret, sizeof(ret), "%s received block not in download queue (piece: %u block: %u)" - , torrent_alert::message().c_str(), piece_index, block_index); - return ret; - } + virtual std::string message() const; int block_index; int piece_index; @@ -1077,13 +928,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(listen_failed_alert); const static int static_category = alert::status_notification | alert::error_notification; - virtual std::string message() const - { - char ret[200]; - snprintf(ret, sizeof(ret), "listening on %s failed: %s" - , print_endpoint(endpoint).c_str(), error.message().c_str()); - return ret; - } + virtual std::string message() const; tcp::endpoint endpoint; error_code error; @@ -1098,12 +943,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(listen_succeeded_alert); const static int static_category = alert::status_notification; - virtual std::string message() const - { - char ret[200]; - snprintf(ret, sizeof(ret), "successfully listening on %s", print_endpoint(endpoint).c_str()); - return ret; - } + virtual std::string message() const; tcp::endpoint endpoint; }; @@ -1122,12 +962,7 @@ namespace libtorrent const static int static_category = alert::port_mapping_notification | alert::error_notification; - virtual std::string message() const - { - static char const* type_str[] = {"NAT-PMP", "UPnP"}; - return std::string("could not map port using ") + type_str[map_type] - + ": " + error.message(); - } + virtual std::string message() const; int mapping; int map_type; @@ -1146,14 +981,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(portmap_alert); const static int static_category = alert::port_mapping_notification; - virtual std::string message() const - { - static char const* type_str[] = {"NAT-PMP", "UPnP"}; - char ret[200]; - snprintf(ret, sizeof(ret), "successfully mapped port using %s. external port: %u" - , type_str[map_type], external_port); - return ret; - } + virtual std::string message() const; int mapping; int external_port; @@ -1169,13 +997,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(portmap_log_alert); const static int static_category = alert::port_mapping_notification; - virtual std::string message() const - { - static char const* type_str[] = {"NAT-PMP", "UPnP"}; - char ret[200]; - snprintf(ret, sizeof(ret), "%s: %s", type_str[map_type], msg.c_str()); - return ret; - } + virtual std::string message() const; int map_type; std::string msg; @@ -1238,16 +1060,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(dht_announce_alert); const static int static_category = alert::dht_notification; - virtual std::string message() const - { - error_code ec; - char ih_hex[41]; - to_hex((const char*)&info_hash[0], 20, ih_hex); - char msg[200]; - snprintf(msg, sizeof(msg), "incoming dht announce: %s:%u (%s)" - , ip.to_string(ec).c_str(), port, ih_hex); - return msg; - } + virtual std::string message() const; address ip; int port; @@ -1263,14 +1076,7 @@ namespace libtorrent TORRENT_DEFINE_ALERT(dht_get_peers_alert); const static int static_category = alert::dht_notification; - virtual std::string message() const - { - char ih_hex[41]; - to_hex((const char*)&info_hash[0], 20, ih_hex); - char msg[200]; - snprintf(msg, sizeof(msg), "incoming dht get_peers: %s", ih_hex); - return msg; - } + virtual std::string message() const; sha1_hash info_hash; }; diff --git a/src/alert.cpp b/src/alert.cpp index b4a446a56..22c9067c1 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -33,7 +33,9 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include "libtorrent/alert.hpp" +#include "libtorrent/alert_types.hpp" #include "libtorrent/io_service.hpp" +#include "libtorrent/socket_io.hpp" #include "libtorrent/time.hpp" #include @@ -43,6 +45,282 @@ namespace libtorrent { alert::~alert() {} ptime alert::timestamp() const { return m_timestamp; } + + std::string torrent_alert::message() const + { + if (!handle.is_valid()) return " - "; + if (handle.name().empty()) + { + char msg[41]; + to_hex((char const*)&handle.info_hash()[0], 20, msg); + return msg; + } + return handle.name(); + } + + std::string peer_alert::message() const + { + error_code ec; + return torrent_alert::message() + " peer (" + ip.address().to_string(ec) + + ", " + identify_client(pid) + ")"; + } + + std::string tracker_alert::message() const + { + return torrent_alert::message() + " (" + url + ")"; + } + + std::string read_piece_alert::message() const + { + char msg[200]; + snprintf(msg, sizeof(msg), "%s: piece %s %u", torrent_alert::message().c_str() + , buffer ? "successful" : "failed", piece); + return msg; + } + + std::string file_completed_alert::message() const + { + char msg[200 + TORRENT_MAX_PATH]; + snprintf(msg, sizeof(msg), "%s: file %d finished downloading" + , torrent_alert::message().c_str(), index); + return msg; + } + + std::string file_renamed_alert::message() const + { + char msg[200 + TORRENT_MAX_PATH * 2]; + snprintf(msg, sizeof(msg), "%s: file %d renamed to %s", torrent_alert::message().c_str() + , index, name.c_str()); + return msg; + } + + std::string file_rename_failed_alert::message() const + { + char ret[200 + TORRENT_MAX_PATH * 2]; + snprintf(ret, sizeof(ret), "%s: failed to rename file %d: %s" + , torrent_alert::message().c_str(), index, error.message().c_str()); + return ret; + } + + std::string performance_alert::message() const + { + static char const* warning_str[] = + { + "max outstanding disk writes reached", + "max outstanding piece requests reached", + "upload limit too low (download rate will suffer)", + "download limit too low (upload rate will suffer)", + "send buffer watermark too low (upload rate will suffer)" + }; + + return torrent_alert::message() + ": performance warning: " + + warning_str[warning_code]; + } + + std::string state_changed_alert::message() const + { + static char const* state_str[] = + {"checking (q)", "checking", "dl metadata" + , "downloading", "finished", "seeding", "allocating" + , "checking (r)"}; + + return torrent_alert::message() + ": state changed to: " + + state_str[state]; + } + + std::string tracker_error_alert::message() const + { + char ret[400]; + snprintf(ret, sizeof(ret), "%s (%d) %s (%d)" + , torrent_alert::message().c_str(), status_code + , msg.c_str(), times_in_row); + return ret; + } + + std::string tracker_warning_alert::message() const + { + return tracker_alert::message() + " warning: " + msg; + } + + std::string scrape_reply_alert::message() const + { + char ret[400]; + snprintf(ret, sizeof(ret), "%s scrape reply: %u %u" + , torrent_alert::message().c_str(), incomplete, complete); + return ret; + } + + std::string scrape_failed_alert::message() const + { + return tracker_alert::message() + " scrape failed: " + msg; + } + + std::string tracker_reply_alert::message() const + { + char ret[400]; + snprintf(ret, sizeof(ret), "%s received peers: %u" + , torrent_alert::message().c_str(), num_peers); + return ret; + } + + std::string dht_reply_alert::message() const + { + char ret[400]; + snprintf(ret, sizeof(ret), "%s received DHT peers: %u" + , torrent_alert::message().c_str(), num_peers); + return ret; + } + + std::string tracker_announce_alert::message() const + { + const static char* event_str[] = {"none", "completed", "started", "stopped"}; + return tracker_alert::message() + " sending announce (" + event_str[event] + ")"; + } + + std::string hash_failed_alert::message() const + { + char ret[400]; + snprintf(ret, sizeof(ret), "%s hash for piece %u failed" + , torrent_alert::message().c_str(), piece_index); + return ret; + } + + std::string peer_ban_alert::message() const + { + return peer_alert::message() + " banned peer"; + } + + std::string peer_unsnubbed_alert::message() const + { + return peer_alert::message() + " peer unsnubbed"; + } + + std::string peer_snubbed_alert::message() const + { + return peer_alert::message() + " peer snubbed"; + } + + + + std::string invalid_request_alert::message() const + { + char ret[200]; + snprintf(ret, sizeof(ret), "%s peer sent an invalid piece request (piece: %u start: %u len: %u)" + , torrent_alert::message().c_str(), request.piece, request.start, request.length); + return ret; + } + + + std::string piece_finished_alert::message() const + { + char ret[200]; + snprintf(ret, sizeof(ret), "%s piece: %u finished downloading" + , torrent_alert::message().c_str(), piece_index); + return ret; + } + + + std::string request_dropped_alert::message() const + { + char ret[200]; + snprintf(ret, sizeof(ret), "%s peer dropped block ( piece: %u block: %u)" + , torrent_alert::message().c_str(), piece_index, block_index); + return ret; + } + + std::string block_timeout_alert::message() const + { + char ret[200]; + snprintf(ret, sizeof(ret), "%s peer timed out request ( piece: %u block: %u)" + , torrent_alert::message().c_str(), piece_index, block_index); + return ret; + } + + std::string block_finished_alert::message() const + { + char ret[200]; + snprintf(ret, sizeof(ret), "%s block finished downloading (piece: %u block: %u)" + , torrent_alert::message().c_str(), piece_index, block_index); + return ret; + } + + std::string block_downloading_alert::message() const + { + char ret[200]; + snprintf(ret, sizeof(ret), "%s requested block (piece: %u block: %u) %s" + , torrent_alert::message().c_str(), piece_index, block_index, peer_speedmsg); + return ret; + } + + std::string unwanted_block_alert::message() const + { + char ret[200]; + snprintf(ret, sizeof(ret), "%s received block not in download queue (piece: %u block: %u)" + , torrent_alert::message().c_str(), piece_index, block_index); + return ret; + } + + std::string listen_failed_alert::message() const + { + char ret[200]; + snprintf(ret, sizeof(ret), "listening on %s failed: %s" + , print_endpoint(endpoint).c_str(), error.message().c_str()); + return ret; + } + + std::string listen_succeeded_alert::message() const + { + char ret[200]; + snprintf(ret, sizeof(ret), "successfully listening on %s", print_endpoint(endpoint).c_str()); + return ret; + } + + std::string portmap_error_alert::message() const + { + static char const* type_str[] = {"NAT-PMP", "UPnP"}; + return std::string("could not map port using ") + type_str[map_type] + + ": " + error.message(); + } + + std::string portmap_alert::message() const + { + static char const* type_str[] = {"NAT-PMP", "UPnP"}; + char ret[200]; + snprintf(ret, sizeof(ret), "successfully mapped port using %s. external port: %u" + , type_str[map_type], external_port); + return ret; + } + + std::string portmap_log_alert::message() const + { + static char const* type_str[] = {"NAT-PMP", "UPnP"}; + char ret[200]; + snprintf(ret, sizeof(ret), "%s: %s", type_str[map_type], msg.c_str()); + return ret; + } + + std::string dht_announce_alert::message() const + { + error_code ec; + char ih_hex[41]; + to_hex((const char*)&info_hash[0], 20, ih_hex); + char msg[200]; + snprintf(msg, sizeof(msg), "incoming dht announce: %s:%u (%s)" + , ip.to_string(ec).c_str(), port, ih_hex); + return msg; + } + + std::string dht_get_peers_alert::message() const + { + char ih_hex[41]; + to_hex((const char*)&info_hash[0], 20, ih_hex); + char msg[200]; + snprintf(msg, sizeof(msg), "incoming dht get_peers: %s", ih_hex); + return msg; + } + + + alert_manager::alert_manager(io_service& ios) : m_alert_mask(alert::error_notification) , m_queue_size_limit(queue_size_limit_default)