forked from premiere/premiere-libtorrent
fixing sign-conversion warnings, part 14, minor refactor
This commit is contained in:
parent
76f91c3fc9
commit
4d5ddc90f5
|
@ -769,7 +769,7 @@ namespace libtorrent
|
|||
|
||||
#ifndef TORRENT_NO_DEPRECATE
|
||||
// the alert pointers stored in m_alerts
|
||||
mutable std::vector<alert*> m_alert_pointers;
|
||||
mutable aux::vector<alert*> m_alert_pointers;
|
||||
|
||||
// if not all the alerts in m_alert_pointers have been delivered to
|
||||
// the client. This is the offset into m_alert_pointers where the next
|
||||
|
|
|
@ -943,7 +943,7 @@ namespace libtorrent
|
|||
// a sent-piece counter for the allowed fast set
|
||||
// to avoid exploitation. Each slot is a counter
|
||||
// for one of the pieces from the allowed-fast set
|
||||
std::vector<std::uint16_t> m_accept_fast_piece_cnt;
|
||||
aux::vector<std::uint16_t> m_accept_fast_piece_cnt;
|
||||
|
||||
// the pieces the peer will send us if
|
||||
// requested (regardless of choke state)
|
||||
|
|
|
@ -41,16 +41,17 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
#include "libtorrent/identify_client.hpp"
|
||||
#include "libtorrent/fingerprint.hpp"
|
||||
#include "libtorrent/string_util.hpp"
|
||||
#include "libtorrent/aux_/numeric_cast.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using namespace libtorrent;
|
||||
|
||||
int decode_digit(char c)
|
||||
int decode_digit(std::uint8_t c)
|
||||
{
|
||||
if (is_digit(c)) return c - '0';
|
||||
return unsigned(c) - 'A' + 10;
|
||||
if (is_digit(char(c))) return c - '0';
|
||||
return c - 'A' + 10;
|
||||
}
|
||||
|
||||
// takes a peer id and returns a valid boost::optional
|
||||
|
@ -61,14 +62,14 @@ namespace
|
|||
{
|
||||
fingerprint ret("..", 0, 0, 0, 0);
|
||||
|
||||
if (id[0] != '-' || !is_print(id[1]) || (id[2] < '0')
|
||||
if (id[0] != '-' || !is_print(char(id[1])) || (id[2] < '0')
|
||||
|| (id[3] < '0') || (id[4] < '0')
|
||||
|| (id[5] < '0') || (id[6] < '0')
|
||||
|| id[7] != '-')
|
||||
return boost::optional<fingerprint>();
|
||||
|
||||
ret.name[0] = id[1];
|
||||
ret.name[1] = id[2];
|
||||
ret.name[0] = char(id[1]);
|
||||
ret.name[1] = char(id[2]);
|
||||
ret.major_version = decode_digit(id[3]);
|
||||
ret.minor_version = decode_digit(id[4]);
|
||||
ret.revision_version = decode_digit(id[5]);
|
||||
|
@ -83,10 +84,10 @@ namespace
|
|||
{
|
||||
fingerprint ret("..", 0, 0, 0, 0);
|
||||
|
||||
if (!is_alpha(id[0]) && !is_digit(id[0]))
|
||||
if (!is_alpha(char(id[0])) && !is_digit(char(id[0])))
|
||||
return boost::optional<fingerprint>();
|
||||
|
||||
if (std::equal(id.begin()+4, id.begin()+6, "--"))
|
||||
if (std::equal(id.begin() + 4, id.begin() + 6, "--"))
|
||||
{
|
||||
if ((id[1] < '0') || (id[2] < '0')
|
||||
|| (id[3] < '0'))
|
||||
|
@ -104,7 +105,7 @@ namespace
|
|||
ret.revision_version = id[3];
|
||||
}
|
||||
|
||||
ret.name[0] = id[0];
|
||||
ret.name[0] = char(id[0]);
|
||||
ret.name[1] = 0;
|
||||
|
||||
ret.tag_version = 0;
|
||||
|
@ -327,7 +328,7 @@ namespace
|
|||
|
||||
if (f.tag_version != 0)
|
||||
{
|
||||
std::snprintf(identity + num_chars, sizeof(identity) - num_chars
|
||||
std::snprintf(identity + num_chars, sizeof(identity) - aux::numeric_cast<std::size_t>(num_chars)
|
||||
, ".%u", f.tag_version);
|
||||
}
|
||||
|
||||
|
@ -417,7 +418,7 @@ namespace libtorrent
|
|||
std::string unknown("Unknown [");
|
||||
for (peer_id::const_iterator i = p.begin(); i != p.end(); ++i)
|
||||
{
|
||||
unknown += is_print(char(*i))?*i:'.';
|
||||
unknown += is_print(char(*i)) ? char(*i) : '.';
|
||||
}
|
||||
unknown += "]";
|
||||
return unknown;
|
||||
|
|
|
@ -740,7 +740,7 @@ namespace libtorrent
|
|||
{
|
||||
TORRENT_ASSERT(prio >= 0);
|
||||
TORRENT_ASSERT(prio < int(m_priority_boundaries.size()));
|
||||
return prio == 0 ? prio_index_t(0) : m_priority_boundaries[prio-1];
|
||||
return prio == 0 ? prio_index_t(0) : m_priority_boundaries[prio - 1];
|
||||
}
|
||||
|
||||
prio_index_t piece_picker::priority_end(int const prio) const
|
||||
|
@ -754,7 +754,7 @@ namespace libtorrent
|
|||
{
|
||||
TORRENT_ASSERT(prio >= 0);
|
||||
TORRENT_ASSERT(prio < int(m_priority_boundaries.size()));
|
||||
return { priority_begin(prio), priority_end(prio) };
|
||||
return {priority_begin(prio), priority_end(prio)};
|
||||
}
|
||||
|
||||
void piece_picker::add(piece_index_t index)
|
||||
|
@ -776,7 +776,9 @@ namespace libtorrent
|
|||
auto const range = priority_range(priority);
|
||||
prio_index_t new_index = (range.second == range.first)
|
||||
? range.first
|
||||
: prio_index_t(random(static_cast<int>(range.second - range.first)) + static_cast<int>(range.first));
|
||||
: prio_index_t(
|
||||
int(random(aux::numeric_cast<std::uint32_t>(static_cast<int>(range.second - range.first))))
|
||||
+ static_cast<int>(range.first));
|
||||
|
||||
#ifdef TORRENT_PICKER_LOG
|
||||
std::cerr << "[" << this << "] " << "add " << index << " (" << priority << ")" << std::endl;
|
||||
|
@ -994,7 +996,9 @@ namespace libtorrent
|
|||
TORRENT_ASSERT(m_piece_map[m_pieces[elem_index]].priority(this) == priority);
|
||||
|
||||
auto const range = priority_range(priority);
|
||||
prio_index_t const other_index(random(static_cast<int>(range.second - range.first) - 1) + static_cast<int>(range.first));
|
||||
prio_index_t const other_index(
|
||||
int(random(aux::numeric_cast<std::uint32_t>(static_cast<int>(range.second - range.first) - 1)))
|
||||
+ static_cast<int>(range.first));
|
||||
|
||||
if (other_index == elem_index) return;
|
||||
|
||||
|
@ -2133,7 +2137,7 @@ namespace libtorrent
|
|||
// we're not using rarest first (only for the first
|
||||
// bucket, since that's where the currently downloading
|
||||
// pieces are)
|
||||
piece_index_t const start_piece = piece_index_t(random(std::uint32_t(m_piece_map.size()-1)));
|
||||
piece_index_t const start_piece = piece_index_t(int(random(aux::numeric_cast<std::uint32_t>(m_piece_map.size() - 1))));
|
||||
|
||||
piece_index_t piece = start_piece;
|
||||
while (num_blocks > 0)
|
||||
|
@ -2307,7 +2311,7 @@ get_out:
|
|||
while (partials_size > 0)
|
||||
{
|
||||
pc.inc_stats_counter(counters::piece_picker_busy_loops);
|
||||
int piece = int(random(partials_size - 1));
|
||||
int piece = int(random(aux::numeric_cast<std::uint32_t>(partials_size - 1)));
|
||||
downloading_piece const* dp = partials[piece];
|
||||
TORRENT_ASSERT(pieces[dp->index]);
|
||||
TORRENT_ASSERT(piece_priority(dp->index) > 0);
|
||||
|
@ -3436,7 +3440,7 @@ get_out:
|
|||
d.clear();
|
||||
int const state = m_piece_map[index].download_queue();
|
||||
int const num_blocks = blocks_in_piece(index);
|
||||
d.reserve(num_blocks);
|
||||
d.reserve(aux::numeric_cast<std::size_t>(num_blocks));
|
||||
|
||||
if (state == piece_pos::piece_open)
|
||||
{
|
||||
|
|
|
@ -30,14 +30,16 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
*/
|
||||
|
||||
#include <libtorrent/receive_buffer.hpp>
|
||||
#include <libtorrent/invariant_check.hpp>
|
||||
#include "libtorrent/receive_buffer.hpp"
|
||||
#include "libtorrent/invariant_check.hpp"
|
||||
#include "libtorrent/aux_/numeric_cast.hpp"
|
||||
#include "libtorrent/aux_/typed_span.hpp"
|
||||
|
||||
namespace libtorrent {
|
||||
|
||||
int receive_buffer::max_receive() const
|
||||
{
|
||||
return int(m_recv_buffer.size() - m_recv_end);
|
||||
return int(m_recv_buffer.size()) - m_recv_end;
|
||||
}
|
||||
|
||||
span<char> receive_buffer::reserve(int const size)
|
||||
|
@ -52,8 +54,8 @@ span<char> receive_buffer::reserve(int const size)
|
|||
if (int(m_recv_buffer.size()) < m_recv_end + size)
|
||||
{
|
||||
int const new_size = std::max(m_recv_end + size, m_packet_size);
|
||||
buffer new_buffer(new_size
|
||||
, span<char const>(m_recv_buffer.data(), m_recv_end));
|
||||
buffer new_buffer(aux::numeric_cast<std::size_t>(new_size)
|
||||
, {m_recv_buffer.data(), aux::numeric_cast<std::size_t>(m_recv_end)});
|
||||
m_recv_buffer = std::move(new_buffer);
|
||||
|
||||
// since we just increased the size of the buffer, reset the watermark to
|
||||
|
@ -61,7 +63,7 @@ span<char> receive_buffer::reserve(int const size)
|
|||
m_watermark = sliding_average<20>();
|
||||
}
|
||||
|
||||
return span<char>(m_recv_buffer).subspan(m_recv_end, size);
|
||||
return aux::typed_span<char>(m_recv_buffer).subspan(m_recv_end, size);
|
||||
}
|
||||
|
||||
void receive_buffer::grow(int const limit)
|
||||
|
@ -75,8 +77,8 @@ void receive_buffer::grow(int const limit)
|
|||
? m_packet_size : std::min(current_size * 3 / 2, limit);
|
||||
|
||||
// re-allocate the buffer and copy over the part of it that's used
|
||||
buffer new_buffer(new_size
|
||||
, span<char const>(m_recv_buffer.data(), m_recv_end));
|
||||
buffer new_buffer(aux::numeric_cast<std::size_t>(new_size)
|
||||
, {m_recv_buffer.data(), aux::numeric_cast<std::size_t>(m_recv_end)});
|
||||
m_recv_buffer = std::move(new_buffer);
|
||||
|
||||
// since we just increased the size of the buffer, reset the watermark to
|
||||
|
@ -116,7 +118,7 @@ void receive_buffer::cut(int const size, int const packet_size, int const offset
|
|||
{
|
||||
std::memmove(&m_recv_buffer[0] + m_recv_start + offset
|
||||
, &m_recv_buffer[0] + m_recv_start + offset + size
|
||||
, m_recv_end - m_recv_start - size - offset);
|
||||
, aux::numeric_cast<std::size_t>(m_recv_end - m_recv_start - size - offset));
|
||||
}
|
||||
|
||||
m_recv_pos -= size;
|
||||
|
@ -145,14 +147,14 @@ span<char const> receive_buffer::get() const
|
|||
}
|
||||
|
||||
TORRENT_ASSERT(m_recv_start + m_recv_pos <= int(m_recv_buffer.size()));
|
||||
return span<char const>(m_recv_buffer).subspan(m_recv_start, m_recv_pos);
|
||||
return aux::typed_span<char const>(m_recv_buffer).subspan(m_recv_start, m_recv_pos);
|
||||
}
|
||||
|
||||
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
||||
span<char> receive_buffer::mutable_buffer()
|
||||
{
|
||||
INVARIANT_CHECK;
|
||||
return span<char>(m_recv_buffer).subspan(m_recv_start, m_recv_pos);
|
||||
return aux::typed_span<char>(m_recv_buffer).subspan(m_recv_start, m_recv_pos);
|
||||
}
|
||||
|
||||
span<char> receive_buffer::mutable_buffer(int const bytes)
|
||||
|
@ -161,7 +163,7 @@ span<char> receive_buffer::mutable_buffer(int const bytes)
|
|||
// bytes is the number of bytes we just received, and m_recv_pos has
|
||||
// already been adjusted for these bytes. The receive pos immediately
|
||||
// before we received these bytes was (m_recv_pos - bytes)
|
||||
return span<char>(m_recv_buffer).subspan(m_recv_start + m_recv_pos - bytes, bytes);
|
||||
return aux::typed_span<char>(m_recv_buffer).subspan(m_recv_start + m_recv_pos - bytes, bytes);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -181,20 +183,19 @@ void receive_buffer::normalize(int const force_shrink)
|
|||
bool const shrink_buffer = int(m_recv_buffer.size()) / 2 > m_watermark.mean()
|
||||
&& m_watermark.mean() > (m_recv_end - m_recv_start);
|
||||
|
||||
span<char const> bytes_to_shift(
|
||||
m_recv_buffer.data() + m_recv_start
|
||||
, m_recv_end - m_recv_start);
|
||||
span<char const> bytes_to_shift(m_recv_buffer.data() + m_recv_start
|
||||
, aux::numeric_cast<std::size_t>(m_recv_end - m_recv_start));
|
||||
|
||||
if (force_shrink)
|
||||
{
|
||||
int const target_size = std::max(std::max(force_shrink
|
||||
, int(bytes_to_shift.size())), m_packet_size);
|
||||
buffer new_buffer(target_size, bytes_to_shift);
|
||||
buffer new_buffer(aux::numeric_cast<std::size_t>(target_size), bytes_to_shift);
|
||||
m_recv_buffer = std::move(new_buffer);
|
||||
}
|
||||
else if (shrink_buffer)
|
||||
{
|
||||
buffer new_buffer(m_watermark.mean(), bytes_to_shift);
|
||||
buffer new_buffer(aux::numeric_cast<std::size_t>(m_watermark.mean()), bytes_to_shift);
|
||||
m_recv_buffer = std::move(new_buffer);
|
||||
}
|
||||
else if (m_recv_end > m_recv_start
|
||||
|
@ -320,7 +321,7 @@ span<char const> crypto_receive_buffer::get() const
|
|||
{
|
||||
span<char const> recv_buffer = m_connection_buffer.get();
|
||||
if (m_recv_pos < m_connection_buffer.pos())
|
||||
recv_buffer = recv_buffer.first(m_recv_pos);
|
||||
recv_buffer = recv_buffer.first(aux::numeric_cast<std::size_t>(m_recv_pos));
|
||||
return recv_buffer;
|
||||
}
|
||||
|
||||
|
|
|
@ -1214,7 +1214,7 @@ namespace aux {
|
|||
return m_peer_class_type_filter;
|
||||
}
|
||||
|
||||
void session_impl::set_peer_classes(peer_class_set* s, address const& a, int st)
|
||||
void session_impl::set_peer_classes(peer_class_set* s, address const& a, int const st)
|
||||
{
|
||||
std::uint32_t peer_class_mask = m_peer_class_filter.access(a);
|
||||
|
||||
|
@ -2315,7 +2315,7 @@ namespace aux {
|
|||
|
||||
for (;;)
|
||||
{
|
||||
std::array<udp_socket::packet, 50> p;
|
||||
aux::array<udp_socket::packet, 50> p;
|
||||
error_code err;
|
||||
int const num_packets = s->read(p, err);
|
||||
|
||||
|
@ -3226,8 +3226,8 @@ namespace aux {
|
|||
int total_peers = num_peers[0][i] + num_peers[1][i];
|
||||
// this are 64 bits since it's multiplied by the number
|
||||
// of peers, which otherwise might overflow an int
|
||||
std::uint64_t rate = stat_rate[i];
|
||||
tcp_channel[i].throttle((std::max)(int(rate * num_peers[0][i] / total_peers), lower_limit[i]));
|
||||
std::int64_t rate = stat_rate[i];
|
||||
tcp_channel[i].throttle(std::max(int(rate * num_peers[0][i] / total_peers), lower_limit[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3274,7 +3274,7 @@ namespace aux {
|
|||
std::printf("\033[2J\033[0;0H");
|
||||
#endif
|
||||
|
||||
std::vector<torrent*>& want_tick = m_torrent_lists[torrent_want_tick];
|
||||
aux::vector<torrent*>& want_tick = m_torrent_lists[torrent_want_tick];
|
||||
for (int i = 0; i < int(want_tick.size()); ++i)
|
||||
{
|
||||
torrent& t = *want_tick[i];
|
||||
|
@ -3328,7 +3328,7 @@ namespace aux {
|
|||
--m_auto_scrape_time_scaler;
|
||||
if (m_auto_scrape_time_scaler <= 0)
|
||||
{
|
||||
std::vector<torrent*>& want_scrape = m_torrent_lists[torrent_want_scrape];
|
||||
aux::vector<torrent*>& want_scrape = m_torrent_lists[torrent_want_scrape];
|
||||
m_auto_scrape_time_scaler = m_settings.get_int(settings_pack::auto_scrape_interval)
|
||||
/ (std::max)(1, int(want_scrape.size()));
|
||||
if (m_auto_scrape_time_scaler < m_settings.get_int(settings_pack::auto_scrape_min_interval))
|
||||
|
@ -3462,13 +3462,13 @@ namespace aux {
|
|||
|
||||
void session_impl::received_buffer(int s)
|
||||
{
|
||||
int index = (std::min)(log2(s >> 3), 17);
|
||||
int index = std::min(log2(std::uint32_t(s >> 3)), 17);
|
||||
m_stats_counters.inc_stats_counter(counters::socket_recv_size3 + index);
|
||||
}
|
||||
|
||||
void session_impl::sent_buffer(int s)
|
||||
{
|
||||
int index = (std::min)(log2(s >> 3), 17);
|
||||
int index = std::min(log2(std::uint32_t(s >> 3)), 17);
|
||||
m_stats_counters.inc_stats_counter(counters::socket_send_size3 + index);
|
||||
}
|
||||
|
||||
|
@ -4032,10 +4032,10 @@ namespace aux {
|
|||
// attempts over time, to prevent connecting a large number of
|
||||
// sockets, wait 10 seconds, and then try again
|
||||
if (m_settings.get_bool(settings_pack::smooth_connects) && max_connections > (limit+1) / 2)
|
||||
max_connections = (limit+1) / 2;
|
||||
max_connections = (limit + 1) / 2;
|
||||
|
||||
std::vector<torrent*>& want_peers_download = m_torrent_lists[torrent_want_peers_download];
|
||||
std::vector<torrent*>& want_peers_finished = m_torrent_lists[torrent_want_peers_finished];
|
||||
aux::vector<torrent*>& want_peers_download = m_torrent_lists[torrent_want_peers_download];
|
||||
aux::vector<torrent*>& want_peers_finished = m_torrent_lists[torrent_want_peers_finished];
|
||||
|
||||
// if no torrent want any peers, just return
|
||||
if (want_peers_download.empty() && want_peers_finished.empty()) return;
|
||||
|
@ -4980,10 +4980,10 @@ namespace aux {
|
|||
// local endpoint for this socket is bound to one of the allowed
|
||||
// interfaces. the list can be a mixture of interfaces and IP
|
||||
// addresses.
|
||||
for (int i = 0; i < int(m_outgoing_interfaces.size()); ++i)
|
||||
for (auto const& s : m_outgoing_interfaces)
|
||||
{
|
||||
error_code err;
|
||||
address ip = address::from_string(m_outgoing_interfaces[i].c_str(), err);
|
||||
address ip = address::from_string(s.c_str(), err);
|
||||
if (err) continue;
|
||||
if (ip == addr) return true;
|
||||
}
|
||||
|
@ -4996,9 +4996,9 @@ namespace aux {
|
|||
// if no device was found to have this address, we fail
|
||||
if (device.empty()) return false;
|
||||
|
||||
for (int i = 0; i < int(m_outgoing_interfaces.size()); ++i)
|
||||
for (auto const& s : m_outgoing_interfaces)
|
||||
{
|
||||
if (m_outgoing_interfaces[i] == device) return true;
|
||||
if (s == device) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -5243,7 +5243,7 @@ namespace aux {
|
|||
if (print.size() > 20) print.resize(20);
|
||||
|
||||
// the client's fingerprint
|
||||
std::copy(print.begin(), print.begin() + print.length(), m_peer_id.begin());
|
||||
std::copy(print.begin(), print.begin() + int(print.length()), m_peer_id.begin());
|
||||
if (print.length() < 20)
|
||||
{
|
||||
url_random(m_peer_id.data() + print.length(), m_peer_id.data() + 20);
|
||||
|
@ -6304,10 +6304,10 @@ namespace aux {
|
|||
int to_disconnect = num_connections() - m_settings.get_int(settings_pack::connections_limit);
|
||||
|
||||
int last_average = 0;
|
||||
int average = int(m_settings.get_int(settings_pack::connections_limit) / m_torrents.size());
|
||||
int average = m_settings.get_int(settings_pack::connections_limit) / int(m_torrents.size());
|
||||
|
||||
// the number of slots that are unused by torrents
|
||||
int extra = m_settings.get_int(settings_pack::connections_limit) % m_torrents.size();
|
||||
int extra = m_settings.get_int(settings_pack::connections_limit) % int(m_torrents.size());
|
||||
|
||||
// run 3 iterations of this, then we're probably close enough
|
||||
for (int iter = 0; iter < 4; ++iter)
|
||||
|
@ -6434,7 +6434,7 @@ namespace aux {
|
|||
}
|
||||
|
||||
#ifndef TORRENT_NO_DEPRECATE
|
||||
size_t session_impl::set_alert_queue_size_limit(size_t queue_size_limit_)
|
||||
std::size_t session_impl::set_alert_queue_size_limit(std::size_t queue_size_limit_)
|
||||
{
|
||||
m_settings.set_int(settings_pack::alert_queue_size, int(queue_size_limit_));
|
||||
return m_alerts.set_alert_queue_size_limit(int(queue_size_limit_));
|
||||
|
@ -6730,8 +6730,8 @@ namespace aux {
|
|||
TORRENT_ASSERT(is_single_thread());
|
||||
|
||||
#ifdef TORRENT_DISABLE_POOL_ALLOCATOR
|
||||
int num_bytes = send_buffer_size();
|
||||
return ses_buffer_holder(*this, static_cast<char*>(malloc(num_bytes)));
|
||||
std::size_t num_bytes = aux::numeric_cast<std::size_t>(send_buffer_size());
|
||||
return ses_buffer_holder(*this, static_cast<char*>(std::malloc(num_bytes)));
|
||||
#else
|
||||
return ses_buffer_holder(*this, static_cast<char*>(m_send_buffers.malloc()));
|
||||
#endif
|
||||
|
@ -6768,7 +6768,7 @@ namespace aux {
|
|||
}
|
||||
|
||||
int const num_gauges = counters::num_error_torrents - counters::num_checking_torrents + 1;
|
||||
std::array<int, num_gauges> torrent_state_gauges;
|
||||
aux::array<int, num_gauges> torrent_state_gauges;
|
||||
torrent_state_gauges.fill(0);
|
||||
|
||||
#if defined TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
||||
|
@ -6792,7 +6792,7 @@ namespace aux {
|
|||
++torrent_state_gauges[state];
|
||||
}
|
||||
|
||||
int pos = t->queue_position();
|
||||
int const pos = t->queue_position();
|
||||
if (pos < 0)
|
||||
{
|
||||
TORRENT_ASSERT(pos == -1);
|
||||
|
|
|
@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
#include "libtorrent/time.hpp"
|
||||
#include "libtorrent/debug.hpp"
|
||||
#include "libtorrent/deadline_timer.hpp"
|
||||
#include "libtorrent/aux_/numeric_cast.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
|
@ -192,7 +193,7 @@ int udp_socket::read(span<packet> pkts, error_code& ec)
|
|||
}
|
||||
else
|
||||
{
|
||||
p.data = span<char>(m_buf->data(), len);
|
||||
p.data = {m_buf->data(), aux::numeric_cast<std::size_t>(len)};
|
||||
|
||||
// support packets coming from the SOCKS5 proxy
|
||||
if (m_socks5_connection && m_socks5_connection->active())
|
||||
|
@ -206,7 +207,7 @@ int udp_socket::read(span<packet> pkts, error_code& ec)
|
|||
else if (m_force_proxy) continue;
|
||||
}
|
||||
|
||||
pkts[ret] = p;
|
||||
pkts[aux::numeric_cast<std::size_t>(ret)] = p;
|
||||
++ret;
|
||||
|
||||
// we only have a single buffer for now, so we can only return a
|
||||
|
@ -298,7 +299,7 @@ void udp_socket::wrap(udp::endpoint const& ep, span<char const> p
|
|||
write_endpoint(ep, h);
|
||||
|
||||
std::array<boost::asio::const_buffer, 2> iovec;
|
||||
iovec[0] = boost::asio::const_buffer(header, h - header);
|
||||
iovec[0] = boost::asio::const_buffer(header, aux::numeric_cast<std::size_t>(h - header));
|
||||
iovec[1] = boost::asio::const_buffer(p.data(), p.size());
|
||||
|
||||
// set the DF flag for the socket and clear it again in the destructor
|
||||
|
@ -320,14 +321,14 @@ void udp_socket::wrap(char const* hostname, int const port, span<char const> p
|
|||
write_uint16(0, h); // reserved
|
||||
write_uint8(0, h); // fragment
|
||||
write_uint8(3, h); // atyp
|
||||
int const hostlen = (std::min)(int(strlen(hostname)), 255);
|
||||
write_uint8(std::uint8_t(hostlen), h); // hostname len
|
||||
std::size_t const hostlen = std::min(std::strlen(hostname), std::size_t(255));
|
||||
write_uint8(hostlen, h); // hostname len
|
||||
std::memcpy(h, hostname, hostlen);
|
||||
h += hostlen;
|
||||
write_uint16(std::uint16_t(port), h);
|
||||
write_uint16(port, h);
|
||||
|
||||
std::array<boost::asio::const_buffer, 2> iovec;
|
||||
iovec[0] = boost::asio::const_buffer(header, h - header);
|
||||
iovec[0] = boost::asio::const_buffer(header, aux::numeric_cast<std::size_t>(h - header));
|
||||
iovec[1] = boost::asio::const_buffer(p.data(), p.size());
|
||||
|
||||
// set the DF flag for the socket and clear it again in the destructor
|
||||
|
@ -346,7 +347,7 @@ bool udp_socket::unwrap(udp::endpoint& from, span<char>& buf)
|
|||
using namespace libtorrent::detail;
|
||||
|
||||
// the minimum socks5 header size
|
||||
int const size = int(buf.size());
|
||||
int const size = aux::numeric_cast<int>(buf.size());
|
||||
if (size <= 10) return false;
|
||||
|
||||
char* p = buf.data();
|
||||
|
@ -381,7 +382,7 @@ bool udp_socket::unwrap(udp::endpoint& from, span<char>& buf)
|
|||
from = udp::endpoint(addr, read_uint16(p));
|
||||
}
|
||||
|
||||
buf = span<char>(p, size - (p - buf.data()));
|
||||
buf = {p, aux::numeric_cast<std::size_t>(size - (p - buf.data()))};
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -561,7 +562,8 @@ void socks5::on_connected(error_code const& e)
|
|||
}
|
||||
TORRENT_ASSERT_VAL(p - m_tmp_buf < int(sizeof(m_tmp_buf)), (p - m_tmp_buf));
|
||||
ADD_OUTSTANDING_ASYNC("socks5::on_handshake1");
|
||||
boost::asio::async_write(m_socks5_sock, boost::asio::buffer(m_tmp_buf, p - m_tmp_buf)
|
||||
boost::asio::async_write(m_socks5_sock, boost::asio::buffer(m_tmp_buf
|
||||
, aux::numeric_cast<std::size_t>(p - m_tmp_buf))
|
||||
, std::bind(&socks5::handshake1, self(), _1));
|
||||
}
|
||||
|
||||
|
@ -620,7 +622,8 @@ void socks5::handshake2(error_code const& e)
|
|||
write_string(m_proxy_settings.password, p);
|
||||
TORRENT_ASSERT_VAL(p - m_tmp_buf < int(sizeof(m_tmp_buf)), (p - m_tmp_buf));
|
||||
ADD_OUTSTANDING_ASYNC("socks5::on_handshake3");
|
||||
boost::asio::async_write(m_socks5_sock, boost::asio::buffer(m_tmp_buf, p - m_tmp_buf)
|
||||
boost::asio::async_write(m_socks5_sock
|
||||
, boost::asio::buffer(m_tmp_buf, aux::numeric_cast<std::size_t>(p - m_tmp_buf))
|
||||
, std::bind(&socks5::handshake3, self(), _1));
|
||||
}
|
||||
else
|
||||
|
@ -673,7 +676,8 @@ void socks5::socks_forward_udp()
|
|||
write_uint16(0, p); // :0
|
||||
TORRENT_ASSERT_VAL(p - m_tmp_buf < int(sizeof(m_tmp_buf)), (p - m_tmp_buf));
|
||||
ADD_OUTSTANDING_ASYNC("socks5::connect1");
|
||||
boost::asio::async_write(m_socks5_sock, boost::asio::buffer(m_tmp_buf, p - m_tmp_buf)
|
||||
boost::asio::async_write(m_socks5_sock
|
||||
, boost::asio::buffer(m_tmp_buf, aux::numeric_cast<std::size_t>(p - m_tmp_buf))
|
||||
, std::bind(&socks5::connect1, self(), _1));
|
||||
}
|
||||
|
||||
|
|
|
@ -123,11 +123,11 @@ namespace libtorrent { namespace
|
|||
TORRENT_ASSERT(hasher(m_metadata.get(), m_metadata_size).final()
|
||||
== m_torrent.torrent_file().info_hash());
|
||||
}
|
||||
return span<char const>(m_metadata.get(), m_metadata_size);
|
||||
return {m_metadata.get(), aux::numeric_cast<std::size_t>(m_metadata_size)};
|
||||
}
|
||||
|
||||
bool received_metadata(ut_metadata_peer_plugin& source
|
||||
, char const* buf, int size, int piece, int total_size);
|
||||
, char const* buf, int const size, int const piece, int const total_size);
|
||||
|
||||
// returns a piece of the metadata that
|
||||
// we should request.
|
||||
|
@ -174,7 +174,7 @@ namespace libtorrent { namespace
|
|||
// this vector keeps track of how many times each metadata
|
||||
// block has been requested and who we ended up getting it from
|
||||
// std::numeric_limits<int>::max() means we have the piece
|
||||
std::vector<metadata_piece> m_requested_metadata;
|
||||
aux::vector<metadata_piece> m_requested_metadata;
|
||||
|
||||
// explicitly disallow assignment, to silence msvc warning
|
||||
ut_metadata_plugin& operator=(ut_metadata_plugin const&);
|
||||
|
@ -382,7 +382,7 @@ namespace libtorrent { namespace
|
|||
|
||||
m_sent_requests.erase(i);
|
||||
entry const* total_size = msg.find_key("total_size");
|
||||
m_tp.received_metadata(*this, body.begin() + len, int(body.size() - len), piece
|
||||
m_tp.received_metadata(*this, body.begin() + len, int(body.size()) - int(len), piece
|
||||
, (total_size && total_size->type() == entry::int_t) ? int(total_size->integer()) : 0);
|
||||
maybe_send_request();
|
||||
}
|
||||
|
@ -500,7 +500,7 @@ namespace libtorrent { namespace
|
|||
i = m_requested_metadata.begin();
|
||||
}
|
||||
|
||||
int piece = int(i - m_requested_metadata.begin());
|
||||
int const piece = int(i - m_requested_metadata.begin());
|
||||
|
||||
// don't request the same block more than once every 3 seconds
|
||||
time_point now = aux::time_now();
|
||||
|
@ -521,7 +521,7 @@ namespace libtorrent { namespace
|
|||
|
||||
bool ut_metadata_plugin::received_metadata(
|
||||
ut_metadata_peer_plugin& source
|
||||
, char const* buf, int size, int piece, int total_size)
|
||||
, char const* buf, int const size, int const piece, int const total_size)
|
||||
{
|
||||
if (m_torrent.valid_metadata())
|
||||
{
|
||||
|
@ -551,7 +551,7 @@ namespace libtorrent { namespace
|
|||
m_metadata_size = total_size;
|
||||
}
|
||||
|
||||
if (piece < 0 || piece >= int(m_requested_metadata.size()))
|
||||
if (piece < 0 || piece >= m_requested_metadata.end_index())
|
||||
{
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
source.m_pc.peer_log(peer_log_alert::info, "UT_METADATA"
|
||||
|
@ -577,17 +577,17 @@ namespace libtorrent { namespace
|
|||
return false;
|
||||
}
|
||||
|
||||
std::memcpy(&m_metadata[piece * 16 * 1024], buf, size);
|
||||
std::memcpy(&m_metadata[piece * 16 * 1024], buf, aux::numeric_cast<std::size_t>(size));
|
||||
// mark this piece has 'have'
|
||||
m_requested_metadata[piece].num_requests = (std::numeric_limits<int>::max)();
|
||||
m_requested_metadata[piece].num_requests = std::numeric_limits<int>::max();
|
||||
m_requested_metadata[piece].source = source.shared_from_this();
|
||||
|
||||
bool have_all = std::all_of(m_requested_metadata.begin(), m_requested_metadata.end()
|
||||
, [](metadata_piece const& mp) -> bool { return mp.num_requests == (std::numeric_limits<int>::max)(); });
|
||||
, [](metadata_piece const& mp) { return mp.num_requests == std::numeric_limits<int>::max(); });
|
||||
|
||||
if (!have_all) return false;
|
||||
|
||||
if (!m_torrent.set_metadata({m_metadata.get(), size_t(m_metadata_size)}))
|
||||
if (!m_torrent.set_metadata({m_metadata.get(), aux::numeric_cast<std::size_t>(m_metadata_size)}))
|
||||
{
|
||||
if (!m_torrent.valid_metadata())
|
||||
{
|
||||
|
@ -599,11 +599,10 @@ namespace libtorrent { namespace
|
|||
// if we only have one block, and thus requested it from a single
|
||||
// peer, we bump up the retry time a lot more to try other peers
|
||||
bool single_peer = m_requested_metadata.size() == 1;
|
||||
for (int i = 0; i < int(m_requested_metadata.size()); ++i)
|
||||
for (auto& mp : m_requested_metadata)
|
||||
{
|
||||
m_requested_metadata[i].num_requests = 0;
|
||||
std::shared_ptr<ut_metadata_peer_plugin> peer
|
||||
= m_requested_metadata[i].source.lock();
|
||||
mp.num_requests = 0;
|
||||
auto peer = mp.source.lock();
|
||||
if (!peer) continue;
|
||||
|
||||
peer->failed_hash_check(single_peer ? now + minutes(5) : now);
|
||||
|
|
|
@ -261,7 +261,7 @@ piece_block_progress web_peer_connection::downloading_piece_progress() const
|
|||
// bounds. If the entire piece is downloaded, the block_index
|
||||
// would otherwise point to one past the end
|
||||
int correction = m_piece.size() ? -1 : 0;
|
||||
ret.block_index = int((m_requests.front().start + m_piece.size() + correction) / t->block_size());
|
||||
ret.block_index = (m_requests.front().start + int(m_piece.size()) + correction) / t->block_size();
|
||||
TORRENT_ASSERT(ret.block_index < int(piece_block::invalid.block_index));
|
||||
TORRENT_ASSERT(ret.piece_index < piece_block::invalid.piece_index);
|
||||
|
||||
|
@ -807,7 +807,7 @@ void web_peer_connection::on_receive(error_code const& error
|
|||
|
||||
m_server_string = get_peer_name(m_parser, m_host);
|
||||
|
||||
recv_buffer = recv_buffer.subspan(m_body_start);
|
||||
recv_buffer = recv_buffer.subspan(aux::numeric_cast<std::size_t>(m_body_start));
|
||||
|
||||
m_body_start = m_parser.body_start();
|
||||
m_received_body = 0;
|
||||
|
@ -889,7 +889,7 @@ void web_peer_connection::on_receive(error_code const& error
|
|||
}
|
||||
incoming_payload(recv_buffer.data(), copy_size);
|
||||
|
||||
recv_buffer = recv_buffer.subspan(copy_size);
|
||||
recv_buffer = recv_buffer.subspan(aux::numeric_cast<std::size_t>(copy_size));
|
||||
m_chunk_pos -= copy_size;
|
||||
|
||||
if (recv_buffer.size() == 0) goto done;
|
||||
|
@ -899,13 +899,13 @@ void web_peer_connection::on_receive(error_code const& error
|
|||
|
||||
int header_size = 0;
|
||||
std::int64_t chunk_size = 0;
|
||||
span<char const> chunk_start = recv_buffer.subspan(m_chunk_pos);
|
||||
span<char const> chunk_start = recv_buffer.subspan(aux::numeric_cast<std::size_t>(m_chunk_pos));
|
||||
TORRENT_ASSERT(chunk_start[0] == '\r'
|
||||
|| aux::is_hex({chunk_start.data(), 1}));
|
||||
bool const ret = m_parser.parse_chunk_header(chunk_start, &chunk_size, &header_size);
|
||||
if (!ret)
|
||||
{
|
||||
received_bytes(0, int(chunk_start.size() - m_partial_chunk_header));
|
||||
received_bytes(0, int(chunk_start.size()) - m_partial_chunk_header);
|
||||
m_partial_chunk_header = int(chunk_start.size());
|
||||
goto done;
|
||||
}
|
||||
|
@ -917,10 +917,10 @@ void web_peer_connection::on_receive(error_code const& error
|
|||
received_bytes(0, header_size - m_partial_chunk_header);
|
||||
m_partial_chunk_header = 0;
|
||||
TORRENT_ASSERT(chunk_size != 0
|
||||
|| int(chunk_start.size()) <= header_size || chunk_start[header_size] == 'H');
|
||||
|| int(chunk_start.size()) <= header_size || chunk_start[std::size_t(header_size)] == 'H');
|
||||
TORRENT_ASSERT(m_body_start + m_chunk_pos < INT_MAX);
|
||||
m_chunk_pos += int(chunk_size);
|
||||
recv_buffer = recv_buffer.subspan(header_size);
|
||||
recv_buffer = recv_buffer.subspan(aux::numeric_cast<std::size_t>(header_size));
|
||||
|
||||
// a chunk size of zero means the request is complete. Make sure the
|
||||
// number of payload bytes we've received matches the number we
|
||||
|
@ -930,7 +930,7 @@ void web_peer_connection::on_receive(error_code const& error
|
|||
TORRENT_ASSERT_VAL(m_chunk_pos == 0, m_chunk_pos);
|
||||
|
||||
#if TORRENT_USE_ASSERTS
|
||||
span<char const> chunk = recv_buffer.subspan(m_chunk_pos);
|
||||
span<char const> chunk = recv_buffer.subspan(aux::numeric_cast<std::size_t>(m_chunk_pos));
|
||||
TORRENT_ASSERT(chunk.size() == 0 || chunk[0] == 'H');
|
||||
#endif
|
||||
m_chunk_pos = -1;
|
||||
|
@ -975,7 +975,7 @@ void web_peer_connection::on_receive(error_code const& error
|
|||
int const copy_size = (std::min)(file_req.length - m_received_body
|
||||
, int(recv_buffer.size()));
|
||||
incoming_payload(recv_buffer.data(), copy_size);
|
||||
recv_buffer = recv_buffer.subspan(copy_size);
|
||||
recv_buffer = recv_buffer.subspan(aux::numeric_cast<std::size_t>(copy_size));
|
||||
|
||||
TORRENT_ASSERT(m_received_body <= file_req.length);
|
||||
if (m_received_body == file_req.length)
|
||||
|
@ -1031,7 +1031,7 @@ void web_peer_connection::incoming_payload(char const* buf, int len)
|
|||
// to not exceed the size of the next bittorrent request to be delivered.
|
||||
// m_piece can only hold the response for a single BT request at a time
|
||||
m_piece.resize(piece_size + copy_size);
|
||||
std::memcpy(m_piece.data() + piece_size, buf, copy_size);
|
||||
std::memcpy(m_piece.data() + piece_size, buf, aux::numeric_cast<std::size_t>(copy_size));
|
||||
len -= copy_size;
|
||||
buf += copy_size;
|
||||
|
||||
|
@ -1147,7 +1147,7 @@ void web_peer_connection::handle_padfile()
|
|||
TORRENT_ASSERT(int(m_piece.size()) < front_request.length);
|
||||
|
||||
int pad_size = int(std::min(file_size
|
||||
, std::int64_t(front_request.length - m_piece.size())));
|
||||
, front_request.length - std::int64_t(m_piece.size())));
|
||||
TORRENT_ASSERT(pad_size > 0);
|
||||
file_size -= pad_size;
|
||||
|
||||
|
|
Loading…
Reference in New Issue