Exemplo n.º 1
0
 public function send()
 {
     $response = array();
     if ($this->failure_reason) {
         $response['failure reason'] = $this->failure_reason;
         echo Bencode::encode($response);
     } else {
         $response['interval'] = $this->interval;
         $response['complete'] = $this->complete;
         $response['incomplete'] = $this->incomplete;
         $response['peers'] = $this->peers;
         echo Bencode::encode($response);
     }
 }
Exemplo n.º 2
0
 private function torrent_data_extractor($str)
 {
     $TorrentProcessor = new Bencode();
     try {
         $result = $TorrentProcessor->decode($str);
     } catch (Exception $e) {
         throw new TorrentInvalidError($e->getMessage());
     }
     $size = 0;
     if ($result['info']['length']) {
         $size = $result['info']['length'];
     } else {
         foreach ($result['info']['files'] as $file) {
             $size += $file['length'];
         }
     }
     // end sum files
     $infohash = sha1($TorrentProcessor->encode($result['info']));
     $filecount = 1;
     if ($result['info']['files']) {
         $filecount = count($result['info']['files']);
     }
     return array('name' => $result['info']['name'], 'size' => $size, 'infohash' => $infohash, 'files' => $filecount);
 }
Exemplo n.º 3
0
 /**
  * bencode编码
  * @param  mixed $msg 要编码的数据
  * @return   string 编码后的数据
  */
 public static function encode($msg)
 {
     return Bencode::encode($msg);
 }
Exemplo n.º 4
0
/**
 * Shorthand for {@link Bencode::encode()}.
 *
 * @param   mixed
 * @return  string
 * @throws  Exception
 * @see     Bencode::encode()
 */
function bencode($value)
{
    return Bencode::encode($value);
}
Exemplo n.º 5
0
 function __construct($password = NULL, $host = "127.0.0.1", $port = 10010)
 {
     $this->socket = stream_socket_client("udp://" . $host . ":" . $port, $errorno, $errorstr);
     if (!$this->socket) {
         die("Failed to connect, Error #{$errorno}: {$errorstr}");
     }
     fwrite($this->socket, Bencode::encode(array("q" => "ping")));
     // Try to ping it
     $returndata = fread($this->socket, $this->buffersize);
     if (!endsWith($returndata, "1:q4:ponge")) {
         die("{$returndata}");
     }
     $this->password = $password;
     $page = 0;
     while (True) {
         $request = array("q" => "Admin_availableFunctions", "args" => array("page" => $page));
         fwrite($this->socket, Bencode::encode($request));
         $result = Bencode::decode(fread($this->socket, $this->buffersize));
         foreach ($result['availableFunctions'] as $function => $description) {
             $this->functions[$function] = $description;
         }
         if (isset($result['more'])) {
             $page++;
         } else {
             break;
         }
     }
 }