premiere-libtorrent/include/libtorrent/torrent.hpp

449 lines
13 KiB
C++
Raw Normal View History

/*
Copyright (c) 2003, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TORRENT_TORRENT_HPP_INCLUDE
#define TORRENT_TORRENT_HPP_INCLUDE
#include <algorithm>
#include <vector>
#include <set>
#include <list>
#include <iostream>
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <boost/limits.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
2003-10-26 18:35:23 +01:00
#include "libtorrent/torrent_handle.hpp"
#include "libtorrent/entry.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/policy.hpp"
#include "libtorrent/storage.hpp"
2004-01-31 11:46:15 +01:00
#include "libtorrent/tracker_manager.hpp"
2003-12-07 06:53:04 +01:00
#include "libtorrent/stat.hpp"
2003-12-22 08:14:35 +01:00
#include "libtorrent/alert.hpp"
2004-03-23 23:58:18 +01:00
#include "libtorrent/resource_request.hpp"
namespace libtorrent
{
#ifndef NDEBUG
struct logger;
#endif
2004-01-14 13:53:17 +01:00
std::string escape_string(const char* str, int len);
2004-01-14 20:24:11 +01:00
std::string unescape_string(std::string const& s);
2004-01-14 13:53:17 +01:00
2003-12-22 08:14:35 +01:00
namespace detail
{
struct session_impl;
}
// a torrent is a class that holds information
// for a specific download. It updates itself against
// the tracker
class torrent: public request_callback
{
public:
2003-12-07 06:53:04 +01:00
torrent(
detail::session_impl& ses
, const torrent_info& torrent_file
2004-02-26 01:27:06 +01:00
, const boost::filesystem::path& save_path
, address const& net_interface);
2003-12-14 06:56:12 +01:00
~torrent();
2004-03-28 19:45:37 +02:00
// this will flag the torrent as aborted. The main
// loop in session_impl will check for this state
// on all torrents once every second, and take
// the necessary actions then.
void abort() { m_abort = true; m_event = tracker_request::stopped; }
bool is_aborted() const { return m_abort; }
2004-03-28 19:45:37 +02:00
// is called every second by session. This will
// caclulate the upload/download and number
// of connections this torrent needs. And prepare
// it for being used by allocate_resources.
void second_tick();
2004-03-28 19:45:37 +02:00
// debug purpose only
void print(std::ostream& os) const;
2004-01-08 18:03:04 +01:00
void check_files(
detail::piece_checker_data& data
, boost::mutex& mutex);
2003-12-07 06:53:04 +01:00
stat statistics() const { return m_stat; }
size_type bytes_left() const;
2004-01-15 17:45:34 +01:00
size_type bytes_done() const;
2004-03-21 03:03:37 +01:00
void pause();
void resume();
bool is_paused() const { return m_paused; }
2003-10-31 05:02:51 +01:00
torrent_status status() const;
2004-02-26 01:27:06 +01:00
void use_interface(const char* net_interface);
2004-01-15 17:45:34 +01:00
peer_connection& connect_to_peer(const address& a);
2004-01-12 04:05:10 +01:00
void set_ratio(float ratio)
2004-01-25 13:37:15 +01:00
{ assert(ratio >= 0.0f); m_ratio = ratio; }
2004-01-12 04:05:10 +01:00
float ratio() const
2004-01-25 13:37:15 +01:00
{ return m_ratio; }
2004-01-12 04:05:10 +01:00
// --------------------------------------------
// PEER MANAGEMENT
// used by peer_connection to attach itself to a torrent
// since incoming connections don't know what torrent
// they're a part of until they have received an info_hash.
void attach_peer(peer_connection* p);
// this will remove the peer and make sure all
// the pieces it had have their reference counter
// decreased in the piece_picker
2004-01-15 17:45:34 +01:00
// called from the peer_connection destructor
void remove_peer(peer_connection* p);
2004-01-13 04:08:59 +01:00
peer_connection* connection_for(const address& a)
{
peer_iterator i = m_connections.find(a);
if (i == m_connections.end()) return 0;
return i->second;
}
// the number of peers that belong to this torrent
int num_peers() const { return (int)m_connections.size(); }
2004-01-13 04:08:59 +01:00
typedef std::map<address, peer_connection*>::iterator peer_iterator;
typedef std::map<address, peer_connection*>::const_iterator const_peer_iterator;
2003-11-05 00:27:06 +01:00
2004-01-13 04:08:59 +01:00
const_peer_iterator begin() const { return m_connections.begin(); }
const_peer_iterator end() const { return m_connections.end(); }
2003-11-05 00:27:06 +01:00
peer_iterator begin() { return m_connections.begin(); }
peer_iterator end() { return m_connections.end(); }
// --------------------------------------------
// TRACKER MANAGEMENT
2004-03-28 19:45:37 +02:00
// these are callbacks called by the tracker_connection instance
// (either http_tracker_connection or udp_tracker_connection)
// when this torrent got a response from its tracker request
2004-03-28 19:45:37 +02:00
// or when a failure occured
virtual void tracker_response(std::vector<peer_entry>& e, int interval);
2003-12-22 08:14:35 +01:00
virtual void tracker_request_timed_out();
virtual void tracker_request_error(int response_code, const std::string& str);
// generates a request string for sending
// to the tracker
2004-03-21 03:03:37 +01:00
tracker_request generate_tracker_request();
2004-03-28 19:45:37 +02:00
// if no password and username is set
// this will return an empty string, otherwise
// it will concatenate the login and password
// ready to be sent over http (but without
// base64 encoding).
std::string tracker_login() const;
// returns the absolute time when the next tracker
// announce will take place.
boost::posix_time::ptime next_announce() const;
// returns true if it is time for this torrent to make another
// tracker request
bool should_request() const;
// forcefully sets next_announce to the current time
void force_tracker_request();
// sets the username and password that will be sent to
// the tracker
void set_tracker_login(std::string const& name, std::string const& pw);
// the address of the tracker that we managed to
// announce ourself at the last time we tried to announce
const address& current_tracker() const;
// --------------------------------------------
// PIECE MANAGEMENT
// returns true if we have downloaded the given piece
2004-01-25 05:18:08 +01:00
bool have_piece(int index) const
2004-01-25 13:37:15 +01:00
{
assert(index >= 0 && index < (signed)m_have_pieces.size());
return m_have_pieces[index];
}
2003-12-17 04:40:13 +01:00
const std::vector<bool>& pieces() const
{ return m_have_pieces; }
// when we get a have- or bitfield- messages, this is called for every
// piece a peer has gained.
2003-12-18 04:30:41 +01:00
void peer_has(int index)
2004-01-25 13:37:15 +01:00
{
assert(index >= 0 && index < (signed)m_have_pieces.size());
m_picker.inc_refcount(index);
}
// when peer disconnects, this is called for every piece it had
void peer_lost(int index)
2004-01-25 13:37:15 +01:00
{
assert(index >= 0 && index < (signed)m_have_pieces.size());
m_picker.dec_refcount(index);
}
2004-01-25 13:37:15 +01:00
int block_size() const { return m_block_size; }
// this will tell all peers that we just got his piece
// and also let the piece picker know that we have this piece
// so it wont pick it for download
void announce_piece(int index);
2004-01-20 23:59:21 +01:00
void disconnect_all();
// this is called wheh the torrent has completed
// the download. It will post an event, disconnect
// all seeds and let the tracker know we're finished.
void completed();
2003-12-07 06:53:04 +01:00
bool verify_piece(int piece_index);
2003-12-01 22:27:27 +01:00
// this is called from the peer_connection
// each time a piece has failed the hash
// test
void piece_failed(int index);
2003-12-07 06:53:04 +01:00
float priority() const
{ return m_priority; }
void set_priority(float p)
{
2004-01-25 05:18:08 +01:00
assert(p >= 0.f && p <= 1.f);
2003-12-07 06:53:04 +01:00
m_priority = p;
}
2003-12-09 19:09:34 +01:00
bool is_seed() const
{ return m_num_pieces == m_torrent_file.num_pieces(); }
2003-12-07 06:53:04 +01:00
boost::filesystem::path save_path() const
{ return m_storage.save_path(); }
2004-01-07 01:48:02 +01:00
alert_manager& alerts() const;
2004-03-28 19:45:37 +02:00
piece_picker& picker() { return m_picker; }
policy& get_policy() { return *m_policy; }
piece_manager& filesystem() { return m_storage; }
torrent_info const& torrent_file() const { return m_torrent_file; }
2004-03-28 19:45:37 +02:00
torrent_handle get_handle() const;
2004-03-01 01:50:00 +01:00
// DEBUG
#ifndef NDEBUG
logger* spawn_logger(const char* title);
virtual void debug_log(const std::string& line);
2003-12-14 06:56:12 +01:00
void check_invariant();
#endif
2004-03-28 19:45:37 +02:00
// --------------------------------------------
// RESOURCE MANAGEMENT
2004-03-23 23:58:18 +01:00
// this will distribute the given upload/download
2004-03-28 19:45:37 +02:00
// quotas and number of connections, among the peers
2004-03-23 23:58:18 +01:00
void distribute_resources();
2004-03-28 19:45:37 +02:00
resource_request m_ul_bandwidth_quota;
resource_request m_dl_bandwidth_quota;
resource_request m_unchoked_quota;
resource_request m_connections_quota;
2004-03-23 23:58:18 +01:00
2004-03-28 19:45:37 +02:00
void set_upload_limit(int limit);
void set_download_limit(int limit);
2004-03-23 23:58:18 +01:00
2003-11-26 15:11:25 +01:00
private:
void try_next_tracker();
// the size of a request block
// each piece is divided into these
// blocks when requested
int m_block_size;
2003-10-23 18:55:52 +02:00
// is set to true when the torrent has
// been aborted.
bool m_abort;
2004-03-21 03:03:37 +01:00
// is true if this torrent has been paused
bool m_paused;
tracker_request::event_t m_event;
2004-01-15 17:45:34 +01:00
void parse_response(const entry& e, std::vector<peer_entry>& peer_list);
torrent_info m_torrent_file;
2003-12-07 02:26:57 +01:00
piece_manager m_storage;
// the time of next tracker request
boost::posix_time::ptime m_next_request;
// -----------------------------
// DATA FROM TRACKER RESPONSE
// the number number of seconds between requests
// from the tracker
int m_duration;
2004-01-13 04:08:59 +01:00
std::map<address, peer_connection*> m_connections;
2003-12-07 06:53:04 +01:00
// this is the upload and download statistics for the whole torrent.
// it's updated from all its peers once every second.
libtorrent::stat m_stat;
// -----------------------------
boost::shared_ptr<policy> m_policy;
2003-12-07 06:53:04 +01:00
// a back reference to the session
// this torrent belongs to.
detail::session_impl& m_ses;
piece_picker m_picker;
// this is an index into m_torrent_file.trackers()
int m_last_working_tracker;
int m_currently_trying_tracker;
// the number of connection attempts that has
// failed in a row
int m_failed_trackers;
// this is a counter that is increased every
// second, and when it reaches 10, the policy::pulse()
// is called and the time scaler is reset to 0.
int m_time_scaler;
2003-12-07 06:53:04 +01:00
// this is the priority of this torrent. It is used
// to weight the assigned upload bandwidth between peers
// it should be within the range [0, 1]
float m_priority;
// the bitmask that says which pieces we have
std::vector<bool> m_have_pieces;
// the number of pieces we have. The same as
// std::accumulate(m_have_pieces.begin(),
// m_have_pieces.end(), 0)
int m_num_pieces;
2003-12-22 08:14:35 +01:00
// is false by default and set to
// true when the first tracker reponse
// is received
bool m_got_tracker_response;
2004-01-12 04:05:10 +01:00
// the upload/download ratio that each peer
// tries to maintain.
// 0 is infinite
float m_ratio;
2004-04-18 14:28:02 +02:00
// the number of bytes that has been
// downloaded that failed the hash-test
size_type m_total_failed_bytes;
std::string m_username;
std::string m_password;
2004-03-23 23:58:18 +01:00
// the network interface all outgoing connections
// are opened through
2004-02-26 01:27:06 +01:00
address m_net_interface;
2004-03-23 23:58:18 +01:00
// the max number of bytes this torrent
// can upload per second
int m_upload_bandwidth_limit;
2004-03-28 19:45:37 +02:00
int m_download_bandwidth_limit;
};
2004-03-28 19:45:37 +02:00
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();
}
inline void torrent::force_tracker_request()
{
namespace time = boost::posix_time;
m_next_request = time::second_clock::local_time();
}
inline void torrent::set_tracker_login(
std::string const& name
, std::string const& pw)
{
m_username = name;
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