updated examples page

This commit is contained in:
Arvid Norberg
2008-08-09 07:34:12 +00:00
parent 8488d5eb59
commit cd71f3fb9b
2 changed files with 200 additions and 177 deletions

View File

@@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" /> <meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/" />
<title>libtorrent Examples</title> <title>libtorrent Examples</title>
<meta name="author" content="Arvid Norberg, arvid&#64;rasterbar.com" /> <meta name="author" content="Arvid Norberg, arvid&#64;rasterbar.com" />
<link rel="stylesheet" href="style.css" type="text/css" /> <link rel="stylesheet" href="style.css" type="text/css" />
@@ -49,27 +49,48 @@ print information about it to std out:</p>
#include &quot;libtorrent/entry.hpp&quot; #include &quot;libtorrent/entry.hpp&quot;
#include &quot;libtorrent/bencode.hpp&quot; #include &quot;libtorrent/bencode.hpp&quot;
#include &quot;libtorrent/torrent_info.hpp&quot; #include &quot;libtorrent/torrent_info.hpp&quot;
#include &quot;libtorrent/lazy_entry.hpp&quot;
#include &lt;boost/filesystem/operations.hpp&gt;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
using namespace libtorrent; using namespace libtorrent;
using namespace boost::filesystem;
if (argc != 2) if (argc != 2)
{ {
std::cerr &lt;&lt; &quot;usage: dump_torrent torrent-file\n&quot;; std::cerr &lt;&lt; &quot;usage: dump_torrent torrent-file\n&quot;;
return 1; return 1;
} }
#if BOOST_VERSION &lt; 103400
boost::filesystem::path::default_name_check(boost::filesystem::no_check);
#endif
#ifndef BOOST_NO_EXCEPTIONS
try try
{ {
std::ifstream in(argv[1], std::ios_base::binary); #endif
in.unsetf(std::ios_base::skipws);
entry e = bdecode(std::istream_iterator&lt;char&gt;(in) int size = file_size(argv[1]);
, std::istream_iterator&lt;char&gt;()); if (size &gt; 10 * 1000000)
{
std::cerr &lt;&lt; &quot;file too big (&quot; &lt;&lt; size &lt;&lt; &quot;), aborting\n&quot;;
return 1;
}
std::vector&lt;char&gt; buf(size);
std::ifstream(argv[1], std::ios_base::binary).read(&amp;buf[0], size);
lazy_entry e;
int ret = lazy_bdecode(&amp;buf[0], &amp;buf[0] + buf.size(), e);
if (ret != 0)
{
std::cerr &lt;&lt; &quot;invalid bencoding: &quot; &lt;&lt; ret &lt;&lt; std::endl;
return 1;
}
std::cout &lt;&lt; &quot;\n\n----- raw info -----\n\n&quot;; std::cout &lt;&lt; &quot;\n\n----- raw info -----\n\n&quot;;
e.print(std::cout); std::cout &lt;&lt; e &lt;&lt; std::endl;
torrent_info t(e); torrent_info t(e);
@@ -83,10 +104,9 @@ int main(int argc, char* argv[])
{ {
std::cout &lt;&lt; i-&gt;first &lt;&lt; &quot;:&quot; &lt;&lt; i-&gt;second &lt;&lt; &quot;\n&quot;; std::cout &lt;&lt; i-&gt;first &lt;&lt; &quot;:&quot; &lt;&lt; i-&gt;second &lt;&lt; &quot;\n&quot;;
} }
std::cout &lt;&lt; &quot;trackers:\n&quot;; std::cout &lt;&lt; &quot;trackers:\n&quot;;
for (std::vector&lt;announce_entry&gt;::const_iterator i for (std::vector&lt;announce_entry&gt;::const_iterator i = t.trackers().begin();
= t.trackers().begin(); i != t.trackers().end(); ++i) i != t.trackers().end(); ++i)
{ {
std::cout &lt;&lt; i-&gt;tier &lt;&lt; &quot;: &quot; &lt;&lt; i-&gt;url &lt;&lt; &quot;\n&quot;; std::cout &lt;&lt; i-&gt;tier &lt;&lt; &quot;: &quot; &lt;&lt; i-&gt;url &lt;&lt; &quot;\n&quot;;
} }
@@ -97,18 +117,24 @@ int main(int argc, char* argv[])
std::cout &lt;&lt; &quot;comment: &quot; &lt;&lt; t.comment() &lt;&lt; &quot;\n&quot;; std::cout &lt;&lt; &quot;comment: &quot; &lt;&lt; t.comment() &lt;&lt; &quot;\n&quot;;
std::cout &lt;&lt; &quot;created by: &quot; &lt;&lt; t.creator() &lt;&lt; &quot;\n&quot;; std::cout &lt;&lt; &quot;created by: &quot; &lt;&lt; t.creator() &lt;&lt; &quot;\n&quot;;
std::cout &lt;&lt; &quot;files:\n&quot;; std::cout &lt;&lt; &quot;files:\n&quot;;
int index = 0;
for (torrent_info::file_iterator i = t.begin_files(); for (torrent_info::file_iterator i = t.begin_files();
i != t.end_files(); ++i) i != t.end_files(); ++i, ++index)
{ {
int first = t.map_file(index, 0, 1).piece;
int last = t.map_file(index, i-&gt;size - 1, 1).piece;
std::cout &lt;&lt; &quot; &quot; &lt;&lt; std::setw(11) &lt;&lt; i-&gt;size std::cout &lt;&lt; &quot; &quot; &lt;&lt; std::setw(11) &lt;&lt; i-&gt;size
&lt;&lt; &quot; &quot; &lt;&lt; i-&gt;path.string() &lt;&lt; &quot;\n&quot;; &lt;&lt; &quot; &quot; &lt;&lt; i-&gt;path.string() &lt;&lt; &quot;[ &quot; &lt;&lt; first &lt;&lt; &quot;, &quot;
&lt;&lt; last &lt;&lt; &quot; ]\n&quot;;
} }
#ifndef BOOST_NO_EXCEPTIONS
} }
catch (std::exception&amp; e) catch (std::exception&amp; e)
{ {
std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;; std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
} }
#endif
return 0; return 0;
} }
@@ -118,49 +144,43 @@ int main(int argc, char* argv[])
<h2><a id="simple-client" name="simple-client">simple client</a></h2> <h2><a id="simple-client" name="simple-client">simple client</a></h2>
<p>This is a simple client. It doesn't have much output to keep it simple:</p> <p>This is a simple client. It doesn't have much output to keep it simple:</p>
<pre class="literal-block"> <pre class="literal-block">
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;iterator&gt;
#include &lt;exception&gt;
#include &lt;boost/format.hpp&gt;
#include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
#include &quot;libtorrent/entry.hpp&quot;
#include &quot;libtorrent/bencode.hpp&quot;
#include &quot;libtorrent/session.hpp&quot;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
using namespace libtorrent; using namespace libtorrent;
#if BOOST_VERSION &lt; 103400
namespace fs = boost::filesystem;
fs::path::default_name_check(fs::no_check);
#endif
if (argc != 2) if (argc != 2)
{ {
std::cerr &lt;&lt; &quot;usage: ./simple_cient torrent-file\n&quot; std::cerr &lt;&lt; &quot;usage: ./simple_client torrent-file\n&quot;
&quot;to stop the client, press return.\n&quot;; &quot;to stop the client, press return.\n&quot;;
return 1; return 1;
} }
#ifndef BOOST_NO_EXCEPTIONS
try try
#endif
{ {
session s; session s;
s.listen_on(std::make_pair(6881, 6889)); s.listen_on(std::make_pair(6881, 6889));
add_torrent_params p;
std::ifstream in(argv[1], std::ios_base::binary); p.save_path = &quot;./&quot;;
in.unsetf(std::ios_base::skipws); p.ti = new torrent_info(argv[1]);
entry e = bdecode(std::istream_iterator&lt;char&gt;(in) s.add_torrent(p);
, std::istream_iterator&lt;char&gt;());
s.add_torrent(torrent_info(e), &quot;&quot;);
// wait for the user to end // wait for the user to end
char a; char a;
std::cin.unsetf(std::ios_base::skipws); std::cin.unsetf(std::ios_base::skipws);
std::cin &gt;&gt; a; std::cin &gt;&gt; a;
} }
#ifndef BOOST_NO_EXCEPTIONS
catch (std::exception&amp; e) catch (std::exception&amp; e)
{ {
std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;; std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
} }
#endif
return 0; return 0;
} }
</pre> </pre>
@@ -180,31 +200,28 @@ int main(int argc, char* argv[])
#include &quot;libtorrent/file.hpp&quot; #include &quot;libtorrent/file.hpp&quot;
#include &quot;libtorrent/storage.hpp&quot; #include &quot;libtorrent/storage.hpp&quot;
#include &quot;libtorrent/hasher.hpp&quot; #include &quot;libtorrent/hasher.hpp&quot;
#include &quot;libtorrent/file_pool.hpp&quot; #include &quot;libtorrent/create_torrent.hpp&quot;
#include &lt;boost/filesystem/operations.hpp&gt; #include &lt;boost/filesystem/operations.hpp&gt;
#include &lt;boost/filesystem/path.hpp&gt; #include &lt;boost/filesystem/path.hpp&gt;
#include &lt;boost/filesystem/fstream.hpp&gt; #include &lt;boost/filesystem/fstream.hpp&gt;
#include &lt;boost/bind.hpp&gt;
using namespace boost::filesystem; using namespace boost::filesystem;
using namespace libtorrent; using namespace libtorrent;
void add_files( // do not include files and folders whose
torrent_info&amp; t // name starts with a .
, path const&amp; p bool file_filter(boost::filesystem::path const&amp; filename)
, path const&amp; l)
{ {
path f(p / l); if (filename.leaf()[0] == '.') return false;
if (is_directory(f)) std::cerr &lt;&lt; filename &lt;&lt; std::endl;
{ return true;
for (directory_iterator i(f), end; i != end; ++i) }
add_files(t, p, l / i-&gt;leaf());
} void print_progress(int i, int num)
else {
{ std::cerr &lt;&lt; &quot;\r&quot; &lt;&lt; (i+1) &lt;&lt; &quot;/&quot; &lt;&lt; num;
std::cerr &lt;&lt; &quot;adding \&quot;&quot; &lt;&lt; l.string() &lt;&lt; &quot;\&quot;\n&quot;;
t.add_file(l, file_size(f));
}
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
@@ -212,52 +229,48 @@ int main(int argc, char* argv[])
using namespace libtorrent; using namespace libtorrent;
using namespace boost::filesystem; using namespace boost::filesystem;
path::default_name_check(no_check);
if (argc != 4)
{
std::cerr &lt;&lt; &quot;usage: make_torrent &lt;output torrent-file&gt; &quot;
&quot;&lt;announce url&gt; &lt;file or directory to create torrent from&gt;\n&quot;;
return 1;
}
try
{
torrent_info t;
path full_path = complete(path(argv[3]));
ofstream out(complete(path(argv[1])), std::ios_base::binary);
int piece_size = 256 * 1024; int piece_size = 256 * 1024;
char const* creator_str = &quot;libtorrent&quot;; char const* creator_str = &quot;libtorrent&quot;;
add_files(t, full_path.branch_path(), full_path.leaf()); path::default_name_check(no_check);
t.set_piece_size(piece_size);
file_pool fp; if (argc != 4 &amp;&amp; argc != 5)
storage st(t, full_path.branch_path(), fp);
t.add_tracker(argv[2]);
// calculate the hash for all pieces
int num = t.num_pieces();
std::vector&lt;char&gt; buf(piece_size);
for (int i = 0; i &lt; num; ++i)
{ {
st.read(&amp;buf[0], i, 0, t.piece_size(i)); std::cerr &lt;&lt; &quot;usage: make_torrent &lt;output torrent-file&gt; &quot;
hasher h(&amp;buf[0], t.piece_size(i)); &quot;&lt;announce url&gt; &lt;file or directory to create torrent from&gt; &quot;
t.set_hash(i, h.final()); &quot;[url-seed]\n&quot;;
std::cerr &lt;&lt; (i+1) &lt;&lt; &quot;/&quot; &lt;&lt; num &lt;&lt; &quot;\r&quot;; return 1;
} }
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
file_storage fs;
file_pool fp;
path full_path = complete(path(argv[3]));
add_files(fs, full_path, file_filter);
create_torrent t(fs, piece_size);
t.add_tracker(argv[2]);
set_piece_hashes(t, full_path.branch_path()
, boost::bind(&amp;print_progress, _1, t.num_pieces()));
std::cerr &lt;&lt; std::endl;
t.set_creator(creator_str); t.set_creator(creator_str);
if (argc == 5) t.add_url_seed(argv[4]);
// create the torrent and print it to out // create the torrent and print it to out
entry e = t.create_torrent(); ofstream out(complete(path(argv[1])), std::ios_base::binary);
libtorrent::bencode(std::ostream_iterator&lt;char&gt;(out), e); bencode(std::ostream_iterator&lt;char&gt;(out), t.generate());
#ifndef BOOST_NO_EXCEPTIONS
} }
catch (std::exception&amp; e) catch (std::exception&amp; e)
{ {
std::cerr &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;; std::cerr &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
} }
#endif
return 0; return 0;
} }

View File

@@ -24,7 +24,6 @@ dump_torrent
This is an example of a program that will take a torrent-file as a parameter and This is an example of a program that will take a torrent-file as a parameter and
print information about it to std out:: print information about it to std out::
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <iterator> #include <iterator>
@@ -33,27 +32,48 @@ print information about it to std out::
#include "libtorrent/entry.hpp" #include "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp" #include "libtorrent/bencode.hpp"
#include "libtorrent/torrent_info.hpp" #include "libtorrent/torrent_info.hpp"
#include "libtorrent/lazy_entry.hpp"
#include <boost/filesystem/operations.hpp>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
using namespace libtorrent; using namespace libtorrent;
using namespace boost::filesystem;
if (argc != 2) if (argc != 2)
{ {
std::cerr << "usage: dump_torrent torrent-file\n"; std::cerr << "usage: dump_torrent torrent-file\n";
return 1; return 1;
} }
#if BOOST_VERSION < 103400
boost::filesystem::path::default_name_check(boost::filesystem::no_check);
#endif
#ifndef BOOST_NO_EXCEPTIONS
try try
{ {
std::ifstream in(argv[1], std::ios_base::binary); #endif
in.unsetf(std::ios_base::skipws);
entry e = bdecode(std::istream_iterator<char>(in) int size = file_size(argv[1]);
, std::istream_iterator<char>()); if (size > 10 * 1000000)
{
std::cerr << "file too big (" << size << "), aborting\n";
return 1;
}
std::vector<char> buf(size);
std::ifstream(argv[1], std::ios_base::binary).read(&buf[0], size);
lazy_entry e;
int ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e);
if (ret != 0)
{
std::cerr << "invalid bencoding: " << ret << std::endl;
return 1;
}
std::cout << "\n\n----- raw info -----\n\n"; std::cout << "\n\n----- raw info -----\n\n";
e.print(std::cout); std::cout << e << std::endl;
torrent_info t(e); torrent_info t(e);
@@ -67,10 +87,9 @@ print information about it to std out::
{ {
std::cout << i->first << ":" << i->second << "\n"; std::cout << i->first << ":" << i->second << "\n";
} }
std::cout << "trackers:\n"; std::cout << "trackers:\n";
for (std::vector<announce_entry>::const_iterator i for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
= t.trackers().begin(); i != t.trackers().end(); ++i) i != t.trackers().end(); ++i)
{ {
std::cout << i->tier << ": " << i->url << "\n"; std::cout << i->tier << ": " << i->url << "\n";
} }
@@ -81,71 +100,70 @@ print information about it to std out::
std::cout << "comment: " << t.comment() << "\n"; std::cout << "comment: " << t.comment() << "\n";
std::cout << "created by: " << t.creator() << "\n"; std::cout << "created by: " << t.creator() << "\n";
std::cout << "files:\n"; std::cout << "files:\n";
int index = 0;
for (torrent_info::file_iterator i = t.begin_files(); for (torrent_info::file_iterator i = t.begin_files();
i != t.end_files(); ++i) i != t.end_files(); ++i, ++index)
{ {
int first = t.map_file(index, 0, 1).piece;
int last = t.map_file(index, i->size - 1, 1).piece;
std::cout << " " << std::setw(11) << i->size std::cout << " " << std::setw(11) << i->size
<< " " << i->path.string() << "\n"; << " " << i->path.string() << "[ " << first << ", "
<< last << " ]\n";
} }
#ifndef BOOST_NO_EXCEPTIONS
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::cout << e.what() << "\n"; std::cout << e.what() << "\n";
} }
#endif
return 0; return 0;
} }
simple client simple client
------------- -------------
This is a simple client. It doesn't have much output to keep it simple:: This is a simple client. It doesn't have much output to keep it simple::
#include <iostream>
#include <fstream>
#include <iterator>
#include <exception>
#include <boost/format.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/session.hpp"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
using namespace libtorrent; using namespace libtorrent;
#if BOOST_VERSION < 103400
namespace fs = boost::filesystem;
fs::path::default_name_check(fs::no_check);
#endif
if (argc != 2) if (argc != 2)
{ {
std::cerr << "usage: ./simple_cient torrent-file\n" std::cerr << "usage: ./simple_client torrent-file\n"
"to stop the client, press return.\n"; "to stop the client, press return.\n";
return 1; return 1;
} }
#ifndef BOOST_NO_EXCEPTIONS
try try
#endif
{ {
session s; session s;
s.listen_on(std::make_pair(6881, 6889)); s.listen_on(std::make_pair(6881, 6889));
add_torrent_params p;
std::ifstream in(argv[1], std::ios_base::binary); p.save_path = "./";
in.unsetf(std::ios_base::skipws); p.ti = new torrent_info(argv[1]);
entry e = bdecode(std::istream_iterator<char>(in) s.add_torrent(p);
, std::istream_iterator<char>());
s.add_torrent(torrent_info(e), "");
// wait for the user to end // wait for the user to end
char a; char a;
std::cin.unsetf(std::ios_base::skipws); std::cin.unsetf(std::ios_base::skipws);
std::cin >> a; std::cin >> a;
} }
#ifndef BOOST_NO_EXCEPTIONS
catch (std::exception& e) catch (std::exception& e)
{ {
std::cout << e.what() << "\n"; std::cout << e.what() << "\n";
} }
#endif
return 0; return 0;
} }
@@ -165,31 +183,28 @@ Shows how to create a torrent from a directory tree::
#include "libtorrent/file.hpp" #include "libtorrent/file.hpp"
#include "libtorrent/storage.hpp" #include "libtorrent/storage.hpp"
#include "libtorrent/hasher.hpp" #include "libtorrent/hasher.hpp"
#include "libtorrent/file_pool.hpp" #include "libtorrent/create_torrent.hpp"
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
#include <boost/bind.hpp>
using namespace boost::filesystem; using namespace boost::filesystem;
using namespace libtorrent; using namespace libtorrent;
void add_files( // do not include files and folders whose
torrent_info& t // name starts with a .
, path const& p bool file_filter(boost::filesystem::path const& filename)
, path const& l)
{ {
path f(p / l); if (filename.leaf()[0] == '.') return false;
if (is_directory(f)) std::cerr << filename << std::endl;
{ return true;
for (directory_iterator i(f), end; i != end; ++i)
add_files(t, p, l / i->leaf());
} }
else
void print_progress(int i, int num)
{ {
std::cerr << "adding \"" << l.string() << "\"\n"; std::cerr << "\r" << (i+1) << "/" << num;
t.add_file(l, file_size(f));
}
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
@@ -197,54 +212,49 @@ Shows how to create a torrent from a directory tree::
using namespace libtorrent; using namespace libtorrent;
using namespace boost::filesystem; using namespace boost::filesystem;
path::default_name_check(no_check);
if (argc != 4)
{
std::cerr << "usage: make_torrent <output torrent-file> "
"<announce url> <file or directory to create torrent from>\n";
return 1;
}
try
{
torrent_info t;
path full_path = complete(path(argv[3]));
ofstream out(complete(path(argv[1])), std::ios_base::binary);
int piece_size = 256 * 1024; int piece_size = 256 * 1024;
char const* creator_str = "libtorrent"; char const* creator_str = "libtorrent";
add_files(t, full_path.branch_path(), full_path.leaf()); path::default_name_check(no_check);
t.set_piece_size(piece_size);
file_pool fp; if (argc != 4 && argc != 5)
storage st(t, full_path.branch_path(), fp);
t.add_tracker(argv[2]);
// calculate the hash for all pieces
int num = t.num_pieces();
std::vector<char> buf(piece_size);
for (int i = 0; i < num; ++i)
{ {
st.read(&buf[0], i, 0, t.piece_size(i)); std::cerr << "usage: make_torrent <output torrent-file> "
hasher h(&buf[0], t.piece_size(i)); "<announce url> <file or directory to create torrent from> "
t.set_hash(i, h.final()); "[url-seed]\n";
std::cerr << (i+1) << "/" << num << "\r"; return 1;
} }
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
file_storage fs;
file_pool fp;
path full_path = complete(path(argv[3]));
add_files(fs, full_path, file_filter);
create_torrent t(fs, piece_size);
t.add_tracker(argv[2]);
set_piece_hashes(t, full_path.branch_path()
, boost::bind(&print_progress, _1, t.num_pieces()));
std::cerr << std::endl;
t.set_creator(creator_str); t.set_creator(creator_str);
if (argc == 5) t.add_url_seed(argv[4]);
// create the torrent and print it to out // create the torrent and print it to out
entry e = t.create_torrent(); ofstream out(complete(path(argv[1])), std::ios_base::binary);
libtorrent::bencode(std::ostream_iterator<char>(out), e); bencode(std::ostream_iterator<char>(out), t.generate());
#ifndef BOOST_NO_EXCEPTIONS
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::cerr << e.what() << "\n"; std::cerr << e.what() << "\n";
} }
#endif
return 0; return 0;
} }