merge send_message utility functions in bt_peer_connections into a template

This commit is contained in:
arvidn 2017-04-18 20:02:20 -04:00 committed by Arvid Norberg
parent cdf066c4e1
commit 8eafd84273
7 changed files with 57 additions and 77 deletions

View File

@ -53,6 +53,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/piece_block_progress.hpp"
#include "libtorrent/config.hpp"
#include "libtorrent/pe_crypto.hpp"
#include "libtorrent/io.hpp"
namespace libtorrent {
@ -125,7 +126,7 @@ namespace libtorrent {
enum message_type
{
// standard messages
// standard messages
msg_choke = 0,
msg_unchoke,
msg_interested,
@ -272,14 +273,30 @@ namespace libtorrent {
#endif
private:
template <typename... Args>
void send_message(message_type const type
, counters::stats_counter_t const counter
, std::uint32_t flags
, Args... args)
{
TORRENT_ASSERT(m_sent_handshake);
TORRENT_ASSERT(m_sent_bitfield);
char msg[5 + sizeof...(Args) * 4]
= { 0,0,0,1 + sizeof...(Args) * 4, static_cast<char>(type) };
char* ptr = msg + 5;
TORRENT_UNUSED(ptr);
int tmp[] = {0, (detail::write_int32(args, ptr), 0)...};
TORRENT_UNUSED(tmp);
send_buffer(msg, sizeof(msg), flags);
stats_counters().inc_stats_counter(counter);
}
void write_dht_port();
void send_simple_message(message_type type,
counters::stats_counter_t counter);
void send_piece_message(message_type type,
counters::stats_counter_t counter, piece_index_t index);
void send_request_message(message_type type,
counters::stats_counter_t counter, peer_request const& r,
int flag);
bool dispatch_message(int received);
// returns the block currently being

View File

@ -75,10 +75,11 @@ namespace detail {
return static_cast<std::int8_t>(*start++);
}
template <class T, class In, class OutIt, typename Cond
= typename std::enable_if<std::is_integral<In>::value
|| std::is_enum<In>::value>::type>
inline void write_impl(In data, OutIt& start)
template <class T, class In, class OutIt>
typename std::enable_if<(std::is_integral<In>::value
&& !std::is_same<In, bool>::value)
|| std::is_enum<In>::value, void>::type
write_impl(In data, OutIt& start)
{
// Note: the test for [OutItT==void] below is necessary because
// in C++11 std::back_insert_iterator::value_type is void.
@ -97,9 +98,10 @@ namespace detail {
}
}
template <class T, class OutIt>
inline void write_impl(bool val, OutIt& start)
{ write_impl<std::uint8_t>(val ? 1 : 0, start); }
template <class T, class Val, class OutIt>
typename std::enable_if<std::is_same<Val, bool>::value, void>::type
write_impl(Val val, OutIt& start)
{ write_impl<T>(val ? 1 : 0, start); }
// -- adaptors

View File

@ -620,7 +620,7 @@ namespace aux {
virtual piece_block_progress downloading_piece_progress() const;
enum message_type_flags { message_type_request = 1 };
void send_buffer(char const* begin, int size, int flags = 0);
void send_buffer(char const* begin, int size, std::uint32_t flags = 0);
void setup_send();
template <typename Holder>

View File

@ -101,7 +101,7 @@ struct TORRENT_EXPORT peer_connection_handle
bool in_handshake() const;
void send_buffer(char const* begin, int size, int flags = 0);
void send_buffer(char const* begin, int size, std::uint32_t flags = 0);
time_t last_seen_complete() const;
time_point time_of_last_unchoke() const;

View File

@ -358,50 +358,6 @@ namespace libtorrent {
stats_counters().inc_stats_counter(counters::num_outgoing_dht_port);
}
void bt_peer_connection::send_piece_message(message_type const type
, counters::stats_counter_t const counter, piece_index_t const index)
{
TORRENT_ASSERT(m_sent_handshake);
TORRENT_ASSERT(m_sent_bitfield);
char msg[] = { 0,0,0,5, static_cast<char>(type), 0, 0, 0, 0 };
char* ptr = msg + 5;
detail::write_int32(static_cast<int>(index), ptr);
send_buffer(msg, sizeof(msg));
stats_counters().inc_stats_counter(counter);
}
void bt_peer_connection::send_simple_message(message_type const type
, counters::stats_counter_t const counter)
{
TORRENT_ASSERT(m_sent_handshake);
TORRENT_ASSERT(m_sent_bitfield);
char msg[] = { 0,0,0,1,static_cast<char>(type) };
send_buffer(msg, sizeof(msg));
stats_counters().inc_stats_counter(counter);
}
void bt_peer_connection::send_request_message(message_type const type
, counters::stats_counter_t const counter, peer_request const& r, int const flag)
{
TORRENT_ASSERT(m_sent_handshake);
TORRENT_ASSERT(m_sent_bitfield);
TORRENT_ASSERT(associated_torrent().lock()->valid_metadata());
char msg[17] = { 0,0,0,13, static_cast<char>(type) };
char* ptr = msg + 5;
detail::write_int32(static_cast<int>(r.piece), ptr); // index
detail::write_int32(r.start, ptr); // begin
detail::write_int32(r.length, ptr); // length
send_buffer(msg, sizeof(msg), flag);
stats_counters().inc_stats_counter(counter);
}
void bt_peer_connection::write_have_all()
{
INVARIANT_CHECK;
@ -410,7 +366,7 @@ namespace libtorrent {
#ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::outgoing_message, "HAVE_ALL");
#endif
send_simple_message(msg_have_all, counters::num_outgoing_have_all);
send_message(msg_have_all, counters::num_outgoing_have_all, 0);
}
void bt_peer_connection::write_have_none()
@ -420,7 +376,7 @@ namespace libtorrent {
#ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::outgoing_message, "HAVE_NONE");
#endif
send_simple_message(msg_have_none, counters::num_outgoing_have_none);
send_message(msg_have_none, counters::num_outgoing_have_none, 0);
}
void bt_peer_connection::write_reject_request(peer_request const& r)
@ -437,7 +393,8 @@ namespace libtorrent {
, r.start, r.length);
#endif
send_request_message(msg_reject_request, counters::num_outgoing_reject, r, 0);
send_message(msg_reject_request, counters::num_outgoing_reject, 0
, static_cast<int>(r.piece), r.start, r.length);
}
void bt_peer_connection::write_allow_fast(piece_index_t const piece)
@ -453,7 +410,8 @@ namespace libtorrent {
TORRENT_ASSERT(associated_torrent().lock()->valid_metadata());
send_piece_message(msg_allowed_fast, counters::num_outgoing_allowed_fast, piece);
send_message(msg_allowed_fast, counters::num_outgoing_allowed_fast, 0
, static_cast<int>(piece));
}
void bt_peer_connection::write_suggest(piece_index_t const piece)
@ -475,7 +433,8 @@ namespace libtorrent {
}
#endif
send_piece_message(msg_suggest_piece, counters::num_outgoing_suggest, piece);
send_message(msg_suggest_piece, counters::num_outgoing_suggest, 0
, static_cast<int>(piece));
}
void bt_peer_connection::get_specific_peer_info(peer_info& p) const
@ -2046,17 +2005,18 @@ namespace libtorrent {
{
INVARIANT_CHECK;
send_request_message(msg_cancel, counters::num_outgoing_cancel, r, 0);
send_message(msg_cancel, counters::num_outgoing_cancel, 0
, static_cast<int>(r.piece), r.start, r.length);
if (!m_supports_fast)
incoming_reject_request(r);
if (!m_supports_fast) incoming_reject_request(r);
}
void bt_peer_connection::write_request(peer_request const& r)
{
INVARIANT_CHECK;
send_request_message(msg_request, counters::num_outgoing_request, r, message_type_request);
send_message(msg_request, counters::num_outgoing_request, message_type_request
, static_cast<int>(r.piece), r.start, r.length);
}
void bt_peer_connection::write_bitfield()
@ -2292,14 +2252,14 @@ namespace libtorrent {
INVARIANT_CHECK;
if (is_choked()) return;
send_simple_message(msg_choke, counters::num_outgoing_choke);
send_message(msg_choke, counters::num_outgoing_choke, 0);
}
void bt_peer_connection::write_unchoke()
{
INVARIANT_CHECK;
send_simple_message(msg_unchoke, counters::num_outgoing_unchoke);
send_message(msg_unchoke, counters::num_outgoing_unchoke, 0);
#ifndef TORRENT_DISABLE_EXTENSIONS
for (auto const& e : m_extensions)
@ -2313,14 +2273,14 @@ namespace libtorrent {
{
INVARIANT_CHECK;
send_simple_message(msg_interested, counters::num_outgoing_interested);
send_message(msg_interested, counters::num_outgoing_interested, 0);
}
void bt_peer_connection::write_not_interested()
{
INVARIANT_CHECK;
send_simple_message(msg_not_interested, counters::num_outgoing_not_interested);
send_message(msg_not_interested, counters::num_outgoing_not_interested, 0);
}
void bt_peer_connection::write_have(piece_index_t index)
@ -2334,7 +2294,8 @@ namespace libtorrent {
// there instead
if (!m_sent_bitfield) return;
send_piece_message(msg_have, counters::num_outgoing_have, index);
send_message(msg_have, counters::num_outgoing_have, 0
, static_cast<int>(index));
}
void bt_peer_connection::write_dont_have(piece_index_t const index)

View File

@ -5655,7 +5655,7 @@ namespace libtorrent {
return piece_block_progress();
}
void peer_connection::send_buffer(char const* buf, int size, int flags)
void peer_connection::send_buffer(char const* buf, int size, std::uint32_t flags)
{
TORRENT_ASSERT(is_single_thread());
TORRENT_UNUSED(flags);

View File

@ -265,7 +265,7 @@ bool peer_connection_handle::in_handshake() const
return pc->in_handshake();
}
void peer_connection_handle::send_buffer(char const* begin, int size, int flags)
void peer_connection_handle::send_buffer(char const* begin, int size, std::uint32_t flags)
{
std::shared_ptr<peer_connection> pc = native_handle();
TORRENT_ASSERT(pc);