Example #1
0
 /**
  * Sending initial bitfield to the clint
  * letting it know that we have to entire file.
  *
  * The bitfield looks like:
  * [11111111-11111111-11100000]
  * Meaning that we have all the 19 pieces (padding bits must be 0).
  *
  * @param Client $client Client
  *
  * @return void
  */
 protected function sendBitField(Client $client)
 {
     $n_pieces = ceil($client->getTorrent()->length / $client->getTorrent()->size_piece);
     $message = pack('C', 5);
     while ($n_pieces > 0) {
         if ($n_pieces >= 8) {
             $message .= pack('C', 255);
             $n_pieces -= 8;
         } else {
             // Last byte of the bitfield, like 11100000.
             $message .= pack('C', 256 - pow(2, 8 - $n_pieces));
             $n_pieces = 0;
         }
     }
     $client->socketWrite(pack('N', strlen($message)) . $message);
 }