make bandwidth state flags use type-safe flags

This commit is contained in:
arvidn 2017-07-17 17:55:25 -07:00 committed by Arvid Norberg
parent 06070ea499
commit f9c45db942
6 changed files with 50 additions and 37 deletions

View File

@ -293,6 +293,7 @@ void bind_converters()
to_python_converter<lt::torrent_flags_t, from_bitfield_flag<lt::torrent_flags_t>>(); to_python_converter<lt::torrent_flags_t, from_bitfield_flag<lt::torrent_flags_t>>();
to_python_converter<lt::peer_flags_t, from_bitfield_flag<lt::peer_flags_t>>(); to_python_converter<lt::peer_flags_t, from_bitfield_flag<lt::peer_flags_t>>();
to_python_converter<lt::peer_source_flags_t, from_bitfield_flag<lt::peer_source_flags_t>>(); to_python_converter<lt::peer_source_flags_t, from_bitfield_flag<lt::peer_source_flags_t>>();
to_python_converter<lt::bandwidth_state_flags_t, from_bitfield_flag<lt::bandwidth_state_flags_t>>();
// work-around types // work-around types
to_python_converter<lt::aux::noexcept_movable<lt::address>, address_to_tuple< to_python_converter<lt::aux::noexcept_movable<lt::address>, address_to_tuple<
@ -338,4 +339,5 @@ void bind_converters()
to_bitfield_flag<lt::torrent_flags_t>(); to_bitfield_flag<lt::torrent_flags_t>();
to_bitfield_flag<lt::peer_flags_t>(); to_bitfield_flag<lt::peer_flags_t>();
to_bitfield_flag<lt::peer_source_flags_t>(); to_bitfield_flag<lt::peer_source_flags_t>();
to_bitfield_flag<lt::bandwidth_state_flags_t>();
} }

View File

@ -138,13 +138,13 @@ void bind_peer_info()
pi.attr("resume_data") = peer_info::resume_data; pi.attr("resume_data") = peer_info::resume_data;
// read/write state // read/write state
pi.attr("bw_idle") = (int)peer_info::bw_idle; pi.attr("bw_idle") = peer_info::bw_idle;
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
pi.attr("bw_torrent") = (int)peer_info::bw_torrent; pi.attr("bw_torrent") = peer_info::bw_torrent;
pi.attr("bw_global") = (int)peer_info::bw_global; pi.attr("bw_global") = peer_info::bw_global;
#endif #endif
pi.attr("bw_limit") = (int)peer_info::bw_limit; pi.attr("bw_limit") = peer_info::bw_limit;
pi.attr("bw_network") = (int)peer_info::bw_network; pi.attr("bw_network") = peer_info::bw_network;
pi.attr("bw_disk") = (int)peer_info::bw_disk; pi.attr("bw_disk") = peer_info::bw_disk;
} }

View File

@ -799,7 +799,7 @@ namespace aux {
public: public:
// upload and download channel state // upload and download channel state
// enum from peer_info::bw_state // enum from peer_info::bw_state
char m_channel_state[2]; bandwidth_state_flags_t m_channel_state[2];
protected: protected:
receive_buffer m_recv_buffer; receive_buffer m_recv_buffer;

View File

@ -47,6 +47,7 @@ namespace libtorrent {
// hidden // hidden
struct peer_flags_tag; struct peer_flags_tag;
struct peer_source_flags_tag; struct peer_source_flags_tag;
struct bandwidth_state_flags_tag;
// flags for the peer_info::flags field. Indicates various states // flags for the peer_info::flags field. Indicates various states
// the peer may be in. These flags are not mutually exclusive, but // the peer may be in. These flags are not mutually exclusive, but
@ -58,6 +59,10 @@ namespace libtorrent {
// multiple sources // multiple sources
using peer_source_flags_t = flags::bitfield_flag<std::uint8_t, peer_source_flags_tag>; using peer_source_flags_t = flags::bitfield_flag<std::uint8_t, peer_source_flags_tag>;
// flags indicating what is blocking network transfers in up- and down
// direction
using bandwidth_state_flags_t = flags::bitfield_flag<std::uint8_t, bandwidth_state_flags_tag>;
// holds information and statistics about one peer // holds information and statistics about one peer
// that libtorrent is connected to // that libtorrent is connected to
struct TORRENT_EXPORT peer_info struct TORRENT_EXPORT peer_info
@ -374,37 +379,33 @@ namespace libtorrent {
// multi-homed clients with multiple interfaces to the internet. // multi-homed clients with multiple interfaces to the internet.
tcp::endpoint local_endpoint; tcp::endpoint local_endpoint;
// bits for the read_state and write_state // The peer is not waiting for any external events to
enum bw_state // send or receive data.
{ static constexpr bandwidth_state_flags_t bw_idle{0};
// The peer is not waiting for any external events to
// send or receive data.
bw_idle = 0,
// The peer is waiting for the rate limiter. // The peer is waiting for the rate limiter.
bw_limit = 1, static constexpr bandwidth_state_flags_t bw_limit{1};
// The peer has quota and is currently waiting for a // The peer has quota and is currently waiting for a
// network read or write operation to complete. This is // network read or write operation to complete. This is
// the state all peers are in if there are no bandwidth // the state all peers are in if there are no bandwidth
// limits. // limits.
bw_network = 2, static constexpr bandwidth_state_flags_t bw_network{2};
// The peer is waiting for the disk I/O thread to catch // The peer is waiting for the disk I/O thread to catch
// up writing buffers to disk before downloading more. // up writing buffers to disk before downloading more.
bw_disk = 4 static constexpr bandwidth_state_flags_t bw_disk{4};
};
#ifndef TORRENT_NO_DEPRECATE
enum bw_state_deprecated { bw_torrent = bw_limit, bw_global = bw_limit };
#endif
// bitmasks indicating what state this peer // bitmasks indicating what state this peer
// is in with regards to sending and receiving data. The states are declared in the // is in with regards to sending and receiving data. The states are declared in the
// bw_state enum. // bw_state enum.
char read_state; bandwidth_state_flags_t read_state;
char write_state; bandwidth_state_flags_t write_state;
#ifndef TORRENT_NO_DEPRECATE #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 // 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. // 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. // The global limit and the torrent limit may also be enforced.

View File

@ -2818,7 +2818,7 @@ namespace libtorrent {
// down to 0 and unblock all peers. // down to 0 and unblock all peers.
if (exceeded && m_outstanding_writing_bytes > 0) 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_counters.inc_stats_counter(counters::num_peers_down_disk);
m_channel_state[download_channel] |= peer_info::bw_disk; m_channel_state[download_channel] |= peer_info::bw_disk;
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -4124,7 +4124,7 @@ namespace libtorrent {
} }
#endif #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 // make sure we free up all send buffers that are owned
// by the disk thread // by the disk thread
@ -4760,7 +4760,7 @@ namespace libtorrent {
// if we can't read, it means we're blocked on the rate-limiter // 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 // or the disk, not the peer itself. In this case, don't blame
// the peer and disconnect it // 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()! // 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 // Hook this up to connect timeout as well. This would improve performance
@ -5419,7 +5419,7 @@ namespace libtorrent {
} }
#endif #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); bandwidth_manager* manager = m_ses.get_bandwidth_manager(channel);
@ -5500,7 +5500,7 @@ namespace libtorrent {
&& m_reading_bytes > 0 && m_reading_bytes > 0
&& quota_left > 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_counters.inc_stats_counter(counters::num_peers_up_disk);
m_channel_state[upload_channel] |= peer_info::bw_disk; m_channel_state[upload_channel] |= peer_info::bw_disk;
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -5574,7 +5574,7 @@ namespace libtorrent {
TORRENT_ASSERT(amount_to_send > 0); 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 #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::outgoing, "ASYNC_WRITE", "bytes: %d", amount_to_send); peer_log(peer_log_alert::outgoing, "ASYNC_WRITE", "bytes: %d", amount_to_send);
#endif #endif
@ -5596,7 +5596,7 @@ namespace libtorrent {
void peer_connection::on_disk() void peer_connection::on_disk()
{ {
TORRENT_ASSERT(is_single_thread()); 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<peer_connection> me(self()); std::shared_ptr<peer_connection> me(self());
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -5661,7 +5661,7 @@ namespace libtorrent {
if (max_receive == 0) return; if (max_receive == 0) return;
span<char> const vec = m_recv_buffer.reserve(max_receive); span<char> 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; m_channel_state[download_channel] |= peer_info::bw_network;
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::incoming, "ASYNC_READ" peer_log(peer_log_alert::incoming, "ASYNC_READ"

View File

@ -66,5 +66,15 @@ namespace libtorrent {
constexpr peer_source_flags_t peer_info::resume_data; constexpr peer_source_flags_t peer_info::resume_data;
constexpr peer_source_flags_t peer_info::incoming; 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
} }