From f9c45db94256664cccf9815330a2b2ae1aca5330 Mon Sep 17 00:00:00 2001 From: arvidn Date: Mon, 17 Jul 2017 17:55:25 -0700 Subject: [PATCH] make bandwidth state flags use type-safe flags --- bindings/python/src/converters.cpp | 2 ++ bindings/python/src/peer_info.cpp | 12 +++---- include/libtorrent/peer_connection.hpp | 2 +- include/libtorrent/peer_info.hpp | 45 +++++++++++++------------- src/peer_connection.cpp | 16 ++++----- src/peer_info.cpp | 10 ++++++ 6 files changed, 50 insertions(+), 37 deletions(-) diff --git a/bindings/python/src/converters.cpp b/bindings/python/src/converters.cpp index bc7865e7e..ac494edb3 100644 --- a/bindings/python/src/converters.cpp +++ b/bindings/python/src/converters.cpp @@ -293,6 +293,7 @@ void bind_converters() to_python_converter>(); to_python_converter>(); to_python_converter>(); + to_python_converter>(); // work-around types to_python_converter, address_to_tuple< @@ -338,4 +339,5 @@ void bind_converters() to_bitfield_flag(); to_bitfield_flag(); to_bitfield_flag(); + to_bitfield_flag(); } diff --git a/bindings/python/src/peer_info.cpp b/bindings/python/src/peer_info.cpp index eec68e24a..86ff4090d 100644 --- a/bindings/python/src/peer_info.cpp +++ b/bindings/python/src/peer_info.cpp @@ -138,13 +138,13 @@ void bind_peer_info() pi.attr("resume_data") = peer_info::resume_data; // read/write state - pi.attr("bw_idle") = (int)peer_info::bw_idle; + pi.attr("bw_idle") = peer_info::bw_idle; #ifndef TORRENT_NO_DEPRECATE - pi.attr("bw_torrent") = (int)peer_info::bw_torrent; - pi.attr("bw_global") = (int)peer_info::bw_global; + pi.attr("bw_torrent") = peer_info::bw_torrent; + pi.attr("bw_global") = peer_info::bw_global; #endif - pi.attr("bw_limit") = (int)peer_info::bw_limit; - pi.attr("bw_network") = (int)peer_info::bw_network; - pi.attr("bw_disk") = (int)peer_info::bw_disk; + pi.attr("bw_limit") = peer_info::bw_limit; + pi.attr("bw_network") = peer_info::bw_network; + pi.attr("bw_disk") = peer_info::bw_disk; } diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 70a02c888..df098540f 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -799,7 +799,7 @@ namespace aux { public: // upload and download channel state // enum from peer_info::bw_state - char m_channel_state[2]; + bandwidth_state_flags_t m_channel_state[2]; protected: receive_buffer m_recv_buffer; diff --git a/include/libtorrent/peer_info.hpp b/include/libtorrent/peer_info.hpp index daf7683a0..c1235f70e 100644 --- a/include/libtorrent/peer_info.hpp +++ b/include/libtorrent/peer_info.hpp @@ -47,6 +47,7 @@ namespace libtorrent { // hidden struct peer_flags_tag; struct peer_source_flags_tag; + struct bandwidth_state_flags_tag; // flags for the peer_info::flags field. Indicates various states // the peer may be in. These flags are not mutually exclusive, but @@ -58,6 +59,10 @@ namespace libtorrent { // multiple sources using peer_source_flags_t = flags::bitfield_flag; + // flags indicating what is blocking network transfers in up- and down + // direction + using bandwidth_state_flags_t = flags::bitfield_flag; + // holds information and statistics about one peer // that libtorrent is connected to struct TORRENT_EXPORT peer_info @@ -374,37 +379,33 @@ namespace libtorrent { // multi-homed clients with multiple interfaces to the internet. tcp::endpoint local_endpoint; - // bits for the read_state and write_state - enum bw_state - { - // The peer is not waiting for any external events to - // send or receive data. - bw_idle = 0, + // The peer is not waiting for any external events to + // send or receive data. + static constexpr bandwidth_state_flags_t bw_idle{0}; - // The peer is waiting for the rate limiter. - bw_limit = 1, + // The peer is waiting for the rate limiter. + static constexpr bandwidth_state_flags_t bw_limit{1}; - // The peer has quota and is currently waiting for a - // network read or write operation to complete. This is - // the state all peers are in if there are no bandwidth - // limits. - bw_network = 2, + // The peer has quota and is currently waiting for a + // network read or write operation to complete. This is + // the state all peers are in if there are no bandwidth + // limits. + static constexpr bandwidth_state_flags_t bw_network{2}; - // The peer is waiting for the disk I/O thread to catch - // up writing buffers to disk before downloading more. - bw_disk = 4 - }; -#ifndef TORRENT_NO_DEPRECATE - enum bw_state_deprecated { bw_torrent = bw_limit, bw_global = bw_limit }; -#endif + // The peer is waiting for the disk I/O thread to catch + // up writing buffers to disk before downloading more. + static constexpr bandwidth_state_flags_t bw_disk{4}; // bitmasks indicating what state this peer // is in with regards to sending and receiving data. The states are declared in the // bw_state enum. - char read_state; - char write_state; + bandwidth_state_flags_t read_state; + bandwidth_state_flags_t write_state; #ifndef TORRENT_NO_DEPRECATE + static constexpr bandwidth_state_flags_t bw_torrent = bw_limit; + static constexpr bandwidth_state_flags_t bw_global = bw_limit; + // the number of bytes per second we are allowed to send to or receive // from this peer. It may be -1 if there's no local limit on the peer. // The global limit and the torrent limit may also be enforced. diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index ff3b35433..5a144f62c 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -2818,7 +2818,7 @@ namespace libtorrent { // down to 0 and unblock all peers. if (exceeded && m_outstanding_writing_bytes > 0) { - if ((m_channel_state[download_channel] & peer_info::bw_disk) == 0) + if (!(m_channel_state[download_channel] & peer_info::bw_disk)) m_counters.inc_stats_counter(counters::num_peers_down_disk); m_channel_state[download_channel] |= peer_info::bw_disk; #ifndef TORRENT_DISABLE_LOGGING @@ -4124,7 +4124,7 @@ namespace libtorrent { } #endif - if ((m_channel_state[upload_channel] & peer_info::bw_network) == 0) + if (!(m_channel_state[upload_channel] & peer_info::bw_network)) { // make sure we free up all send buffers that are owned // by the disk thread @@ -4760,7 +4760,7 @@ namespace libtorrent { // if we can't read, it means we're blocked on the rate-limiter // or the disk, not the peer itself. In this case, don't blame // the peer and disconnect it - bool const may_timeout = (m_channel_state[download_channel] & peer_info::bw_network) != 0; + bool const may_timeout = bool(m_channel_state[download_channel] & peer_info::bw_network); // TODO: 2 use a deadline_timer for timeouts. Don't rely on second_tick()! // Hook this up to connect timeout as well. This would improve performance @@ -5419,7 +5419,7 @@ namespace libtorrent { } #endif - TORRENT_ASSERT((m_channel_state[channel] & peer_info::bw_limit) == 0); + TORRENT_ASSERT(!(m_channel_state[channel] & peer_info::bw_limit)); bandwidth_manager* manager = m_ses.get_bandwidth_manager(channel); @@ -5500,7 +5500,7 @@ namespace libtorrent { && m_reading_bytes > 0 && quota_left > 0) { - if ((m_channel_state[upload_channel] & peer_info::bw_disk) == 0) + if (!(m_channel_state[upload_channel] & peer_info::bw_disk)) m_counters.inc_stats_counter(counters::num_peers_up_disk); m_channel_state[upload_channel] |= peer_info::bw_disk; #ifndef TORRENT_DISABLE_LOGGING @@ -5574,7 +5574,7 @@ namespace libtorrent { TORRENT_ASSERT(amount_to_send > 0); - TORRENT_ASSERT((m_channel_state[upload_channel] & peer_info::bw_network) == 0); + TORRENT_ASSERT(!(m_channel_state[upload_channel] & peer_info::bw_network)); #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::outgoing, "ASYNC_WRITE", "bytes: %d", amount_to_send); #endif @@ -5596,7 +5596,7 @@ namespace libtorrent { void peer_connection::on_disk() { TORRENT_ASSERT(is_single_thread()); - if ((m_channel_state[download_channel] & peer_info::bw_disk) == 0) return; + if (!(m_channel_state[download_channel] & peer_info::bw_disk)) return; std::shared_ptr me(self()); #ifndef TORRENT_DISABLE_LOGGING @@ -5661,7 +5661,7 @@ namespace libtorrent { if (max_receive == 0) return; span const vec = m_recv_buffer.reserve(max_receive); - TORRENT_ASSERT((m_channel_state[download_channel] & peer_info::bw_network) == 0); + TORRENT_ASSERT(!(m_channel_state[download_channel] & peer_info::bw_network)); m_channel_state[download_channel] |= peer_info::bw_network; #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::incoming, "ASYNC_READ" diff --git a/src/peer_info.cpp b/src/peer_info.cpp index d07401af0..a207817ca 100644 --- a/src/peer_info.cpp +++ b/src/peer_info.cpp @@ -66,5 +66,15 @@ namespace libtorrent { constexpr peer_source_flags_t peer_info::resume_data; constexpr peer_source_flags_t peer_info::incoming; + constexpr bandwidth_state_flags_t peer_info::bw_idle; + constexpr bandwidth_state_flags_t peer_info::bw_limit; + constexpr bandwidth_state_flags_t peer_info::bw_network; + constexpr bandwidth_state_flags_t peer_info::bw_disk; + +#ifndef TORRENT_NO_DEPRECATE + constexpr bandwidth_state_flags_t peer_info::bw_torrent; + constexpr bandwidth_state_flags_t peer_info::bw_global; +#endif + }