protected function sendUpdate() { $query = http_build_query(array('info_hash' => $this->download->getTorrent()->getInfoHash(), 'peer_id' => $this->download->getPeerID(), 'downloaded' => $this->download->getDownloaded(), 'uploaded' => $this->download->getUploaded(), 'left' => $this->download->getRemaining(), 'port' => 0, 'compact' => 1)); $response = file_get_contents("{$this->url}?{$query}"); if ($response === false || strlen($response) <= 0) { $this->error = "No HTTP response"; return; } try { $response = Bencode::decode($response); } catch (Exception $e) { trigger_error($e); $this->error = "Bencoding error"; return; } var_dump($response); if (empty($response)) { return; } $this->setNext(time() + (int) $response['interval']); if (empty($response['peers'])) { return; } if (is_string($response['peers'])) { $response['peers'] = $this->decodePeerList($response['peers']); } foreach ($response['peers'] as $item) { $this->download->addPeer($item['ip'], $item['port']); } }
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 string $msg 要解码的数据 * @return mixed 解码后的数据 */ public static function decode($msg) { return Bencode::decode($msg); }
/** * Shorthand for {@link Bencode::decode()}. * * @param string * @return mixed * @throws Exception * @see Bencode::decode() */ function bdecode($value) { return Bencode::decode($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; } } }