minor cleanup and refactor in peer connection
This commit is contained in:
parent
c672467331
commit
75f307c187
|
@ -787,7 +787,7 @@ namespace libtorrent
|
||||||
std::memcpy(ptr, version_string, string_len);
|
std::memcpy(ptr, version_string, string_len);
|
||||||
ptr += string_len;
|
ptr += string_len;
|
||||||
// 8 zeroes
|
// 8 zeroes
|
||||||
memset(ptr, 0, 8);
|
std::memset(ptr, 0, 8);
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_DHT
|
#ifndef TORRENT_DISABLE_DHT
|
||||||
// indicate that we support the DHT messages
|
// indicate that we support the DHT messages
|
||||||
|
@ -2717,16 +2717,16 @@ namespace libtorrent
|
||||||
{
|
{
|
||||||
received_bytes(0, int(bytes_transferred));
|
received_bytes(0, int(bytes_transferred));
|
||||||
|
|
||||||
std::size_t bytes_processed = int(recv_buffer.size()) - 20;
|
int const bytes_processed = int(recv_buffer.size()) - 20;
|
||||||
m_sync_bytes_read += int(bytes_processed);
|
m_sync_bytes_read += bytes_processed;
|
||||||
if (m_sync_bytes_read >= 512)
|
if (m_sync_bytes_read >= 512)
|
||||||
{
|
{
|
||||||
disconnect(errors::sync_hash_not_found, op_encryption, 1);
|
disconnect(errors::sync_hash_not_found, op_encryption, 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_recv_buffer.cut(int(bytes_processed), (std::min)(m_recv_buffer.packet_size()
|
m_recv_buffer.cut(bytes_processed, std::min(m_recv_buffer.packet_size()
|
||||||
, (512+20) - m_sync_bytes_read));
|
, (512 + 20) - m_sync_bytes_read));
|
||||||
|
|
||||||
TORRENT_ASSERT(!m_recv_buffer.packet_finished());
|
TORRENT_ASSERT(!m_recv_buffer.packet_finished());
|
||||||
return;
|
return;
|
||||||
|
@ -2734,11 +2734,11 @@ namespace libtorrent
|
||||||
// found complete sync
|
// found complete sync
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::size_t bytes_processed = syncoffset + 20;
|
int const bytes_processed = syncoffset + 20;
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
peer_log(peer_log_alert::info, "ENCRYPTION"
|
peer_log(peer_log_alert::info, "ENCRYPTION"
|
||||||
, "sync point (hash) found at offset %d"
|
, "sync point (hash) found at offset %d"
|
||||||
, int(m_sync_bytes_read + bytes_processed - 20));
|
, m_sync_bytes_read + bytes_processed - 20);
|
||||||
#endif
|
#endif
|
||||||
m_state = state_t::read_pe_skey_vc;
|
m_state = state_t::read_pe_skey_vc;
|
||||||
// skey,vc - 28 bytes
|
// skey,vc - 28 bytes
|
||||||
|
@ -2747,7 +2747,7 @@ namespace libtorrent
|
||||||
TORRENT_ASSERT(transferred_used <= int(bytes_transferred));
|
TORRENT_ASSERT(transferred_used <= int(bytes_transferred));
|
||||||
received_bytes(0, transferred_used);
|
received_bytes(0, transferred_used);
|
||||||
bytes_transferred -= transferred_used;
|
bytes_transferred -= transferred_used;
|
||||||
m_recv_buffer.cut(int(bytes_processed), 28);
|
m_recv_buffer.cut(bytes_processed, 28);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2854,8 +2854,8 @@ namespace libtorrent
|
||||||
// No sync
|
// No sync
|
||||||
if (syncoffset == -1)
|
if (syncoffset == -1)
|
||||||
{
|
{
|
||||||
std::size_t bytes_processed = int(recv_buffer.size()) - 8;
|
int const bytes_processed = int(recv_buffer.size()) - 8;
|
||||||
m_sync_bytes_read += int(bytes_processed);
|
m_sync_bytes_read += bytes_processed;
|
||||||
received_bytes(0, int(bytes_transferred));
|
received_bytes(0, int(bytes_transferred));
|
||||||
|
|
||||||
if (m_sync_bytes_read >= 512)
|
if (m_sync_bytes_read >= 512)
|
||||||
|
@ -2864,26 +2864,26 @@ namespace libtorrent
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_recv_buffer.cut(int(bytes_processed), (std::min)(m_recv_buffer.packet_size()
|
m_recv_buffer.cut(bytes_processed, std::min(m_recv_buffer.packet_size()
|
||||||
, (512+8) - m_sync_bytes_read));
|
, (512 + 8) - m_sync_bytes_read));
|
||||||
|
|
||||||
TORRENT_ASSERT(!m_recv_buffer.packet_finished());
|
TORRENT_ASSERT(!m_recv_buffer.packet_finished());
|
||||||
}
|
}
|
||||||
// found complete sync
|
// found complete sync
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::size_t bytes_processed = syncoffset + 8;
|
int const bytes_processed = syncoffset + 8;
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
peer_log(peer_log_alert::info, "ENCRYPTION"
|
peer_log(peer_log_alert::info, "ENCRYPTION"
|
||||||
, "sync point (verification constant) found at offset %d"
|
, "sync point (verification constant) found at offset %d"
|
||||||
, int(m_sync_bytes_read + bytes_processed - 8));
|
, m_sync_bytes_read + bytes_processed - 8);
|
||||||
#endif
|
#endif
|
||||||
int transferred_used = int(bytes_processed - int(recv_buffer.size()) + bytes_transferred);
|
int transferred_used = int(bytes_processed - int(recv_buffer.size()) + bytes_transferred);
|
||||||
TORRENT_ASSERT(transferred_used <= int(bytes_transferred));
|
TORRENT_ASSERT(transferred_used <= int(bytes_transferred));
|
||||||
received_bytes(0, transferred_used);
|
received_bytes(0, transferred_used);
|
||||||
bytes_transferred -= transferred_used;
|
bytes_transferred -= transferred_used;
|
||||||
|
|
||||||
m_recv_buffer.cut(int(bytes_processed), 4 + 2);
|
m_recv_buffer.cut(bytes_processed, 4 + 2);
|
||||||
|
|
||||||
// delete verification constant
|
// delete verification constant
|
||||||
m_sync_vc.reset();
|
m_sync_vc.reset();
|
||||||
|
|
|
@ -2507,10 +2507,9 @@ namespace libtorrent
|
||||||
m_receiving_block = b;
|
m_receiving_block = b;
|
||||||
|
|
||||||
bool in_req_queue = false;
|
bool in_req_queue = false;
|
||||||
for (std::vector<pending_block>::iterator i = m_download_queue.begin()
|
for (auto const& pb : m_download_queue)
|
||||||
, end(m_download_queue.end()); i != end; ++i)
|
|
||||||
{
|
{
|
||||||
if (i->block != b) continue;
|
if (pb.block != b) continue;
|
||||||
in_req_queue = true;
|
in_req_queue = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -2833,11 +2832,11 @@ namespace libtorrent
|
||||||
, std::bind(&peer_connection::on_disk_write_complete
|
, std::bind(&peer_connection::on_disk_write_complete
|
||||||
, self(), _1, p, t));
|
, self(), _1, p, t));
|
||||||
|
|
||||||
std::uint64_t const write_queue_size = m_counters.inc_stats_counter(
|
std::int64_t const write_queue_size = m_counters.inc_stats_counter(
|
||||||
counters::queued_write_bytes, p.length);
|
counters::queued_write_bytes, p.length);
|
||||||
m_outstanding_writing_bytes += p.length;
|
m_outstanding_writing_bytes += p.length;
|
||||||
|
|
||||||
std::uint64_t const max_queue_size = m_settings.get_int(
|
std::int64_t const max_queue_size = m_settings.get_int(
|
||||||
settings_pack::max_queued_disk_bytes);
|
settings_pack::max_queued_disk_bytes);
|
||||||
if (write_queue_size > max_queue_size
|
if (write_queue_size > max_queue_size
|
||||||
&& write_queue_size - p.length < max_queue_size
|
&& write_queue_size - p.length < max_queue_size
|
||||||
|
@ -4324,7 +4323,6 @@ namespace libtorrent
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_disconnecting = true;
|
m_disconnecting = true;
|
||||||
error_code e;
|
|
||||||
|
|
||||||
async_shutdown(*m_socket, m_socket);
|
async_shutdown(*m_socket, m_socket);
|
||||||
|
|
||||||
|
@ -4996,7 +4994,7 @@ namespace libtorrent
|
||||||
if (t->alerts().should_post<block_timeout_alert>())
|
if (t->alerts().should_post<block_timeout_alert>())
|
||||||
{
|
{
|
||||||
t->alerts().emplace_alert<block_timeout_alert>(t->get_handle()
|
t->alerts().emplace_alert<block_timeout_alert>(t->get_handle()
|
||||||
, remote(), pid(), int(qe.block.block_index)
|
, remote(), pid(), qe.block.block_index
|
||||||
, qe.block.piece_index);
|
, qe.block.piece_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5452,7 +5450,7 @@ namespace libtorrent
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
peer_log(peer_log_alert::outgoing, "CORKED_WRITE", "bytes: %d"
|
peer_log(peer_log_alert::outgoing, "CORKED_WRITE", "bytes: %d"
|
||||||
, int(m_send_buffer.size()));
|
, m_send_buffer.size());
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -5558,7 +5556,7 @@ namespace libtorrent
|
||||||
}
|
}
|
||||||
|
|
||||||
int const amount_to_send = std::min({
|
int const amount_to_send = std::min({
|
||||||
int(m_send_buffer.size())
|
m_send_buffer.size()
|
||||||
, quota_left
|
, quota_left
|
||||||
, m_send_barrier});
|
, m_send_barrier});
|
||||||
|
|
||||||
|
@ -6002,7 +6000,7 @@ namespace libtorrent
|
||||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||||
#endif
|
#endif
|
||||||
// add this RTT to the PRNG seed, to add more unpredictability
|
// add this RTT to the PRNG seed, to add more unpredictability
|
||||||
std::uint64_t now = total_microseconds(completed - m_connect);
|
std::int64_t now = total_microseconds(completed - m_connect);
|
||||||
// assume 12 bits of entropy (i.e. about 8 milliseconds)
|
// assume 12 bits of entropy (i.e. about 8 milliseconds)
|
||||||
RAND_add(&now, 8, 1.5);
|
RAND_add(&now, 8, 1.5);
|
||||||
#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO
|
#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO
|
||||||
|
@ -6553,7 +6551,7 @@ namespace libtorrent
|
||||||
// if m_num_pieces == 0, we probably don't have the
|
// if m_num_pieces == 0, we probably don't have the
|
||||||
// metadata yet.
|
// metadata yet.
|
||||||
std::shared_ptr<torrent> t = m_torrent.lock();
|
std::shared_ptr<torrent> t = m_torrent.lock();
|
||||||
return m_num_pieces == int(m_have_piece.size())
|
return m_num_pieces == m_have_piece.size()
|
||||||
&& m_num_pieces > 0 && t && t->valid_metadata();
|
&& m_num_pieces > 0 && t && t->valid_metadata();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue