00001
00002
00003
00004
00005
00006
00007
00008 #include <ctime>
00009 #include <iostream>
00010 #include <fstream>
00011 #include <iomanip>
00012 #include <iterator>
00013 #include <algorithm>
00014 #include <set>
00015 #include <cctype>
00016 #include <algorithm>
00017
00018 #ifdef _MSC_VER
00019 #pragma warning(push, 1)
00020 #endif
00021
00022 #include <boost/lexical_cast.hpp>
00023 #include <boost/filesystem/convenience.hpp>
00024 #include <boost/filesystem/exception.hpp>
00025 #include <boost/limits.hpp>
00026 #include <boost/bind.hpp>
00027
00028 #ifdef _MSC_VER
00029 #pragma warning(pop)
00030 #endif
00031
00032 #include "libtorrent/peer_id.hpp"
00033 #include "libtorrent/torrent_info.hpp"
00034 #include "libtorrent/tracker_manager.hpp"
00035 #include "libtorrent/bencode.hpp"
00036 #include "libtorrent/hasher.hpp"
00037 #include "libtorrent/entry.hpp"
00038 #include "libtorrent/session.hpp"
00039 #include "libtorrent/fingerprint.hpp"
00040 #include "libtorrent/entry.hpp"
00041 #include "libtorrent/alert_types.hpp"
00042 #include "libtorrent/invariant_check.hpp"
00043 #include "libtorrent/file.hpp"
00044 #include "libtorrent/allocate_resources.hpp"
00045 #include "libtorrent/bt_peer_connection.hpp"
00046 #include "libtorrent/ip_filter.hpp"
00047 #include "libtorrent/socket.hpp"
00048 #include "libtorrent/aux_/session_impl.hpp"
00049 #include "libtorrent/kademlia/dht_tracker.hpp"
00050
00051 using namespace boost::posix_time;
00052 using boost::shared_ptr;
00053 using boost::weak_ptr;
00054 using boost::bind;
00055 using boost::mutex;
00056 using libtorrent::aux::session_impl;
00057
00058 namespace libtorrent
00059 {
00060
00061 namespace aux
00062 {
00063 filesystem_init::filesystem_init()
00064 {
00065 using namespace boost::filesystem;
00066 if (path::default_name_check_writable())
00067 path::default_name_check(no_check);
00068 }
00069 }
00070
00071 session::session(
00072 fingerprint const& id
00073 , std::pair<int, int> listen_port_range
00074 , char const* listen_interface)
00075 : m_impl(new session_impl(listen_port_range, id, listen_interface))
00076 {
00077
00078 assert(listen_port_range.first > 0);
00079 assert(listen_port_range.first < listen_port_range.second);
00080 #ifndef NDEBUG
00081
00082
00083
00084 boost::function0<void> test = boost::ref(*m_impl);
00085 assert(!test.empty());
00086 #endif
00087 }
00088
00089 session::session(fingerprint const& id)
00090 : m_impl(new session_impl(std::make_pair(0, 0), id))
00091 {
00092 #ifndef NDEBUG
00093 boost::function0<void> test = boost::ref(*m_impl);
00094 assert(!test.empty());
00095 #endif
00096 }
00097
00098 session::~session()
00099 {
00100 assert(m_impl);
00101
00102
00103
00104 if (!m_impl.unique())
00105 m_impl->abort();
00106 }
00107
00108 void session::add_extension(boost::function<boost::shared_ptr<torrent_plugin>(torrent*)> ext)
00109 {
00110 m_impl->add_extension(ext);
00111 }
00112
00113 void session::set_ip_filter(ip_filter const& f)
00114 {
00115 m_impl->set_ip_filter(f);
00116 }
00117
00118 void session::set_peer_id(peer_id const& id)
00119 {
00120 m_impl->set_peer_id(id);
00121 }
00122
00123 void session::set_key(int key)
00124 {
00125 m_impl->set_key(key);
00126 }
00127
00128 std::vector<torrent_handle> session::get_torrents() const
00129 {
00130 return m_impl->get_torrents();
00131 }
00132
00133 torrent_handle session::find_torrent(sha1_hash const& info_hash) const
00134 {
00135 return m_impl->find_torrent_handle(info_hash);
00136 }
00137
00138
00139 torrent_handle session::add_torrent(
00140 torrent_info const& ti
00141 , boost::filesystem::path const& save_path
00142 , entry const& resume_data
00143 , bool compact_mode
00144 , int block_size)
00145 {
00146 return m_impl->add_torrent(ti, save_path, resume_data
00147 , compact_mode, block_size);
00148 }
00149
00150 torrent_handle session::add_torrent(
00151 char const* tracker_url
00152 , sha1_hash const& info_hash
00153 , char const* name
00154 , boost::filesystem::path const& save_path
00155 , entry const& e
00156 , bool compact_mode
00157 , int block_size)
00158 {
00159 return m_impl->add_torrent(tracker_url, info_hash, name, save_path, e
00160 , compact_mode, block_size);
00161 }
00162
00163 void session::remove_torrent(const torrent_handle& h)
00164 {
00165 m_impl->remove_torrent(h);
00166 }
00167
00168 bool session::listen_on(
00169 std::pair<int, int> const& port_range
00170 , const char* net_interface)
00171 {
00172 return m_impl->listen_on(port_range, net_interface);
00173 }
00174
00175 unsigned short session::listen_port() const
00176 {
00177 return m_impl->listen_port();
00178 }
00179
00180 session_status session::status() const
00181 {
00182 return m_impl->status();
00183 }
00184
00185 #ifndef TORRENT_DISABLE_DHT
00186
00187 void session::start_dht(entry const& startup_state)
00188 {
00189 m_impl->start_dht(startup_state);
00190 }
00191
00192 void session::stop_dht()
00193 {
00194 m_impl->stop_dht();
00195 }
00196
00197 void session::set_dht_settings(dht_settings const& settings)
00198 {
00199 m_impl->set_dht_settings(settings);
00200 }
00201
00202 entry session::dht_state() const
00203 {
00204 return m_impl->dht_state();
00205 }
00206
00207 void session::add_dht_node(std::pair<std::string, int> const& node)
00208 {
00209 m_impl->add_dht_node(node);
00210 }
00211
00212 void session::add_dht_router(std::pair<std::string, int> const& node)
00213 {
00214 m_impl->add_dht_router(node);
00215 }
00216
00217 #endif
00218
00219 bool session::is_listening() const
00220 {
00221 return m_impl->is_listening();
00222 }
00223
00224 void session::set_settings(session_settings const& s)
00225 {
00226 m_impl->set_settings(s);
00227 }
00228
00229 session_settings const& session::settings()
00230 {
00231 return m_impl->settings();
00232 }
00233
00234 void session::set_max_uploads(int limit)
00235 {
00236 m_impl->set_max_uploads(limit);
00237 }
00238
00239 void session::set_max_connections(int limit)
00240 {
00241 m_impl->set_max_connections(limit);
00242 }
00243
00244 void session::set_max_half_open_connections(int limit)
00245 {
00246 m_impl->set_max_half_open_connections(limit);
00247 }
00248
00249 int session::upload_rate_limit() const
00250 {
00251 return m_impl->upload_rate_limit();
00252 }
00253
00254 int session::download_rate_limit() const
00255 {
00256 return m_impl->download_rate_limit();
00257 }
00258
00259 void session::set_upload_rate_limit(int bytes_per_second)
00260 {
00261 m_impl->set_upload_rate_limit(bytes_per_second);
00262 }
00263
00264 void session::set_download_rate_limit(int bytes_per_second)
00265 {
00266 m_impl->set_download_rate_limit(bytes_per_second);
00267 }
00268
00269 int session::num_uploads() const
00270 {
00271 return m_impl->num_uploads();
00272 }
00273
00274 int session::num_connections() const
00275 {
00276 return m_impl->num_connections();
00277 }
00278
00279 std::auto_ptr<alert> session::pop_alert()
00280 {
00281 return m_impl->pop_alert();
00282 }
00283
00284 void session::set_severity_level(alert::severity_t s)
00285 {
00286 m_impl->set_severity_level(s);
00287 }
00288
00289 }
00290