h1. Creating a custom PiecePicker [[Creating a custom PiecePicker#Example-1|Example 1]] Creating a picker to prioritise the first piece in a file h2. Example 1
public class MyPicker : PiecePicker {
public MyPicker (PiecePicker basePicker)
: base (basePicker)
{
}
public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List otherPeers, int count, int startIndex, int endIndex)
{
// If we do not have the first piece and the other peer does have it
if (!HaveFirstPieceAlready && peerBitfield[0] == true) {
BitField bf = new BitField (peerBitfield.Length);
bf [0] = true;
MessageBundle msg = base.PickPiece (id, bf, otherPeers, count, startIndex, endIndex);
if (msg != null)
return msg;
}
return base.PickPiece (id, peerBitfield, otherPeers, count, startIndex, endIndex);
}
}
// This picker can then be added to the default picking pipeline by doing:
torrentManager.ChangePicker (new MyPicker (torrentManager.CreateDefaultPicker ()));