diff --git a/src/command_download.cc b/src/command_download.cc index d87454bc..97b98b32 100644 --- a/src/command_download.cc +++ b/src/command_download.cc @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include diff --git a/src/command_throttle.cc b/src/command_throttle.cc index 06935e2c..18267634 100644 --- a/src/command_throttle.cc +++ b/src/command_throttle.cc @@ -70,33 +70,35 @@ parse_address_range(const torrent::Object::list_type& args, torrent::Object::lis torrent::Object apply_throttle(const torrent::Object::list_type& args, bool up) { - torrent::Object::list_const_iterator argItr = args.begin(); + auto arg_itr = args.begin(); - if (argItr == args.end()) + if (arg_itr == args.end()) throw torrent::input_error("Missing throttle name."); - const std::string& name = argItr->as_string(); + const std::string& name = arg_itr->as_string(); if (name.empty() || name == "NULL") throw torrent::input_error("Invalid throttle name '" + name + "'."); - if (++argItr == args.end() || argItr->as_string().empty()) + if (++arg_itr == args.end() || arg_itr->as_string().empty()) throw torrent::input_error("Missing throttle rate for '" + name + "'."); int64_t rate; - rpc::parse_whole_value_nothrow(argItr->as_string().c_str(), &rate); + rpc::parse_whole_value_nothrow(arg_itr->as_string().c_str(), &rate); if (rate < 0) throw torrent::input_error("Throttle rate must be non-negative."); - core::ThrottleMap::iterator itr = control->core()->throttles().find(name); - if (itr == control->core()->throttles().end()) - itr = control->core()->throttles().insert(std::make_pair(name, torrent::ThrottlePair(NULL, NULL))).first; + auto itr = control->core()->throttles().find(name); - torrent::Throttle*& throttle = up ? itr->second.first : itr->second.second; - if (rate != 0 && throttle == NULL) + if (itr == control->core()->throttles().end()) + itr = control->core()->throttles().insert(std::make_pair(name, core::ThrottlePair(nullptr, nullptr))).first; + + auto*& throttle = up ? itr->second.first : itr->second.second; + + if (rate != 0 && throttle == nullptr) throttle = (up ? torrent::up_throttle_global() : torrent::down_throttle_global())->create_slave(); - if (throttle != NULL) + if (throttle != nullptr) throttle->set_max_rate(rate * 1024); return torrent::Object(); @@ -109,10 +111,10 @@ static const int throttle_info_rate = (1 << 3); torrent::Object retrieve_throttle_info(const torrent::Object::string_type& name, int flags) { - core::ThrottleMap::iterator itr = control->core()->throttles().find(name); - torrent::ThrottlePair throttles = itr == control->core()->throttles().end() ? torrent::ThrottlePair(NULL, NULL) : itr->second; - torrent::Throttle* throttle = flags & throttle_info_down ? throttles.second : throttles.first; - torrent::Throttle* global = flags & throttle_info_down ? torrent::down_throttle_global() : torrent::up_throttle_global(); + auto itr = control->core()->throttles().find(name); + auto throttles = (itr == control->core()->throttles().end()) ? core::ThrottlePair(nullptr, nullptr) : itr->second; + auto* throttle = flags & throttle_info_down ? throttles.second : throttles.first; + auto* global = flags & throttle_info_down ? torrent::down_throttle_global() : torrent::up_throttle_global(); if (throttle == NULL && name.empty()) throttle = global; @@ -127,20 +129,6 @@ retrieve_throttle_info(const torrent::Object::string_type& name, int flags) { return (int64_t)throttle->max_rate(); } -torrent::Object -apply_address_throttle(const torrent::Object::list_type& args) { - if (args.size() < 2 || args.size() > 3) - throw torrent::input_error("Incorrect number of arguments."); - - std::pair range = parse_address_range(args, ++args.begin()); - core::ThrottleMap::iterator throttleItr = control->core()->throttles().find(args.begin()->as_string().c_str()); - if (throttleItr == control->core()->throttles().end()) - throw torrent::input_error("Throttle not found."); - - control->core()->set_address_throttle(range.first, range.second, throttleItr->second); - return torrent::Object(); -} - torrent::Object throttle_update(const char* variable, int64_t value) { rpc::commands.call_command(variable, value); @@ -198,7 +186,6 @@ initialize_command_throttle() { // than kB. CMD2_ANY_LIST ("throttle.up", std::bind(&apply_throttle, std::placeholders::_2, true)); CMD2_ANY_LIST ("throttle.down", std::bind(&apply_throttle, std::placeholders::_2, false)); - CMD2_ANY_LIST ("throttle.ip", std::bind(&apply_address_throttle, std::placeholders::_2)); CMD2_ANY_STRING ("throttle.up.max", std::bind(&retrieve_throttle_info, std::placeholders::_2, throttle_info_up | throttle_info_max)); CMD2_ANY_STRING ("throttle.up.rate", std::bind(&retrieve_throttle_info, std::placeholders::_2, throttle_info_up | throttle_info_rate)); diff --git a/src/core/download.cc b/src/core/download.cc index be53b3f2..c28badc8 100644 --- a/src/core/download.cc +++ b/src/core/download.cc @@ -88,15 +88,16 @@ Download::distributed_copies() const { } void -Download::set_throttle_name(const std::string& throttleName) { +Download::set_throttle_name(const std::string& name) { if (m_download.info()->is_active()) throw torrent::input_error("Cannot set throttle on active download."); - torrent::ThrottlePair throttles = control->core()->get_throttle(throttleName); + auto throttles = control->core()->get_throttle(name); + m_download.set_upload_throttle(throttles.first); m_download.set_download_throttle(throttles.second); - m_download.bencode()->get_key("rtorrent").insert_key("throttle_name", throttleName); + m_download.bencode()->get_key("rtorrent").insert_key("throttle_name", name); } void diff --git a/src/core/download.h b/src/core/download.h index e654e285..67049c36 100644 --- a/src/core/download.h +++ b/src/core/download.h @@ -80,7 +80,7 @@ public: void set_root_directory(const std::string& path); - void set_throttle_name(const std::string& throttleName); + void set_throttle_name(const std::string& name); bool operator == (const std::string& str) const; diff --git a/src/core/manager.cc b/src/core/manager.cc index ae853a02..bb1bd66f 100644 --- a/src/core/manager.cc +++ b/src/core/manager.cc @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -87,10 +86,10 @@ Manager::set_hashing_view(View* v) { m_hashingView->signal_changed().push_back(std::bind(&Manager::receive_hashing_changed, this)); } -torrent::ThrottlePair +ThrottlePair Manager::get_throttle(const std::string& name) { - ThrottleMap::const_iterator itr = m_throttles.find(name); - torrent::ThrottlePair throttles = (itr == m_throttles.end() ? torrent::ThrottlePair(nullptr, nullptr) : itr->second); + auto itr = m_throttles.find(name); + auto throttles = (itr == m_throttles.end() ? ThrottlePair(nullptr, nullptr) : itr->second); if (throttles.first == nullptr) throttles.first = torrent::up_throttle_global(); @@ -101,20 +100,6 @@ Manager::get_throttle(const std::string& name) { return throttles; } -void -Manager::set_address_throttle(uint32_t begin, uint32_t end, torrent::ThrottlePair throttles) { - m_addressThrottles.set_merge(begin, end, throttles); - torrent::connection_manager()->address_throttle() = std::bind(&core::Manager::get_address_throttle, control->core(), std::placeholders::_1); -} - -torrent::ThrottlePair -Manager::get_address_throttle(const sockaddr* addr) { - if (addr->sa_family != AF_INET) - return torrent::ThrottlePair(nullptr, nullptr); - - return m_addressThrottles.get(ntohl(reinterpret_cast(addr)->sin_addr.s_addr), torrent::ThrottlePair(nullptr, nullptr)); -} - int64_t Manager::retrieve_throttle_value(const torrent::Object::string_type& name, bool rate, bool up) { ThrottleMap::iterator itr = throttles().find(name); diff --git a/src/core/manager.h b/src/core/manager.h index 5464000b..5b08ebdc 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -5,7 +5,6 @@ #include #include #include -#include #include #include "download_list.h" @@ -23,7 +22,8 @@ namespace core { class HttpQueue; -typedef std::map ThrottleMap; +using ThrottlePair = std::pair; +using ThrottleMap = std::map; class View; @@ -37,26 +37,22 @@ public: bool is_download_shutdown_completed(); - DownloadList* download_list() { return m_download_list.get(); } - FileStatusCache* file_status_cache() { return m_file_status_cache.get(); } + DownloadList* download_list() { return m_download_list.get(); } + FileStatusCache* file_status_cache() { return m_file_status_cache.get(); } - HttpQueue* http_queue() { return m_http_queue.get(); } + HttpQueue* http_queue() { return m_http_queue.get(); } - View* hashing_view() { return m_hashingView; } + View* hashing_view() { return m_hashingView; } void set_hashing_view(View* v); - torrent::log_buffer* log_important() { return m_log_important.get(); } - torrent::log_buffer* log_complete() { return m_log_complete.get(); } + auto* log_important() { return m_log_important.get(); } + auto* log_complete() { return m_log_complete.get(); } - ThrottleMap& throttles() { return m_throttles; } - torrent::ThrottlePair get_throttle(const std::string& name); + ThrottleMap& throttles() { return m_throttles; } + ThrottlePair get_throttle(const std::string& name); int64_t retrieve_throttle_value(const torrent::Object::string_type& name, bool rate, bool up); - // Use custom throttle for the given range of IP addresses. - void set_address_throttle(uint32_t begin, uint32_t end, torrent::ThrottlePair throttles); - torrent::ThrottlePair get_address_throttle(const sockaddr* addr); - void cleanup(); void listen_open(); @@ -87,8 +83,6 @@ public: void try_create_download_from_meta_download(torrent::Object* bencode, const std::string& metafile); private: - typedef RangeMap AddressThrottleMap; - void create_http(const std::string& uri); void create_final(std::istream* s); @@ -104,7 +98,6 @@ private: View* m_hashingView{}; ThrottleMap m_throttles; - AddressThrottleMap m_addressThrottles; torrent::log_buffer_ptr m_log_important; torrent::log_buffer_ptr m_log_complete; diff --git a/src/main.cc b/src/main.cc index 9d5e1923..6fc782cf 100644 --- a/src/main.cc +++ b/src/main.cc @@ -387,6 +387,11 @@ main(int argc, char** argv) { CMD2_ANY_STRING_V("encoding.add", [](auto, auto) { lt_log_print(torrent::LOG_WARN, "The 'encoding.add' command is deprecated and does nothing."); }); + + CMD2_ANY_LIST ("throttle.ip", []( auto, auto) { + lt_log_print(torrent::LOG_WARN, "The 'throttle.ip' command is deprecated and does nothing."); + return torrent::Object(); + }); } { diff --git a/src/ui/download.cc b/src/ui/download.cc index 71eb99a5..c17479d0 100644 --- a/src/ui/download.cc +++ b/src/ui/download.cc @@ -4,7 +4,6 @@ #include #include -#include #include #include #include