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); } }
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); }
/** * bencode编码 * @param mixed $msg 要编码的数据 * @return string 编码后的数据 */ public static function encode($msg) { return Bencode::encode($msg); }
/** * Shorthand for {@link Bencode::encode()}. * * @param mixed * @return string * @throws Exception * @see Bencode::encode() */ function bencode($value) { return Bencode::encode($value); }
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; } } }