Posts Tagged ‘p2p’

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 »

Once the Metainfo file can be read, the client ought to know which files compose the transfer, and how to connect to sources for the transfer. In this post, we will define the elements we need to perform the I/O operations, that is, to read data from (and write it to) the disk.
Read more »

To exchange files, a BitTorrent client needs some information about the exchanged file(s). This information is contained within a file named the “metainfo” file.

More details are explaind in the Metainfo File Structure of the BitTorrent specification web page.

In short, the structure is a UTF-8 bencoded dictionary containing some required and optional key/value pairs.

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 »

We had previously given a way to implement two-way traffic control on a strict basis using a function which looped constantly to see if any data was available o read/write, and which processed it accordingly.

The following approach is based on a per-request basis. That is, an actors asks for n tokens to read (or write), and the manager sends it an answer when the actor is allowed perform the action. In effect, if the actor asks for 100 bytes and only 50 bytes can be exchanged per second, it will get a green light only after two seconds have elapsed.

Read more »

The following code allows us to measure (and keep track of) the exchange rate (whether upload or download), and the time needed to exchange n additional bytes (assuming the rate is stable).

Read more »

This post describes how to implement two-way (TCP for instance) traffic control in F# by modifying slightly a Trolltech article available at

Read more »