premiere-libtorrent/include/libtorrent/torrent_handle.hpp

237 lines
6.1 KiB
C++
Raw Normal View History

2003-10-26 18:35:23 +01:00
/*
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_HANDLE_HPP_INCLUDED
#define TORRENT_TORRENT_HANDLE_HPP_INCLUDED
#include <vector>
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <boost/date_time/posix_time/posix_time.hpp>
2003-10-26 18:35:23 +01:00
#ifdef _MSC_VER
#pragma warning(pop)
#endif
2003-10-26 18:35:23 +01:00
#include "libtorrent/peer_id.hpp"
#include "libtorrent/peer_info.hpp"
#include "libtorrent/piece_picker.hpp"
2003-11-28 18:29:27 +01:00
#include "libtorrent/torrent_info.hpp"
2003-10-26 18:35:23 +01:00
namespace libtorrent
{
namespace detail
{
struct session_impl;
2003-10-31 05:02:51 +01:00
struct checker_impl;
2003-10-26 18:35:23 +01:00
}
struct duplicate_torrent: std::exception
{
2003-11-05 00:27:06 +01:00
virtual const char* what() const throw()
{ return "torrent already exists in session"; }
};
2003-11-28 18:29:27 +01:00
struct invalid_handle: std::exception
{
virtual const char* what() const throw()
{ return "invalid torrent handle used"; }
};
2003-10-31 05:02:51 +01:00
struct torrent_status
2003-10-26 18:35:23 +01:00
{
2003-12-22 08:14:35 +01:00
torrent_status()
: state(queued_for_checking)
, progress(0.f)
, total_download(0)
, total_upload(0)
, total_payload_download(0)
, total_payload_upload(0)
, download_rate(0)
, upload_rate(0)
2004-01-15 02:01:09 +01:00
, num_peers(0)
2004-01-15 17:45:34 +01:00
, pieces(0)
2003-12-22 08:14:35 +01:00
, total_done(0)
{}
2003-10-26 18:35:23 +01:00
enum state_t
{
2003-10-31 05:02:51 +01:00
queued_for_checking,
2003-10-26 18:35:23 +01:00
checking_files,
2003-12-22 08:14:35 +01:00
connecting_to_tracker,
2003-10-26 18:35:23 +01:00
downloading,
seeding
};
2003-10-31 05:02:51 +01:00
state_t state;
float progress;
2003-11-05 00:27:06 +01:00
boost::posix_time::time_duration next_announce;
2004-01-17 21:04:19 +01:00
boost::posix_time::time_duration announce_interval;
2004-01-31 11:20:19 +01:00
std::string current_tracker;
// transferred this session!
2003-12-22 08:14:35 +01:00
// total, payload plus protocol
2004-01-15 17:45:34 +01:00
size_type total_download;
size_type total_upload;
2003-12-22 08:14:35 +01:00
// payload only
2004-01-15 17:45:34 +01:00
size_type total_payload_download;
size_type total_payload_upload;
2003-12-22 08:14:35 +01:00
// current transfer rate
// payload plus protocol
2003-12-07 06:53:04 +01:00
float download_rate;
float upload_rate;
2003-12-22 08:14:35 +01:00
2004-01-15 02:01:09 +01:00
// the number of peers this torrent
// is connected to.
int num_peers;
2004-01-15 17:45:34 +01:00
const std::vector<bool>* pieces;
// the number of bytes of the file we have
2004-01-15 17:45:34 +01:00
size_type total_done;
2003-10-31 05:02:51 +01:00
};
2003-10-26 18:35:23 +01:00
struct partial_piece_info
{
enum { max_blocks_per_piece = piece_picker::max_blocks_per_piece };
int piece_index;
int blocks_in_piece;
std::bitset<max_blocks_per_piece> requested_blocks;
std::bitset<max_blocks_per_piece> finished_blocks;
2004-01-12 21:31:27 +01:00
address peer[max_blocks_per_piece];
int num_downloads[max_blocks_per_piece];
};
2003-10-31 05:02:51 +01:00
struct torrent_handle
{
2004-02-29 17:39:52 +01:00
friend class invariant_access;
2003-10-31 05:02:51 +01:00
friend class session;
2003-12-22 08:14:35 +01:00
friend class torrent;
2003-10-31 05:02:51 +01:00
torrent_handle(): m_ses(0) {}
2003-12-07 06:53:04 +01:00
void get_peer_info(std::vector<peer_info>& v) const;
torrent_status status() const;
void get_download_queue(std::vector<partial_piece_info>& queue) const;
2003-11-28 18:29:27 +01:00
2003-12-07 06:53:04 +01:00
const torrent_info& get_torrent_info() const;
bool is_valid() const;
2004-02-26 01:27:06 +01:00
// set the interface to bind outgoing connections
// to.
void use_interface(const char* net_interface);
2004-02-24 17:22:43 +01:00
// TODO: there must be a way to get resume data and
// shut down the torrent in one atomic operation
2004-02-25 00:55:42 +01:00
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.
2004-01-02 21:46:24 +01:00
2004-01-08 18:03:04 +01:00
// forces this torrent to reannounce
// (make a rerequest from the tracker)
void force_reannounce() const;
2003-10-30 00:28:09 +01:00
2004-02-26 13:59:01 +01:00
// TODO: add a feature where the user can tell the torrent
2003-11-28 18:29:27 +01:00
// to finish all pieces currently in the pipeline, and then
// abort the torrent.
2003-10-26 18:35:23 +01:00
2004-02-01 01:50:18 +01:00
// TODO: add a max upload rate per torrent too.
2004-01-08 18:03:04 +01:00
// manually connect a peer
void connect_peer(const address& adr) const;
2004-01-12 04:05:10 +01:00
// valid ratios are 0 (infinite ratio) or [ 1.0 , inf )
// the ratio is uploaded / downloaded. less than 1 is not allowed
void set_ratio(float up_down_ratio);
2003-12-14 06:56:12 +01:00
// TODO: add finish_file_allocation, which will force the
// torrent to allocate storage for all pieces.
2003-12-07 06:53:04 +01:00
boost::filesystem::path save_path() const;
2003-12-14 06:56:12 +01:00
// -1 means unlimited unchokes
void set_max_uploads(int max_uploads);
2004-01-21 01:59:38 +01:00
// -1 means unlimited connections
void set_max_connections(int max_connections);
void set_tracker_login(std::string const& name, std::string const& password);
2003-12-07 06:53:04 +01:00
const sha1_hash& info_hash() const
{ return m_info_hash; }
bool operator==(const torrent_handle& h) const
{ return m_info_hash == h.m_info_hash; }
bool operator!=(const torrent_handle& h) const
{ return m_info_hash != h.m_info_hash; }
bool operator<(const torrent_handle& h) const
{ return m_info_hash < h.m_info_hash; }
2003-11-28 18:29:27 +01:00
private:
2003-11-08 03:16:26 +01:00
2003-10-31 05:02:51 +01:00
torrent_handle(detail::session_impl* s,
detail::checker_impl* c,
const sha1_hash& h)
2003-10-26 18:35:23 +01:00
: m_ses(s)
2003-10-31 05:02:51 +01:00
, m_chk(c)
2003-10-26 18:35:23 +01:00
, m_info_hash(h)
2004-02-29 17:39:52 +01:00
{
assert(m_ses != 0);
}
#ifndef NDEBUG
void check_invariant() const;
#endif
2003-10-26 18:35:23 +01:00
detail::session_impl* m_ses;
2003-10-31 05:02:51 +01:00
detail::checker_impl* m_chk;
2004-01-03 04:22:53 +01:00
sha1_hash m_info_hash;
2003-10-26 18:35:23 +01:00
};
}
#endif // TORRENT_TORRENT_HANDLE_HPP_INCLUDED