The bitfield tracks which pieces of the transfer are already downloaded, and which aren’t. We therefore only need a structure which can track two states per value, which is why we use a bitarray library. In fact, we use only a subset of these functions, and rename a few to make them more expressive.
We also need to be able to translate the bitfield from/to the network where the high bit (right-most in network order) in the first byte corresponds to piece index 0.
Read more »
Bencoding values for the BitTorrent protocol is explained in a Wikipedia article on Bencoding or in the more general BitTorrent protocol page.
Briefly, there are four types :
- byte strings : [string length encoded in base ten ASCII]:[string data] ;
- integers : i[integer encoded in base ten ASCII]e ;
- lists : l[bencoded values]e ;
- and dictionaries : d[bencoded string][bencoded element]e.
Byte strings can be strings represented as byte strings (such as the description of the exchanged data), as well as an array of bytes (such as a hash). Representing them with the .Net strings can thus lead to errors. Hence we shall keep the byte array representation.
Integers can be longs, hence we represent them with int64.
Read more »