Skip to content

Commit d22c44c

Browse files
committed
fix kludge and down-cast to send upload_only. Promote upload-only to a top-level peer feature
1 parent 1d9b10a commit d22c44c

6 files changed

+55
-32
lines changed

include/libtorrent/bt_peer_connection.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ namespace libtorrent {
242242
void write_piece(peer_request const& r, disk_buffer_holder buffer) override;
243243
void write_keepalive() override;
244244
void write_handshake();
245+
void write_upload_only(bool enabled) override;
245246
#ifndef TORRENT_DISABLE_EXTENSIONS
246247
void write_extensions();
247-
void write_upload_only();
248248
void write_share_mode();
249249
void write_holepunch_msg(int type, tcp::endpoint const& ep, int error);
250250
#endif

include/libtorrent/peer_connection.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ namespace aux {
576576
void send_interested();
577577
void send_not_interested();
578578
void send_suggest(piece_index_t piece);
579+
void send_upload_only(bool enabled);
579580

580581
void snub_peer();
581582
// reject any request in the request
@@ -706,6 +707,7 @@ namespace aux {
706707

707708
virtual void write_reject_request(peer_request const& r) = 0;
708709
virtual void write_allow_fast(piece_index_t piece) = 0;
710+
virtual void write_upload_only(bool enabled) = 0;
709711

710712
virtual void on_connected() = 0;
711713
virtual void on_tick() {}

include/libtorrent/web_connection_base.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ namespace libtorrent {
9393
void write_allow_fast(piece_index_t) override {}
9494
void write_suggest(piece_index_t) override {}
9595
void write_bitfield() override {}
96+
void write_upload_only(bool) override {}
9697

9798
#if TORRENT_USE_INVARIANT_CHECKS
9899
void check_invariant() const;

src/bt_peer_connection.cpp

+17-19
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,17 @@ namespace {
313313
// connections that are still in the handshake
314314
// will send their bitfield when the handshake
315315
// is done
316+
std::shared_ptr<torrent> t = associated_torrent().lock();
316317
#ifndef TORRENT_DISABLE_EXTENSIONS
317-
write_upload_only();
318+
if (!t->share_mode())
319+
{
320+
bool const upload_only_enabled = t->is_upload_only() && !t->super_seeding();
321+
send_upload_only(upload_only_enabled);
322+
}
318323
#endif
319324

320325
if (m_sent_bitfield) return;
321326

322-
std::shared_ptr<torrent> t = associated_torrent().lock();
323327
TORRENT_ASSERT(t);
324328
write_bitfield();
325329
TORRENT_ASSERT(m_sent_bitfield);
@@ -1902,42 +1906,36 @@ namespace {
19021906
return finished;
19031907
}
19041908

1905-
#ifndef TORRENT_DISABLE_EXTENSIONS
1906-
void bt_peer_connection::write_upload_only()
1909+
void bt_peer_connection::write_upload_only(bool const enabled)
19071910
{
1911+
#ifndef TORRENT_DISABLE_EXTENSIONS
19081912
INVARIANT_CHECK;
19091913

1914+
#if TORRENT_USE_ASSERTS
19101915
std::shared_ptr<torrent> t = associated_torrent().lock();
1916+
TORRENT_ASSERT(!t->share_mode());
1917+
#endif
1918+
19111919
if (m_upload_only_id == 0) return;
1912-
if (t->share_mode()) return;
19131920

19141921
// if we send upload-only, the other end is very likely to disconnect
19151922
// us, at least if it's a seed. If we don't want to close redundant
19161923
// connections, don't sent upload-only
19171924
if (!m_settings.get_bool(settings_pack::close_redundant_connections)) return;
19181925

1919-
#ifndef TORRENT_DISABLE_LOGGING
1920-
if (should_log(peer_log_alert::outgoing_message))
1921-
{
1922-
peer_log(peer_log_alert::outgoing_message, "UPLOAD_ONLY", "%d"
1923-
, int(t->is_upload_only() && !t->super_seeding()));
1924-
}
1925-
#endif
1926-
19271926
char msg[7] = {0, 0, 0, 3, msg_extended};
19281927
char* ptr = msg + 5;
19291928
detail::write_uint8(m_upload_only_id, ptr);
1930-
// if we're super seeding, we don't want to make peers
1931-
// think that we only have a single piece and is upload
1932-
// only, since they might disconnect immediately when
1933-
// they have downloaded a single piece, although we'll
1934-
// make another piece available
1935-
detail::write_uint8(t->is_upload_only() && !t->super_seeding(), ptr);
1929+
detail::write_uint8(enabled, ptr);
19361930
send_buffer(msg);
19371931

19381932
stats_counters().inc_stats_counter(counters::num_outgoing_extended);
1933+
#else
1934+
TORRENT_UNUSED(enabled);
1935+
#endif
19391936
}
19401937

1938+
#ifndef TORRENT_DISABLE_EXTENSIONS
19411939
void bt_peer_connection::write_share_mode()
19421940
{
19431941
INVARIANT_CHECK;

src/peer_connection.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -3764,10 +3764,29 @@ namespace libtorrent {
37643764
m_became_uninteresting = aux::time_now();
37653765

37663766
#ifndef TORRENT_DISABLE_LOGGING
3767-
peer_log(peer_log_alert::outgoing_message, "NOT_INTERESTED");
3767+
if (should_log(peer_log_alert::outgoing_message))
3768+
{
3769+
peer_log(peer_log_alert::outgoing_message, "NOT_INTERESTED");
3770+
}
37683771
#endif
37693772
}
37703773

3774+
void peer_connection::send_upload_only(bool const enabled)
3775+
{
3776+
TORRENT_ASSERT(is_single_thread());
3777+
if (m_connecting || in_handshake()) return;
3778+
3779+
#ifndef TORRENT_DISABLE_LOGGING
3780+
if (should_log(peer_log_alert::outgoing_message))
3781+
{
3782+
peer_log(peer_log_alert::outgoing_message, "UPLOAD_ONLY", "%d"
3783+
, int(enabled));
3784+
}
3785+
#endif
3786+
3787+
write_upload_only(enabled);
3788+
}
3789+
37713790
void peer_connection::send_piece_suggestions(int const num)
37723791
{
37733792
std::shared_ptr<torrent> t = m_torrent.lock();

src/torrent.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -879,21 +879,24 @@ namespace libtorrent {
879879
if (share_mode()) return;
880880
if (super_seeding()) return;
881881

882+
// if we send upload-only, the other end is very likely to disconnect
883+
// us, at least if it's a seed. If we don't want to close redundant
884+
// connections, don't sent upload-only
885+
if (!settings().get_bool(settings_pack::close_redundant_connections)) return;
886+
887+
// if we're super seeding, we don't want to make peers
888+
// think that we only have a single piece and is upload
889+
// only, since they might disconnect immediately when
890+
// they have downloaded a single piece, although we'll
891+
// make another piece available
892+
bool const upload_only_enabled = is_upload_only() && !super_seeding();
893+
882894
for (auto p : m_connections)
883895
{
884896
TORRENT_INCREMENT(m_iterating_connections);
885-
if (p->type() != connection_type::bittorrent)
886-
continue;
887897

888-
// TODO: 3 the general peer interface should have a hook to be notified
889-
// of upload only mode to avoid this downcast
890-
bt_peer_connection* btp = static_cast<bt_peer_connection*>(p);
891-
std::shared_ptr<peer_connection> me(btp->self());
892-
if (!btp->is_disconnecting())
893-
{
894-
btp->send_not_interested();
895-
btp->write_upload_only();
896-
}
898+
p->send_not_interested();
899+
p->send_upload_only(upload_only_enabled);
897900
}
898901
#endif
899902
}

0 commit comments

Comments
 (0)