Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Send HAVE_ALL oor HAVE_NONE to fast peers

  • Loading branch information...
commit 6c94f367a5eb31c3b51504b0d0d6e6aaf745b4dc 1 parent 936b140
@sanko authored
Showing with 15 additions and 2 deletions.
  1. +15 −2 lib/AnyEvent/BitTorrent.pm
View
17 lib/AnyEvent/BitTorrent.pm
@@ -727,7 +727,7 @@ sub _on_read_incoming {
$s->peers->{$h}{peerid} = $packet->{payload}[2];
$h->push_write(
build_handshake($s->reserved, $s->infohash, $s->peerid));
- $h->push_write(build_bitfield($s->bitfield));
+ $s->_send_bitfield($h);
$s->peers->{$h}{timeout}
= AE::timer(60, 0, sub { $s->_del_peer($h) });
$s->peers->{$h}{bitfield} = pack 'b*', (0 x $s->piece_count);
@@ -758,7 +758,7 @@ sub _on_read {
return $s->_del_peer($h)
if $packet->{payload}[1] ne $s->infohash;
$s->peers->{$h}{peerid} = $packet->{payload}[2];
- $h->push_write(build_bitfield($s->bitfield));
+ $s->_send_bitfield($h);
$s->peers->{$h}{timeout}
= AE::timer(60, 0, sub { $s->_del_peer($h) });
$s->peers->{$h}{bitfield} = pack 'b*', (0 x $s->piece_count);
@@ -936,6 +936,19 @@ sub _on_read {
}
}
+sub _send_bitfield {
+ my ($s, $h) = @_;
+ if (vec($s->peers->{$h}{reserved}, 7, 1) & 0x04) {
+ if ($s->seed) { return $h->push_write(build_haveall()) }
+ elsif ($s->bitfield() !~ m[[^\0]]) {
+ return $h->push_write(build_havenone());
+ }
+ }
+
+ # XXX - If it's cheaper to send HAVE packets than a full BITFIELD, do it
+ $h->push_write(build_bitfield($s->bitfield));
+}
+
sub _broadcast {
my ($s, $data, $qualifier) = @_;
$qualifier //= sub {1};
Please sign in to comment.
Something went wrong with that request. Please try again.