diff --git a/bindings/python/src/error_code.cpp b/bindings/python/src/error_code.cpp index e23bbc511..099dc3bfe 100644 --- a/bindings/python/src/error_code.cpp +++ b/bindings/python/src/error_code.cpp @@ -73,30 +73,30 @@ void bind_error_code() .def("assign", &error_code::assign) ; - def("get_libtorrent_category", &get_libtorrent_category - , return_value_policy()); - - def("get_upnp_category", &get_upnp_category - , return_value_policy()); - - def("get_http_category", &get_http_category - , return_value_policy()); - - def("get_socks_category", &get_socks_category - , return_value_policy()); +using return_existing = return_value_policy; + def("libtorrent_category", &libtorrent_category, return_existing()); + def("upnp_category", &upnp_category, return_existing()); + def("http_category", &http_category, return_existing()); + def("socks_category", &socks_category, return_existing()); + def("bdecode_category", &bdecode_category, return_existing()); #if TORRENT_USE_I2P - def("get_i2p_category", &get_i2p_category - , return_value_policy()); + def("i2p_category", &i2p_category, return_existing()); #endif - def("get_bdecode_category", &get_bdecode_category - , return_value_policy()); +#ifndef TORRENT_NO_DEPRECATE + def("get_libtorrent_category", &libtorrent_category, return_existing()); + def("get_upnp_category", &upnp_category, return_existing()); + def("get_http_category", &http_category, return_existing()); + def("get_socks_category", &socks_category, return_existing()); + def("get_bdecode_category", &bdecode_category, return_existing()); +#if TORRENT_USE_I2P + def("get_i2p_category", &i2p_category, return_existing()); +#endif +#endif // TORRENT_NO_DEPRECATE - def("generic_category", &boost::system::generic_category - , return_value_policy()); + def("generic_category", &boost::system::generic_category, return_existing()); - def("system_category", &boost::system::system_category - , return_value_policy()); + def("system_category", &boost::system::system_category, return_existing()); } diff --git a/docs/manual.rst b/docs/manual.rst index b0a0868db..6b04cbd49 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -117,7 +117,7 @@ for system errors. That is, errors that belong to the generic or system category Errors that belong to the libtorrent error category are not localized however, they are only available in english. In order to translate libtorrent errors, compare the -error category of the ``error_code`` object against ``libtorrent::get_libtorrent_category()``, +error category of the ``error_code`` object against ``libtorrent::libtorrent_category()``, and if matches, you know the error code refers to the list above. You can provide your own mapping from error code to string, which is localized. In this case, you cannot rely on ``error_code::message()`` to generate your strings. @@ -131,7 +131,7 @@ Here's a simple example of how to translate error codes: std::string error_code_to_string(boost::system::error_code const& ec) { - if (ec.category() != libtorrent::get_libtorrent_category()) + if (ec.category() != libtorrent::libtorrent_category()) { return ec.message(); } diff --git a/examples/client_test.cpp b/examples/client_test.cpp index a82285f4c..6c3d2ef9c 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -934,8 +934,7 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a // handshake. The peers that we successfully connect to and then // disconnect is more interesting. if (pd->operation == op_connect - || pd->error == error_code(errors::timed_out_no_handshake - , get_libtorrent_category())) + || pd->error == errors::timed_out_no_handshake) return true; } diff --git a/include/libtorrent/bdecode.hpp b/include/libtorrent/bdecode.hpp index 704b80d0d..6c24f756d 100644 --- a/include/libtorrent/bdecode.hpp +++ b/include/libtorrent/bdecode.hpp @@ -99,12 +99,18 @@ example layout: namespace libtorrent { -TORRENT_EXPORT boost::system::error_category& get_bdecode_category(); +TORRENT_EXPORT boost::system::error_category& bdecode_category(); + +#ifndef TORRENT_NO_DEPRECATED +TORRENT_DEPRECATED +inline boost::system::error_category& get_bdecode_category() +{ return bdecode_category(); } +#endif namespace bdecode_errors { // libtorrent uses boost.system's ``error_code`` class to represent - // errors. libtorrent has its own error category get_bdecode_category() + // errors. libtorrent has its own error category bdecode_category() // with the error codes defined by error_code_enum. enum error_code_enum { @@ -139,8 +145,6 @@ namespace boost { namespace system { template<> struct is_error_code_enum { static const bool value = true; }; - template<> struct is_error_condition_enum - { static const bool value = true; }; } } namespace libtorrent { diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 2820b33a8..d1f83195a 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -540,13 +540,13 @@ namespace libtorrent disk_io_thread_pool& pool_for_job(disk_io_job* j); // set to true once we start shutting down - std::atomic m_abort; + std::atomic m_abort{false}; // this is a counter of how many threads are currently running. // it's used to identify the last thread still running while // shutting down. This last thread is responsible for cleanup // must hold the job mutex to access - int m_num_running_threads; + int m_num_running_threads = 0; // std::mutex to protect the m_generic_io_jobs and m_hash_io_jobs lists mutable std::mutex m_job_mutex; @@ -566,12 +566,12 @@ namespace libtorrent void* m_userdata; // the last time we expired write blocks from the cache - time_point m_last_cache_expiry; + time_point m_last_cache_expiry = min_time(); time_point m_last_file_check; // LRU cache of open files - file_pool m_file_pool; + file_pool m_file_pool{40}; // disk cache mutable std::mutex m_cache_mutex; @@ -582,7 +582,7 @@ namespace libtorrent cache_check_active, cache_check_reinvoke }; - int m_cache_check_state; + int m_cache_check_state = cache_check_idle; // total number of blocks in use by both the read // and the write cache. This is not supposed to @@ -608,7 +608,7 @@ namespace libtorrent io_service& m_ios; // used to rate limit disk performance warnings - time_point m_last_disk_aio_performance_warning; + time_point m_last_disk_aio_performance_warning = min_time(); // jobs that are completed are put on this queue // whenever the queue size grows from 0 to 1 @@ -626,10 +626,10 @@ namespace libtorrent // when this is true, there is an outstanding message in the // message queue that will reclaim all blocks in // m_blocks_to_reclaim, there's no need to send another one - bool m_outstanding_reclaim_message; + bool m_outstanding_reclaim_message = false; #if TORRENT_USE_ASSERTS - int m_magic; - std::atomic m_jobs_aborted; + int m_magic = 0x1337; + std::atomic m_jobs_aborted{false}; #endif }; } diff --git a/include/libtorrent/error_code.hpp b/include/libtorrent/error_code.hpp index d101a8303..3e467e8d4 100644 --- a/include/libtorrent/error_code.hpp +++ b/include/libtorrent/error_code.hpp @@ -47,7 +47,7 @@ namespace libtorrent { // libtorrent uses boost.system's ``error_code`` class to represent // errors. libtorrent has its own error category - // get_libtorrent_category() with the error codes defined by + // libtorrent_category() with the error codes defined by // error_code_enum. enum error_code_enum { @@ -466,10 +466,10 @@ namespace libtorrent // return the instance of the libtorrent_error_category which // maps libtorrent error codes to human readable error messages. - TORRENT_EXPORT boost::system::error_category& get_libtorrent_category(); + TORRENT_EXPORT boost::system::error_category& libtorrent_category(); // returns the error_category for HTTP errors - TORRENT_EXPORT boost::system::error_category& get_http_category(); + TORRENT_EXPORT boost::system::error_category& http_category(); using boost::system::error_code; using boost::system::error_condition; @@ -483,6 +483,14 @@ namespace libtorrent #ifndef TORRENT_NO_DEPRECATE using system_error = boost::system::system_error; + + TORRENT_DEPRECATED + inline boost::system::error_category& get_libtorrent_category() + { return libtorrent_category(); } + + TORRENT_DEPRECATED + inline boost::system::error_category& get_http_category() + { return http_category(); } #endif #endif @@ -548,14 +556,8 @@ namespace boost { namespace system { template<> struct is_error_code_enum { static const bool value = true; }; - template<> struct is_error_condition_enum - { static const bool value = true; }; - template<> struct is_error_code_enum { static const bool value = true; }; - - template<> struct is_error_condition_enum - { static const bool value = true; }; } } #endif diff --git a/include/libtorrent/gzip.hpp b/include/libtorrent/gzip.hpp index 7df99549b..8596c70d7 100644 --- a/include/libtorrent/gzip.hpp +++ b/include/libtorrent/gzip.hpp @@ -48,7 +48,13 @@ namespace libtorrent , error_code& error); // get the ``error_category`` for zip errors - TORRENT_EXPORT boost::system::error_category& get_gzip_category(); + TORRENT_EXPORT boost::system::error_category& gzip_category(); + +#ifndef TORRENT_NO_DEPRECATE + TORRENT_DEPRECATED + inline boost::system::error_category& get_gzip_category() + { return gzip_category(); } +#endif namespace gzip_errors { diff --git a/include/libtorrent/i2p_stream.hpp b/include/libtorrent/i2p_stream.hpp index 463759a3f..a699ff304 100644 --- a/include/libtorrent/i2p_stream.hpp +++ b/include/libtorrent/i2p_stream.hpp @@ -70,7 +70,13 @@ namespace libtorrent { } // returns the error category for I2P errors - TORRENT_EXPORT boost::system::error_category& get_i2p_category(); + TORRENT_EXPORT boost::system::error_category& i2p_category(); + +#ifndef TORRENT_NO_DEPRECATE + TORRENT_DEPRECATED + inline boost::system::error_category& get_i2p_category() + { return i2p_category(); } +#endif class i2p_stream : public proxy_base { @@ -227,10 +233,6 @@ template<> struct is_error_code_enum { static const bool value = true; }; -template<> -struct is_error_condition_enum -{ static const bool value = true; }; - } } #endif // TORRENT_USE_I2P diff --git a/include/libtorrent/socks5_stream.hpp b/include/libtorrent/socks5_stream.hpp index 489afab21..aca262688 100644 --- a/include/libtorrent/socks5_stream.hpp +++ b/include/libtorrent/socks5_stream.hpp @@ -69,7 +69,13 @@ namespace socks_error { } // namespace socks_error // returns the error_category for SOCKS5 errors -TORRENT_EXPORT boost::system::error_category& get_socks_category(); +TORRENT_EXPORT boost::system::error_category& socks_category(); + +#ifndef TORRENT_NO_DEPRECATE +TORRENT_DEPRECATED +inline boost::system::error_category& get_socks_category() +{ return socks_category(); } +#endif class socks5_stream : public proxy_base { @@ -254,8 +260,6 @@ namespace boost { namespace system { template<> struct is_error_code_enum { static const bool value = true; }; - template<> struct is_error_condition_enum - { static const bool value = true; }; } } #endif diff --git a/include/libtorrent/upnp.hpp b/include/libtorrent/upnp.hpp index c863860fe..7b4559dd9 100644 --- a/include/libtorrent/upnp.hpp +++ b/include/libtorrent/upnp.hpp @@ -91,7 +91,13 @@ namespace libtorrent } // the boost.system error category for UPnP errors - TORRENT_EXPORT boost::system::error_category& get_upnp_category(); + TORRENT_EXPORT boost::system::error_category& upnp_category(); + +#ifndef TORRENT_NO_DEPRECATED + TORRENT_DEPRECATED + inline boost::system::error_category& get_upnp_category() + { return upnp_category(); } +#endif struct parse_state { @@ -193,7 +199,6 @@ private: void next(rootdevice& d, int i); void update_map(rootdevice& d, int i); - void on_upnp_xml(error_code const& e , libtorrent::http_parser const& p, rootdevice& d , http_connection& c); @@ -377,8 +382,6 @@ namespace boost { namespace system { template<> struct is_error_code_enum { static const bool value = true; }; - template<> struct is_error_condition_enum - { static const bool value = true; }; } } #endif diff --git a/simulation/test_tracker.cpp b/simulation/test_tracker.cpp index b44c7fa02..156bc46eb 100644 --- a/simulation/test_tracker.cpp +++ b/simulation/test_tracker.cpp @@ -476,8 +476,7 @@ TORRENT_TEST(test_error) TEST_EQUAL(ae.is_working(), false); TEST_EQUAL(ae.message, "test"); TEST_EQUAL(ae.url, "http://tracker.com:8080/announce"); - TEST_EQUAL(ae.last_error, error_code(errors::tracker_failure - , get_libtorrent_category())); + TEST_EQUAL(ae.last_error, error_code(errors::tracker_failure)); TEST_EQUAL(ae.fails, 1); }); } @@ -574,7 +573,7 @@ TORRENT_TEST(test_http_status) TEST_EQUAL(ae.is_working(), false); TEST_EQUAL(ae.message, "Not A Tracker"); TEST_EQUAL(ae.url, "http://tracker.com:8080/announce"); - TEST_EQUAL(ae.last_error, error_code(410, get_http_category())); + TEST_EQUAL(ae.last_error, error_code(410, http_category())); TEST_EQUAL(ae.fails, 1); }); } @@ -621,7 +620,7 @@ TORRENT_TEST(test_invalid_bencoding) TEST_EQUAL(ae.message, ""); TEST_EQUAL(ae.url, "http://tracker.com:8080/announce"); TEST_EQUAL(ae.last_error, error_code(bdecode_errors::expected_value - , get_bdecode_category())); + , bdecode_category())); TEST_EQUAL(ae.fails, 1); }); } diff --git a/src/bdecode.cpp b/src/bdecode.cpp index 013c3ea62..5b79be6f4 100644 --- a/src/bdecode.cpp +++ b/src/bdecode.cpp @@ -179,7 +179,7 @@ namespace libtorrent return msgs[ev]; } - boost::system::error_category& get_bdecode_category() + boost::system::error_category& bdecode_category() { static bdecode_error_category bdecode_category; return bdecode_category; @@ -189,7 +189,7 @@ namespace libtorrent { boost::system::error_code make_error_code(error_code_enum e) { - return boost::system::error_code(e, get_bdecode_category()); + return boost::system::error_code(e, bdecode_category()); } } @@ -632,7 +632,7 @@ namespace libtorrent } #define TORRENT_FAIL_BDECODE(code) do { \ - ec = make_error_code(code); \ + ec = code; \ if (error_pos) *error_pos = start - orig_start; \ goto done; \ } TORRENT_WHILE_0 @@ -646,7 +646,7 @@ namespace libtorrent if (end - start > bdecode_token::max_offset) { if (error_pos) *error_pos = 0; - ec = make_error_code(bdecode_errors::limit_exceeded); + ec = bdecode_errors::limit_exceeded; return -1; } diff --git a/src/close_reason.cpp b/src/close_reason.cpp index c7e66756a..6331fb340 100644 --- a/src/close_reason.cpp +++ b/src/close_reason.cpp @@ -39,7 +39,7 @@ namespace libtorrent close_reason_t error_to_close_reason(error_code const& ec) { - if (ec.category() == get_libtorrent_category()) + if (ec.category() == libtorrent_category()) { #define TORRENT_MAP(error, close_reason) \ case errors:: error : \ @@ -157,7 +157,7 @@ namespace libtorrent return close_no_memory; } } - else if (ec.category() == get_http_category()) + else if (ec.category() == http_category()) { return close_no_memory; } diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index b46ca2e90..1f94dbc85 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -252,13 +252,13 @@ namespace libtorrent if (t.files().num_files() == 0) { - ec = error_code(errors::no_files_in_torrent, get_libtorrent_category()); + ec = errors::no_files_in_torrent; return; } if (t.files().total_size() == 0) { - ec = error_code(errors::torrent_invalid_length, get_libtorrent_category()); + ec = errors::torrent_invalid_length; return; } diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index d8014a733..d6b9b889f 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -137,27 +137,16 @@ namespace libtorrent disk_io_thread::disk_io_thread(io_service& ios , counters& cnt , void* userdata - , int block_size) - : m_abort(false) - , m_num_running_threads(0) - , m_generic_io_jobs(*this, generic_thread) + , int const block_size) + : m_generic_io_jobs(*this, generic_thread) , m_generic_threads(m_generic_io_jobs, ios) , m_hash_io_jobs(*this, hasher_thread) , m_hash_threads(m_hash_io_jobs, ios) , m_userdata(userdata) - , m_last_cache_expiry(min_time()) , m_last_file_check(clock_type::now()) - , m_file_pool(40) , m_disk_cache(block_size, ios, std::bind(&disk_io_thread::trigger_cache_trim, this)) - , m_cache_check_state(cache_check_idle) , m_stats_counters(cnt) , m_ios(ios) - , m_last_disk_aio_performance_warning(min_time()) - , m_outstanding_reclaim_message(false) -#if TORRENT_USE_ASSERTS - , m_magic(0x1337) - , m_jobs_aborted(false) -#endif { ADD_OUTSTANDING_ASYNC("disk_io_thread::work"); error_code ec; @@ -168,8 +157,8 @@ namespace libtorrent // futexes, shared objects etc. // 80% of the available file descriptors should go to connections // 20% goes towards regular files - const int max_files = (std::min)((std::max)(5 - , (max_open_files() - 20) * 2 / 10) + const int max_files = std::min(std::max(5 + , (max_open_files() - 20) * 2 / 10) , m_file_pool.size_limit()); m_file_pool.resize(max_files); } diff --git a/src/entry.cpp b/src/entry.cpp index 5376d176c..90cb34ce6 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -74,8 +74,7 @@ namespace libtorrent TORRENT_NO_RETURN inline void throw_error() { #ifndef BOOST_NO_EXCEPTIONS - throw system_error(error_code(errors::invalid_entry_type - , get_libtorrent_category())); + throw system_error(errors::invalid_entry_type); #else std::terminate(); #endif diff --git a/src/error_code.cpp b/src/error_code.cpp index 11f96a740..31108fcfa 100644 --- a/src/error_code.cpp +++ b/src/error_code.cpp @@ -270,7 +270,7 @@ namespace libtorrent return msgs[ev]; } - boost::system::error_category& get_libtorrent_category() + boost::system::error_category& libtorrent_category() { static libtorrent_error_category libtorrent_category; return libtorrent_category; @@ -313,7 +313,7 @@ namespace libtorrent { return boost::system::error_condition(ev, *this); } }; - boost::system::error_category& get_http_category() + boost::system::error_category& http_category() { static http_error_category http_category; return http_category; @@ -324,7 +324,7 @@ namespace libtorrent // hidden boost::system::error_code make_error_code(error_code_enum e) { - return boost::system::error_code(e, get_libtorrent_category()); + return boost::system::error_code(e, libtorrent_category()); } } diff --git a/src/file.cpp b/src/file.cpp index 91f7ae70c..6686de4e9 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -532,7 +532,7 @@ namespace libtorrent // something failed. Does the filesystem not support hard links? // TODO: 3 find out what error code is reported when the filesystem // does not support hard links. - DWORD error = GetLastError(); + DWORD const error = GetLastError(); if (error != ERROR_NOT_SUPPORTED && error != ERROR_ACCESS_DENIED) { // it's possible CreateHardLink will copy the file internally too, diff --git a/src/gzip.cpp b/src/gzip.cpp index 20c029605..1da70ee3f 100644 --- a/src/gzip.cpp +++ b/src/gzip.cpp @@ -95,17 +95,17 @@ namespace libtorrent return msgs[ev]; } - boost::system::error_category& get_gzip_category() + boost::system::error_category& gzip_category() { - static gzip_error_category gzip_category; - return gzip_category; + static gzip_error_category category; + return category; } namespace gzip_errors { boost::system::error_code make_error_code(error_code_enum e) { - return boost::system::error_code(e, get_gzip_category()); + return boost::system::error_code(e, gzip_category()); } } diff --git a/src/http_connection.cpp b/src/http_connection.cpp index 69c95d80c..16fc9735d 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -298,7 +298,7 @@ void http_connection::start(std::string const& hostname, int port if (is_i2p && i2p_conn->proxy().type != settings_pack::i2p_proxy) { m_timer.get_io_service().post(std::bind(&http_connection::callback - , me, error_code(errors::no_i2p_router, get_libtorrent_category()), static_cast(nullptr), 0)); + , me, error_code(errors::no_i2p_router), static_cast(nullptr), 0)); return; } #endif diff --git a/src/http_seed_connection.cpp b/src/http_seed_connection.cpp index 6923dbf27..7fc79fd1a 100644 --- a/src/http_seed_connection.cpp +++ b/src/http_seed_connection.cpp @@ -279,7 +279,7 @@ namespace libtorrent , error_msg); } received_bytes(0, int(bytes_transferred)); - disconnect(error_code(m_parser.status_code(), get_http_category()), op_bittorrent, 1); + disconnect(error_code(m_parser.status_code(), http_category()), op_bittorrent, 1); return; } if (!m_parser.header_finished()) @@ -414,7 +414,7 @@ namespace libtorrent received_bytes(0, int(bytes_transferred)); // temporarily unavailable, retry later t->retry_web_seed(this, retry_time); - disconnect(error_code(m_parser.status_code(), get_http_category()), op_bittorrent, 1); + disconnect(error_code(m_parser.status_code(), http_category()), op_bittorrent, 1); return; } diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index db5d992f7..a869a9426 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -85,7 +85,7 @@ namespace libtorrent std::size_t pos = url.find("announce"); if (pos == std::string::npos) { - tracker_connection::fail(error_code(errors::scrape_not_available)); + tracker_connection::fail(errors::scrape_not_available); return; } url.replace(pos, 8, "scrape"); @@ -162,7 +162,7 @@ namespace libtorrent { if (tracker_req().i2pconn->local_endpoint().empty()) { - fail(error_code(errors::no_i2p_endpoint), -1, "Waiting for i2p acceptor from SAM bridge", 5); + fail(errors::no_i2p_endpoint, -1, "Waiting for i2p acceptor from SAM bridge", 5); return; } else @@ -281,7 +281,7 @@ namespace libtorrent } #endif if (endpoints.empty()) - fail(error_code(errors::banned_by_ip_filter)); + fail(errors::banned_by_ip_filter); } void http_tracker_connection::on_connect(http_connection& c) @@ -312,7 +312,7 @@ namespace libtorrent if (parser.status_code() != 200) { - fail(error_code(parser.status_code(), get_http_category()) + fail(error_code(parser.status_code(), http_category()) , parser.status_code(), parser.message().c_str()); return; } @@ -378,7 +378,7 @@ namespace libtorrent // extract peer id (if any) if (info.type() != bdecode_node::dict_t) { - ec.assign(errors::invalid_peer_dict, get_libtorrent_category()); + ec = errors::invalid_peer_dict; return false; } bdecode_node i = info.dict_find_string("peer id"); @@ -396,7 +396,7 @@ namespace libtorrent i = info.dict_find_string("ip"); if (i == 0) { - ec.assign(errors::invalid_tracker_response, get_libtorrent_category()); + ec = errors::invalid_tracker_response; return false; } ret.hostname = i.string_value().to_string(); @@ -405,7 +405,7 @@ namespace libtorrent i = info.dict_find_int("port"); if (i == 0) { - ec.assign(errors::invalid_tracker_response, get_libtorrent_category()); + ec = errors::invalid_tracker_response; return false; } ret.port = std::uint16_t(i.int_value()); @@ -425,7 +425,7 @@ namespace libtorrent if (res != 0 || e.type() != bdecode_node::dict_t) { - ec.assign(errors::invalid_tracker_response, get_libtorrent_category()); + ec = errors::invalid_tracker_response; return resp; } @@ -446,7 +446,7 @@ namespace libtorrent if (failure) { resp.failure_reason = failure.string_value().to_string(); - ec.assign(errors::tracker_failure, get_libtorrent_category()); + ec = errors::tracker_failure; return resp; } @@ -459,7 +459,7 @@ namespace libtorrent bdecode_node files = e.dict_find_dict("files"); if (!files) { - ec.assign(errors::invalid_files_entry, get_libtorrent_category()); + ec = errors::invalid_files_entry; return resp; } @@ -468,7 +468,7 @@ namespace libtorrent if (!scrape_data) { - ec.assign(errors::invalid_hash_entry, get_libtorrent_category()); + ec = errors::invalid_hash_entry; return resp; } @@ -573,7 +573,7 @@ namespace libtorrent if (peers_ent == 0 && ipv6_peers == 0 && tracker_req().event != tracker_request::stopped) { - ec.assign(errors::invalid_peers_entry, get_libtorrent_category()); + ec = errors::invalid_peers_entry; return resp; } */ diff --git a/src/i2p_stream.cpp b/src/i2p_stream.cpp index eaaf9cc8d..11c66bd53 100644 --- a/src/i2p_stream.cpp +++ b/src/i2p_stream.cpp @@ -78,7 +78,7 @@ namespace libtorrent }; - boost::system::error_category& get_i2p_category() + boost::system::error_category& i2p_category() { static i2p_error_category i2p_category; return i2p_category; @@ -88,7 +88,7 @@ namespace libtorrent { boost::system::error_code make_error_code(i2p_error_code e) { - return error_code(e, get_i2p_category()); + return error_code(e, i2p_category()); } } @@ -307,7 +307,7 @@ namespace libtorrent } error_code invalid_response(i2p_error::parse_failed - , get_i2p_category()); + , i2p_category()); // 0-terminate the string and parse it m_buffer.push_back(0); @@ -391,7 +391,7 @@ namespace libtorrent } } - error_code ec(result, get_i2p_category()); + error_code ec(result, i2p_category()); switch (result) { case i2p_error::no_error: diff --git a/src/natpmp.cpp b/src/natpmp.cpp index c5ec29253..27c101486 100644 --- a/src/natpmp.cpp +++ b/src/natpmp.cpp @@ -563,7 +563,8 @@ void natpmp::on_reply(error_code const& e if (result != 0) { - int errors[] = + // TODO: 3 it would be nice to have a separate NAT-PMP error category + errors::error_code_enum errors[] = { errors::unsupported_protocol_version, errors::natpmp_not_authorized, @@ -571,21 +572,19 @@ void natpmp::on_reply(error_code const& e errors::no_resources, errors::unsupported_opcode, }; - int ev = errors::no_error; + errors::error_code_enum ev = errors::no_error; if (result >= 1 && result <= 5) ev = errors[result - 1]; m->expires = aux::time_now() + hours(2); portmap_protocol const proto = m->protocol; m_callback.on_port_mapping(index, address(), 0, proto - , error_code(ev, get_libtorrent_category()) - , aux::portmap_transport::natpmp); + , ev, aux::portmap_transport::natpmp); } else if (m->act == mapping_t::action::add) { portmap_protocol const proto = m->protocol; m_callback.on_port_mapping(index, m_external_ip, m->external_port, proto - , error_code(errors::no_error, get_libtorrent_category()) - , aux::portmap_transport::natpmp); + , errors::no_error, aux::portmap_transport::natpmp); } if (m_abort) return; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 450e75d90..e29078c6a 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -2124,7 +2124,7 @@ namespace libtorrent if (t->share_mode()) return false; if (m_upload_only && t->is_upload_only() - && can_disconnect(error_code(errors::upload_upload_connection, get_libtorrent_category()))) + && can_disconnect(errors::upload_upload_connection)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "UPLOAD_ONLY", "the peer is upload-only and our torrent is also upload-only"); @@ -2137,7 +2137,7 @@ namespace libtorrent && !m_interesting && m_bitfield_received && t->are_files_checked() - && can_disconnect(error_code(errors::uninteresting_upload_peer, get_libtorrent_category()))) + && can_disconnect(errors::uninteresting_upload_peer)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "UPLOAD_ONLY", "the peer is upload-only and we're not interested in it"); @@ -2352,8 +2352,7 @@ namespace libtorrent // may be a legitimate number of requests to have in flight when // getting choked if (m_num_invalid_requests > 300 && !m_peer_choked - && can_disconnect(error_code(errors::too_many_requests_when_choked - , get_libtorrent_category()))) + && can_disconnect(errors::too_many_requests_when_choked)) { disconnect(errors::too_many_requests_when_choked, op_bittorrent, 2); return; @@ -2375,7 +2374,7 @@ namespace libtorrent // disconnect peers that downloads more than foo times an allowed // fast piece if (m_choked && fast_idx != -1 && m_accept_fast_piece_cnt[fast_idx] >= 3 * blocks_per_piece - && can_disconnect(error_code(errors::too_many_requests_when_choked, get_libtorrent_category()))) + && can_disconnect(errors::too_many_requests_when_choked)) { disconnect(errors::too_many_requests_when_choked, op_bittorrent, 2); return; @@ -2394,7 +2393,7 @@ namespace libtorrent // allow peers to send request up to 2 seconds after getting choked, // then disconnect them if (aux::time_now() - seconds(2) > m_last_choke - && can_disconnect(error_code(errors::too_many_requests_when_choked, get_libtorrent_category()))) + && can_disconnect(errors::too_many_requests_when_choked)) { disconnect(errors::too_many_requests_when_choked, op_bittorrent, 2); return; @@ -4079,8 +4078,7 @@ namespace libtorrent break; } - if (ec == error_code(boost::asio::error::eof - , boost::asio::error::get_misc_category()) + if (ec == boost::asio::error::eof && !in_handshake() && !is_connecting() && aux::time_now() - connected_time() < seconds(15)) @@ -4144,29 +4142,29 @@ namespace libtorrent m_counters.inc_stats_counter(counters::invalid_arg_peers); else if (ec == error::operation_aborted) m_counters.inc_stats_counter(counters::aborted_peers); - else if (ec == error_code(errors::upload_upload_connection) - || ec == error_code(errors::uninteresting_upload_peer) - || ec == error_code(errors::torrent_aborted) - || ec == error_code(errors::self_connection) - || ec == error_code(errors::torrent_paused)) + else if (ec == errors::upload_upload_connection + || ec == errors::uninteresting_upload_peer + || ec == errors::torrent_aborted + || ec == errors::self_connection + || ec == errors::torrent_paused) m_counters.inc_stats_counter(counters::uninteresting_peers); - if (ec == error_code(errors::timed_out) + if (ec == errors::timed_out || ec == error::timed_out) m_counters.inc_stats_counter(counters::transport_timeout_peers); - if (ec == error_code(errors::timed_out_inactivity) - || ec == error_code(errors::timed_out_no_request) - || ec == error_code(errors::timed_out_no_interest)) + if (ec == errors::timed_out_inactivity + || ec == errors::timed_out_no_request + || ec == errors::timed_out_no_interest) m_counters.inc_stats_counter(counters::timeout_peers); - if (ec == error_code(errors::no_memory)) + if (ec == errors::no_memory) m_counters.inc_stats_counter(counters::no_memory_peers); - if (ec == error_code(errors::too_many_connections)) + if (ec == errors::too_many_connections) m_counters.inc_stats_counter(counters::too_many_peers); - if (ec == error_code(errors::timed_out_no_handshake)) + if (ec == errors::timed_out_no_handshake) m_counters.inc_stats_counter(counters::connect_timeouts); if (error > 0) @@ -4235,7 +4233,7 @@ namespace libtorrent { if (ec) { - if ((error > 1 || ec.category() == get_socks_category()) + if ((error > 1 || ec.category() == socks_category()) && t->alerts().should_post()) { t->alerts().emplace_alert(handle, remote() @@ -4715,7 +4713,7 @@ namespace libtorrent #endif if (d > seconds(connect_timeout) - && can_disconnect(error_code(errors::timed_out, get_libtorrent_category()))) + && can_disconnect(errors::timed_out)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "CONNECT_FAILED", "waited %d seconds" @@ -4736,7 +4734,7 @@ namespace libtorrent // because of less work in second_tick(), and might let use remove ticking // entirely eventually if (may_timeout && d > seconds(timeout()) && !m_connecting && m_reading_bytes == 0 - && can_disconnect(error_code(errors::timed_out_inactivity, get_libtorrent_category()))) + && can_disconnect(errors::timed_out_inactivity)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "LAST_ACTIVITY", "%d seconds ago" @@ -4779,7 +4777,7 @@ namespace libtorrent && m_peer_interested && t && t->is_upload_only() && d > seconds(60) - && can_disconnect(error_code(errors::timed_out_no_request, get_libtorrent_category()))) + && can_disconnect(errors::timed_out_no_request)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "NO_REQUEST", "waited %d seconds" @@ -4809,7 +4807,7 @@ namespace libtorrent && d2 > time_limit && (m_ses.num_connections() >= m_settings.get_int(settings_pack::connections_limit) || (t && t->num_peers() >= t->max_connections())) - && can_disconnect(error_code(errors::timed_out_no_interest))) + && can_disconnect(errors::timed_out_no_interest)) { #ifndef TORRENT_DISABLE_LOGGING if (should_log(peer_log_alert::info)) @@ -6402,12 +6400,12 @@ namespace libtorrent && !t->share_mode()) { bool const ok_to_disconnect = - can_disconnect(error_code(errors::upload_upload_connection)) - || can_disconnect(error_code(errors::uninteresting_upload_peer)) - || can_disconnect(error_code(errors::too_many_requests_when_choked)) - || can_disconnect(error_code(errors::timed_out_no_interest)) - || can_disconnect(error_code(errors::timed_out_no_request)) - || can_disconnect(error_code(errors::timed_out_inactivity)); + can_disconnect(errors::upload_upload_connection) + || can_disconnect(errors::uninteresting_upload_peer) + || can_disconnect(errors::too_many_requests_when_choked) + || can_disconnect(errors::timed_out_no_interest) + || can_disconnect(errors::timed_out_no_request) + || can_disconnect(errors::timed_out_inactivity); // make sure upload only peers are disconnected if (t->is_upload_only() diff --git a/src/read_resume_data.cpp b/src/read_resume_data.cpp index dc23d707f..f2c233bcf 100644 --- a/src/read_resume_data.cpp +++ b/src/read_resume_data.cpp @@ -72,14 +72,14 @@ namespace libtorrent if (rd.dict_find_string_value("file-format") != "libtorrent resume file") { - ec = error_code(errors::invalid_file_tag, get_libtorrent_category()); + ec = errors::invalid_file_tag; return ret; } auto info_hash = rd.dict_find_string_value("info-hash"); if (info_hash.size() != 20) { - ec = error_code(errors::missing_info_hash, get_libtorrent_category()); + ec = errors::missing_info_hash; return ret; } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index ffc240b3c..bb97b357f 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2821,7 +2821,7 @@ namespace aux { { m_alerts.emplace_alert(torrent_handle(), endp, peer_id() , op_bittorrent, s->type() - , error_code(errors::too_many_connections, get_libtorrent_category()) + , error_code(errors::too_many_connections) , close_no_reason); } #ifndef TORRENT_DISABLE_LOGGING @@ -3419,7 +3419,7 @@ namespace aux { int(i->second->num_peers() * m_settings.get_int(settings_pack::peer_turnover) / 100), 1) , i->second->num_connect_candidates()); i->second->disconnect_peers(peers_to_disconnect - , error_code(errors::optimistic_disconnect, get_libtorrent_category())); + , error_code(errors::optimistic_disconnect)); } else { @@ -3441,7 +3441,7 @@ namespace aux { * m_settings.get_int(settings_pack::peer_turnover) / 100), 1) , t->num_connect_candidates()); t->disconnect_peers(peers_to_disconnect - , error_code(errors::optimistic_disconnect, get_libtorrent_category())); + , error_code(errors::optimistic_disconnect)); } } } @@ -6402,7 +6402,7 @@ namespace aux { int const disconnect = std::min(to_disconnect, num - my_average); to_disconnect -= disconnect; t.second->disconnect_peers(disconnect - , error_code(errors::too_many_connections, get_libtorrent_category())); + , error_code(errors::too_many_connections)); } } } diff --git a/src/socks5_stream.cpp b/src/socks5_stream.cpp index d71c4fc2f..9df62a2cc 100644 --- a/src/socks5_stream.cpp +++ b/src/socks5_stream.cpp @@ -42,7 +42,7 @@ namespace libtorrent { boost::system::error_code make_error_code(socks_error_code e) { - return error_code(e, get_socks_category()); + return error_code(e, socks_category()); } } @@ -74,10 +74,10 @@ namespace libtorrent { return boost::system::error_condition(ev, *this); } }; - TORRENT_EXPORT boost::system::error_category& get_socks_category() + TORRENT_EXPORT boost::system::error_category& socks_category() { - static socks_error_category socks_category; - return socks_category; + static socks_error_category cat; + return cat; } namespace @@ -386,7 +386,7 @@ namespace libtorrent } if (response != 0) { - error_code ec(socks_error::general_failure, get_socks_category()); + error_code ec(socks_error::general_failure); switch (response) { case 2: ec = boost::asio::error::no_permission; break; @@ -483,7 +483,7 @@ namespace libtorrent return; } - error_code ec(socks_error::general_failure, get_socks_category()); + error_code ec(socks_error::general_failure); switch (response) { case 91: ec = boost::asio::error::connection_refused; break; diff --git a/src/torrent.cpp b/src/torrent.cpp index a1b014f51..70d02f210 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -421,7 +421,7 @@ namespace libtorrent if (parser.status_code() != 200) { - set_error(error_code(parser.status_code(), get_http_category()), torrent_status::error_file_url); + set_error(error_code(parser.status_code(), http_category()), torrent_status::error_file_url); pause(); return; } @@ -468,7 +468,7 @@ namespace libtorrent // TODO: if the existing torrent doesn't have metadata, insert // the metadata we just downloaded into it. - set_error(error_code(errors::duplicate_torrent, get_libtorrent_category()), torrent_status::error_file_url); + set_error(errors::duplicate_torrent, torrent_status::error_file_url); abort(); return; } @@ -5638,7 +5638,7 @@ namespace libtorrent { if (alerts().should_post()) alerts().emplace_alert(get_handle() - , error_code(errors::not_an_ssl_torrent), ""); + , errors::not_an_ssl_torrent, ""); return; } @@ -5884,7 +5884,7 @@ namespace libtorrent if (m_ses.alerts().should_post()) { m_ses.alerts().emplace_alert(get_handle(), web->url - , error_code(libtorrent::errors::peer_banned, get_libtorrent_category())); + , libtorrent::errors::peer_banned); } // never try it again remove_web_seed_iter(web); @@ -6836,8 +6836,7 @@ namespace libtorrent // we have an i2p torrent, but we're not connected to an i2p // SAM proxy. if (alerts().should_post()) - alerts().emplace_alert(error_code(errors::no_i2p_router - , get_libtorrent_category())); + alerts().emplace_alert(errors::no_i2p_router); return false; } @@ -7003,7 +7002,7 @@ namespace libtorrent if (alerts().should_post()) { alerts().emplace_alert(get_handle() - , error_code(errors::mismatching_info_hash, get_libtorrent_category())); + , errors::mismatching_info_hash); } return false; } @@ -7989,8 +7988,7 @@ namespace libtorrent { if (alerts().should_post()) alerts().emplace_alert(get_handle() - , index, error_code(errors::session_is_closing - , get_libtorrent_category())); + , index, errors::session_is_closing); return; } @@ -8414,7 +8412,7 @@ namespace libtorrent if (num_peers() > int(m_max_connections)) { disconnect_peers(num_peers() - m_max_connections - , error_code(errors::too_many_connections, get_libtorrent_category())); + , errors::too_many_connections); } if (state_update) diff --git a/src/upnp.cpp b/src/upnp.cpp index f61e2d08d..220edd3ca 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -62,7 +62,7 @@ namespace upnp_errors { boost::system::error_code make_error_code(error_code_enum e) { - return error_code(e, get_upnp_category()); + return error_code(e, upnp_category()); } } // upnp_errors namespace @@ -1221,7 +1221,7 @@ struct upnp_error_category : boost::system::error_category } }; -boost::system::error_category& get_upnp_category() +boost::system::error_category& upnp_category() { static upnp_error_category cat; return cat; @@ -1493,7 +1493,7 @@ void upnp::return_error(int mapping, int code) error_string += e->msg; } portmap_protocol const proto = m_mappings[mapping].protocol; - m_callback.on_port_mapping(mapping, address(), 0, proto, error_code(code, get_upnp_category()) + m_callback.on_port_mapping(mapping, address(), 0, proto, error_code(code, upnp_category()) , aux::portmap_transport::upnp); } @@ -1558,8 +1558,8 @@ void upnp::on_upnp_unmap_response(error_code const& e portmap_protocol const proto = m_mappings[mapping].protocol; m_callback.on_port_mapping(mapping, address(), 0, proto, p.status_code() != 200 - ? error_code(p.status_code(), get_http_category()) - : error_code(s.error_code, get_upnp_category()) + ? error_code(p.status_code(), http_category()) + : error_code(s.error_code, upnp_category()) , aux::portmap_transport::upnp); d.mapping[mapping].protocol = portmap_protocol::none; diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index dc2dc96a5..517d8e3f7 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -557,7 +557,7 @@ void web_peer_connection::handle_error(int bytes_left) , error_msg); } received_bytes(0, bytes_left); - disconnect(error_code(m_parser.status_code(), get_http_category()), op_bittorrent, 1); + disconnect(error_code(m_parser.status_code(), http_category()), op_bittorrent, 1); return; } diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index da9fe998a..8238ed40d 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -83,8 +83,8 @@ TORRENT_TEST(primitives) TEST_CHECK(error_code(errors::http_parse_error).message() == "Invalid HTTP header"); TEST_CHECK(error_code(errors::error_code_max).message() == "Unknown error"); - TEST_CHECK(error_code(errors::unauthorized, get_http_category()).message() == "401 Unauthorized"); - TEST_CHECK(error_code(errors::service_unavailable, get_http_category()).message() == "503 Service Unavailable"); + TEST_CHECK(error_code(errors::unauthorized, http_category()).message() == "401 Unauthorized"); + TEST_CHECK(error_code(errors::service_unavailable, http_category()).message() == "503 Service Unavailable"); // test snprintf @@ -128,11 +128,11 @@ TORRENT_TEST(primitives) #if TORRENT_USE_IPV6 TEST_EQUAL(print_address(v6("2001:ff::1")), "2001:ff::1"); parse_endpoint("[ff::1]", ec); - TEST_EQUAL(ec, error_code(errors::invalid_port, get_libtorrent_category())); + TEST_EQUAL(ec, error_code(errors::invalid_port)); #endif parse_endpoint("[ff::1:5", ec); - TEST_EQUAL(ec, error_code(errors::expected_close_bracket_in_address, get_libtorrent_category())); + TEST_EQUAL(ec, error_code(errors::expected_close_bracket_in_address)); // test address_to_bytes TEST_EQUAL(address_to_bytes(address_v4::from_string("10.11.12.13")), "\x0a\x0b\x0c\x0d"); @@ -166,3 +166,14 @@ TORRENT_TEST(printf_trunc) std::snprintf(buffer, sizeof(buffer), "%d %s", val, "end"); TEST_EQUAL(buffer, std::string("184")) } + +TORRENT_TEST(error_condition) +{ +#ifdef TORRENT_WINDOWS + error_code ec(ERROR_FILE_NOT_FOUND, system_category()); +#else + error_code ec(ENOENT, system_category()); +#endif + TEST_CHECK(ec == boost::system::errc::no_such_file_or_directory); +} + diff --git a/test/test_read_resume.cpp b/test/test_read_resume.cpp index 34aa38c1f..0339e021a 100644 --- a/test/test_read_resume.cpp +++ b/test/test_read_resume.cpp @@ -124,7 +124,7 @@ TORRENT_TEST(read_resume_missing_info_hash) error_code ec; add_torrent_params atp = read_resume_data(&resume_data[0], int(resume_data.size()), ec); - TEST_EQUAL(ec, error_code(errors::missing_info_hash, get_libtorrent_category())); + TEST_EQUAL(ec, error_code(errors::missing_info_hash)); } TORRENT_TEST(read_resume_missing_file_format) @@ -140,7 +140,7 @@ TORRENT_TEST(read_resume_missing_file_format) error_code ec; add_torrent_params atp = read_resume_data(&resume_data[0], int(resume_data.size()), ec); - TEST_EQUAL(ec, error_code(errors::invalid_file_tag, get_libtorrent_category())); + TEST_EQUAL(ec, error_code(errors::invalid_file_tag)); } TORRENT_TEST(read_resume_mismatching_torrent) diff --git a/test/test_tracker.cpp b/test/test_tracker.cpp index d0bdafa62..70034d82f 100644 --- a/test/test_tracker.cpp +++ b/test/test_tracker.cpp @@ -201,7 +201,7 @@ TORRENT_TEST(parse_failure_reason) tracker_response resp = parse_tracker_response(response, sizeof(response) - 1 , ec, false, sha1_hash()); - TEST_EQUAL(ec, error_code(errors::tracker_failure)); + TEST_EQUAL(ec, errors::tracker_failure); TEST_EQUAL(resp.peers.size(), 0); TEST_EQUAL(resp.failure_reason, "test message"); } @@ -296,21 +296,21 @@ TORRENT_TEST(extract_peer_not_a_dictionary) { // not a dictionary peer_entry result = extract_peer("2:ip11:example.com" - , error_code(errors::invalid_peer_dict, get_libtorrent_category()), false); + , errors::invalid_peer_dict, false); } TORRENT_TEST(extract_peer_missing_ip) { // missing IP peer_entry result = extract_peer("d7:peer id20:abababababababababab4:porti1337ee" - , error_code(errors::invalid_tracker_response, get_libtorrent_category()), false); + , errors::invalid_tracker_response, false); } TORRENT_TEST(extract_peer_missing_port) { // missing port peer_entry result = extract_peer("d7:peer id20:abababababababababab2:ip4:abcde" - , error_code(errors::invalid_tracker_response, get_libtorrent_category()), false); + , errors::invalid_tracker_response, false); } TORRENT_TEST(udp_tracker)