1 | |
---|
2 | // Copyright Eóin O'Callaghan 2006 - 2008. |
---|
3 | // Distributed under the Boost Software License, Version 1.0. |
---|
4 | // (See accompanying file LICENSE_1_0.txt or copy at |
---|
5 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | #include "stdAfx.hpp" |
---|
8 | |
---|
9 | #include <libtorrent/file.hpp> |
---|
10 | #include <libtorrent/hasher.hpp> |
---|
11 | #include <libtorrent/storage.hpp> |
---|
12 | #include <libtorrent/file_pool.hpp> |
---|
13 | #include <libtorrent/alert_types.hpp> |
---|
14 | #include <libtorrent/entry.hpp> |
---|
15 | #include <libtorrent/bencode.hpp> |
---|
16 | #include <libtorrent/session.hpp> |
---|
17 | #include <libtorrent/ip_filter.hpp> |
---|
18 | #include <libtorrent/torrent_handle.hpp> |
---|
19 | #include <libtorrent/peer_connection.hpp> |
---|
20 | #include <libtorrent/extensions/metadata_transfer.hpp> |
---|
21 | #include <libtorrent/extensions/ut_pex.hpp> |
---|
22 | |
---|
23 | #include "global/wtl_app.hpp" |
---|
24 | #include "global/string_conv.hpp" |
---|
25 | #include "global/ini_adapter.hpp" |
---|
26 | |
---|
27 | #include "halTorrent.hpp" |
---|
28 | #include "halTypes.hpp" |
---|
29 | #include "halEvent.hpp" |
---|
30 | #include "halSignaler.hpp" |
---|
31 | |
---|
32 | #include "halTorrentInternal.hpp" |
---|
33 | #include "halSession.hpp" |
---|
34 | //#include "halSessionAlert.hpp" |
---|
35 | |
---|
36 | namespace hal |
---|
37 | { |
---|
38 | libtorrent::session* torrent_internal::the_session_ = 0; |
---|
39 | wpath torrent_internal::workingDir_; |
---|
40 | } |
---|
41 | |
---|
42 | namespace hal |
---|
43 | { |
---|
44 | |
---|
45 | bit& bittorrent() |
---|
46 | { |
---|
47 | static bit t; |
---|
48 | return t; |
---|
49 | } |
---|
50 | |
---|
51 | const PeerDetails& torrent_details::peerDetails() const |
---|
52 | { |
---|
53 | if (!peerDetailsFilled_) |
---|
54 | { |
---|
55 | bittorrent().getAllPeerDetails(hal::to_utf8(name_), peerDetails_); |
---|
56 | peerDetailsFilled_ = true; |
---|
57 | } |
---|
58 | |
---|
59 | return peerDetails_; |
---|
60 | } |
---|
61 | |
---|
62 | const FileDetails& torrent_details::fileDetails() const |
---|
63 | { |
---|
64 | if (!fileDetailsFilled_) |
---|
65 | { |
---|
66 | bittorrent().getAllFileDetails(hal::to_utf8(name_), fileDetails_); |
---|
67 | fileDetailsFilled_ = true; |
---|
68 | } |
---|
69 | |
---|
70 | return fileDetails_; |
---|
71 | } |
---|
72 | |
---|
73 | bool nameLess(const torrent_details_ptr& left, const torrent_details_ptr& right) |
---|
74 | { |
---|
75 | return left->state() < right->state(); |
---|
76 | } |
---|
77 | |
---|
78 | void torrent_details_manager::sort( |
---|
79 | boost::function<bool (const torrent_details_ptr&, const torrent_details_ptr&)> fn) const |
---|
80 | { |
---|
81 | std::stable_sort(torrents_.begin(), torrents_.end(), fn); |
---|
82 | } |
---|
83 | |
---|
84 | web_seed_or_dht_node_detail::web_seed_or_dht_node_detail() : |
---|
85 | url(L""), |
---|
86 | port(-1), |
---|
87 | type(hal::app().res_wstr(HAL_INT_NEWT_ADD_PEERS_WEB)) |
---|
88 | {} |
---|
89 | |
---|
90 | web_seed_or_dht_node_detail::web_seed_or_dht_node_detail(std::wstring u) : |
---|
91 | url(u), |
---|
92 | port(-1), |
---|
93 | type(hal::app().res_wstr(HAL_INT_NEWT_ADD_PEERS_WEB)) |
---|
94 | {} |
---|
95 | |
---|
96 | web_seed_or_dht_node_detail::web_seed_or_dht_node_detail(std::wstring u, int p) : |
---|
97 | url(u), |
---|
98 | port(p), |
---|
99 | type(hal::app().res_wstr(HAL_INT_NEWT_ADD_PEERS_DHT)) |
---|
100 | {} |
---|
101 | wpath bit_impl::workingDirectory = hal::app().working_directory(); |
---|
102 | |
---|
103 | bit::bit() : |
---|
104 | pimpl(new bit_impl()) |
---|
105 | {} |
---|
106 | |
---|
107 | #define HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH(FUNCTION) \ |
---|
108 | catch (const libt::invalid_handle&) \ |
---|
109 | {\ |
---|
110 | event_log.post(shared_ptr<EventDetail>( \ |
---|
111 | new EventInvalidTorrent(event_logger::critical, event_logger::invalidTorrent, name, std::string(FUNCTION)))); \ |
---|
112 | }\ |
---|
113 | catch (const invalidTorrent& t) \ |
---|
114 | {\ |
---|
115 | event_log.post(shared_ptr<EventDetail>( \ |
---|
116 | new EventInvalidTorrent(event_logger::info, event_logger::invalidTorrent, t.who(), std::string(FUNCTION)))); \ |
---|
117 | }\ |
---|
118 | catch (const std::exception& e) \ |
---|
119 | {\ |
---|
120 | event_log.post(shared_ptr<EventDetail>( \ |
---|
121 | new EventTorrentException(event_logger::critical, event_logger::torrentException, std::string(e.what()), name, std::string(FUNCTION)))); \ |
---|
122 | } |
---|
123 | |
---|
124 | void bit::shutDownSession() |
---|
125 | { |
---|
126 | pimpl.reset(); |
---|
127 | } |
---|
128 | |
---|
129 | void bit::save_torrent_data() |
---|
130 | { |
---|
131 | pimpl->save_torrent_data(); |
---|
132 | } |
---|
133 | |
---|
134 | bool bit::create_torrent(const create_torrent_params& params, fs::wpath out_file, progress_callback fn) |
---|
135 | { |
---|
136 | return pimpl->create_torrent(params, out_file, fn); |
---|
137 | } |
---|
138 | |
---|
139 | bit::torrent bit::get_wstr(const std::wstring& filename) |
---|
140 | { |
---|
141 | return bit::torrent(pimpl->the_torrents_.get(filename)); |
---|
142 | } |
---|
143 | |
---|
144 | bool bit::listen_on(std::pair<int, int> const& range) |
---|
145 | { |
---|
146 | return pimpl->listen_on(range); |
---|
147 | } |
---|
148 | |
---|
149 | int bit::is_listening_on() |
---|
150 | { |
---|
151 | return pimpl->is_listening_on(); |
---|
152 | } |
---|
153 | |
---|
154 | void bit::stop_listening() |
---|
155 | { |
---|
156 | pimpl->stop_listening(); |
---|
157 | } |
---|
158 | |
---|
159 | bool bit::ensure_dht_on() |
---|
160 | { |
---|
161 | return pimpl->ensure_dht_on(); |
---|
162 | } |
---|
163 | |
---|
164 | void bit::ensure_dht_off() |
---|
165 | { |
---|
166 | pimpl->ensure_dht_off(); |
---|
167 | } |
---|
168 | |
---|
169 | void bit::set_dht_settings(int max_peers_reply, int search_branching, |
---|
170 | int service_port, int max_fail_count) |
---|
171 | { |
---|
172 | pimpl->set_dht_settings(max_peers_reply, search_branching, service_port, max_fail_count); |
---|
173 | } |
---|
174 | |
---|
175 | void bit::set_mapping(int mapping) |
---|
176 | { |
---|
177 | pimpl->set_mapping(mapping); |
---|
178 | } |
---|
179 | |
---|
180 | queue_settings bit::get_queue_settings() |
---|
181 | { |
---|
182 | return pimpl->get_queue_settings(); |
---|
183 | } |
---|
184 | |
---|
185 | void bit::set_queue_settings(const queue_settings& s) |
---|
186 | { |
---|
187 | pimpl->set_queue_settings(s); |
---|
188 | } |
---|
189 | |
---|
190 | timeouts bit::get_timeouts() |
---|
191 | { |
---|
192 | return pimpl->get_timeouts(); |
---|
193 | } |
---|
194 | |
---|
195 | void bit::set_timeouts(const timeouts& t) |
---|
196 | { |
---|
197 | pimpl->set_timeouts(t); |
---|
198 | } |
---|
199 | |
---|
200 | void bit::set_session_limits(int maxConn, int maxUpload) |
---|
201 | { |
---|
202 | pimpl->set_session_limits(maxConn, maxUpload); |
---|
203 | } |
---|
204 | |
---|
205 | void bit::set_session_speed(float download, float upload) |
---|
206 | { |
---|
207 | pimpl->set_session_speed(download, upload); |
---|
208 | } |
---|
209 | |
---|
210 | bool bit::ensure_ip_filter_on(progress_callback fn) |
---|
211 | { |
---|
212 | return pimpl->ensure_ip_filter_on(fn); |
---|
213 | } |
---|
214 | |
---|
215 | void bit::ensure_ip_filter_off() |
---|
216 | { |
---|
217 | pimpl->ensure_ip_filter_off(); |
---|
218 | } |
---|
219 | |
---|
220 | #ifndef TORRENT_DISABLE_ENCRYPTION |
---|
221 | |
---|
222 | void bit::ensure_pe_on(int enc_level, int in_enc_policy, int out_enc_policy, bool prefer_rc4) |
---|
223 | { |
---|
224 | pimpl->ensure_pe_on(enc_level, in_enc_policy, out_enc_policy, prefer_rc4); |
---|
225 | } |
---|
226 | |
---|
227 | void bit::ensure_pe_off() |
---|
228 | { |
---|
229 | pimpl->ensure_pe_off(); |
---|
230 | } |
---|
231 | #endif |
---|
232 | |
---|
233 | void bit::ip_v4_filter_block(asio::ip::address_v4 first, asio::ip::address_v4 last) |
---|
234 | { |
---|
235 | pimpl->ip_filter_.add_rule(first, last, libt::ip_filter::blocked); |
---|
236 | pimpl->ip_filter_count(); |
---|
237 | pimpl->ip_filter_changed_ = true; |
---|
238 | } |
---|
239 | |
---|
240 | void bit::ip_v6_filter_block(asio::ip::address_v6 first, asio::ip::address_v6 last) |
---|
241 | { |
---|
242 | pimpl->ip_v6_filter_block(first, last); |
---|
243 | } |
---|
244 | |
---|
245 | size_t bit::ip_filter_size() |
---|
246 | { |
---|
247 | return pimpl->ip_filter_size(); |
---|
248 | } |
---|
249 | |
---|
250 | void bit::clear_ip_filter() |
---|
251 | { |
---|
252 | pimpl->clear_ip_filter(); |
---|
253 | } |
---|
254 | |
---|
255 | bool bit::ip_filter_import_dat(boost::filesystem::path file, progress_callback fn, bool octalFix) |
---|
256 | { |
---|
257 | return pimpl->ip_filter_import_dat(file, fn, octalFix); |
---|
258 | } |
---|
259 | |
---|
260 | const SessionDetail bit::getSessionDetails() |
---|
261 | { |
---|
262 | SessionDetail details; |
---|
263 | |
---|
264 | details.port = pimpl->session_.is_listening() ? pimpl->session_.listen_port() : -1; |
---|
265 | |
---|
266 | libt::session_status status = pimpl->session_.status(); |
---|
267 | |
---|
268 | details.speed = std::pair<double, double>(status.download_rate, status.upload_rate); |
---|
269 | |
---|
270 | details.dht_on = pimpl->dht_on_; |
---|
271 | details.dht_nodes = status.dht_nodes; |
---|
272 | details.dht_torrents = status.dht_torrents; |
---|
273 | |
---|
274 | details.ip_filter_on = pimpl->ip_filter_on_; |
---|
275 | details.ip_ranges_filtered = pimpl->ip_filter_count_; |
---|
276 | |
---|
277 | return details; |
---|
278 | } |
---|
279 | |
---|
280 | void bit::setSessionHalfOpenLimit(int halfConn) |
---|
281 | { |
---|
282 | pimpl->session_.set_max_half_open_connections(halfConn); |
---|
283 | |
---|
284 | event_log.post(shared_ptr<EventDetail>(new EventMsg( |
---|
285 | hal::wform(L"Set half-open connections limit to %1%.") % pimpl->session_.max_half_open_connections()))); |
---|
286 | } |
---|
287 | |
---|
288 | void bit::setTorrentDefaults(int maxConn, int maxUpload, float download, float upload) |
---|
289 | { |
---|
290 | pimpl->defTorrentMaxConn_ = maxConn; |
---|
291 | pimpl->defTorrentMaxUpload_ = maxUpload; |
---|
292 | |
---|
293 | event_log.post(shared_ptr<EventDetail>(new EventMsg( |
---|
294 | hal::wform(L"Set torrent connections total %1% and uploads %2%.") % maxConn % maxUpload))); |
---|
295 | |
---|
296 | pimpl->defTorrentDownload_ = download; |
---|
297 | pimpl->defTorrentUpload_ = upload; |
---|
298 | |
---|
299 | event_log.post(shared_ptr<EventDetail>(new EventMsg( |
---|
300 | hal::wform(L"Set torrent default rates at %1$.2fkb/s down and %2$.2fkb/s upload.") % download % upload))); |
---|
301 | } |
---|
302 | |
---|
303 | void bit::add_torrent(wpath file, wpath saveDirectory, bool startStopped, bool compactStorage, |
---|
304 | boost::filesystem::wpath moveToDirectory, bool useMoveTo) |
---|
305 | { |
---|
306 | pimpl->add_torrent(file, saveDirectory, startStopped, compactStorage, moveToDirectory, useMoveTo); |
---|
307 | } |
---|
308 | |
---|
309 | const torrent_details_manager& bit::torrentDetails() |
---|
310 | { |
---|
311 | return torrentDetails_; |
---|
312 | } |
---|
313 | |
---|
314 | const torrent_details_manager& bit::updatetorrent_details_manager(const wstring& focused, const std::set<wstring>& selected) |
---|
315 | { |
---|
316 | try { |
---|
317 | |
---|
318 | mutex_t::scoped_lock l(torrentDetails_.mutex_); |
---|
319 | |
---|
320 | torrentDetails_.clearAll(l); |
---|
321 | torrentDetails_.torrents_.reserve(pimpl->the_torrents_.size()); |
---|
322 | |
---|
323 | for (TorrentManager::torrentByName::iterator i=pimpl->the_torrents_.begin(), e=pimpl->the_torrents_.end(); i != e; ++i) |
---|
324 | { |
---|
325 | wstring utf8Name = (*i).torrent->name(); |
---|
326 | torrent_details_ptr pT = (*i).torrent->gettorrent_details_ptr(); |
---|
327 | |
---|
328 | if (selected.find(utf8Name) != selected.end()) |
---|
329 | { |
---|
330 | torrentDetails_.selectedTorrents_.push_back(pT); |
---|
331 | } |
---|
332 | |
---|
333 | if (focused == utf8Name) |
---|
334 | torrentDetails_.selectedTorrent_ = pT; |
---|
335 | |
---|
336 | torrentDetails_.torrentMap_[(*i).torrent->name()] = pT; |
---|
337 | torrentDetails_.torrents_.push_back(pT); |
---|
338 | } |
---|
339 | |
---|
340 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH("Torrent Unknown!", "updatetorrent_details_manager") |
---|
341 | |
---|
342 | return torrentDetails_; |
---|
343 | } |
---|
344 | |
---|
345 | void bit::resume_all() |
---|
346 | { |
---|
347 | pimpl->resume_all(); |
---|
348 | } |
---|
349 | |
---|
350 | void bit::close_all(boost::optional<report_num_active> fn) |
---|
351 | { |
---|
352 | pimpl->close_all(fn); |
---|
353 | } |
---|
354 | |
---|
355 | PeerDetail::PeerDetail(libt::peer_info& peerInfo) : |
---|
356 | ipAddress(hal::from_utf8_safe(peerInfo.ip.address().to_string())), |
---|
357 | country(L""), |
---|
358 | speed(std::make_pair(peerInfo.payload_down_speed, peerInfo.payload_up_speed)), |
---|
359 | client(hal::from_utf8_safe(peerInfo.client)) |
---|
360 | { |
---|
361 | std::vector<wstring> status_vec; |
---|
362 | |
---|
363 | #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES |
---|
364 | if (peerInfo.country[0] != 0 && peerInfo.country[1] != 0) |
---|
365 | country = (hal::wform(L"(%1%)") % hal::from_utf8_safe(string(peerInfo.country, 2))).str().c_str(); |
---|
366 | #endif |
---|
367 | |
---|
368 | if (peerInfo.flags & libt::peer_info::handshake) |
---|
369 | { |
---|
370 | status_vec.push_back(app().res_wstr(HAL_PEER_HANDSHAKE)); |
---|
371 | } |
---|
372 | else if (peerInfo.flags & libt::peer_info::connecting) |
---|
373 | { |
---|
374 | status_vec.push_back(app().res_wstr(HAL_PEER_CONNECTING)); |
---|
375 | } |
---|
376 | else |
---|
377 | { |
---|
378 | #ifndef TORRENT_DISABLE_ENCRYPTION |
---|
379 | if (peerInfo.flags & libt::peer_info::rc4_encrypted) |
---|
380 | status_vec.push_back(app().res_wstr(HAL_PEER_RC4_ENCRYPTED)); |
---|
381 | if (peerInfo.flags & libt::peer_info::plaintext_encrypted) |
---|
382 | status_vec.push_back(app().res_wstr(HAL_PEER_PLAINTEXT_ENCRYPTED)); |
---|
383 | #endif |
---|
384 | |
---|
385 | if (peerInfo.flags & libt::peer_info::interesting) |
---|
386 | status_vec.push_back(app().res_wstr(HAL_PEER_INTERESTING)); |
---|
387 | if (peerInfo.flags & libt::peer_info::choked) |
---|
388 | status_vec.push_back(app().res_wstr(HAL_PEER_CHOKED)); |
---|
389 | if (peerInfo.flags & libt::peer_info::remote_interested) |
---|
390 | status_vec.push_back(app().res_wstr(HAL_PEER_REMOTE_INTERESTING)); |
---|
391 | if (peerInfo.flags & libt::peer_info::remote_choked) |
---|
392 | status_vec.push_back(app().res_wstr(HAL_PEER_REMOTE_CHOKED)); |
---|
393 | if (peerInfo.flags & libt::peer_info::supports_extensions) |
---|
394 | status_vec.push_back(app().res_wstr(HAL_PEER_SUPPORT_EXTENSIONS)); |
---|
395 | // if (peerInfo.flags & libt::peer_info::local_connection) // Not sure whats up here? |
---|
396 | // status_vec.push_back(app().res_wstr(HAL_PEER_LOCAL_CONNECTION)); |
---|
397 | if (peerInfo.flags & libt::peer_info::queued) |
---|
398 | status_vec.push_back(app().res_wstr(HAL_PEER_QUEUED)); |
---|
399 | } |
---|
400 | |
---|
401 | seed = (peerInfo.flags & libt::peer_info::seed) ? true : false; |
---|
402 | |
---|
403 | if (!status_vec.empty()) status = status_vec[0]; |
---|
404 | |
---|
405 | if (status_vec.size() > 1) |
---|
406 | { |
---|
407 | for (size_t i=1; i<status_vec.size(); ++i) |
---|
408 | { |
---|
409 | status += L"; "; |
---|
410 | status += status_vec[i]; |
---|
411 | } |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | void bit::getAllPeerDetails(const std::string& filename, PeerDetails& peerContainer) |
---|
416 | { |
---|
417 | getAllPeerDetails(from_utf8_safe(filename), peerContainer); |
---|
418 | } |
---|
419 | |
---|
420 | void bit::getAllPeerDetails(const std::wstring& filename, PeerDetails& peerContainer) |
---|
421 | { |
---|
422 | try { |
---|
423 | |
---|
424 | pimpl->the_torrents_.get(filename)->getPeerDetails(peerContainer); |
---|
425 | |
---|
426 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(filename, "getAllPeerDetails") |
---|
427 | } |
---|
428 | |
---|
429 | void bit::getAllFileDetails(const std::string& filename, FileDetails& fileDetails) |
---|
430 | { |
---|
431 | getAllFileDetails(from_utf8_safe(filename), fileDetails); |
---|
432 | } |
---|
433 | |
---|
434 | void bit::getAllFileDetails(const std::wstring& filename, FileDetails& fileDetails) |
---|
435 | { |
---|
436 | try { |
---|
437 | |
---|
438 | pimpl->the_torrents_.get(filename)->getFileDetails(fileDetails); |
---|
439 | |
---|
440 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(filename, "getAllFileDetails") |
---|
441 | } |
---|
442 | |
---|
443 | bool bit::isTorrent(const std::string& filename) |
---|
444 | { |
---|
445 | return isTorrent(hal::to_wstr_shim(filename)); |
---|
446 | } |
---|
447 | |
---|
448 | bool bit::isTorrent(const std::wstring& filename) |
---|
449 | { |
---|
450 | try { |
---|
451 | |
---|
452 | return pimpl->the_torrents_.exists(filename); |
---|
453 | |
---|
454 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(filename, "isTorrent") |
---|
455 | |
---|
456 | return false; |
---|
457 | } |
---|
458 | |
---|
459 | void bit::pauseTorrent(const std::string& filename) |
---|
460 | { |
---|
461 | pauseTorrent(hal::to_wstr_shim(filename)); |
---|
462 | } |
---|
463 | |
---|
464 | void bit::pauseTorrent(const std::wstring& filename) |
---|
465 | { |
---|
466 | try { |
---|
467 | |
---|
468 | pimpl->the_torrents_.get(filename)->pause(); |
---|
469 | |
---|
470 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(filename, "pauseTorrent") |
---|
471 | } |
---|
472 | |
---|
473 | void bit::resumeTorrent(const std::string& filename) |
---|
474 | { |
---|
475 | resumeTorrent(hal::to_wstr_shim(filename)); |
---|
476 | } |
---|
477 | |
---|
478 | void bit::resumeTorrent(const std::wstring& filename) |
---|
479 | { |
---|
480 | try { |
---|
481 | |
---|
482 | pimpl->the_torrents_.get(filename)->resume(); |
---|
483 | |
---|
484 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(filename, "resumeTorrent") |
---|
485 | } |
---|
486 | |
---|
487 | void bit::stopTorrent(const std::string& filename) |
---|
488 | { |
---|
489 | stopTorrent(hal::to_wstr_shim(filename)); |
---|
490 | } |
---|
491 | |
---|
492 | void bit::stopTorrent(const std::wstring& filename) |
---|
493 | { |
---|
494 | try { |
---|
495 | |
---|
496 | pimpl->the_torrents_.get(filename)->stop(); |
---|
497 | |
---|
498 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(filename, "stopTorrent") |
---|
499 | } |
---|
500 | |
---|
501 | bool bit::isTorrentActive(const std::string& filename) |
---|
502 | { |
---|
503 | return isTorrentActive(hal::to_wstr_shim(filename)); |
---|
504 | } |
---|
505 | |
---|
506 | bool bit::isTorrentActive(const std::wstring& filename) |
---|
507 | { |
---|
508 | try { |
---|
509 | |
---|
510 | return pimpl->the_torrents_.get(filename)->is_active(); |
---|
511 | |
---|
512 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(filename, "isTorrentActive") |
---|
513 | |
---|
514 | return false; // ??? is this correct |
---|
515 | } |
---|
516 | |
---|
517 | void bit::reannounceTorrent(const std::string& filename) |
---|
518 | { |
---|
519 | reannounceTorrent(hal::to_wstr_shim(filename)); |
---|
520 | } |
---|
521 | |
---|
522 | void bit::reannounceTorrent(const std::wstring& filename) |
---|
523 | { |
---|
524 | try { |
---|
525 | |
---|
526 | pimpl->the_torrents_.get(filename)->handle().force_reannounce(); |
---|
527 | |
---|
528 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(filename, "reannounceTorrent") |
---|
529 | } |
---|
530 | |
---|
531 | |
---|
532 | void bit::recheckTorrent(const std::string& filename) |
---|
533 | { |
---|
534 | recheckTorrent(hal::to_wstr_shim(filename)); |
---|
535 | } |
---|
536 | |
---|
537 | void bit::recheckTorrent(const std::wstring& filename) |
---|
538 | { |
---|
539 | try { |
---|
540 | |
---|
541 | pimpl->the_torrents_.get(filename)->force_recheck(); |
---|
542 | |
---|
543 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(filename, "recheckTorrent") |
---|
544 | } |
---|
545 | |
---|
546 | void bit::remove_torrent_wstr(const std::wstring& filename) |
---|
547 | { |
---|
548 | pimpl->remove_torrent(filename); |
---|
549 | } |
---|
550 | |
---|
551 | void bit::remove_torrent_wipe_files_wstr(const std::wstring& filename) |
---|
552 | { |
---|
553 | pimpl->remove_torrent_wipe_files(hal::to_wstr_shim(filename)); |
---|
554 | } |
---|
555 | |
---|
556 | void bit::pauseAllTorrents() |
---|
557 | { |
---|
558 | try { |
---|
559 | |
---|
560 | for (TorrentManager::torrentByName::iterator i=pimpl->the_torrents_.begin(), e=pimpl->the_torrents_.end(); |
---|
561 | i != e; ++i) |
---|
562 | { |
---|
563 | if ((*i).torrent->in_session()) |
---|
564 | (*i).torrent->pause(); |
---|
565 | } |
---|
566 | |
---|
567 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH("Torrent Unknown!", "pauseAllTorrents") |
---|
568 | } |
---|
569 | |
---|
570 | void bit::unpauseAllTorrents() |
---|
571 | { |
---|
572 | try { |
---|
573 | |
---|
574 | for (TorrentManager::torrentByName::iterator i=pimpl->the_torrents_.begin(), e=pimpl->the_torrents_.end(); |
---|
575 | i != e; ++i) |
---|
576 | { |
---|
577 | if ((*i).torrent->in_session() && (*i).torrent->state() == torrent_details::torrent_paused) |
---|
578 | (*i).torrent->resume(); |
---|
579 | } |
---|
580 | |
---|
581 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH("Torrent Unknown!", "unpauseAllTorrents") |
---|
582 | } |
---|
583 | |
---|
584 | bit::torrent::torrent() |
---|
585 | {} |
---|
586 | |
---|
587 | bit::torrent::torrent(boost::shared_ptr<torrent_internal> p) : |
---|
588 | ptr(p) |
---|
589 | {} |
---|
590 | |
---|
591 | bool bit::torrent::is_open() const |
---|
592 | { |
---|
593 | return ptr; |
---|
594 | } |
---|
595 | |
---|
596 | bit::torrent::exec_around_ptr::proxy::proxy(torrent_internal* t) : |
---|
597 | t_(t), |
---|
598 | l_(t->mutex_) |
---|
599 | { |
---|
600 | HAL_DEV_MSG(L"Ctor proxy"); |
---|
601 | } |
---|
602 | |
---|
603 | bit::torrent::exec_around_ptr::proxy::~proxy() |
---|
604 | { |
---|
605 | HAL_DEV_MSG(L"Dtor proxy"); |
---|
606 | } |
---|
607 | |
---|
608 | const std::wstring bit::torrent::get_name() const |
---|
609 | { |
---|
610 | try { |
---|
611 | |
---|
612 | return ptr->name(); |
---|
613 | |
---|
614 | } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(L"Torrent Unknown", "torrent::get_name()") |
---|
615 | |
---|
616 | return 0; |
---|
617 | } |
---|
618 | |
---|
619 | float bit::torrent::get_ratio() const |
---|
620 | { |
---|
621 | try { |
---|
622 | |
---|
623 | return ptr->get_ratio(); |
---|
624 | |
---|
625 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::get_ratio") |
---|
626 | |
---|
627 | return 0; |
---|
628 | } |
---|
629 | |
---|
630 | void bit::torrent::set_ratio(float r) |
---|
631 | { |
---|
632 | try { |
---|
633 | |
---|
634 | ptr->set_ratio(r); |
---|
635 | |
---|
636 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::set_ratio") |
---|
637 | } |
---|
638 | |
---|
639 | std::pair<int, int> bit::torrent::get_connection_limits() const |
---|
640 | { |
---|
641 | try { |
---|
642 | |
---|
643 | return ptr->getConnectionLimit(); |
---|
644 | |
---|
645 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::get_connection_limits") |
---|
646 | |
---|
647 | return std::make_pair(-1, -1); |
---|
648 | } |
---|
649 | |
---|
650 | void bit::torrent::set_connection_limits(const std::pair<int, int>& l) |
---|
651 | { |
---|
652 | try { |
---|
653 | |
---|
654 | ptr->setConnectionLimit(l.first, l.second); |
---|
655 | |
---|
656 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::set_connection_limits") |
---|
657 | } |
---|
658 | |
---|
659 | std::pair<float, float> bit::torrent::get_rate_limits() const |
---|
660 | { |
---|
661 | try { |
---|
662 | |
---|
663 | return ptr->getTransferSpeed(); |
---|
664 | |
---|
665 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::get_rate_limits") |
---|
666 | |
---|
667 | return std::pair<float, float>(-1.0, -1.0); |
---|
668 | } |
---|
669 | |
---|
670 | void bit::torrent::set_rate_limits(const std::pair<float, float>& l) |
---|
671 | { |
---|
672 | try { |
---|
673 | |
---|
674 | ptr->setTransferSpeed(l.first, l.second); |
---|
675 | |
---|
676 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::set_rate_limits") |
---|
677 | } |
---|
678 | |
---|
679 | wpath bit::torrent::get_save_directory() const |
---|
680 | { |
---|
681 | try { |
---|
682 | |
---|
683 | return ptr->get_save_directory(); |
---|
684 | |
---|
685 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::get_save_directory") |
---|
686 | |
---|
687 | return L""; |
---|
688 | } |
---|
689 | |
---|
690 | void bit::torrent::set_save_directory(const wpath& s) |
---|
691 | { |
---|
692 | try { |
---|
693 | |
---|
694 | ptr->set_save_directory(s); |
---|
695 | |
---|
696 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::set_save_directory") |
---|
697 | } |
---|
698 | |
---|
699 | wpath bit::torrent::get_move_to_directory() const |
---|
700 | { |
---|
701 | try { |
---|
702 | |
---|
703 | return ptr->get_move_to_directory(); |
---|
704 | |
---|
705 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::get_save_directory") |
---|
706 | |
---|
707 | return L""; |
---|
708 | } |
---|
709 | |
---|
710 | void bit::torrent::set_move_to_directory(const wpath& m) |
---|
711 | { |
---|
712 | try { |
---|
713 | |
---|
714 | ptr->set_move_to_directory(m); |
---|
715 | |
---|
716 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::set_move_to_directory") |
---|
717 | } |
---|
718 | |
---|
719 | std::pair<wstring, wstring> bit::torrent::get_tracker_login() const |
---|
720 | { |
---|
721 | try { |
---|
722 | |
---|
723 | return ptr->getTrackerLogin(); |
---|
724 | |
---|
725 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("get_tracker_login") |
---|
726 | |
---|
727 | return std::make_pair(L"!!! exception thrown !!!", L"!!! exception thrown !!!"); |
---|
728 | } |
---|
729 | |
---|
730 | void bit::torrent::set_tracker_login(const std::pair<wstring, wstring>& p) |
---|
731 | { |
---|
732 | try { |
---|
733 | |
---|
734 | ptr->setTrackerLogin(p.first, p.second); |
---|
735 | |
---|
736 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::set_tracker_login") |
---|
737 | } |
---|
738 | |
---|
739 | bool bit::torrent::get_is_active() const |
---|
740 | { |
---|
741 | try { |
---|
742 | |
---|
743 | return ptr->is_active(); |
---|
744 | |
---|
745 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::get_is_active") |
---|
746 | |
---|
747 | return L""; |
---|
748 | } |
---|
749 | |
---|
750 | bool bit::torrent::get_in_session() const |
---|
751 | { |
---|
752 | try { |
---|
753 | |
---|
754 | return ptr->in_session(); |
---|
755 | |
---|
756 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::get_in_session") |
---|
757 | |
---|
758 | return L""; |
---|
759 | } |
---|
760 | |
---|
761 | std::vector<tracker_detail> bit::torrent::get_trackers() const |
---|
762 | { |
---|
763 | try { |
---|
764 | |
---|
765 | return ptr->getTrackers(); |
---|
766 | |
---|
767 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::get_trackers") |
---|
768 | |
---|
769 | return std::vector<tracker_detail>(); |
---|
770 | } |
---|
771 | |
---|
772 | void bit::torrent::set_trackers(const std::vector<tracker_detail>& trackers) |
---|
773 | { |
---|
774 | try { |
---|
775 | |
---|
776 | ptr->setTrackers(trackers); |
---|
777 | |
---|
778 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::set_trackers") |
---|
779 | } |
---|
780 | |
---|
781 | void bit::torrent::reset_trackers() |
---|
782 | { |
---|
783 | try { |
---|
784 | |
---|
785 | ptr->resetTrackers(); |
---|
786 | |
---|
787 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::set_trackers") |
---|
788 | } |
---|
789 | |
---|
790 | void bit::torrent::set_file_priorities(const std::pair<std::vector<int>, int>& p) |
---|
791 | { |
---|
792 | try { |
---|
793 | |
---|
794 | ptr->setFilePriorities(p.first, p.second); |
---|
795 | |
---|
796 | } HAL_GENERIC_TORRENT_PROP_EXCEPTION_CATCH("torrent::set_trackers") |
---|
797 | } |
---|
798 | |
---|
799 | void bit::startEventReceiver() |
---|
800 | { |
---|
801 | event_log.post(shared_ptr<EventDetail>(new EventMsg(L"Starting event handler."))); |
---|
802 | |
---|
803 | pimpl->start_alert_handler(); |
---|
804 | } |
---|
805 | |
---|
806 | void bit::stopEventReceiver() |
---|
807 | { |
---|
808 | event_log.post(shared_ptr<EventDetail>(new EventMsg(L"Stopping event handler."))); |
---|
809 | |
---|
810 | pimpl->stop_alert_handler(); |
---|
811 | } |
---|
812 | |
---|
813 | int bit::defTorrentMaxConn() { return pimpl->defTorrentMaxConn_; } |
---|
814 | int bit::defTorrentMaxUpload() { return pimpl->defTorrentMaxUpload_; } |
---|
815 | float bit::defTorrentDownload() { return pimpl->defTorrentDownload_; } |
---|
816 | float bit::defTorrentUpload() { return pimpl->defTorrentUpload_; } |
---|
817 | |
---|
818 | }; |
---|