*** empty log message ***

This commit is contained in:
Arvid Norberg 2004-03-23 22:58:18 +00:00
parent 27c8533716
commit 870b4aeaab
14 changed files with 296 additions and 332 deletions

View File

@ -480,11 +480,14 @@ class torrent_info
public:
torrent_info(const entry& torrent_file)
torrent_info(int piece_size, const char* name);
torrent_info(
int piece_size
, const char* name
, const char* comment = 0);
entry create_torrent(const char* created_by = 0) const;
void set_comment(char const* str);
void set_creator(char const* str);
void set_hash(int index, const sha1_hash& h);
void add_tracker(std::string const& url, int tier = 0);
void add_file(boost::filesystem::path file, size_type size);
typedef std::vector>file_entry>::const_iterator file_iterator;
typedef std::vector<file_entry>::const_reverse_iterator reverse_file_iterator;
@ -583,21 +586,20 @@ struct torrent_handle
entry write_resume_data();
void force_reannounce();
void connect_peer(const address& adr) const;
void set_ratio(float ratio);
void set_tracker_login(std::string const& username, std::string const& password);
void set_max_uploads(int max_uploads);
void set_max_connections(int max_connections);
void set_upload_limit(int limit);
void use_interface(const char* net_interface);
void pause();
void resume();
bool is_paused() const;
void set_tracker_login(std::string const& username, std::string const& password);
void use_interface(const char* net_interface);
boost::filsystem::path save_path() const;
void set_max_uploads(int max_uploads);
void set_max_connections(int max_connections);
sha1_hash info_hash() const;
bool operator==(const torrent_handle&) const;
@ -625,6 +627,8 @@ in return. With this setting it will work much like the standard clients.</p>
attempt to upload in return for each download. e.g. if set to 2, the client will try to upload
2 bytes for every byte received. The default setting for this is 0, which will make it work
as a standard client.</p>
<p><tt class="literal"><span class="pre">set_upload_limit</span></tt> will limit the upload bandwidth used by this particular torrent to the
limit you set. It is given as the number of bytes per second the torrent is allowed to upload.</p>
<p><tt class="literal"><span class="pre">pause()</span></tt>, and <tt class="literal"><span class="pre">resume()</span></tt> will disconnect all peers and reconnect all peers respectively.
When a torrent is paused, it will however remember all share ratios to all peers and remember
all potential (not connected) peers. You can use <tt class="literal"><span class="pre">is_paused()</span></tt> to determine if a torrent

View File

@ -460,11 +460,14 @@ The ``torrent_info`` has the following synopsis::
public:
torrent_info(const entry& torrent_file)
torrent_info(int piece_size, const char* name);
torrent_info(
int piece_size
, const char* name
, const char* comment = 0);
entry create_torrent(const char* created_by = 0) const;
void set_comment(char const* str);
void set_creator(char const* str);
void set_hash(int index, const sha1_hash& h);
void add_tracker(std::string const& url, int tier = 0);
void add_file(boost::filesystem::path file, size_type size);
typedef std::vector>file_entry>::const_iterator file_iterator;
typedef std::vector<file_entry>::const_reverse_iterator reverse_file_iterator;
@ -582,21 +585,20 @@ Its declaration looks like this::
entry write_resume_data();
void force_reannounce();
void connect_peer(const address& adr) const;
void set_ratio(float ratio);
void set_tracker_login(std::string const& username, std::string const& password);
void set_max_uploads(int max_uploads);
void set_max_connections(int max_connections);
void set_upload_limit(int limit);
void use_interface(const char* net_interface);
void pause();
void resume();
bool is_paused() const;
void set_tracker_login(std::string const& username, std::string const& password);
void use_interface(const char* net_interface);
boost::filsystem::path save_path() const;
void set_max_uploads(int max_uploads);
void set_max_connections(int max_connections);
sha1_hash info_hash() const;
bool operator==(const torrent_handle&) const;
@ -630,6 +632,9 @@ attempt to upload in return for each download. e.g. if set to 2, the client will
2 bytes for every byte received. The default setting for this is 0, which will make it work
as a standard client.
``set_upload_limit`` will limit the upload bandwidth used by this particular torrent to the
limit you set. It is given as the number of bytes per second the torrent is allowed to upload.
``pause()``, and ``resume()`` will disconnect all peers and reconnect all peers respectively.
When a torrent is paused, it will however remember all share ratios to all peers and remember
all potential (not connected) peers. You can use ``is_paused()`` to determine if a torrent

View File

@ -25,5 +25,6 @@ exe dump_torrent
<threading>multi
: debug release
;
stage bin : dump_torrent simple_client client_test ;

View File

@ -340,6 +340,7 @@ int main(int argc, char* argv[])
}
// loop through the alert queue to see if anything has happened.
std::auto_ptr<alert> a;
a = ses.pop_alert();
while (a.get())
@ -347,9 +348,10 @@ int main(int argc, char* argv[])
torrent_finished_alert* p = dynamic_cast<torrent_finished_alert*>(a.get());
if (p)
{
ses.set_upload_rate_limit(30000);
// limit the bandwidth for all seeding torrents
p->handle.set_max_connections(10);
p->handle.set_max_uploads(5);
p->handle.set_upload_limit(30000);
}
if (events.size() >= 10) events.pop_front();
events.push_back(a->msg());

View File

@ -39,11 +39,16 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/shared_ptr.hpp>
#include "libtorrent/resource_request.hpp"
#include "libtorrent/peer_id.hpp"
#include "libtorrent/socket.hpp"
namespace libtorrent
{
class socket;
class peer_connection;
class torrent;
int saturated_add(int a, int b);
// Function to allocate a limited resource fairly among many consumers.
// It takes into account the current use, and the consumer's desired use.
@ -51,11 +56,21 @@ namespace libtorrent
// sure "used" is updated between calls!).
// If resources = std::numeric_limits<int>::max() it means there is an infinite
// supply of resources (so everyone can get what they want).
/*
void allocate_resources(
int resources
, std::map<boost::shared_ptr<socket>, boost::shared_ptr<peer_connection> >& connections
, resource_request peer_connection::* res);
*/
void allocate_resources(
int resources
, std::map<sha1_hash, boost::shared_ptr<torrent> >& torrents
, resource_request torrent::* res);
void allocate_resources(
int resources
, std::map<address, peer_connection*>& connections
, resource_request peer_connection::* res);
}

View File

@ -60,6 +60,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/tracker_manager.hpp"
#include "libtorrent/stat.hpp"
#include "libtorrent/alert.hpp"
#include "libtorrent/resource_request.hpp"
namespace libtorrent
{
@ -285,6 +286,19 @@ namespace libtorrent
void check_invariant();
#endif
// this will distribute the given upload/download
// quotas among the peers
void distribute_resources();
resource_request m_upload_bandwidth_quota;
void set_upload_limit(int limit)
{
assert(limit >= -1);
if (limit == -1) limit = std::numeric_limits<int>::max();
m_upload_bandwidth_limit = limit;
}
private:
void try_next_tracker();
@ -369,7 +383,14 @@ namespace libtorrent
std::string m_username;
std::string m_password;
// the network interface all outgoing connections
// are opened through
address m_net_interface;
// the max number of bytes this torrent
// can upload per second
int m_upload_bandwidth_limit;
};
}

View File

@ -166,12 +166,6 @@ namespace libtorrent
// shut down the torrent in one atomic operation
entry write_resume_data() const;
// TODO: support pause/resume
// that will not delete the torrent or its
// policy. If those are deleted all share
// ratios to all clients are lost, we don't
// want that.
// forces this torrent to reannounce
// (make a rerequest from the tracker)
void force_reannounce() const;
@ -180,7 +174,7 @@ namespace libtorrent
// to finish all pieces currently in the pipeline, and then
// abort the torrent.
// TODO: add a max upload rate per torrent too.
void set_upload_limit(int limit);
// manually connect a peer
void connect_peer(const address& adr) const;

View File

@ -86,19 +86,15 @@ namespace libtorrent
public:
torrent_info(const entry& torrent_file);
torrent_info(int piece_size, const char* name);
torrent_info(
int piece_size
, const char* name
, const char* comment = 0);
entry create_torrent(const char* created_by = 0) const;
entry create_torrent() const;
void set_comment(char const* str);
void set_creator(char const* str);
void set_hash(int index, const sha1_hash& h);
void add_tracker(std::string const& url, int tier = 0);
void add_file(boost::filesystem::path file, size_type size);
// TODO: set name
typedef std::vector<file_entry>::const_iterator file_iterator;
typedef std::vector<file_entry>::const_reverse_iterator reverse_file_iterator;
@ -174,6 +170,10 @@ namespace libtorrent
// if a comment is found in the torrent file
// this will be set to that comment
std::string m_comment;
// an optional string naming the software used
// to create the torrent file
std::string m_created_by;
};
}

View File

@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/allocate_resources.hpp"
#include "libtorrent/size_type.hpp"
#include "libtorrent/peer_connection.hpp"
#include "libtorrent/torrent.hpp"
#include <cassert>
#include <algorithm>
@ -45,22 +46,22 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
int saturated_add(int a, int b)
{
assert(a >= 0);
assert(b >= 0);
assert(std::numeric_limits<int>::max() + std::numeric_limits<int>::max() < 0);
int sum = a + b;
if(sum < 0)
sum = std::numeric_limits<int>::max();
assert(sum >= a && sum >= b);
return sum;
}
namespace
{
int saturated_add(int a, int b)
{
assert(a >= 0);
assert(b >= 0);
assert(std::numeric_limits<int>::max() + std::numeric_limits<int>::max() < 0);
int sum = a + b;
if(sum < 0)
sum = std::numeric_limits<int>::max();
assert(sum >= a && sum >= b);
return sum;
}
// give num_resources to r,
// return how how many were actually accepted.
int give(resource_request& r, int num_resources)
@ -215,9 +216,21 @@ namespace libtorrent
return *p.second;
}
peer_connection& pick_peer2(
std::pair<address, peer_connection*> const& p)
{
return *p.second;
}
torrent& deref(std::pair<sha1_hash, boost::shared_ptr<torrent> > const& p)
{
return *p.second;
}
} // namespace anonymous
/*
void allocate_resources(
int resources
, std::map<boost::shared_ptr<socket>, boost::shared_ptr<peer_connection> >& c
@ -233,5 +246,37 @@ namespace libtorrent
, new_iter(c.end(), &pick_peer)
, res);
}
*/
void allocate_resources(
int resources
, std::map<sha1_hash, boost::shared_ptr<torrent> >& c
, resource_request torrent::* res)
{
typedef std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator orig_iter;
typedef std::pair<sha1_hash, boost::shared_ptr<torrent> > in_param;
typedef boost::transform_iterator<torrent& (*)(in_param const&), orig_iter> new_iter;
allocate_resources_impl(
resources
, new_iter(c.begin(), &deref)
, new_iter(c.end(), &deref)
, res);
}
void allocate_resources(
int resources
, std::map<address, peer_connection*>& c
, resource_request peer_connection::* res)
{
typedef std::map<address, peer_connection*>::iterator orig_iter;
typedef std::pair<address, peer_connection*> in_param;
typedef boost::transform_iterator<peer_connection& (*)(in_param const&), orig_iter> new_iter;
allocate_resources_impl(
resources
, new_iter(c.begin(), &pick_peer2)
, new_iter(c.end(), &pick_peer2)
, res);
}
} // namespace libtorrent

View File

@ -222,6 +222,11 @@ namespace libtorrent
// non standard encodings
// ----------------------
if (std::equal(PID, PID + 12, "-G3g3rmz "))
{
return "G3 Torrent";
}
if (std::equal(PID, PID + 4, "exbc"))
{
std::stringstream s;

View File

@ -671,8 +671,7 @@ namespace libtorrent { namespace detail
// check each torrent for abortion or
// tracker updates
for (std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i
= m_torrents.begin();
i != m_torrents.end();)
= m_torrents.begin(); i != m_torrents.end();)
{
if (i->second->is_aborted())
{
@ -685,9 +684,7 @@ namespace libtorrent { namespace detail
#ifndef NDEBUG
sha1_hash i_hash = i->second->torrent_file().info_hash();
#endif
std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator j = i;
++i;
m_torrents.erase(j);
m_torrents.erase(i++);
assert(m_torrents.find(i_hash) == m_torrents.end());
continue;
}
@ -701,23 +698,24 @@ namespace libtorrent { namespace detail
, boost::get_pointer(i->second));
}
// tick() will set the used upload quota
i->second->second_tick();
++i;
}
purge_connections();
// distribute the maximum upload rate among the peers
// distribute the maximum upload rate among the torrents
allocate_resources(m_upload_rate == -1
? std::numeric_limits<int>::max()
: m_upload_rate
, m_connections
, &peer_connection::m_upload_bandwidth_quota);
, m_torrents
, &torrent::m_upload_bandwidth_quota);
for (detail::session_impl::connection_map::iterator c = m_connections.begin();
c != m_connections.end(); ++c)
for (std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i
= m_torrents.begin(); i != m_torrents.end(); ++i)
{
c->second->reset_upload_quota();
i->second->distribute_resources();
}
m_tracker_manager.tick();

View File

@ -238,6 +238,7 @@ namespace libtorrent
, m_got_tracker_response(false)
, m_ratio(0.f)
, m_net_interface(net_interface.ip(), address::any_port)
, m_upload_bandwidth_limit(std::numeric_limits<int>::max())
{
assert(torrent_file.begin_files() != torrent_file.end_files());
m_have_pieces.resize(torrent_file.num_pieces(), false);
@ -708,19 +709,41 @@ namespace libtorrent
m_policy->pulse();
}
m_upload_bandwidth_quota.used = 0;
m_upload_bandwidth_quota.wanted = 0;
for (peer_iterator i = m_connections.begin();
i != m_connections.end();
++i)
{
peer_connection* p = i->second;
const stat& s = p->statistics();
m_stat += s;
m_stat += p->statistics();
p->second_tick();
m_upload_bandwidth_quota.used += p->m_upload_bandwidth_quota.used;
m_upload_bandwidth_quota.wanted = saturated_add(
m_upload_bandwidth_quota.wanted
, p->m_upload_bandwidth_quota.wanted);
}
m_upload_bandwidth_quota.wanted
= std::min(m_upload_bandwidth_quota.wanted, m_upload_bandwidth_limit);
m_stat.second_tick();
}
void torrent::distribute_resources()
{
allocate_resources(m_upload_bandwidth_quota.given
, m_connections
, &peer_connection::m_upload_bandwidth_quota);
for (std::map<address, peer_connection*>::iterator i = m_connections.begin();
i != m_connections.end(); ++i)
{
i->second->reset_upload_quota();
}
}
bool torrent::verify_piece(int piece_index)
{
assert(piece_index >= 0);

View File

@ -47,6 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/lexical_cast.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/optional.hpp>
#include <boost/bind.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
@ -72,6 +73,35 @@ namespace std
namespace libtorrent
{
namespace
{
template<class Ret, class F>
Ret call_member(
detail::session_impl* ses
, detail::checker_impl* chk
, sha1_hash const& hash
, F f)
{
if (ses == 0) throw invalid_handle();
{
boost::mutex::scoped_lock l(ses->m_mutex);
torrent* t = ses->find_torrent(hash);
if (t != 0) return f(*t);
}
if (chk)
{
boost::mutex::scoped_lock l(chk->m_mutex);
detail::piece_checker_data* d = chk->find_torrent(hash);
if (d != 0) return f(*d->torrent_ptr);
}
throw invalid_handle();
}
}
#ifndef NDEBUG
void torrent_handle::check_invariant() const
@ -81,103 +111,80 @@ namespace libtorrent
#endif
void torrent_handle::set_max_uploads(int max_uploads)
{
INVARIANT_CHECK;
assert(max_uploads >= 2 || max_uploads == -1);
if (m_ses == 0) throw invalid_handle();
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0)
{
t->get_policy().set_max_uploads(max_uploads);
return;
}
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0)
{
d->torrent_ptr->get_policy().set_max_uploads(max_uploads);
return;
}
}
throw invalid_handle();
call_member<void>(m_ses, m_chk, m_info_hash
, boost::bind(&policy::set_max_uploads
, boost::bind(&torrent::get_policy, _1)
, max_uploads));
}
void torrent_handle::use_interface(const char* net_interface)
{
INVARIANT_CHECK;
if (m_ses == 0) throw invalid_handle();
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0)
{
t->use_interface(net_interface);
return;
}
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0)
{
d->torrent_ptr->use_interface(net_interface);
return;
}
}
throw invalid_handle();
call_member<void>(m_ses, m_chk, m_info_hash
, boost::bind(&torrent::use_interface, _1, net_interface));
}
void torrent_handle::set_max_connections(int max_connections)
{
INVARIANT_CHECK;
assert(max_connections > 2 || max_connections == -1);
if (m_ses == 0) throw invalid_handle();
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0)
{
t->get_policy().set_max_connections(max_connections);
return;
}
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0)
{
d->torrent_ptr->get_policy().set_max_connections(max_connections);
return;
}
}
throw invalid_handle();
call_member<void>(m_ses, m_chk, m_info_hash
, boost::bind(&policy::set_max_connections
, boost::bind(&torrent::get_policy, _1), max_connections));
}
void torrent_handle::set_upload_limit(int limit)
{
INVARIANT_CHECK;
assert(limit >= -1);
call_member<void>(m_ses, m_chk, m_info_hash
, boost::bind(&torrent::set_upload_limit, _1, limit));
}
bool torrent_handle::is_paused() const
{
INVARIANT_CHECK;
return call_member<bool>(m_ses, m_chk, m_info_hash
, boost::bind(&torrent::is_paused, _1));
}
void torrent_handle::pause()
{
INVARIANT_CHECK;
call_member<void>(m_ses, m_chk, m_info_hash
, boost::bind(&torrent::pause, _1));
}
void torrent_handle::resume()
{
INVARIANT_CHECK;
call_member<void>(m_ses, m_chk, m_info_hash
, boost::bind(&torrent::resume, _1));
}
void torrent_handle::set_tracker_login(std::string const& name, std::string const& password)
{
INVARIANT_CHECK;
call_member<void>(m_ses, m_chk, m_info_hash
, boost::bind(&torrent::set_tracker_login, _1, name, password));
}
torrent_status torrent_handle::status() const
{
INVARIANT_CHECK;
@ -204,18 +211,7 @@ namespace libtorrent
else
st.state = torrent_status::queued_for_checking;
st.progress = d->progress;
st.paused = false;
st.next_announce = boost::posix_time::time_duration();
st.announce_interval = boost::posix_time::time_duration();
st.total_download = 0;
st.total_upload = 0;
st.total_payload_download = 0;
st.total_payload_upload = 0;
st.download_rate = 0.f;
st.upload_rate = 0.f;
st.num_peers = 0;
st.pieces = 0;
st.total_done = 0;
st.paused = d->torrent_ptr->is_paused();
return st;
}
}
@ -227,119 +223,8 @@ namespace libtorrent
{
INVARIANT_CHECK;
if (m_ses == 0) throw invalid_handle();
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0) return t->torrent_file();
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0) return d->torrent_ptr->torrent_file();
}
throw invalid_handle();
}
bool torrent_handle::is_paused() const
{
INVARIANT_CHECK;
if (m_ses == 0) throw invalid_handle();
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0) return t->is_paused();
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0) return d->torrent_ptr->is_paused();
}
throw invalid_handle();
}
void torrent_handle::pause()
{
INVARIANT_CHECK;
if (m_ses == 0) throw invalid_handle();
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0) return t->pause();
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0) return d->torrent_ptr->pause();
}
throw invalid_handle();
}
void torrent_handle::resume()
{
INVARIANT_CHECK;
if (m_ses == 0) throw invalid_handle();
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0) return t->resume();
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0) return d->torrent_ptr->resume();
}
throw invalid_handle();
}
void torrent_handle::set_tracker_login(std::string const& name, std::string const& password)
{
INVARIANT_CHECK;
if (m_ses == 0) throw invalid_handle();
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0)
{
t->set_tracker_login(name, password);
return;
}
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0)
{
d->torrent_ptr->set_tracker_login(name, password);
return;
}
}
throw invalid_handle();
return call_member<torrent_info const&>(m_ses, m_chk, m_info_hash
, boost::bind(&torrent::torrent_file, _1));
}
bool torrent_handle::is_valid() const
@ -482,37 +367,8 @@ namespace libtorrent
{
INVARIANT_CHECK;
if (m_ses == 0) throw invalid_handle();
// copy the path into this local variable before
// unlocking and returning. Since we could get really
// unlucky and having the path removed after we
// have unlocked the data but before the return
// value has been copied into the destination
boost::filesystem::path ret;
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0)
{
ret = t->save_path();
return ret;
}
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0)
{
ret = d->save_path;
return ret;
}
}
throw invalid_handle();
return call_member<boost::filesystem::path>(m_ses, m_chk, m_info_hash
, boost::bind(&torrent::save_path, _1));
}
void torrent_handle::connect_peer(const address& adr) const
@ -549,29 +405,11 @@ namespace libtorrent
assert(ratio >= 0.f);
if (m_ses == 0) throw invalid_handle();
if (ratio < 1.f && ratio > 0.f)
ratio = 1.f;
{
boost::mutex::scoped_lock l(m_ses->m_mutex);
torrent* t = m_ses->find_torrent(m_info_hash);
if (t != 0)
{
t->set_ratio(ratio);
}
}
if (m_chk)
{
boost::mutex::scoped_lock l(m_chk->m_mutex);
detail::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0)
{
d->torrent_ptr->set_ratio(ratio);
}
}
call_member<void>(m_ses, m_chk, m_info_hash
, boost::bind(&torrent::set_ratio, _1, ratio));
}
void torrent_handle::get_peer_info(std::vector<peer_info>& v) const

View File

@ -114,16 +114,12 @@ namespace libtorrent
// just the necessary to use it with piece manager
torrent_info::torrent_info(
int piece_size
, const char* name
, const char* comment)
, const char* name)
: m_piece_length(piece_size)
, m_total_size(0)
, m_creation_date(second_clock::local_time())
{
m_info_hash.clear();
if (comment)
m_comment = comment;
}
@ -132,7 +128,6 @@ namespace libtorrent
void torrent_info::read_torrent_info(const entry& torrent_file)
{
// extract the url of the tracker
// const entry::dictionary_type& dict = torrent_file.dict();
entry const* i = torrent_file.find_key("announce-list");
if (i)
{
@ -191,6 +186,13 @@ namespace libtorrent
m_comment = i->string();
}
// extract comment
i = torrent_file.find_key("created by");
if (i != 0 && i->type() == entry::string_t)
{
m_created_by = i->string();
}
i = torrent_file.find_key("info");
if (i == 0) throw invalid_torrent_file();
entry const& info = *i;
@ -278,8 +280,8 @@ namespace libtorrent
m_total_size += size;
int num_pieces = static_cast<int>((m_total_size + m_piece_length - 1) / m_piece_length);
int num_pieces = static_cast<int>(
(m_total_size + m_piece_length - 1) / m_piece_length);
int old_num_pieces = static_cast<int>(m_piece_hash.size());
m_piece_hash.resize(num_pieces);
@ -292,7 +294,17 @@ namespace libtorrent
}
entry torrent_info::create_torrent(const char* created_by) const
void torrent_info::set_comment(char const* str)
{
m_comment = str;
}
void torrent_info::set_creator(char const* str)
{
m_created_by = str;
}
entry torrent_info::create_torrent() const
{
using namespace boost::gregorian;
using namespace boost::posix_time;
@ -309,14 +321,15 @@ namespace libtorrent
}
dict["announce"] = m_urls.front().url;
if (!m_comment.empty())
dict["comment"] = m_comment;
dict["creation date"] =
to_seconds(m_creation_date - ptime(date(1970, Jan, 1)));
if (created_by)
dict["created by"] = std::string(created_by);
if (!m_created_by.empty())
dict["created by"] = m_created_by;
entry& info = dict["info"];
info = entry(entry::dictionary_t);