back-port error_code cleanup from master (#1173)

back-port error_code cleanup from master
This commit is contained in:
Arvid Norberg 2016-10-03 16:32:40 -07:00 committed by GitHub
parent ab846d2d31
commit 7a52a285a1
32 changed files with 207 additions and 169 deletions

View File

@ -73,7 +73,7 @@ install:
# shipping with the system having marked all functions as deprecated. Since
# we're building with -Werror, we can't have those warnings
- 'echo "using darwin : cpp11 : ccache clang++ : <cflags>-std=c11 <cxxflags>-std=c++11 <compileflags>-Wno-deprecated-declarations ;" >> ~/user-config.jam'
- 'echo "using darwin : cpp98 : ccache clang++ : <cflags>-std=c99 <cxxflags>-std=c++98 <compileflags>-Wno-deprecated-declarations ;" >> ~/user-config.jam'
- 'echo "using darwin : cpp98 : ccache clang++ : <warnings>off <cflags>-std=c99 <cxxflags>-std=c++98 <compileflags>-Wno-deprecated-declarations ;" >> ~/user-config.jam'
- 'echo "using python : 2.7 ;" >> ~/user-config.jam'
- ccache -V && ccache --show-stats && ccache --zero-stats
- if [[ $docs == "1" && $TRAVIS_OS_NAME == "osx" ]]; then rst2html.py --version; fi

View File

@ -274,7 +274,6 @@ rule warnings ( properties * )
{
# disable warning C4503: decorated name length exceeded, name was truncated
result += <cflags>/wd4503 ;
result += <warnings>all ;
# enable these warnings again, once the other ones are dealt with

View File

@ -60,30 +60,30 @@ void bind_error_code()
.def("assign", &error_code::assign)
;
def("get_libtorrent_category", &get_libtorrent_category
, return_internal_reference<>());
def("get_upnp_category", &get_upnp_category
, return_internal_reference<>());
def("get_http_category", &get_http_category
, return_internal_reference<>());
def("get_socks_category", &get_socks_category
, return_internal_reference<>());
typedef return_value_policy<reference_existing_object> return_existing;
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_internal_reference<>());
def("i2p_category", &i2p_category, return_existing());
#endif
def("get_bdecode_category", &get_bdecode_category
, return_internal_reference<>());
#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_internal_reference<>());
def("generic_category", &boost::system::generic_category, return_existing());
def("system_category", &boost::system::system_category
, return_internal_reference<>());
def("system_category", &boost::system::system_category, return_existing());
}

View File

@ -119,7 +119,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.
@ -133,7 +133,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();
}

View File

@ -1001,8 +1001,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;
}

View File

@ -98,12 +98,17 @@ 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 TORRENT_EXPORT
boost::system::error_category& get_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
{
@ -138,8 +143,6 @@ namespace boost { namespace system {
template<> struct is_error_code_enum<libtorrent::bdecode_errors::error_code_enum>
{ static const bool value = true; };
template<> struct is_error_condition_enum<libtorrent::bdecode_errors::error_code_enum>
{ static const bool value = true; };
} }
namespace libtorrent {

View File

@ -323,8 +323,7 @@ namespace libtorrent
// internal
TORRENT_NO_RETURN inline void throw_type_error()
{
throw libtorrent_exception(error_code(errors::invalid_entry_type
, get_libtorrent_category()));
throw libtorrent_exception(errors::invalid_entry_type);
}
#endif

View File

@ -61,7 +61,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
{
@ -480,10 +480,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;
@ -492,6 +492,13 @@ namespace libtorrent
using boost::system::generic_category;
using boost::system::system_category;
#ifndef TORRENT_NO_DEPRECATE
TORRENT_DEPRECATED TORRENT_EXPORT
boost::system::error_category& get_libtorrent_category();
TORRENT_DEPRECATED TORRENT_EXPORT
boost::system::error_category& get_http_category();
#endif
#ifndef BOOST_NO_EXCEPTIONS
struct TORRENT_EXPORT libtorrent_exception: std::exception
{
@ -571,14 +578,8 @@ namespace boost { namespace system {
template<> struct is_error_code_enum<libtorrent::errors::error_code_enum>
{ static const bool value = true; };
template<> struct is_error_condition_enum<libtorrent::errors::error_code_enum>
{ static const bool value = true; };
template<> struct is_error_code_enum<libtorrent::errors::http_errors>
{ static const bool value = true; };
template<> struct is_error_condition_enum<libtorrent::errors::http_errors>
{ static const bool value = true; };
} }
#endif

View File

@ -48,7 +48,12 @@ 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 TORRENT_EXPORT
boost::system::error_category& get_gzip_category();
#endif
namespace gzip_errors
{

View File

@ -77,7 +77,12 @@ 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 TORRENT_EXPORT
boost::system::error_category& get_i2p_category();
#endif
class i2p_stream : public proxy_base
{
@ -232,10 +237,6 @@ template<>
struct is_error_code_enum<libtorrent::i2p_error::i2p_error_code>
{ static const bool value = true; };
template<>
struct is_error_condition_enum<libtorrent::i2p_error::i2p_error_code>
{ static const bool value = true; };
} }
#endif // TORRENT_USE_I2P

View File

@ -76,7 +76,12 @@ 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 TORRENT_EXPORT
boost::system::error_category& get_socks_category();
#endif
class socks5_stream : public proxy_base
{
@ -259,8 +264,6 @@ namespace boost { namespace system {
template<> struct is_error_code_enum<libtorrent::socks_error::socks_error_code>
{ static const bool value = true; };
template<> struct is_error_condition_enum<libtorrent::socks_error::socks_error_code>
{ static const bool value = true; };
} }
#endif

View File

@ -93,7 +93,12 @@ 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 TORRENT_EXPORT
boost::system::error_category& get_upnp_category();
#endif
// int: port-mapping index
// address: external address as queried from router
@ -204,7 +209,6 @@ private:
void next(rootdevice& d, int i, mutex::scoped_lock& l);
void update_map(rootdevice& d, int i, mutex::scoped_lock& l);
void on_upnp_xml(error_code const& e
, libtorrent::http_parser const& p, rootdevice& d
, http_connection& c);
@ -412,8 +416,6 @@ namespace boost { namespace system {
template<> struct is_error_code_enum<libtorrent::upnp_errors::error_code_enum>
{ static const bool value = true; };
template<> struct is_error_condition_enum<libtorrent::upnp_errors::error_code_enum>
{ static const bool value = true; };
} }
#endif

View File

@ -474,8 +474,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);
});
}
@ -572,7 +571,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);
});
}
@ -619,7 +618,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);
});
}

View File

@ -193,17 +193,22 @@ 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;
}
#ifndef TORRENT_NO_DEPRECATED
boost::system::error_category& get_bdecode_category()
{ return bdecode_category(); }
#endif
namespace bdecode_errors
{
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());
}
}
@ -689,7 +694,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
@ -703,7 +708,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;
}

View File

@ -47,7 +47,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 : \
@ -165,7 +165,7 @@ namespace libtorrent
return close_no_memory;
}
}
else if (ec.category() == get_http_category())
else if (ec.category() == http_category())
{
return close_no_memory;
}

View File

@ -259,13 +259,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;
}

View File

@ -273,12 +273,20 @@ 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;
}
#ifndef TORRENT_NO_DEPRECATE
boost::system::error_category& get_libtorrent_category()
{ return libtorrent_category(); }
boost::system::error_category& get_http_category()
{ return http_category(); }
#endif
struct TORRENT_EXPORT http_error_category : boost::system::error_category
{
virtual const char* name() const BOOST_SYSTEM_NOEXCEPT
@ -316,7 +324,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;
@ -345,7 +353,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());
}
}

View File

@ -537,7 +537,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,

View File

@ -95,17 +95,22 @@ 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;
}
#ifndef TORRENT_NO_DEPRECATE
boost::system::error_category& get_gzip_category()
{ return gzip_category(); }
#endif
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());
}
}

View File

@ -306,7 +306,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(boost::bind(&http_connection::callback
, me, error_code(errors::no_i2p_router, get_libtorrent_category()), static_cast<char*>(NULL), 0));
, me, error_code(errors::no_i2p_router), static_cast<char*>(NULL), 0));
return;
}
#endif

View File

@ -294,7 +294,7 @@ namespace libtorrent
, error_msg);
}
received_bytes(0, 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())
@ -430,7 +430,7 @@ namespace libtorrent
received_bytes(0, 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;
}

View File

@ -87,7 +87,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");
@ -166,7 +166,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
@ -285,7 +285,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)
@ -316,7 +316,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;
}
@ -385,7 +385,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");
@ -403,7 +403,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();
@ -412,7 +412,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 = boost::uint16_t(i.int_value());
@ -432,7 +432,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;
}
@ -453,7 +453,7 @@ namespace libtorrent
if (failure)
{
resp.failure_reason = failure.string_value();
ec.assign(errors::tracker_failure, get_libtorrent_category());
ec = errors::tracker_failure;
return resp;
}
@ -466,7 +466,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;
}
@ -475,7 +475,7 @@ namespace libtorrent
if (!scrape_data)
{
ec.assign(errors::invalid_hash_entry, get_libtorrent_category());
ec = errors::invalid_hash_entry;
return resp;
}
@ -580,7 +580,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;
}
*/

View File

@ -77,17 +77,22 @@ namespace libtorrent
};
boost::system::error_category& get_i2p_category()
boost::system::error_category& i2p_category()
{
static i2p_error_category i2p_category;
return i2p_category;
}
#ifndef TORRENT_NO_DEPRECATE
boost::system::error_category& get_i2p_category()
{ return i2p_category(); }
#endif
namespace i2p_error
{
boost::system::error_code make_error_code(i2p_error_code e)
{
return error_code(e, get_i2p_category());
return error_code(e, i2p_category());
}
}
@ -325,7 +330,7 @@ namespace libtorrent
}
error_code invalid_response(i2p_error::parse_failed
, get_i2p_category());
, i2p_category());
// null-terminate the string and parse it
m_buffer.push_back(0);
@ -414,7 +419,7 @@ namespace libtorrent
}
}
error_code ec(result, get_i2p_category());
error_code ec(result, i2p_category());
switch (result)
{
case i2p_error::no_error:

View File

@ -554,7 +554,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,
@ -562,14 +563,13 @@ 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);
int const proto = m->protocol;
l.unlock();
m_callback(index, address(), 0, proto
, error_code(ev, get_libtorrent_category()));
m_callback(index, address(), 0, proto, ev);
l.lock();
}
else if (m->action == mapping_t::action_add)
@ -577,7 +577,7 @@ void natpmp::on_reply(error_code const& e
int const proto = m->protocol;
l.unlock();
m_callback(index, m_external_ip, m->external_port, proto
, error_code(errors::no_error, get_libtorrent_category()));
, errors::no_error);
l.lock();
}

View File

@ -2165,7 +2165,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");
@ -2178,7 +2178,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");
@ -2387,8 +2387,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;
@ -2410,7 +2409,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;
@ -2429,7 +2428,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;
@ -4101,8 +4100,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))
@ -4166,29 +4164,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)
@ -4258,7 +4256,7 @@ namespace libtorrent
{
if (ec)
{
if ((error > 1 || ec.category() == get_socks_category())
if ((error > 1 || ec.category() == socks_category())
&& t->alerts().should_post<peer_error_alert>())
{
t->alerts().emplace_alert<peer_error_alert>(handle, remote()
@ -4801,7 +4799,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"
@ -4822,7 +4820,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"
@ -4865,7 +4863,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"
@ -4895,7 +4893,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
peer_log(peer_log_alert::info, "MUTUAL_NO_INTEREST", "t1: %d t2: %d"
@ -6693,12 +6691,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()

View File

@ -2829,7 +2829,7 @@ retry:
{
m_alerts.emplace_alert<peer_disconnected_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
@ -3496,7 +3496,7 @@ retry:
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
{
@ -3518,7 +3518,7 @@ retry:
* 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));
}
}
}
@ -6638,8 +6638,7 @@ retry:
int disconnect = (std::min)(to_disconnect, num - my_average);
to_disconnect -= disconnect;
i->second->disconnect_peers(disconnect
, error_code(errors::too_many_connections, get_libtorrent_category()));
i->second->disconnect_peers(disconnect, errors::too_many_connections);
}
}
}

View File

@ -41,7 +41,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());
}
}
@ -73,12 +73,17 @@ namespace libtorrent
{ return boost::system::error_condition(ev, *this); }
};
TORRENT_EXPORT boost::system::error_category& get_socks_category()
boost::system::error_category& socks_category()
{
static socks_error_category socks_category;
return socks_category;
static socks_error_category cat;
return cat;
}
#ifndef TORRENT_NO_DEPRECATE
boost::system::error_category& get_socks_category()
{ return socks_category(); }
#endif
namespace
{
// parse out the endpoint from a SOCKS response
@ -413,7 +418,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;
@ -512,7 +517,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;

View File

@ -394,7 +394,7 @@ namespace libtorrent
if (parser.header_finished() && parser.status_code() != 200)
{
set_error(error_code(parser.status_code(), get_http_category()), error_file_url);
set_error(error_code(parser.status_code(), http_category()), error_file_url);
pause();
return;
}
@ -470,7 +470,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()), error_file_url);
set_error(errors::duplicate_torrent, error_file_url);
abort();
return;
}
@ -533,7 +533,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;
}
@ -582,7 +582,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;
}
@ -1869,7 +1869,7 @@ namespace libtorrent
if (m_resume_data && m_resume_data->node.type() == bdecode_node::dict_t)
{
int ev = 0;
errors::error_code_enum ev = errors::no_error;
if (m_resume_data->node.dict_find_string_value("file-format")
!= "libtorrent resume file")
{
@ -1885,16 +1885,15 @@ namespace libtorrent
if (ev && m_ses.alerts().should_post<fastresume_rejected_alert>())
{
error_code ec = error_code(ev, get_libtorrent_category());
m_ses.alerts().emplace_alert<fastresume_rejected_alert>(get_handle()
, ec, "", static_cast<char const*>(0));
, error_code(ev), "", static_cast<char const*>(0));
}
if (ev)
{
#ifndef TORRENT_DISABLE_LOGGING
debug_log("fastresume data rejected: %s"
, error_code(ev, get_libtorrent_category()).message().c_str());
, error_code(ev).message().c_str());
#endif
m_resume_data.reset();
}
@ -6043,7 +6042,7 @@ namespace libtorrent
{
if (alerts().should_post<torrent_error_alert>())
alerts().emplace_alert<torrent_error_alert>(get_handle()
, error_code(errors::not_an_ssl_torrent), "");
, errors::not_an_ssl_torrent, "");
return;
}
@ -6291,7 +6290,7 @@ namespace libtorrent
if (m_ses.alerts().should_post<url_seed_alert>())
{
m_ses.alerts().emplace_alert<url_seed_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(web);
@ -7708,8 +7707,7 @@ namespace libtorrent
// we have an i2p torrent, but we're not connected to an i2p
// SAM proxy.
if (alerts().should_post<i2p_alert>())
alerts().emplace_alert<i2p_alert>(error_code(errors::no_i2p_router
, get_libtorrent_category()));
alerts().emplace_alert<i2p_alert>(errors::no_i2p_router);
return false;
}
@ -7876,7 +7874,7 @@ namespace libtorrent
if (alerts().should_post<metadata_failed_alert>())
{
alerts().emplace_alert<metadata_failed_alert>(get_handle()
, error_code(errors::mismatching_info_hash, get_libtorrent_category()));
, errors::mismatching_info_hash);
}
return false;
}
@ -8850,8 +8848,7 @@ namespace libtorrent
{
if (alerts().should_post<file_rename_failed_alert>())
alerts().emplace_alert<file_rename_failed_alert>(get_handle()
, index, error_code(errors::session_is_closing
, get_libtorrent_category()));
, index, errors::session_is_closing);
return;
}
@ -9280,7 +9277,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)

View File

@ -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
@ -1165,12 +1165,17 @@ 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;
}
#ifndef TORRENT_NO_DEPRECATED
boost::system::error_category& get_upnp_category()
{ return upnp_category(); }
#endif
void upnp::on_upnp_get_ip_address_response(error_code const& e
, libtorrent::http_parser const& p, rootdevice& d
, http_connection& c)
@ -1420,7 +1425,7 @@ void upnp::return_error(int mapping, int code, mutex::scoped_lock& l)
}
const int proto = m_mappings[mapping].protocol;
l.unlock();
m_callback(mapping, address(), 0, proto, error_code(code, get_upnp_category()));
m_callback(mapping, address(), 0, proto, error_code(code, upnp_category()));
l.lock();
}
@ -1477,8 +1482,8 @@ void upnp::on_upnp_unmap_response(error_code const& e
l.unlock();
m_callback(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()));
l.lock();
d.mapping[mapping].protocol = none;

View File

@ -563,7 +563,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;
}

View File

@ -90,8 +90,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
@ -146,11 +146,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");

View File

@ -200,7 +200,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");
}
@ -295,21 +295,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)