📄 session_impl.hpp
字号:
/*Copyright (c) 2006, Arvid NorbergAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare 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, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSEARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BELIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OFSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESSINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER INCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF SUCH DAMAGE.*/#ifndef TORRENT_SESSION_IMPL_HPP_INCLUDED#define TORRENT_SESSION_IMPL_HPP_INCLUDED#include <ctime>#include <algorithm>#include <vector>#include <set>#include <list>#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#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"#include "libtorrent/tracker_manager.hpp"#include "libtorrent/peer_info.hpp"#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/kademlia/dht_tracker.hpp"#include "libtorrent/session_status.hpp"#include "libtorrent/session.hpp"#include "libtorrent/stat.hpp"#include "libtorrent/file_pool.hpp"#include "libtorrent/bandwidth_manager.hpp"#include "libtorrent/natpmp.hpp"#include "libtorrent/upnp.hpp"#include "libtorrent/lsd.hpp"#include "libtorrent/socket_type.hpp"#include "libtorrent/connection_queue.hpp"#include "libtorrent/disk_io_thread.hpp"#include "libtorrent/assert.hpp"namespace libtorrent{ namespace fs = boost::filesystem; namespace aux { struct session_impl; // this data is shared between the main thread and the // thread that initialize pieces struct piece_checker_data { piece_checker_data() : processing(false), progress(0.f), abort(false) {} boost::shared_ptr<torrent> torrent_ptr; fs::path save_path; sha1_hash info_hash; void parse_resume_data( const entry& rd , const torrent_info& info , std::string& error); std::vector<int> piece_map; std::vector<piece_picker::downloading_piece> unfinished_pieces; std::vector<piece_picker::block_info> block_info; std::vector<tcp::endpoint> peers; std::vector<tcp::endpoint> banned_peers; entry resume_data; // 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; // is filled in by storage::initialize_pieces() // and represents the progress. It should be a // value in the range [0, 1] float progress; // abort defaults to false and is typically // filled in by torrent_handle when the user // aborts the torrent bool abort; }; struct checker_impl: boost::noncopyable { checker_impl(session_impl& s): m_ses(s), m_abort(false) {} void operator()(); piece_checker_data* find_torrent(const sha1_hash& info_hash); void remove_torrent(sha1_hash const& info_hash, int options);#ifndef NDEBUG void check_invariant() const;#endif // when the files has been checked // the torrent is added to the session session_impl& m_ses; mutable boost::mutex m_mutex; boost::condition m_cond; // 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; bool m_abort; };#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) struct tracker_logger;#endif // this is the link between the main thread and the // thread started to run the main downloader loop struct session_impl: boost::noncopyable { // the size of each allocation that is chained in the send buffer enum { send_buffer_size = 200 };#ifndef NDEBUG friend class ::libtorrent::peer_connection;#endif friend struct checker_impl; friend class invariant_access; typedef std::set<boost::intrusive_ptr<peer_connection> > connection_map; typedef std::map<sha1_hash, boost::shared_ptr<torrent> > torrent_map; session_impl( std::pair<int, int> listen_port_range , fingerprint const& cl_fprint , char const* listen_interface#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) , fs::path const& logpath#endif ); ~session_impl();#ifndef TORRENT_DISABLE_EXTENSIONS void add_extension(boost::function<boost::shared_ptr<torrent_plugin>( torrent*, void*)> ext);#endif void operator()(); void open_listen_port() throw(); // if we are listening on an IPv6 interface // this will return one of the IPv6 addresses on this // machine, otherwise just an empty endpoint tcp::endpoint get_ipv6_interface() const; void async_accept(boost::shared_ptr<socket_acceptor> const& listener); void on_incoming_connection(boost::shared_ptr<socket_type> const& s , boost::weak_ptr<socket_acceptor> listener, asio::error_code 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); peer_id const& get_peer_id() const { return m_peer_id; } void close_connection(boost::intrusive_ptr<peer_connection> const& p); void connection_failed(boost::intrusive_ptr<peer_connection> const& p , tcp::endpoint const& a, char const* message); void set_settings(session_settings const& s); session_settings const& settings() const { return m_settings; }#ifndef TORRENT_DISABLE_DHT void add_dht_node(std::pair<std::string, int> const& node); void add_dht_node(udp::endpoint n); void add_dht_router(std::pair<std::string, int> const& node); void set_dht_settings(dht_settings const& s); dht_settings const& get_dht_settings() const { return m_dht_settings; } void start_dht(entry const& startup_state); void stop_dht(); entry dht_state() const;#endif#ifndef TORRENT_DISABLE_ENCRYPTION void set_pe_settings(pe_settings const& settings); pe_settings const& get_pe_settings() const { return m_pe_settings; }#endif // called when a port mapping is successful, or a router returns // a failure to map a port void on_port_mapping(int tcp_port, int udp_port, std::string const& errmsg); bool is_aborted() const { return m_abort; } void set_ip_filter(ip_filter const& f); void set_port_filter(port_filter const& f); bool listen_on( std::pair<int, int> const& port_range , const char* net_interface = 0); bool is_listening() const; torrent_handle add_torrent( boost::intrusive_ptr<torrent_info> ti , fs::path const& save_path , entry const& resume_data , storage_mode_t storage_mode , storage_constructor_type sc , bool paused , void* userdata); torrent_handle add_torrent( char const* tracker_url , sha1_hash const& info_hash , char const* name , fs::path const& save_path , entry const& resume_data , storage_mode_t storage_mode , storage_constructor_type sc , bool paused , void* userdata); void remove_torrent(torrent_handle const& h, int options); std::vector<torrent_handle> get_torrents(); void set_severity_level(alert::severity_t s); std::auto_ptr<alert> pop_alert(); alert const* wait_for_alert(time_duration max_wait); int upload_rate_limit() const; int download_rate_limit() const; void set_download_rate_limit(int bytes_per_second); void set_upload_rate_limit(int bytes_per_second); void set_max_half_open_connections(int limit); void set_max_connections(int limit); void set_max_uploads(int limit); int max_connections() const { return m_max_connections; } int max_uploads() const { return m_max_uploads; } int max_half_open_connections() const { return m_half_open.limit(); } int num_uploads() const { return m_num_unchoked; } int num_connections() const { return m_connections.size(); } void unchoke_peer(peer_connection& c) { torrent* t = c.associated_torrent().lock().get(); TORRENT_ASSERT(t); if (t->unchoke_peer(c)) ++m_num_unchoked; } session_status status() const; void set_peer_id(peer_id const& id); void set_key(int key); unsigned short listen_port() const; void abort(); torrent_handle find_torrent_handle(sha1_hash const& info_hash); void announce_lsd(sha1_hash const& ih); void set_peer_proxy(proxy_settings const& s) { m_peer_proxy = s; } void set_web_seed_proxy(proxy_settings const& s) { m_web_seed_proxy = s; } void set_tracker_proxy(proxy_settings const& s) { m_tracker_proxy = s; } proxy_settings const& peer_proxy() const { return m_peer_proxy; } proxy_settings const& web_seed_proxy() const { return m_web_seed_proxy; } proxy_settings const& tracker_proxy() const { return m_tracker_proxy; }#ifndef TORRENT_DISABLE_DHT void set_dht_proxy(proxy_settings const& s)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -