forked from premiere/premiere-libtorrent
*** empty log message ***
This commit is contained in:
parent
13f67cc077
commit
bd296f3657
|
@ -214,6 +214,45 @@ std::string progress_bar(float progress, int width)
|
|||
return std::string(bar.begin(), bar.end());
|
||||
}
|
||||
|
||||
void print_peer_info(std::ostream& out, std::vector<libtorrent::peer_info> const& peers)
|
||||
{
|
||||
using namespace libtorrent;
|
||||
|
||||
out << " down up q r flags block\n";
|
||||
|
||||
for (std::vector<peer_info>::const_iterator i = peers.begin();
|
||||
i != peers.end();
|
||||
++i)
|
||||
{
|
||||
out.fill(' ');
|
||||
out.width(2);
|
||||
out << add_suffix(i->down_speed) << "/s "
|
||||
// << "(" << add_suffix(i->total_download) << ") "
|
||||
<< add_suffix(i->up_speed) << "/s "
|
||||
// << "(" << add_suffix(i->total_upload) << ") "
|
||||
// << "ul:" << add_suffix(i->upload_limit) << "/s "
|
||||
// << "uc:" << add_suffix(i->upload_ceiling) << "/s "
|
||||
// << "df:" << ratio(i->total_download, i->total_upload) << " "
|
||||
<< i->download_queue_length << " "
|
||||
<< i->upload_queue_length << " "
|
||||
<< static_cast<const char*>((i->flags & peer_info::interesting)?"I":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::choked)?"C":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::remote_interested)?"i":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::remote_choked)?"c":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::supports_extensions)?"e":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::local_connection)?"l":"r");
|
||||
|
||||
if (i->downloading_piece_index >= 0)
|
||||
{
|
||||
out << " " << progress_bar(
|
||||
i->downloading_progress / static_cast<float>(i->downloading_total)
|
||||
, 15);
|
||||
}
|
||||
|
||||
out << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
using namespace libtorrent;
|
||||
|
@ -243,7 +282,7 @@ int main(int argc, char* argv[])
|
|||
session ses(fingerprint("LT", 0, 1, 0, 0));
|
||||
|
||||
ses.listen_on(std::make_pair(6881, 6889));
|
||||
// ses.set_upload_rate_limit(1000);
|
||||
ses.set_upload_rate_limit(31 * 1024);
|
||||
// ses.set_download_rate_limit(50000);
|
||||
ses.set_http_settings(settings);
|
||||
ses.set_severity_level(alert::debug);
|
||||
|
@ -288,9 +327,9 @@ int main(int argc, char* argv[])
|
|||
catch (boost::filesystem::filesystem_error&) {}
|
||||
|
||||
handles.push_back(ses.add_torrent(e, save_path, resume_data));
|
||||
handles.back().set_max_connections(200);
|
||||
handles.back().set_max_uploads(20);
|
||||
handles.back().set_ratio(1.02f);
|
||||
handles.back().set_max_connections(100);
|
||||
handles.back().set_max_uploads(3);
|
||||
handles.back().set_ratio(0.f);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
|
@ -303,6 +342,10 @@ int main(int argc, char* argv[])
|
|||
std::vector<peer_info> peers;
|
||||
std::vector<partial_piece_info> queue;
|
||||
|
||||
bool print_peers = false;
|
||||
bool print_log = false;
|
||||
bool print_downloads = false;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
char c;
|
||||
|
@ -355,6 +398,10 @@ int main(int argc, char* argv[])
|
|||
, boost::bind(&torrent_handle::resume, _1));
|
||||
}
|
||||
|
||||
if (c == 'i') print_peers = !print_peers;
|
||||
if (c == 'l') print_log = !print_log;
|
||||
if (c == 'd') print_downloads = !print_downloads;
|
||||
|
||||
}
|
||||
|
||||
// loop through the alert queue to see if anything has happened.
|
||||
|
@ -427,6 +474,7 @@ int main(int argc, char* argv[])
|
|||
break;
|
||||
};
|
||||
|
||||
|
||||
i->get_peer_info(peers);
|
||||
|
||||
out.precision(4);
|
||||
|
@ -450,68 +498,43 @@ int main(int argc, char* argv[])
|
|||
|
||||
out << "___________________________________\n";
|
||||
|
||||
out << " down up q r flags block\n";
|
||||
|
||||
for (std::vector<peer_info>::iterator i = peers.begin();
|
||||
i != peers.end();
|
||||
++i)
|
||||
if (print_peers)
|
||||
{
|
||||
out.fill(' ');
|
||||
out.width(2);
|
||||
out << add_suffix(i->down_speed) << "/s "
|
||||
// << "(" << add_suffix(i->total_download) << ") "
|
||||
<< add_suffix(i->up_speed) << "/s "
|
||||
// << "(" << add_suffix(i->total_upload) << ") "
|
||||
// << "ul:" << add_suffix(i->upload_limit) << "/s "
|
||||
// << "uc:" << add_suffix(i->upload_ceiling) << "/s "
|
||||
// << "df:" << ratio(i->total_download, i->total_upload) << " "
|
||||
<< i->download_queue_length << " "
|
||||
<< i->upload_queue_length << " "
|
||||
<< static_cast<const char*>((i->flags & peer_info::interesting)?"I":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::choked)?"C":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::remote_interested)?"i":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::remote_choked)?"c":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::supports_extensions)?"e":"_")
|
||||
<< static_cast<const char*>((i->flags & peer_info::local_connection)?"l":"r");
|
||||
|
||||
if (i->downloading_piece_index >= 0)
|
||||
{
|
||||
out << " " << progress_bar(
|
||||
i->downloading_progress / static_cast<float>(i->downloading_total)
|
||||
, 15);
|
||||
}
|
||||
|
||||
out << "\n";
|
||||
print_peer_info(out, peers);
|
||||
out << "___________________________________\n";
|
||||
}
|
||||
|
||||
out << "___________________________________\n";
|
||||
|
||||
i->get_download_queue(queue);
|
||||
for (std::vector<partial_piece_info>::iterator i = queue.begin();
|
||||
i != queue.end();
|
||||
++i)
|
||||
if (print_downloads)
|
||||
{
|
||||
out.width(4);
|
||||
out.fill(' ');
|
||||
out << i->piece_index << ": |";
|
||||
for (int j = 0; j < i->blocks_in_piece; ++j)
|
||||
i->get_download_queue(queue);
|
||||
for (std::vector<partial_piece_info>::iterator i = queue.begin();
|
||||
i != queue.end();
|
||||
++i)
|
||||
{
|
||||
if (i->finished_blocks[j]) out << "#";
|
||||
else if (i->requested_blocks[j]) out << "+";
|
||||
else out << ".";
|
||||
out.width(4);
|
||||
out.fill(' ');
|
||||
out << i->piece_index << ": |";
|
||||
for (int j = 0; j < i->blocks_in_piece; ++j)
|
||||
{
|
||||
if (i->finished_blocks[j]) out << "#";
|
||||
else if (i->requested_blocks[j]) out << "+";
|
||||
else out << ".";
|
||||
}
|
||||
out << "|\n";
|
||||
}
|
||||
out << "|\n";
|
||||
|
||||
out << "___________________________________\n";
|
||||
}
|
||||
|
||||
out << "___________________________________\n";
|
||||
|
||||
}
|
||||
|
||||
for (std::deque<std::string>::iterator i = events.begin();
|
||||
i != events.end();
|
||||
++i)
|
||||
if (print_log)
|
||||
{
|
||||
out << *i << "\n";
|
||||
for (std::deque<std::string>::iterator i = events.begin();
|
||||
i != events.end();
|
||||
++i)
|
||||
{
|
||||
out << *i << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
clear();
|
||||
|
|
|
@ -327,8 +327,8 @@ namespace libtorrent
|
|||
void set_upload_rate_limit(int bytes_per_second);
|
||||
void set_download_rate_limit(int bytes_per_second);
|
||||
|
||||
// TODO: add a session_status that contain
|
||||
// some indication of if the listen-port works
|
||||
// TODO: global max connections setting
|
||||
// TODO: global max uploads setting
|
||||
|
||||
std::auto_ptr<alert> pop_alert();
|
||||
void set_severity_level(alert::severity_t s);
|
||||
|
|
|
@ -208,7 +208,7 @@ namespace libtorrent
|
|||
|
||||
// returns true if it is time for this torrent to make another
|
||||
// tracker request
|
||||
bool should_request() const;
|
||||
bool should_request();
|
||||
|
||||
// forcefully sets next_announce to the current time
|
||||
void force_tracker_request();
|
||||
|
@ -343,6 +343,9 @@ namespace libtorrent
|
|||
|
||||
// is true if this torrent has been paused
|
||||
bool m_paused;
|
||||
// this is true from the time when the torrent was
|
||||
// paused to the time should_request() is called
|
||||
bool m_just_paused;
|
||||
|
||||
tracker_request::event_t m_event;
|
||||
|
||||
|
@ -446,20 +449,16 @@ namespace libtorrent
|
|||
// (size 0) if we haven't received any metadata
|
||||
// or if we already have all metadata
|
||||
std::vector<bool> m_have_metadata;
|
||||
// this vector keeps track of how many times each meatdata
|
||||
// block has been requested
|
||||
std::vector<int> m_requested_metadata;
|
||||
|
||||
boost::filesystem::path m_save_path;
|
||||
};
|
||||
|
||||
inline boost::posix_time::ptime torrent::next_announce() const
|
||||
{ return m_next_request; }
|
||||
|
||||
// returns true if it is time for this torrent to make another
|
||||
// tracker request
|
||||
inline bool torrent::should_request() const
|
||||
{
|
||||
namespace time = boost::posix_time;
|
||||
return !m_paused &&
|
||||
m_next_request < time::second_clock::local_time();
|
||||
return m_next_request;
|
||||
}
|
||||
|
||||
inline void torrent::force_tracker_request()
|
||||
|
@ -476,22 +475,6 @@ namespace libtorrent
|
|||
m_password = pw;
|
||||
}
|
||||
|
||||
inline void torrent::set_upload_limit(int limit)
|
||||
{
|
||||
assert(limit >= -1);
|
||||
if (limit == -1) limit = std::numeric_limits<int>::max();
|
||||
if (limit < num_peers() * 10) limit = num_peers() * 10;
|
||||
m_upload_bandwidth_limit = limit;
|
||||
}
|
||||
|
||||
inline void torrent::set_download_limit(int limit)
|
||||
{
|
||||
assert(limit >= -1);
|
||||
if (limit == -1) limit = std::numeric_limits<int>::max();
|
||||
if (limit < num_peers() * 10) limit = num_peers() * 10;
|
||||
m_download_bandwidth_limit = limit;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // TORRENT_TORRENT_HPP_INCLUDED
|
||||
|
|
|
@ -89,6 +89,7 @@ namespace libtorrent
|
|||
torrent_info(int piece_size, const char* name, sha1_hash const& info_hash = sha1_hash(0));
|
||||
|
||||
entry create_torrent() const;
|
||||
entry create_info_metadata() const;
|
||||
void set_comment(char const* str);
|
||||
void set_creator(char const* str);
|
||||
void set_hash(int index, const sha1_hash& h);
|
||||
|
|
|
@ -1078,6 +1078,8 @@ namespace libtorrent
|
|||
(*m_logger) << i->first << "\n";
|
||||
}
|
||||
#endif
|
||||
// TODO: move the requesting into second-tick
|
||||
// of the torrent.
|
||||
if (!m_torrent->valid_metadata()
|
||||
&& supports_extension(extended_metadata_message))
|
||||
{
|
||||
|
|
|
@ -770,7 +770,6 @@ namespace libtorrent
|
|||
// if the connection comes from the tracker,
|
||||
// it's probably just a NAT-check. Ignore the
|
||||
// num connections constraint then.
|
||||
// TODO: make sure this works
|
||||
// TODO: only allow _one_ connection to use this
|
||||
// override at a time
|
||||
if (m_torrent->num_peers() >= m_max_connections
|
||||
|
|
|
@ -615,24 +615,25 @@ namespace libtorrent { namespace detail
|
|||
for (std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i
|
||||
= m_torrents.begin(); i != m_torrents.end();)
|
||||
{
|
||||
if (i->second->is_aborted())
|
||||
torrent& t = *i->second;
|
||||
if (t.is_aborted())
|
||||
{
|
||||
tracker_request req = i->second->generate_tracker_request();
|
||||
tracker_request req = t.generate_tracker_request();
|
||||
req.listen_port = m_listen_interface.port;
|
||||
req.key = m_key;
|
||||
m_tracker_manager.queue_request(req);
|
||||
i->second->disconnect_all();
|
||||
t.disconnect_all();
|
||||
purge_connections();
|
||||
#ifndef NDEBUG
|
||||
sha1_hash i_hash = i->second->torrent_file().info_hash();
|
||||
sha1_hash i_hash = t.torrent_file().info_hash();
|
||||
#endif
|
||||
m_torrents.erase(i++);
|
||||
assert(m_torrents.find(i_hash) == m_torrents.end());
|
||||
continue;
|
||||
}
|
||||
else if (i->second->should_request())
|
||||
else if (t.should_request())
|
||||
{
|
||||
tracker_request req = i->second->generate_tracker_request();
|
||||
tracker_request req = t.generate_tracker_request();
|
||||
req.listen_port = m_listen_interface.port;
|
||||
req.key = m_key;
|
||||
m_tracker_manager.queue_request(
|
||||
|
@ -641,7 +642,7 @@ namespace libtorrent { namespace detail
|
|||
}
|
||||
|
||||
// tick() will set the used upload quota
|
||||
i->second->second_tick(m_stat);
|
||||
t.second_tick(m_stat);
|
||||
++i;
|
||||
}
|
||||
purge_connections();
|
||||
|
|
|
@ -152,12 +152,13 @@ namespace libtorrent
|
|||
: m_torrent_file(metadata)
|
||||
, m_abort(false)
|
||||
, m_paused(false)
|
||||
, m_just_paused(false)
|
||||
, m_event(tracker_request::started)
|
||||
, m_block_size(0)
|
||||
, m_storage(0)
|
||||
, m_next_request(boost::posix_time::second_clock::local_time())
|
||||
, m_duration(1800)
|
||||
, m_policy(new policy(this)) // warning: uses this in member init list
|
||||
, m_policy()
|
||||
, m_ses(ses)
|
||||
, m_picker(0)
|
||||
, m_last_working_tracker(-1)
|
||||
|
@ -174,6 +175,7 @@ namespace libtorrent
|
|||
, m_download_bandwidth_limit(std::numeric_limits<int>::max())
|
||||
, m_save_path(save_path)
|
||||
{
|
||||
m_policy.reset(new policy(this));
|
||||
bencode(std::back_inserter(m_metadata), metadata["info"]);
|
||||
init();
|
||||
}
|
||||
|
@ -187,12 +189,13 @@ namespace libtorrent
|
|||
: m_torrent_file(0, 0, info_hash)
|
||||
, m_abort(false)
|
||||
, m_paused(false)
|
||||
, m_just_paused(false)
|
||||
, m_event(tracker_request::started)
|
||||
, m_block_size(0)
|
||||
, m_storage(0)
|
||||
, m_next_request(boost::posix_time::second_clock::local_time())
|
||||
, m_duration(1800)
|
||||
, m_policy(new policy(this)) // warning: uses this in member init list
|
||||
, m_policy()
|
||||
, m_ses(ses)
|
||||
, m_picker(0)
|
||||
, m_last_working_tracker(-1)
|
||||
|
@ -209,6 +212,8 @@ namespace libtorrent
|
|||
, m_download_bandwidth_limit(std::numeric_limits<int>::max())
|
||||
, m_save_path(save_path)
|
||||
{
|
||||
m_requested_metadata.resize(256, 0);
|
||||
m_policy.reset(new policy(this));
|
||||
m_torrent_file.add_tracker(tracker_url);
|
||||
}
|
||||
|
||||
|
@ -235,6 +240,20 @@ namespace libtorrent
|
|||
m_net_interface = address(net_interface, address::any_port);
|
||||
}
|
||||
|
||||
// returns true if it is time for this torrent to make another
|
||||
// tracker request
|
||||
bool torrent::should_request()
|
||||
{
|
||||
namespace time = boost::posix_time;
|
||||
if (m_just_paused)
|
||||
{
|
||||
m_just_paused = false;
|
||||
return true;
|
||||
}
|
||||
return !m_paused &&
|
||||
m_next_request < time::second_clock::local_time();
|
||||
}
|
||||
|
||||
void torrent::tracker_response(
|
||||
std::vector<peer_entry>& peer_list
|
||||
, int interval)
|
||||
|
@ -695,19 +714,39 @@ namespace libtorrent
|
|||
}
|
||||
#endif
|
||||
|
||||
void torrent::set_upload_limit(int limit)
|
||||
{
|
||||
assert(limit >= -1);
|
||||
if (limit == -1) limit = std::numeric_limits<int>::max();
|
||||
if (limit < num_peers() * 10) limit = num_peers() * 10;
|
||||
m_upload_bandwidth_limit = limit;
|
||||
}
|
||||
|
||||
void torrent::set_download_limit(int limit)
|
||||
{
|
||||
assert(limit >= -1);
|
||||
if (limit == -1) limit = std::numeric_limits<int>::max();
|
||||
if (limit < num_peers() * 10) limit = num_peers() * 10;
|
||||
m_download_bandwidth_limit = limit;
|
||||
}
|
||||
|
||||
void torrent::pause()
|
||||
{
|
||||
disconnect_all();
|
||||
// TODO: announce to tracker that we stopped!
|
||||
// possibly with some delay
|
||||
m_paused = true;
|
||||
// tell the tracker that we stopped
|
||||
m_event = tracker_request::stopped;
|
||||
m_just_paused = true;
|
||||
}
|
||||
|
||||
void torrent::resume()
|
||||
{
|
||||
m_paused = false;
|
||||
// TODO: announce to the tracker that we started.
|
||||
// possibly with some delay
|
||||
|
||||
// tell the tracker that we're back
|
||||
m_event = tracker_request::started;
|
||||
force_tracker_request();
|
||||
|
||||
// make pulse be called as soon as possible
|
||||
m_time_scaler = 0;
|
||||
}
|
||||
|
@ -974,8 +1013,10 @@ namespace libtorrent
|
|||
|
||||
// clear the storage for the bitfield
|
||||
{
|
||||
std::vector<bool> t;
|
||||
m_have_metadata.swap(t);
|
||||
std::vector<bool> t1;
|
||||
m_have_metadata.swap(t1);
|
||||
std::vector<int> t2;
|
||||
m_requested_metadata.swap(t2);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -310,37 +310,11 @@ namespace libtorrent
|
|||
m_created_by = str;
|
||||
}
|
||||
|
||||
entry torrent_info::create_torrent() const
|
||||
entry torrent_info::create_info_metadata() const
|
||||
{
|
||||
assert(m_piece_length > 0);
|
||||
|
||||
using namespace boost::gregorian;
|
||||
using namespace boost::posix_time;
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
entry dict(entry::dictionary_t);
|
||||
|
||||
if (m_urls.empty() || m_files.empty())
|
||||
{
|
||||
// TODO: throw something here
|
||||
// throw
|
||||
return entry();
|
||||
}
|
||||
|
||||
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 (!m_created_by.empty())
|
||||
dict["created by"] = m_created_by;
|
||||
|
||||
entry& info = dict["info"];
|
||||
info = entry(entry::dictionary_t);
|
||||
entry info(entry::dictionary_t);
|
||||
|
||||
info["length"] = m_total_size;
|
||||
|
||||
|
@ -391,6 +365,39 @@ namespace libtorrent
|
|||
{
|
||||
p.append((char*)i->begin(), (char*)i->end());
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
entry torrent_info::create_torrent() const
|
||||
{
|
||||
assert(m_piece_length > 0);
|
||||
|
||||
using namespace boost::gregorian;
|
||||
using namespace boost::posix_time;
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
entry dict(entry::dictionary_t);
|
||||
|
||||
if (m_urls.empty() || m_files.empty())
|
||||
{
|
||||
// TODO: throw something here
|
||||
// throw
|
||||
return entry();
|
||||
}
|
||||
|
||||
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 (!m_created_by.empty())
|
||||
dict["created by"] = m_created_by;
|
||||
|
||||
dict["info"] = create_info_metadata();
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue