made the examples build without exception support. added overloads of add_torrent() and add_magnet_uri() that don't throw

This commit is contained in:
Arvid Norberg 2009-02-26 07:09:56 +00:00
parent f8e72650bd
commit 7aacfca292
14 changed files with 894 additions and 1017 deletions

View File

@ -147,13 +147,6 @@ if test -z "$BOOST_REGEX_LIB"; then
BUILD_TESTCLIENT=no; BUILD_TESTCLIENT=no;
fi fi
AX_BOOST_PROGRAM_OPTIONS
dnl check that Boost.Program_options was found:
if test -z "$BOOST_PROGRAM_OPTIONS_LIB"; then
AC_MSG_RESULT([Unable to find Boost.Program_options library, example test_client will not be build.])
BUILD_TESTCLIENT=no;
fi
dnl Apply boost config. dnl Apply boost config.
CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"

View File

@ -89,6 +89,9 @@ The ``session`` class has the following synopsis::
torrent_handle add_torrent( torrent_handle add_torrent(
add_torrent_params const& params); add_torrent_params const& params);
torrent_handle add_torrent(
add_torrent_params const& params
, error_code& ec);
void pause(); void pause();
void resume(); void resume();
@ -299,10 +302,15 @@ add_torrent()
}; };
torrent_handle add_torrent(add_torrent_params const& params); torrent_handle add_torrent(add_torrent_params const& params);
torrent_handle add_torrent(add_torrent_params const& params
, error_code& ec);
You add torrents through the ``add_torrent()`` function where you give an You add torrents through the ``add_torrent()`` function where you give an
object with all the parameters. object with all the parameters.
The overload that does not take an ``error_code`` throws an exception on
error and is not available when building without exception support.
The only mandatory parameter is ``save_path`` which is the directory where you The only mandatory parameter is ``save_path`` which is the directory where you
want the files to be saved. You also need to specify either the ``ti`` (the want the files to be saved. You also need to specify either the ``ti`` (the
torrent file) or ``info_hash`` (the info hash of the torrent). If you specify the torrent file) or ``info_hash`` (the info hash of the torrent). If you specify the
@ -4173,6 +4181,8 @@ add_magnet_uri()
torrent_handle add_magnet_uri(session& ses, std::string const& uri torrent_handle add_magnet_uri(session& ses, std::string const& uri
add_torrent_params p); add_torrent_params p);
torrent_handle add_magnet_uri(session& ses, std::string const& uri
add_torrent_params p, error_code& ec);
This function parses the magnet URI (``uri``) as a bittorrent magnet link, This function parses the magnet URI (``uri``) as a bittorrent magnet link,
and adds the torrent to the specified session (``ses``). It returns the and adds the torrent to the specified session (``ses``). It returns the
@ -4180,6 +4190,9 @@ handle to the newly added torrent, or an invalid handle in case parsing
failed. To control some initial settings of the torrent, sepcify those in failed. To control some initial settings of the torrent, sepcify those in
the ``add_torrent_params``, ``p``. See `add_torrent()`_. the ``add_torrent_params``, ``p``. See `add_torrent()`_.
The overload that does not take an ``error_code`` throws an exception on
error and is not available when building without exception support.
For more information about magnet links, see `magnet links`_. For more information about magnet links, see `magnet links`_.
make_magnet_uri() make_magnet_uri()
@ -5008,6 +5021,12 @@ code symbol description
------ ---------------------------- ----------------------------------------------------------------- ------ ---------------------------- -----------------------------------------------------------------
19 duplicate_torrent There's already a torrent with that info-hash added to the 19 duplicate_torrent There's already a torrent with that info-hash added to the
session session
------ ---------------------------- -----------------------------------------------------------------
20 invalid_torrent_handle The supplied torrent_handle is not referring to a valid torrent
------ ---------------------------- -----------------------------------------------------------------
21 invalid_entry_type The type requested from the entry did not match its type
------ ---------------------------- -----------------------------------------------------------------
22 missing_info_hash_in_uri The specified URI does not contain a valid info-hash
====== ============================ ================================================================= ====== ============================ =================================================================
The names of these error codes are declared in then ``libtorrent::errors`` namespace. The names of these error codes are declared in then ``libtorrent::errors`` namespace.

View File

@ -9,7 +9,6 @@ if $(BOOST_ROOT)
use-project /boost : $(BOOST_ROOT) ; use-project /boost : $(BOOST_ROOT) ;
} }
lib program-options : : <name>boost_program_options ;
lib regex : : <name>boost_regex ; lib regex : : <name>boost_regex ;
project client_test project client_test
@ -20,8 +19,7 @@ project client_test
; ;
exe client_test : client_test.cpp exe client_test : client_test.cpp
: <library>/boost/program_options : <library>/boost/regex
<library>/boost/regex
; ;
exe simple_client : simple_client.cpp ; exe simple_client : simple_client.cpp ;

View File

@ -6,7 +6,7 @@ EXTRA_PROGRAMS = client_test dump_torrent make_torrent simple_client enum_if
EXTRA_DIST = Jamfile EXTRA_DIST = Jamfile
client_test_SOURCES = client_test.cpp client_test_SOURCES = client_test.cpp
client_test_LDADD = $(top_builddir)/src/libtorrent-rasterbar.la @BOOST_REGEX_LIB@ @BOOST_PROGRAM_OPTIONS_LIB@ client_test_LDADD = $(top_builddir)/src/libtorrent-rasterbar.la @BOOST_REGEX_LIB@
dump_torrent_SOURCES = dump_torrent.cpp dump_torrent_SOURCES = dump_torrent.cpp
dump_torrent_LDADD = $(top_builddir)/src/libtorrent-rasterbar.la dump_torrent_LDADD = $(top_builddir)/src/libtorrent-rasterbar.la

File diff suppressed because it is too large Load Diff

View File

@ -57,81 +57,74 @@ int main(int argc, char* argv[])
boost::filesystem::path::default_name_check(boost::filesystem::no_check); boost::filesystem::path::default_name_check(boost::filesystem::no_check);
#endif #endif
#ifndef BOOST_NO_EXCEPTIONS int size = file_size(argv[1]);
try if (size > 10 * 1000000)
{ {
#endif std::cerr << "file too big (" << size << "), aborting\n";
return 1;
int size = file_size(argv[1]);
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 << e << std::endl;
torrent_info t(e);
// print info about torrent
std::cout << "\n\n----- torrent file info -----\n\n";
std::cout << "nodes:\n";
typedef std::vector<std::pair<std::string, int> > node_vec;
node_vec const& nodes = t.nodes();
for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
i != end; ++i)
{
std::cout << i->first << ":" << i->second << "\n";
}
std::cout << "trackers:\n";
for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
i != t.trackers().end(); ++i)
{
std::cout << i->tier << ": " << i->url << "\n";
}
std::cout << "number of pieces: " << t.num_pieces() << "\n";
std::cout << "piece length: " << t.piece_length() << "\n";
std::cout << "info hash: " << t.info_hash() << "\n";
std::cout << "comment: " << t.comment() << "\n";
std::cout << "created by: " << t.creator() << "\n";
std::cout << "magnet link: " << make_magnet_uri(t) << "\n";
std::cout << "name: " << t.name() << "\n";
std::cout << "files:\n";
int index = 0;
for (torrent_info::file_iterator i = t.begin_files();
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
<< " "
<< (i->pad_file?'p':'-')
<< (i->executable_attribute?'x':'-')
<< (i->hidden_attribute?'h':'-')
<< " "
<< i->path.string() << "[ " << first << ", "
<< last << " ]\n";
}
#ifndef BOOST_NO_EXCEPTIONS
} }
catch (std::exception& e) 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::cout << e.what() << "\n"; std::cerr << "invalid bencoding: " << ret << std::endl;
return 1;
}
std::cout << "\n\n----- raw info -----\n\n";
std::cout << e << std::endl;
error_code ec;
torrent_info t(e, ec);
if (ec)
{
std::cout << ec.message() << std::endl;
return 1;
}
// print info about torrent
std::cout << "\n\n----- torrent file info -----\n\n";
std::cout << "nodes:\n";
typedef std::vector<std::pair<std::string, int> > node_vec;
node_vec const& nodes = t.nodes();
for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
i != end; ++i)
{
std::cout << i->first << ":" << i->second << "\n";
}
std::cout << "trackers:\n";
for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
i != t.trackers().end(); ++i)
{
std::cout << i->tier << ": " << i->url << "\n";
}
std::cout << "number of pieces: " << t.num_pieces() << "\n";
std::cout << "piece length: " << t.piece_length() << "\n";
std::cout << "info hash: " << t.info_hash() << "\n";
std::cout << "comment: " << t.comment() << "\n";
std::cout << "created by: " << t.creator() << "\n";
std::cout << "magnet link: " << make_magnet_uri(t) << "\n";
std::cout << "name: " << t.name() << "\n";
std::cout << "files:\n";
int index = 0;
for (torrent_info::file_iterator i = t.begin_files();
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
<< " "
<< (i->pad_file?'p':'-')
<< (i->executable_attribute?'x':'-')
<< (i->hidden_attribute?'h':'-')
<< " "
<< i->path.string() << "[ " << first << ", "
<< last << " ]\n";
} }
#endif
return 0; return 0;
} }

View File

@ -54,28 +54,28 @@ int main(int argc, char* argv[])
return 1; return 1;
} }
#ifndef BOOST_NO_EXCEPTIONS session s;
try s.listen_on(std::make_pair(6881, 6889));
#endif add_torrent_params p;
p.save_path = "./";
error_code ec;
p.ti = new torrent_info(argv[1], ec);
if (ec)
{ {
session s; std::cout << ec.message() << std::endl;
s.listen_on(std::make_pair(6881, 6889)); return 1;
add_torrent_params p; }
p.save_path = "./"; s.add_torrent(p, ec);
p.ti = new torrent_info(argv[1]); if (ec)
s.add_torrent(p); {
std::cerr << ec.message() << std::endl;
return 1;
}
// 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)
{
std::cout << e.what() << "\n";
}
#endif
return 0; return 0;
} }

View File

@ -73,6 +73,7 @@ namespace libtorrent
duplicate_torrent, duplicate_torrent,
invalid_torrent_handle, invalid_torrent_handle,
invalid_entry_type, invalid_entry_type,
missing_info_hash_in_uri,
}; };
} }

View File

@ -48,6 +48,7 @@ namespace libtorrent
std::string TORRENT_EXPORT make_magnet_uri(torrent_handle const& handle); std::string TORRENT_EXPORT make_magnet_uri(torrent_handle const& handle);
std::string TORRENT_EXPORT make_magnet_uri(torrent_info const& info); std::string TORRENT_EXPORT make_magnet_uri(torrent_info const& info);
#ifndef BOOST_NO_EXCEPTIONS
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
// deprecated in 0.14 // deprecated in 0.14
torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri
@ -60,6 +61,10 @@ namespace libtorrent
torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri
, add_torrent_params p); , add_torrent_params p);
#endif
torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri
, add_torrent_params p, error_code& ec);
} }
#endif #endif

View File

@ -172,7 +172,7 @@ namespace libtorrent
torrent_info(fs::wpath const& filename); torrent_info(fs::wpath const& filename);
#endif #endif
torrent_info(sha1_hash const& info_hash, error_code& ec); torrent_info(sha1_hash const& info_hash);
torrent_info(lazy_entry const& torrent_file, error_code& ec); torrent_info(lazy_entry const& torrent_file, error_code& ec);
torrent_info(char const* buffer, int size, error_code& ec); torrent_info(char const* buffer, int size, error_code& ec);
torrent_info(fs::path const& filename, error_code& ec); torrent_info(fs::path const& filename, error_code& ec);

View File

@ -69,6 +69,7 @@ namespace libtorrent
"torrent already exists in session", "torrent already exists in session",
"invalid torrent handle used", "invalid torrent handle used",
"invalid type requested from entry", "invalid type requested from entry",
"missing info-hash from URI",
}; };
if (ev < 0 || ev >= sizeof(msgs)/sizeof(msgs[0])) if (ev < 0 || ev >= sizeof(msgs)/sizeof(msgs[0]))
return "Unknown error"; return "Unknown error";

View File

@ -89,6 +89,7 @@ namespace libtorrent
return ret.str(); return ret.str();
} }
#ifndef BOOST_NO_EXCEPTIONS
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
torrent_handle add_magnet_uri(session& ses, std::string const& uri torrent_handle add_magnet_uri(session& ses, std::string const& uri
, fs::path const& save_path , fs::path const& save_path
@ -121,27 +122,44 @@ namespace libtorrent
torrent_handle add_magnet_uri(session& ses, std::string const& uri torrent_handle add_magnet_uri(session& ses, std::string const& uri
, add_torrent_params p) , add_torrent_params p)
{
error_code ec;
torrent_handle ret = add_magnet_uri(ses, uri, p, ec);
if (ec) throw libtorrent_exceptions(ec);
return ret;
}
#endif
torrent_handle add_magnet_uri(session& ses, std::string const& uri
, add_torrent_params p, error_code& ec)
{ {
std::string name; std::string name;
std::string tracker; std::string tracker;
error_code ec; error_code e;
boost::optional<std::string> display_name = url_has_argument(uri, "dn"); boost::optional<std::string> display_name = url_has_argument(uri, "dn");
if (display_name) name = unescape_string(display_name->c_str(), ec); if (display_name) name = unescape_string(display_name->c_str(), e);
boost::optional<std::string> tracker_string = url_has_argument(uri, "tr"); boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
if (tracker_string) tracker = unescape_string(tracker_string->c_str(), ec); if (tracker_string) tracker = unescape_string(tracker_string->c_str(), e);
boost::optional<std::string> btih = url_has_argument(uri, "xt"); boost::optional<std::string> btih = url_has_argument(uri, "xt");
if (!btih) return torrent_handle(); if (!btih)
{
ec = error_code(errors::missing_info_hash_in_uri, libtorrent_category);
return torrent_handle();
}
if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle(); if (btih->compare(0, 9, "urn:btih:") != 0)
{
ec = error_code(errors::missing_info_hash_in_uri, libtorrent_category);
return torrent_handle();
}
sha1_hash info_hash(base32decode(btih->substr(9))); sha1_hash info_hash(base32decode(btih->substr(9)));
if (!tracker.empty()) p.tracker_url = tracker.c_str(); if (!tracker.empty()) p.tracker_url = tracker.c_str();
p.info_hash = info_hash; p.info_hash = info_hash;
if (!name.empty()) p.name = name.c_str(); if (!name.empty()) p.name = name.c_str();
return ses.add_torrent(p); return ses.add_torrent(p, ec);
} }
} }

View File

@ -313,6 +313,7 @@ namespace libtorrent
return m_impl->add_torrent(params, ec); return m_impl->add_torrent(params, ec);
} }
#ifndef BOOST_NO_EXCEPTIONS
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
// if the torrent already exists, this will throw duplicate_torrent // if the torrent already exists, this will throw duplicate_torrent
torrent_handle session::add_torrent( torrent_handle session::add_torrent(
@ -381,6 +382,7 @@ namespace libtorrent
p.userdata = userdata; p.userdata = userdata;
return add_torrent(p); return add_torrent(p);
} }
#endif
#endif #endif
void session::remove_torrent(const torrent_handle& h, int options) void session::remove_torrent(const torrent_handle& h, int options)

View File

@ -2420,7 +2420,6 @@ namespace aux {
void session_impl::set_max_connections(int limit) void session_impl::set_max_connections(int limit)
{ {
TORRENT_ASSERT(limit > 0 || limit == -1);
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
INVARIANT_CHECK; INVARIANT_CHECK;
@ -2443,7 +2442,6 @@ namespace aux {
void session_impl::set_max_half_open_connections(int limit) void session_impl::set_max_half_open_connections(int limit)
{ {
TORRENT_ASSERT(limit > 0 || limit == -1);
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
INVARIANT_CHECK; INVARIANT_CHECK;
@ -2454,7 +2452,6 @@ namespace aux {
void session_impl::set_download_rate_limit(int bytes_per_second) void session_impl::set_download_rate_limit(int bytes_per_second)
{ {
TORRENT_ASSERT(bytes_per_second > 0 || bytes_per_second == -1);
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
INVARIANT_CHECK; INVARIANT_CHECK;
@ -2465,7 +2462,6 @@ namespace aux {
void session_impl::set_upload_rate_limit(int bytes_per_second) void session_impl::set_upload_rate_limit(int bytes_per_second)
{ {
TORRENT_ASSERT(bytes_per_second > 0 || bytes_per_second == -1);
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
INVARIANT_CHECK; INVARIANT_CHECK;