premiere-libtorrent/include/libtorrent/session.hpp

450 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.
*/
2003-10-26 18:35:23 +01:00
#ifndef TORRENT_SESSION_HPP_INCLUDED
#define TORRENT_SESSION_HPP_INCLUDED
#include <ctime>
#include <algorithm>
#include <vector>
#include <set>
#include <list>
2003-10-31 05:02:51 +01:00
#include <deque>
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <boost/limits.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/thread.hpp>
#include <boost/thread/recursive_mutex.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/peer_connection.hpp"
#include "libtorrent/peer_id.hpp"
#include "libtorrent/policy.hpp"
2004-01-31 11:46:15 +01:00
#include "libtorrent/tracker_manager.hpp"
#include "libtorrent/peer_info.hpp"
2003-11-29 17:34:07 +01:00
#include "libtorrent/alert.hpp"
#include "libtorrent/fingerprint.hpp"
#include "libtorrent/debug.hpp"
#include "libtorrent/peer_request.hpp"
#include "libtorrent/piece_block_progress.hpp"
#include "libtorrent/ip_filter.hpp"
#include "libtorrent/config.hpp"
#include "libtorrent/session_settings.hpp"
#include "libtorrent/version.hpp"
#include "libtorrent/kademlia/dht_tracker.hpp"
#include "libtorrent/session_status.hpp"
2004-01-02 21:46:24 +01:00
#if !defined(NDEBUG) && defined(_MSC_VER)
# include <float.h>
# include <eh.h>
2004-01-02 00:23:17 +01:00
#endif
namespace libtorrent
{
class torrent;
enum extension_index
{
extended_handshake,
extended_chat_message,
extended_metadata_message,
extended_peer_exchange_message,
num_supported_extensions
};
namespace detail
{
2003-11-05 18:42:27 +01:00
// workaround for microsofts
// hardware exceptions that makes
// it hard to debug stuff
2004-01-24 18:14:03 +01:00
#if defined(_MSC_VER)
2003-11-05 18:42:27 +01:00
struct eh_initializer
{
eh_initializer()
2004-01-02 21:46:24 +01:00
{
2004-01-24 18:14:03 +01:00
#ifndef NDEBUG
2004-01-02 21:46:24 +01:00
_clearfp();
_controlfp(_EM_INEXACT | _EM_UNDERFLOW, _MCW_EM );
::_set_se_translator(straight_to_debugger);
2004-10-16 03:10:42 +02:00
#endif
2004-01-02 21:46:24 +01:00
}
2003-11-05 18:42:27 +01:00
2004-01-02 00:23:17 +01:00
static void straight_to_debugger(unsigned int, _EXCEPTION_POINTERS*)
2003-11-05 18:42:27 +01:00
{ throw; }
};
#else
struct eh_initializer {};
#endif
2003-10-30 00:28:09 +01:00
// this data is shared between the main thread and the
// thread that initialize pieces
struct piece_checker_data
{
2005-01-08 22:12:19 +01:00
piece_checker_data()
2005-01-11 03:13:07 +01:00
: processing(false), progress(0.f), abort(false) {}
2003-10-30 00:28:09 +01:00
boost::shared_ptr<torrent> torrent_ptr;
boost::filesystem::path save_path;
sha1_hash info_hash;
2004-01-02 21:46:24 +01:00
void parse_resume_data(
2004-01-07 01:48:02 +01:00
const entry& rd
2005-06-16 17:41:04 +02:00
, const torrent_info& info
, std::string& error);
2004-01-12 04:05:10 +01:00
2004-01-02 21:46:24 +01:00
std::vector<int> piece_map;
std::vector<piece_picker::downloading_piece> unfinished_pieces;
std::vector<tcp::endpoint> peers;
2004-09-08 01:16:11 +02:00
entry resume_data;
2004-01-02 21:46:24 +01:00
2005-01-08 22:12:19 +01:00
// this is true if this torrent is being processed (checked)
// if it is not being processed, then it can be removed from
// the queue without problems, otherwise the abort flag has
// to be set.
bool processing;
2005-01-08 22:12:19 +01:00
2003-10-30 00:28:09 +01:00
// is filled in by storage::initialize_pieces()
// and represents the progress. It should be a
// value in the range [0, 1]
float progress;
2003-10-30 00:28:09 +01:00
// abort defaults to false and is typically
// filled in by torrent_handle when the user
// aborts the torrent
bool abort;
};
2003-10-31 05:02:51 +01:00
struct checker_impl: boost::noncopyable
{
2004-01-25 13:37:15 +01:00
checker_impl(session_impl& s): m_ses(s), m_abort(false) {}
void operator()();
2003-10-31 05:02:51 +01:00
piece_checker_data* find_torrent(const sha1_hash& info_hash);
2005-01-08 22:12:19 +01:00
void remove_torrent(sha1_hash const& info_hash);
2003-10-31 05:02:51 +01:00
#ifndef NDEBUG
void check_invariant() const;
#endif
2003-10-31 05:02:51 +01:00
// when the files has been checked
// the torrent is added to the session
2004-01-25 13:37:15 +01:00
session_impl& m_ses;
2004-02-26 13:59:01 +01:00
mutable boost::mutex m_mutex;
2003-10-31 05:02:51 +01:00
boost::condition m_cond;
2004-11-18 23:33:50 +01:00
// a list of all torrents that are currently in queue
// or checking their files
std::deque<boost::shared_ptr<piece_checker_data> > m_torrents;
std::deque<boost::shared_ptr<piece_checker_data> > m_processing;
2003-10-31 05:02:51 +01:00
bool m_abort;
};
// this is the link between the main thread and the
// thread started to run the main downloader loop
2003-10-31 05:02:51 +01:00
struct session_impl: boost::noncopyable
{
friend class invariant_access;
typedef std::map<boost::shared_ptr<stream_socket>
, boost::intrusive_ptr<peer_connection> >
connection_map;
2004-02-17 01:18:29 +01:00
typedef std::map<sha1_hash, boost::shared_ptr<torrent> > torrent_map;
typedef std::deque<boost::intrusive_ptr<peer_connection> >
connection_queue;
2004-01-26 11:29:00 +01:00
session_impl(
std::pair<int, int> listen_port_range
2004-03-29 00:44:40 +02:00
, fingerprint const& cl_fprint
, char const* listen_interface = "0.0.0.0");
2004-01-26 11:29:00 +01:00
2003-10-31 05:02:51 +01:00
void operator()();
2004-02-26 13:59:01 +01:00
void open_listen_port();
void async_accept();
void on_incoming_connection(boost::shared_ptr<stream_socket> const& s
, boost::weak_ptr<socket_acceptor> const& as, asio::error const& e);
// must be locked to access the data
// in this struct
typedef boost::recursive_mutex mutex_t;
mutable mutex_t m_mutex;
boost::weak_ptr<torrent> find_torrent(const sha1_hash& info_hash);
2004-12-21 13:30:09 +01:00
peer_id const& get_peer_id() const { return m_peer_id; }
// this is where all active sockets are stored.
// the selector can sleep while there's no activity on
// them
demuxer m_selector;
tracker_manager m_tracker_manager;
2004-02-17 01:18:29 +01:00
torrent_map m_torrents;
2004-01-20 12:01:50 +01:00
// this will see if there are any pending connection attempts
// and in that case initiate new connections until the limit
// is reached.
void process_connection_queue();
void close_connection(boost::intrusive_ptr<peer_connection> const& p);
void connection_completed(boost::intrusive_ptr<peer_connection> const& p);
void connection_failed(boost::shared_ptr<stream_socket> const& s
, tcp::endpoint const& a, char const* message);
void set_settings(session_settings const& s);
2004-01-20 12:01:50 +01:00
// this maps sockets to their peer_connection
// object. It is the complete list of all connected
// peers.
connection_map m_connections;
// this is a list of half-open tcp connections
// (only outgoing connections)
connection_map m_half_open;
// this is a queue of pending outgoing connections. If the
// list of half-open connections is full (given the global
// limit), new outgoing connections are put on this queue,
// waiting for one slot in the half-open queue to open up.
connection_queue m_connection_queue;
// filters incoming connections
ip_filter m_ip_filter;
2004-03-21 03:03:37 +01:00
// the peer id that is generated at the start of the session
peer_id m_peer_id;
2004-03-21 03:03:37 +01:00
// the key is an id that is used to identify the
// client with the tracker only. It is randomized
// at startup
int m_key;
2004-01-26 11:29:00 +01:00
// the range of ports we try to listen on
std::pair<int, int> m_listen_port_range;
2004-02-26 01:27:06 +01:00
// the ip-address of the interface
// we are supposed to listen on.
// if the ip is set to zero, it means
// that we should let the os decide which
// interface to listen on
tcp::endpoint m_listen_interface;
boost::shared_ptr<socket_acceptor> m_listen_socket;
2004-02-26 13:59:01 +01:00
2004-11-01 00:16:08 +01:00
// the entries in this array maps the
// extension index (as specified in peer_connection)
bool m_extension_enabled[num_supported_extensions];
2004-11-01 00:16:08 +01:00
bool extensions_enabled() const;
// the settings for the client
session_settings m_settings;
2003-11-09 19:17:09 +01:00
// set to true when the session object
// is being destructed and the thread
// should exit
2003-10-31 16:06:32 +01:00
volatile bool m_abort;
2003-11-09 19:17:09 +01:00
// maximum upload rate given in
// bytes per second. -1 means
// unlimited
int m_upload_rate;
2004-03-28 19:45:37 +02:00
int m_download_rate;
2004-10-29 15:21:09 +02:00
int m_max_uploads;
int m_max_connections;
// the number of simultaneous half-open tcp
// connections libtorrent will have.
int m_half_open_limit;
2003-11-09 19:17:09 +01:00
2004-04-18 14:28:02 +02:00
// statistics gathered from all torrents.
stat m_stat;
2003-11-29 17:34:07 +01:00
// handles delayed alerts
alert_manager m_alerts;
2003-12-22 08:14:35 +01:00
// is false by default and set to true when
// the first incoming connection is established
// this is used to know if the client is behind
// NAT or not.
bool m_incoming_connection;
2004-01-20 12:01:50 +01:00
// does the actual disconnections
// that are queued up in m_disconnect_peer
void second_tick(asio::error const& e);
boost::posix_time::ptime m_last_tick;
2004-01-20 12:01:50 +01:00
#ifndef TORRENT_DISABLE_DHT
boost::scoped_ptr<dht::dht_tracker> m_dht;
dht_settings m_dht_settings;
#endif
// the timer used to fire the second_tick
deadline_timer m_timer;
2003-11-09 19:17:09 +01:00
#ifndef NDEBUG
void check_invariant(const char *place = 0);
2005-03-19 13:22:40 +01:00
#endif
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
boost::shared_ptr<logger> create_log(std::string const& name, bool append = true);
boost::shared_ptr<logger> m_logger;
#endif
};
}
class TORRENT_EXPORT session: public boost::noncopyable, detail::eh_initializer
{
public:
session(fingerprint const& print = fingerprint("LT"
, LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0));
2004-02-26 01:27:06 +01:00
session(
2004-03-29 00:44:40 +02:00
fingerprint const& print
, std::pair<int, int> listen_port_range
, char const* listen_interface = "0.0.0.0");
~session();
2004-11-18 23:33:50 +01:00
std::vector<torrent_handle> get_torrents();
// all torrent_handles must be destructed before the session is destructed!
2004-01-02 21:46:24 +01:00
torrent_handle add_torrent(
torrent_info const& ti
, boost::filesystem::path const& save_path
, entry const& resume_data = entry()
2005-07-10 12:42:00 +02:00
, bool compact_mode = true
, int block_size = 16 * 1024);
// TODO: deprecated, this is for backwards compatibility only
torrent_handle add_torrent(
entry const& e
, boost::filesystem::path const& save_path
, entry const& resume_data = entry()
, bool compact_mode = true
, int block_size = 16 * 1024)
{
return add_torrent(torrent_info(e), save_path, resume_data
, compact_mode, block_size);
}
torrent_handle add_torrent(
char const* tracker_url
, sha1_hash const& info_hash
, boost::filesystem::path const& save_path
, entry const& resume_data = entry()
2005-07-10 12:42:00 +02:00
, bool compact_mode = true
, int block_size = 16 * 1024);
2004-01-02 21:46:24 +01:00
2004-04-18 14:28:02 +02:00
session_status status() const;
#ifndef TORRENT_DISABLE_DHT
void start_dht(entry const& startup_state = entry());
void stop_dht();
void set_dht_settings(dht_settings const& settings);
entry dht_state() const;
void add_dht_node(std::pair<std::string, int> const& node);
void add_dht_router(std::pair<std::string, int> const& node);
#endif
void enable_extension(extension_index i);
2004-11-01 00:16:08 +01:00
void disable_extensions();
void set_ip_filter(ip_filter const& f);
void set_peer_id(peer_id const& pid);
2005-01-08 22:12:19 +01:00
void set_key(int key);
2004-12-21 13:30:09 +01:00
2004-02-26 13:59:01 +01:00
bool is_listening() const;
// if the listen port failed in some way
// you can retry to listen on another port-
// range with this function. If the listener
// succeeded and is currently listening,
// a call to this function will shut down the
// listen port and reopen it using these new
// properties (the given interface and port range).
// As usual, if the interface is left as 0
// this function will return false on failure.
// If it fails, it will also generate alerts describing
// the error. It will return true on success.
bool listen_on(
std::pair<int, int> const& port_range
2004-03-01 22:54:10 +01:00
, const char* net_interface = 0);
2004-02-26 13:59:01 +01:00
// returns the port we ended up listening on
unsigned short listen_port() const;
2004-02-18 01:08:20 +01:00
2003-11-08 03:16:26 +01:00
void remove_torrent(const torrent_handle& h);
void set_settings(session_settings const& s);
session_settings const& settings();
2003-11-09 19:17:09 +01:00
void set_upload_rate_limit(int bytes_per_second);
2004-03-28 19:45:37 +02:00
void set_download_rate_limit(int bytes_per_second);
2004-10-29 15:21:09 +02:00
void set_max_uploads(int limit);
void set_max_connections(int limit);
void set_max_half_open_connections(int limit);
2003-12-22 08:14:35 +01:00
2003-11-29 17:34:07 +01:00
std::auto_ptr<alert> pop_alert();
2003-12-22 08:14:35 +01:00
void set_severity_level(alert::severity_t s);
2003-11-29 17:34:07 +01:00
private:
2003-10-31 05:02:51 +01:00
// data shared between the main thread
// and the working thread
detail::session_impl m_impl;
2003-10-31 05:02:51 +01:00
// data shared between the main thread
// and the checker thread
detail::checker_impl m_checker_impl;
2003-10-30 00:28:09 +01:00
// the main working thread
boost::thread m_thread;
2003-10-31 05:02:51 +01:00
// the thread that calls initialize_pieces()
// on all torrents before they start downloading
boost::thread m_checker_thread;
};
}
#endif // TORRENT_SESSION_HPP_INCLUDED