2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2006-10-11 16:02:21 +02:00
|
|
|
Copyright (c) 2006, Arvid Norberg
|
2003-10-23 01:00:57 +02:00
|
|
|
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
|
2003-10-23 01:00:57 +02:00
|
|
|
#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>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <boost/limits.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2003-10-26 18:35:23 +01:00
|
|
|
#include "libtorrent/torrent_handle.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
2003-11-29 17:34:07 +01:00
|
|
|
#include "libtorrent/alert.hpp"
|
2006-09-05 01:22:21 +02:00
|
|
|
#include "libtorrent/session_status.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/version.hpp"
|
|
|
|
#include "libtorrent/fingerprint.hpp"
|
|
|
|
|
2006-11-20 22:03:58 +01:00
|
|
|
#include "libtorrent/resource_request.hpp"
|
2007-03-16 06:29:23 +01:00
|
|
|
#include "libtorrent/storage.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-02-21 18:15:47 +01:00
|
|
|
#ifdef _MSC_VER
|
2004-01-02 21:46:24 +01:00
|
|
|
# include <eh.h>
|
2004-01-02 00:23:17 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
struct torrent_plugin;
|
2005-10-13 09:59:05 +02:00
|
|
|
class torrent;
|
2006-10-11 16:02:21 +02:00
|
|
|
class ip_filter;
|
2005-10-13 09:59:05 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-10-11 16:02:21 +02:00
|
|
|
namespace aux
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2003-11-05 18:42:27 +01:00
|
|
|
// workaround for microsofts
|
|
|
|
// hardware exceptions that makes
|
|
|
|
// it hard to debug stuff
|
2007-02-21 18:15:47 +01:00
|
|
|
#ifdef _MSC_VER
|
2003-11-05 18:42:27 +01:00
|
|
|
struct eh_initializer
|
|
|
|
{
|
|
|
|
eh_initializer()
|
2004-01-02 21:46:24 +01:00
|
|
|
{
|
|
|
|
::_set_se_translator(straight_to_debugger);
|
|
|
|
}
|
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
|
2006-10-11 16:02:21 +02:00
|
|
|
struct session_impl;
|
2006-12-31 00:02:21 +01:00
|
|
|
|
|
|
|
struct filesystem_init
|
|
|
|
{
|
|
|
|
filesystem_init();
|
|
|
|
};
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2006-10-11 16:02:21 +02:00
|
|
|
class TORRENT_EXPORT session_proxy
|
|
|
|
{
|
|
|
|
friend class session;
|
2006-10-15 15:46:52 +02:00
|
|
|
public:
|
|
|
|
session_proxy() {}
|
2006-10-11 16:02:21 +02:00
|
|
|
private:
|
|
|
|
session_proxy(boost::shared_ptr<aux::session_impl> impl)
|
|
|
|
: m_impl(impl) {}
|
|
|
|
boost::shared_ptr<aux::session_impl> m_impl;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TORRENT_EXPORT session: public boost::noncopyable, aux::eh_initializer
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
public:
|
2003-10-25 03:31:06 +02:00
|
|
|
|
2006-07-26 12:21:25 +02:00
|
|
|
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
|
2006-05-20 17:30:40 +02:00
|
|
|
, char const* listen_interface = "0.0.0.0");
|
2006-10-11 16:02:21 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
~session();
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
// returns a list of all torrents in this session
|
2006-10-11 16:02:21 +02:00
|
|
|
std::vector<torrent_handle> get_torrents() const;
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
// returns an invalid handle in case the torrent doesn't exist
|
|
|
|
torrent_handle find_torrent(sha1_hash const& info_hash) const;
|
2004-11-18 23:33:50 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// all torrent_handles must be destructed before the session is destructed!
|
2004-01-02 21:46:24 +01:00
|
|
|
torrent_handle add_torrent(
|
2005-10-16 18:58:41 +02:00
|
|
|
torrent_info const& ti
|
2004-06-14 01:30:42 +02:00
|
|
|
, boost::filesystem::path const& save_path
|
2005-05-13 02:39:39 +02:00
|
|
|
, entry const& resume_data = entry()
|
2005-07-10 12:42:00 +02:00
|
|
|
, bool compact_mode = true
|
2007-03-16 06:29:23 +01:00
|
|
|
, int block_size = 16 * 1024
|
|
|
|
, storage_constructor_type sc = default_storage_constructor);
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// TODO: deprecated, this is for backwards compatibility only
|
2005-10-16 18:58:41 +02:00
|
|
|
torrent_handle add_torrent(
|
|
|
|
entry const& e
|
|
|
|
, boost::filesystem::path const& save_path
|
|
|
|
, entry const& resume_data = entry()
|
|
|
|
, bool compact_mode = true
|
2007-03-16 06:29:23 +01:00
|
|
|
, int block_size = 16 * 1024
|
|
|
|
, storage_constructor_type sc = default_storage_constructor)
|
2005-10-16 18:58:41 +02:00
|
|
|
{
|
|
|
|
return add_torrent(torrent_info(e), save_path, resume_data
|
2007-03-16 06:29:23 +01:00
|
|
|
, compact_mode, block_size, sc);
|
2005-10-16 18:58:41 +02:00
|
|
|
}
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
torrent_handle add_torrent(
|
|
|
|
char const* tracker_url
|
|
|
|
, sha1_hash const& info_hash
|
2006-11-14 01:08:16 +01:00
|
|
|
, char const* name
|
2004-06-14 01:30:42 +02:00
|
|
|
, boost::filesystem::path const& save_path
|
2005-05-13 02:39:39 +02:00
|
|
|
, entry const& resume_data = entry()
|
2005-07-10 12:42:00 +02:00
|
|
|
, bool compact_mode = true
|
2007-03-16 06:29:23 +01:00
|
|
|
, int block_size = 16 * 1024
|
|
|
|
, storage_constructor_type sc = default_storage_constructor);
|
2004-01-02 21:46:24 +01:00
|
|
|
|
2006-10-11 16:02:21 +02:00
|
|
|
session_proxy abort() { return session_proxy(m_impl); }
|
|
|
|
|
2004-04-18 14:28:02 +02:00
|
|
|
session_status status() const;
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#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);
|
2006-09-27 19:20:18 +02:00
|
|
|
void add_dht_router(std::pair<std::string, int> const& node);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
|
|
|
|
void add_extension(boost::function<boost::shared_ptr<torrent_plugin>(torrent*)> ext);
|
|
|
|
|
|
|
|
#endif
|
2004-11-01 00:16:08 +01:00
|
|
|
|
2005-07-06 02:58:23 +02:00
|
|
|
void set_ip_filter(ip_filter const& f);
|
2006-04-25 23:04:48 +02:00
|
|
|
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
|
|
|
|
2007-01-01 22:04:30 +01:00
|
|
|
// Get the number of uploads.
|
|
|
|
int num_uploads() const;
|
|
|
|
|
|
|
|
// Get the number of connections. This number also contains the
|
|
|
|
// number of half open connections.
|
|
|
|
int num_connections() const;
|
|
|
|
|
2003-11-08 03:16:26 +01:00
|
|
|
void remove_torrent(const torrent_handle& h);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-05-21 01:24:19 +02:00
|
|
|
void set_settings(session_settings const& s);
|
|
|
|
session_settings const& settings();
|
2007-01-01 22:04:30 +01:00
|
|
|
|
2007-01-02 00:51:24 +01:00
|
|
|
int upload_rate_limit() const;
|
|
|
|
int download_rate_limit() const;
|
|
|
|
|
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);
|
2005-11-02 17:28:39 +01:00
|
|
|
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
|
|
|
|
2006-12-31 00:02:21 +01:00
|
|
|
// Resource management used for global limits.
|
|
|
|
resource_request m_ul_bandwidth_quota;
|
2006-11-20 22:03:58 +01:00
|
|
|
resource_request m_dl_bandwidth_quota;
|
|
|
|
resource_request m_uploads_quota;
|
|
|
|
resource_request m_connections_quota;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
private:
|
|
|
|
|
2006-12-31 00:02:21 +01:00
|
|
|
// just a way to initialize boost.filesystem
|
|
|
|
// before the session_impl is created
|
|
|
|
aux::filesystem_init m_dummy;
|
|
|
|
|
2003-10-31 05:02:51 +01:00
|
|
|
// data shared between the main thread
|
|
|
|
// and the working thread
|
2006-10-11 16:02:21 +02:00
|
|
|
boost::shared_ptr<aux::session_impl> m_impl;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_SESSION_HPP_INCLUDED
|
2005-11-01 19:30:39 +01:00
|
|
|
|