void TryAdvertisePiece(ChokeData data)
{
// If we are seeding to this peer and we have a peer waiting to unchoke
// don't advertise more data
if (!data.Peer.AmChoking && PendingUnchoke)
return;
int advertised = advertisedPieces.FindAll(delegate(SeededPiece p) { return p.Peer == data.Peer; }).Count;
int max = MaxAdvertised;
if (manager.UploadingTo < manager.Settings.UploadSlots)
max = MaxAdvertised;
else if (data.ShareRatio < 0.25)
max = 1;
else if (data.ShareRatio < 0.35)
max = 2;
else if (data.ShareRatio < 0.50)
max = 3;
else
max = MaxAdvertised;
if (advertised >= max)
return;
// List of pieces *not* in the swarm
temp.From(bitfield).Not();
// List of pieces that he wants that aren't in the swarm
temp.NAnd(data.Peer.BitField);
// Ignore all the pieces we've already started sharing
foreach (SeededPiece p in advertisedPieces)
temp[p.Index] = false;
int index = 0;
while (advertised < max)
{
// Get the index of the first piece we can send him
index = temp.FirstTrue(index, temp.Length);
// Looks like he's not interested in us...
if (index == -1)
return;
advertised++;
data.TotalPieces++;
data.CurrentPieces[index] = true;
advertisedPieces.Add(new SeededPiece(data.Peer, index, data.Peer.TorrentManager.Torrent.PieceLength / Piece.BlockSize));
data.Peer.Enqueue(new HaveMessage(index));
index++;
}
}