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 | #define TORRENT_MAX_ALERT_TYPES 32 |
---|
10 | |
---|
11 | #include <boost/utility/in_place_factory.hpp> |
---|
12 | #include <boost/none.hpp> |
---|
13 | |
---|
14 | #include "win32_exception.hpp" |
---|
15 | |
---|
16 | #include "global/wtl_app.hpp" |
---|
17 | #include "global/string_conv.hpp" |
---|
18 | #include "global/ini_adapter.hpp" |
---|
19 | |
---|
20 | #include "halIni.hpp" |
---|
21 | #include "halTypes.hpp" |
---|
22 | #include "halEvent.hpp" |
---|
23 | #include "halSignaler.hpp" |
---|
24 | #include "halSession.hpp" |
---|
25 | |
---|
26 | #pragma warning (push, 1) |
---|
27 | # include <libtorrent/create_torrent.hpp> |
---|
28 | #pragma warning (pop) |
---|
29 | |
---|
30 | namespace hal |
---|
31 | { |
---|
32 | |
---|
33 | bit_impl::bit_impl() : |
---|
34 | session_(libt::fingerprint(HALITE_FINGERPRINT)), |
---|
35 | keepChecking_(false), |
---|
36 | bittorrentIni(L"BitTorrent.xml"), |
---|
37 | the_torrents_(bittorrentIni), |
---|
38 | defTorrentMaxConn_(-1), |
---|
39 | defTorrentMaxUpload_(-1), |
---|
40 | defTorrentDownload_(-1), |
---|
41 | defTorrentUpload_(-1), |
---|
42 | ip_filter_on_(false), |
---|
43 | ip_filter_loaded_(false), |
---|
44 | ip_filter_changed_(false), |
---|
45 | ip_filter_count_(0), |
---|
46 | dht_on_(false) |
---|
47 | { |
---|
48 | try |
---|
49 | { |
---|
50 | |
---|
51 | torrent_internal::the_session_ = &session_; |
---|
52 | |
---|
53 | session_.session::set_alert_mask(libt::alert::all_categories); |
---|
54 | session_.add_extension(&libt::create_metadata_plugin); |
---|
55 | session_.add_extension(&libt::create_ut_pex_plugin); |
---|
56 | session_.set_max_half_open_connections(10); |
---|
57 | |
---|
58 | hal::event_log.post(shared_ptr<hal::EventDetail>( |
---|
59 | new hal::EventMsg(L"Loading BitTorrent.xml.", hal::event_logger::info))); |
---|
60 | bittorrentIni.load_data(); |
---|
61 | hal::event_log.post(shared_ptr<hal::EventDetail>( |
---|
62 | new hal::EventMsg(L"Loading torrent parameters.", hal::event_logger::info))); |
---|
63 | the_torrents_.load_from_ini(); |
---|
64 | hal::event_log.post(shared_ptr<hal::EventDetail>( |
---|
65 | new hal::EventMsg(L"Loading done!", hal::event_logger::info))); |
---|
66 | |
---|
67 | try |
---|
68 | { |
---|
69 | if (fs::exists(hal::app().get_working_directory()/L"Torrents.xml")) |
---|
70 | { |
---|
71 | assert(false); |
---|
72 | #if 0 |
---|
73 | { |
---|
74 | fs::wifstream ifs(hal::app().get_working_directory()/L"Torrents.xml"); |
---|
75 | |
---|
76 | event_log.post(shared_ptr<EventDetail>(new EventMsg(L"Loading old Torrents.xml"))); |
---|
77 | |
---|
78 | TorrentMap torrents; |
---|
79 | boost::archive::xml_wiarchive ia(ifs); |
---|
80 | ia >> boost::serialization::make_nvp("torrents", torrents); |
---|
81 | |
---|
82 | the_torrents_ = torrents; |
---|
83 | } |
---|
84 | |
---|
85 | event_log.post(shared_ptr<EventDetail>(new EventMsg( |
---|
86 | hal::wform(L"Total %1%.") % the_torrents_.size()))); |
---|
87 | |
---|
88 | fs::rename(hal::app().get_working_directory()/L"Torrents.xml", hal::app().get_working_directory()/L"Torrents.xml.safe.to.delete"); |
---|
89 | #endif |
---|
90 | } |
---|
91 | } |
---|
92 | catch(const std::exception& e) |
---|
93 | { |
---|
94 | event_log.post(shared_ptr<EventDetail>( |
---|
95 | new EventStdException(event_logger::fatal, e, L"Loading Old Torrents.xml"))); |
---|
96 | } |
---|
97 | |
---|
98 | if (exists(hal::app().get_working_directory()/L"DHTState.bin")) |
---|
99 | { |
---|
100 | try |
---|
101 | { |
---|
102 | dht_state_ = haldecode(hal::app().get_working_directory()/L"DHTState.bin"); |
---|
103 | } |
---|
104 | catch(const std::exception& e) |
---|
105 | { |
---|
106 | event_log.post(shared_ptr<EventDetail>( |
---|
107 | new EventStdException(event_logger::critical, e, L"Loading DHTState.bin"))); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | { libt::session_settings settings = session_.settings(); |
---|
112 | settings.user_agent = string("Halite ") + HALITE_VERSION_STRING; |
---|
113 | session_.set_settings(settings); |
---|
114 | } |
---|
115 | |
---|
116 | start_alert_handler(); |
---|
117 | |
---|
118 | } HAL_GENERIC_FN_EXCEPTION_CATCH(L"bit_impl::bit_impl()") |
---|
119 | } |
---|
120 | |
---|
121 | bit_impl::~bit_impl() |
---|
122 | { |
---|
123 | try |
---|
124 | { |
---|
125 | HAL_DEV_MSG(L"Commence ~BitTorrent_impl"); |
---|
126 | |
---|
127 | stop_alert_handler(); |
---|
128 | //save_torrent_data(); |
---|
129 | |
---|
130 | if (ip_filter_changed_) |
---|
131 | { |
---|
132 | fs::ofstream ofs(hal::app().get_working_directory()/L"IPFilter.bin", std::ios::binary); |
---|
133 | // boost::archive::binary_oarchive oba(ofs); |
---|
134 | |
---|
135 | libt::ip_filter::filter_tuple_t vectors = ip_filter_.export_filter(); |
---|
136 | |
---|
137 | std::vector<libt::ip_range<boost::asio::ip::address_v4> > v4(vectors.get<0>()); |
---|
138 | std::vector<libt::ip_range<boost::asio::ip::address_v6> > v6(vectors.get<1>()); |
---|
139 | |
---|
140 | v4.erase(std::remove(v4.begin(), v4.end(), 0), v4.end()); |
---|
141 | v6.erase(std::remove(v6.begin(), v6.end(), 0), v6.end()); |
---|
142 | |
---|
143 | write_vec_range(ofs, v4); |
---|
144 | // write_vec_range(ofs, v6); |
---|
145 | } |
---|
146 | |
---|
147 | } HAL_GENERIC_FN_EXCEPTION_CATCH(L"~BitTorrent_impl") |
---|
148 | } |
---|
149 | |
---|
150 | void bit_impl::ip_filter_count() |
---|
151 | { |
---|
152 | libt::ip_filter::filter_tuple_t vectors = ip_filter_.export_filter(); |
---|
153 | |
---|
154 | vectors.get<0>().erase(std::remove(vectors.get<0>().begin(), vectors.get<0>().end(), 0), |
---|
155 | vectors.get<0>().end()); |
---|
156 | vectors.get<1>().erase(std::remove(vectors.get<1>().begin(), vectors.get<1>().end(), 0), |
---|
157 | vectors.get<1>().end()); |
---|
158 | ip_filter_count_ = vectors.get<0>().size() + vectors.get<1>().size(); |
---|
159 | } |
---|
160 | |
---|
161 | void bit_impl::ip_filter_load(progress_callback fn) |
---|
162 | { |
---|
163 | fs::ifstream ifs(hal::app().get_working_directory()/L"IPFilter.bin", std::ios::binary); |
---|
164 | if (ifs) |
---|
165 | { |
---|
166 | size_t v4_size; |
---|
167 | ifs >> v4_size; |
---|
168 | |
---|
169 | size_t total = v4_size/100; |
---|
170 | size_t previous = 0; |
---|
171 | |
---|
172 | for(unsigned i=0; i<v4_size; ++i) |
---|
173 | { |
---|
174 | if (i-previous > total) |
---|
175 | { |
---|
176 | previous = i; |
---|
177 | |
---|
178 | if (fn) if (fn(size_t(i/total), hal::app().res_wstr(HAL_TORRENT_LOAD_FILTERS))) break; |
---|
179 | } |
---|
180 | |
---|
181 | read_range_to_filter<boost::asio::ip::address_v4>(ifs, ip_filter_); |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | void bit_impl::ip_filter_import(std::vector<libt::ip_range<boost::asio::ip::address_v4> >& v4, |
---|
187 | std::vector<libt::ip_range<boost::asio::ip::address_v6> >& v6) |
---|
188 | { |
---|
189 | for(std::vector<libt::ip_range<boost::asio::ip::address_v4> >::iterator i=v4.begin(); |
---|
190 | i != v4.end(); ++i) |
---|
191 | { |
---|
192 | ip_filter_.add_rule(i->first, i->last, libt::ip_filter::blocked); |
---|
193 | } |
---|
194 | /* for(std::vector<libt::ip_range<boost::asio::ip::address_v6> >::iterator i=v6.begin(); |
---|
195 | i != v6.end(); ++i) |
---|
196 | { |
---|
197 | ip_filter_.add_rule(i->first, i->last, libt::ip_filter::blocked); |
---|
198 | } |
---|
199 | */ |
---|
200 | /* Note here we do not set ip_filter_changed_ */ |
---|
201 | } |
---|
202 | |
---|
203 | bool bit_impl::ip_filter_import_dat(boost::filesystem::path file, progress_callback fn, bool octalFix) |
---|
204 | { |
---|
205 | try |
---|
206 | { |
---|
207 | |
---|
208 | fs::ifstream ifs(file); |
---|
209 | if (ifs) |
---|
210 | { |
---|
211 | boost::uintmax_t total = fs::file_size(file)/100; |
---|
212 | boost::uintmax_t progress = 0; |
---|
213 | boost::uintmax_t previous = 0; |
---|
214 | |
---|
215 | boost::regex reg("\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s*-\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s*.*"); |
---|
216 | boost::regex ip_reg("0*(\\d*)\\.0*(\\d*)\\.0*(\\d*)\\.0*(\\d*)"); |
---|
217 | boost::smatch m; |
---|
218 | |
---|
219 | string ip_address_line; |
---|
220 | while (!std::getline(ifs, ip_address_line).eof()) |
---|
221 | { |
---|
222 | progress += (ip_address_line.length() + 2); |
---|
223 | if (progress-previous > total) |
---|
224 | { |
---|
225 | previous = progress; |
---|
226 | if (fn) |
---|
227 | { |
---|
228 | if (fn(size_t(progress/total), hal::app().res_wstr(HAL_TORRENT_IMPORT_FILTERS))) |
---|
229 | break; |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | if (boost::regex_match(ip_address_line, m, reg)) |
---|
234 | { |
---|
235 | string first = m[1]; |
---|
236 | string last = m[2]; |
---|
237 | |
---|
238 | if (octalFix) |
---|
239 | { |
---|
240 | if (boost::regex_match(first, m, ip_reg)) |
---|
241 | { |
---|
242 | first = ((m.length(1) != 0) ? m[1] : string("0")) + "." + |
---|
243 | ((m.length(2) != 0) ? m[2] : string("0")) + "." + |
---|
244 | ((m.length(3) != 0) ? m[3] : string("0")) + "." + |
---|
245 | ((m.length(4) != 0) ? m[4] : string("0")); |
---|
246 | } |
---|
247 | if (boost::regex_match(last, m, ip_reg)) |
---|
248 | { |
---|
249 | last = ((m.length(1) != 0) ? m[1] : string("0")) + "." + |
---|
250 | ((m.length(2) != 0) ? m[2] : string("0")) + "." + |
---|
251 | ((m.length(3) != 0) ? m[3] : string("0")) + "." + |
---|
252 | ((m.length(4) != 0) ? m[4] : string("0")); |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | try |
---|
257 | { |
---|
258 | ip_filter_.add_rule(boost::asio::ip::address_v4::from_string(first), |
---|
259 | boost::asio::ip::address_v4::from_string(last), libt::ip_filter::blocked); |
---|
260 | } |
---|
261 | catch(...) |
---|
262 | { |
---|
263 | hal::event_log.post(shared_ptr<hal::EventDetail>( |
---|
264 | new hal::EventDebug(hal::event_logger::info, |
---|
265 | from_utf8((boost::format("Invalid IP range: %1%-%2%.") % first % last).str())))); |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | ip_filter_changed_ = true; |
---|
272 | ip_filter_count(); |
---|
273 | |
---|
274 | } |
---|
275 | catch(const std::exception& e) |
---|
276 | { |
---|
277 | event_log.post(shared_ptr<EventDetail>( |
---|
278 | new EventStdException(event_logger::critical, e, L"ip_filter_import_dat"))); |
---|
279 | } |
---|
280 | |
---|
281 | return false; |
---|
282 | } |
---|
283 | |
---|
284 | bool bit_impl::create_torrent(const create_torrent_params& params, fs::wpath out_file, progress_callback fn) |
---|
285 | { |
---|
286 | try |
---|
287 | { |
---|
288 | libt::file_storage fs; |
---|
289 | libt::file_pool f_pool; |
---|
290 | |
---|
291 | HAL_DEV_MSG(L"Files"); |
---|
292 | for (file_size_pairs_t::const_iterator i = params.file_size_pairs.begin(), e = params.file_size_pairs.end(); |
---|
293 | i != e; ++i) |
---|
294 | { |
---|
295 | HAL_DEV_MSG(hal::wform(L"file path: %1%, size: %2%") % (*i).first % (*i).second); |
---|
296 | fs.add_file(to_utf8((*i).first.string()), (*i).second); |
---|
297 | } |
---|
298 | |
---|
299 | int piece_size = params.piece_size; |
---|
300 | HAL_DEV_MSG(hal::wform(L"piece size: %1%") % piece_size); |
---|
301 | |
---|
302 | libt::create_torrent t(fs, piece_size); |
---|
303 | |
---|
304 | /* boost::scoped_ptr<libt::storage_interface> store( |
---|
305 | libt::default_storage_constructor(t_info, to_utf8(params.root_path.string()), |
---|
306 | f_pool)); |
---|
307 | */ |
---|
308 | HAL_DEV_MSG(L"Trackers"); |
---|
309 | for (tracker_details_t::const_iterator i = params.trackers.begin(), e = params.trackers.end(); |
---|
310 | i != e; ++i) |
---|
311 | { |
---|
312 | HAL_DEV_MSG(hal::wform(L"URL: %1%, Tier: %2%") % (*i).url % (*i).tier); |
---|
313 | t.add_tracker(to_utf8((*i).url), (*i).tier); |
---|
314 | } |
---|
315 | |
---|
316 | HAL_DEV_MSG(L"Web Seeds"); |
---|
317 | for (web_seed_details_t::const_iterator i = params.web_seeds.begin(), e = params.web_seeds.end(); |
---|
318 | i != e; ++i) |
---|
319 | { |
---|
320 | HAL_DEV_MSG(hal::wform(L"URL: %1%") % (*i).url); |
---|
321 | t.add_url_seed(to_utf8((*i).url)); |
---|
322 | } |
---|
323 | |
---|
324 | HAL_DEV_MSG(L"DHT Nodes"); |
---|
325 | for (dht_node_details_t::const_iterator i = params.dht_nodes.begin(), e = params.dht_nodes.end(); |
---|
326 | i != e; ++i) |
---|
327 | { |
---|
328 | HAL_DEV_MSG(hal::wform(L"URL: %1%, port: %2%") % (*i).url % (*i).port); |
---|
329 | t.add_node(hal::make_pair(to_utf8((*i).url), (*i).port)); |
---|
330 | } |
---|
331 | |
---|
332 | boost::scoped_ptr<libt::storage_interface> store( |
---|
333 | default_storage_constructor(const_cast<libt::file_storage&>(t.files()), to_utf8(params.root_path.string()), |
---|
334 | f_pool)); |
---|
335 | |
---|
336 | // calculate the hash for all pieces |
---|
337 | int num = t.num_pieces(); |
---|
338 | std::vector<char> piece_buf(t.piece_length()); |
---|
339 | |
---|
340 | for (int i = 0; i < num; ++i) |
---|
341 | { |
---|
342 | store->read(&piece_buf[0], i, 0, t.piece_size(i)); |
---|
343 | |
---|
344 | libt::hasher h(&piece_buf[0], t.piece_size(i)); |
---|
345 | t.set_hash(i, h.final()); |
---|
346 | |
---|
347 | if (fn(100*i / num, hal::app().res_wstr(HAL_NEWT_HASHING_PIECES))) |
---|
348 | { |
---|
349 | // User canceled torrent creation. |
---|
350 | |
---|
351 | hal::event_log.post(shared_ptr<hal::EventDetail>( |
---|
352 | new hal::EventMsg(hal::app().res_wstr(HAL_NEWT_CREATION_CANCELED), hal::event_logger::info))); |
---|
353 | |
---|
354 | return true; |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | t.set_creator(to_utf8(params.creator).c_str()); |
---|
359 | t.set_comment(to_utf8(params.comment).c_str()); |
---|
360 | |
---|
361 | t.set_priv(params.private_torrent); |
---|
362 | |
---|
363 | // create the torrent and print it to out |
---|
364 | libt::entry e = t.generate(); |
---|
365 | halencode(out_file, e); |
---|
366 | |
---|
367 | } HAL_GENERIC_FN_EXCEPTION_CATCH(L"bit_impl::create_torrent()") |
---|
368 | |
---|
369 | return false; |
---|
370 | } |
---|
371 | |
---|
372 | void bit_impl::start_alert_handler() |
---|
373 | { |
---|
374 | mutex_t::scoped_lock l(mutex_); |
---|
375 | |
---|
376 | HAL_DEV_MSG(hal::wform(L"start_alert_handler")); |
---|
377 | |
---|
378 | boost::function<void (void)> f = bind(&bit_impl::alert_handler, this); |
---|
379 | |
---|
380 | keepChecking_ = true; |
---|
381 | alert_checker_ = boost::in_place<boost::function<void (void)> >(bind(&bit_impl::alert_handler, this)); |
---|
382 | } |
---|
383 | |
---|
384 | void bit_impl::stop_alert_handler() |
---|
385 | { |
---|
386 | mutex_t::scoped_lock l(mutex_); |
---|
387 | |
---|
388 | keepChecking_ = false; |
---|
389 | |
---|
390 | if (alert_checker_) |
---|
391 | { |
---|
392 | HAL_DEV_MSG(hal::wform(L"Interrupting alert handler")); |
---|
393 | |
---|
394 | alert_checker_->interrupt(); |
---|
395 | alert_checker_ = boost::none; |
---|
396 | } |
---|
397 | else |
---|
398 | { |
---|
399 | HAL_DEV_MSG(hal::wform(L"Alert handler already stopped")); |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | void bit_impl::alert_handler() |
---|
404 | { |
---|
405 | win32_exception::install_handler(); |
---|
406 | |
---|
407 | try |
---|
408 | { |
---|
409 | |
---|
410 | while (keepChecking_) |
---|
411 | { |
---|
412 | |
---|
413 | std::auto_ptr<libt::alert> p_alert = session_.pop_alert(); |
---|
414 | |
---|
415 | class AlertHandler |
---|
416 | { |
---|
417 | public: |
---|
418 | AlertHandler(bit_impl& bit_impl) : |
---|
419 | bit_impl_(bit_impl) |
---|
420 | {} |
---|
421 | |
---|
422 | void operator()(libt::external_ip_alert const& a) const |
---|
423 | { |
---|
424 | event_log.post(shared_ptr<EventDetail>( |
---|
425 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
426 | hal::wform(hal::app().res_wstr(HAL_EXTERNAL_IP_ALERT)) |
---|
427 | % hal::from_utf8_safe(a.message()) |
---|
428 | % hal::from_utf8_safe(a.external_address.to_string())) |
---|
429 | ) ); |
---|
430 | } |
---|
431 | |
---|
432 | void operator()(libt::portmap_error_alert const& a) const |
---|
433 | { |
---|
434 | event_log.post(shared_ptr<EventDetail>( |
---|
435 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
436 | hal::wform(hal::app().res_wstr(HAL_PORTMAP_ERROR_ALERT)) |
---|
437 | % (a.type == 0 ? |
---|
438 | hal::app().res_wstr(HAL_PORTMAP_TYPE_PMP) : |
---|
439 | hal::app().res_wstr(HAL_PORTMAP_TYPE_UPNP))) |
---|
440 | ) ); |
---|
441 | } |
---|
442 | |
---|
443 | void operator()(libt::portmap_alert const& a) const |
---|
444 | { |
---|
445 | event_log.post(shared_ptr<EventDetail>( |
---|
446 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
447 | hal::wform(hal::app().res_wstr(HAL_PORTMAP_ALERT)) |
---|
448 | % (a.type == 0 ? |
---|
449 | hal::app().res_wstr(HAL_PORTMAP_TYPE_PMP) : |
---|
450 | hal::app().res_wstr(HAL_PORTMAP_TYPE_UPNP)) |
---|
451 | % a.external_port) |
---|
452 | ) ); |
---|
453 | } |
---|
454 | |
---|
455 | void operator()(libt::file_error_alert const& a) const |
---|
456 | { |
---|
457 | event_log.post(shared_ptr<EventDetail>( |
---|
458 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
459 | hal::wform(hal::app().res_wstr(HAL_FILE_ERROR_ALERT)) |
---|
460 | % hal::from_utf8_safe(a.file) |
---|
461 | % hal::from_utf8_safe(a.msg)) |
---|
462 | ) ); |
---|
463 | } |
---|
464 | |
---|
465 | void operator()(libt::dht_reply_alert const& a) const |
---|
466 | { |
---|
467 | event_log.post(shared_ptr<EventDetail>( |
---|
468 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
469 | hal::wform(hal::app().res_wstr(HAL_DHT_REPLY_ALERT)) |
---|
470 | % a.num_peers) |
---|
471 | ) ); |
---|
472 | } |
---|
473 | |
---|
474 | void operator()(libt::torrent_finished_alert const& a) const |
---|
475 | { |
---|
476 | HAL_DEV_MSG(L"torrent_finished_alert"); |
---|
477 | |
---|
478 | event_log.post(shared_ptr<EventDetail>( |
---|
479 | new EventMsg((hal::wform(hal::app().res_wstr(LBT_EVENT_TORRENT_FINISHED)) |
---|
480 | % get(a.handle)->name()), |
---|
481 | event_logger::info, a.timestamp()))); |
---|
482 | |
---|
483 | get(a.handle)->finished(); |
---|
484 | } |
---|
485 | |
---|
486 | void operator()(libt::torrent_paused_alert const& a) const |
---|
487 | { |
---|
488 | HAL_DEV_MSG(L"torrent_paused_alert"); |
---|
489 | |
---|
490 | event_log.post(shared_ptr<EventDetail>( |
---|
491 | new EventMsg((hal::wform(hal::app().res_wstr(LBT_EVENT_TORRENT_PAUSED)) |
---|
492 | % get(a.handle)->name()), |
---|
493 | event_logger::info, a.timestamp()))); |
---|
494 | |
---|
495 | get(a.handle)->signals().torrent_paused(); |
---|
496 | } |
---|
497 | |
---|
498 | void operator()(libt::peer_error_alert const& a) const |
---|
499 | { |
---|
500 | event_log.post(shared_ptr<EventDetail>( |
---|
501 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
502 | hal::wform(hal::app().res_wstr(HAL_PEER_ALERT)) |
---|
503 | % hal::from_utf8_safe(a.message()) |
---|
504 | % hal::from_utf8_safe(a.ip.address().to_string())) |
---|
505 | ) ); |
---|
506 | } |
---|
507 | |
---|
508 | void operator()(libt::peer_ban_alert const& a) const |
---|
509 | { |
---|
510 | event_log.post(shared_ptr<EventDetail>( |
---|
511 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
512 | hal::wform(hal::app().res_wstr(HAL_PEER_BAN_ALERT)) |
---|
513 | % get(a.handle)->name() |
---|
514 | % hal::from_utf8_safe(a.ip.address().to_string())) |
---|
515 | ) ); |
---|
516 | } |
---|
517 | |
---|
518 | void operator()(libt::hash_failed_alert const& a) const |
---|
519 | { |
---|
520 | event_log.post(shared_ptr<EventDetail>( |
---|
521 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
522 | hal::wform(hal::app().res_wstr(HAL_HASH_FAIL_ALERT)) |
---|
523 | % get(a.handle)->name() |
---|
524 | % a.piece_index) |
---|
525 | ) ); |
---|
526 | } |
---|
527 | |
---|
528 | void operator()(libt::url_seed_alert const& a) const |
---|
529 | { |
---|
530 | event_log.post(shared_ptr<EventDetail>( |
---|
531 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
532 | hal::wform(hal::app().res_wstr(HAL_URL_SEED_ALERT)) |
---|
533 | % get(a.handle)->name() |
---|
534 | % hal::from_utf8_safe(a.url) |
---|
535 | % hal::from_utf8_safe(a.message())) |
---|
536 | ) ); |
---|
537 | } |
---|
538 | |
---|
539 | void operator()(libt::tracker_warning_alert const& a) const |
---|
540 | { |
---|
541 | event_log.post(shared_ptr<EventDetail>( |
---|
542 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
543 | hal::wform(hal::app().res_wstr(HAL_TRACKER_WARNING_ALERT)) |
---|
544 | % get(a.handle)->name() |
---|
545 | % hal::from_utf8_safe(a.message())) |
---|
546 | ) ); |
---|
547 | } |
---|
548 | |
---|
549 | void operator()(libt::tracker_announce_alert const& a) const |
---|
550 | { |
---|
551 | HAL_DEV_MSG(hal::wform(L"HAL_TRACKER_ANNOUNCE_ALERT")); |
---|
552 | |
---|
553 | event_log.post(shared_ptr<EventDetail>( |
---|
554 | new EventMsg((hal::wform(hal::app().res_wstr(HAL_TRACKER_ANNOUNCE_ALERT)) |
---|
555 | % get(a.handle)->name()), |
---|
556 | event_logger::info, a.timestamp()))); |
---|
557 | } |
---|
558 | |
---|
559 | void operator()(libt::tracker_error_alert const& a) const |
---|
560 | { |
---|
561 | event_log.post(shared_ptr<EventDetail>( |
---|
562 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
563 | hal::wform(hal::app().res_wstr(HAL_TRACKER_ALERT)) |
---|
564 | % get(a.handle)->name() |
---|
565 | % hal::from_utf8_safe(a.message()) |
---|
566 | % a.times_in_row |
---|
567 | % a.status_code) |
---|
568 | ) ); |
---|
569 | } |
---|
570 | |
---|
571 | void operator()(libt::tracker_reply_alert const& a) const |
---|
572 | { |
---|
573 | event_log.post(shared_ptr<EventDetail>( |
---|
574 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
575 | hal::wform(hal::app().res_wstr(HAL_TRACKER_REPLY_ALERT)) |
---|
576 | % get(a.handle)->name() |
---|
577 | % hal::from_utf8_safe(a.message()) |
---|
578 | % a.num_peers) |
---|
579 | ) ); |
---|
580 | } |
---|
581 | |
---|
582 | void operator()(libt::save_resume_data_alert const& a) const |
---|
583 | { |
---|
584 | event_log.post(shared_ptr<EventDetail>( |
---|
585 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
586 | hal::wform(hal::app().res_wstr(HAL_FAST_RESUME_ALERT)) |
---|
587 | % get(a.handle)->name() |
---|
588 | % hal::from_utf8_safe(a.message())) |
---|
589 | ) ); |
---|
590 | } |
---|
591 | |
---|
592 | void operator()(libt::fastresume_rejected_alert const& a) const |
---|
593 | { |
---|
594 | event_log.post(shared_ptr<EventDetail>( |
---|
595 | new EventGeneral(lbt_category_to_event(a.category()), a.timestamp(), |
---|
596 | hal::wform(hal::app().res_wstr(HAL_FAST_RESUME_ALERT)) |
---|
597 | % get(a.handle)->name() |
---|
598 | % hal::from_utf8_safe(a.message())) |
---|
599 | ) ); |
---|
600 | } |
---|
601 | |
---|
602 | void operator()(libt::piece_finished_alert const& a) const |
---|
603 | { |
---|
604 | event_log.post(shared_ptr<EventDetail>( |
---|
605 | new EventGeneral(event_logger::debug, a.timestamp(), |
---|
606 | hal::wform(hal::app().res_wstr(HAL_PIECE_FINISHED_ALERT)) |
---|
607 | % get(a.handle)->name() |
---|
608 | % a.piece_index) |
---|
609 | ) ); |
---|
610 | } |
---|
611 | |
---|
612 | void operator()(libt::block_finished_alert const& a) const |
---|
613 | { |
---|
614 | event_log.post(shared_ptr<EventDetail>( |
---|
615 | new EventGeneral(event_logger::debug, a.timestamp(), |
---|
616 | hal::wform(hal::app().res_wstr(HAL_BLOCK_FINISHED_ALERT)) |
---|
617 | % get(a.handle)->name() |
---|
618 | % a.block_index |
---|
619 | % a.piece_index) |
---|
620 | ) ); |
---|
621 | } |
---|
622 | |
---|
623 | void operator()(libt::block_downloading_alert const& a) const |
---|
624 | { |
---|
625 | event_log.post(shared_ptr<EventDetail>( |
---|
626 | new EventGeneral(event_logger::debug, a.timestamp(), |
---|
627 | hal::wform(hal::app().res_wstr(HAL_BLOCK_DOWNLOADING_ALERT)) |
---|
628 | % get(a.handle)->name() |
---|
629 | % a.block_index |
---|
630 | % a.piece_index) |
---|
631 | ) ); |
---|
632 | } |
---|
633 | |
---|
634 | void operator()(libt::listen_failed_alert const& a) const |
---|
635 | { |
---|
636 | if (a.endpoint.address().is_v6()) |
---|
637 | { |
---|
638 | event_log.post(shared_ptr<EventDetail>( |
---|
639 | new EventGeneral(event_logger::info, a.timestamp(), |
---|
640 | hal::app().res_wstr(HAL_LISTEN_V6_FAILED_ALERT)) |
---|
641 | ) ); |
---|
642 | } |
---|
643 | else |
---|
644 | { |
---|
645 | event_log.post(shared_ptr<EventDetail>( |
---|
646 | new EventGeneral(event_logger::info, a.timestamp(), |
---|
647 | hal::wform(hal::app().res_wstr(HAL_LISTEN_FAILED_ALERT)) |
---|
648 | % hal::from_utf8_safe(a.message())) |
---|
649 | ) ); |
---|
650 | } |
---|
651 | } |
---|
652 | |
---|
653 | void operator()(libt::listen_succeeded_alert const& a) const |
---|
654 | { |
---|
655 | event_log.post(shared_ptr<EventDetail>( |
---|
656 | new EventGeneral(event_logger::info, a.timestamp(), |
---|
657 | hal::wform(hal::app().res_wstr(HAL_LISTEN_SUCCEEDED_ALERT)) |
---|
658 | % hal::from_utf8_safe(a.message())) |
---|
659 | ) ); |
---|
660 | |
---|
661 | bit_impl_.signals.successful_listen(); |
---|
662 | } |
---|
663 | |
---|
664 | void operator()(libt::peer_blocked_alert const& a) const |
---|
665 | { |
---|
666 | event_log.post(shared_ptr<EventDetail>( |
---|
667 | new EventGeneral(event_logger::debug, a.timestamp(), |
---|
668 | hal::wform(hal::app().res_wstr(HAL_IPFILTER_ALERT)) |
---|
669 | % hal::from_utf8_safe(a.ip.to_string()) |
---|
670 | % hal::from_utf8_safe(a.message())) |
---|
671 | ) ); |
---|
672 | } |
---|
673 | |
---|
674 | /* void operator()(libt::alert const& a) const |
---|
675 | { |
---|
676 | event_log.post(shared_ptr<EventDetail>( |
---|
677 | new EventLibtorrent(lbtAlertToHalEvent(a.severity()), |
---|
678 | a.timestamp(), event_logger::unclassified, hal::from_utf8_safe(a.message())))); |
---|
679 | }*/ |
---|
680 | |
---|
681 | private: |
---|
682 | bit_impl& bit_impl_; |
---|
683 | |
---|
684 | torrent_internal_ptr get(libt::torrent_handle h) const |
---|
685 | { |
---|
686 | return bit_impl_.the_torrents_.get(from_utf8_safe(h.get_torrent_info().name())); |
---|
687 | } |
---|
688 | |
---|
689 | } handler(*this); |
---|
690 | |
---|
691 | while (p_alert.get()) |
---|
692 | { |
---|
693 | try |
---|
694 | { |
---|
695 | mutex_t::scoped_lock l(mutex_); |
---|
696 | |
---|
697 | libt::handle_alert< |
---|
698 | libt::save_resume_data_alert, |
---|
699 | libt::external_ip_alert, |
---|
700 | libt::portmap_error_alert, |
---|
701 | libt::portmap_alert, |
---|
702 | libt::file_error_alert, |
---|
703 | libt::torrent_finished_alert, |
---|
704 | libt::torrent_paused_alert, |
---|
705 | libt::peer_error_alert, |
---|
706 | libt::peer_ban_alert, |
---|
707 | libt::hash_failed_alert, |
---|
708 | libt::url_seed_alert, |
---|
709 | libt::dht_reply_alert, |
---|
710 | libt::tracker_error_alert, |
---|
711 | libt::tracker_warning_alert, |
---|
712 | libt::tracker_announce_alert, |
---|
713 | libt::tracker_reply_alert, |
---|
714 | libt::fastresume_rejected_alert, |
---|
715 | libt::piece_finished_alert, |
---|
716 | libt::block_finished_alert, |
---|
717 | libt::block_downloading_alert, |
---|
718 | libt::listen_failed_alert, |
---|
719 | libt::listen_succeeded_alert, |
---|
720 | libt::peer_blocked_alert |
---|
721 | >::handle_alert(p_alert, handler); |
---|
722 | |
---|
723 | } |
---|
724 | catch(libt::unhandled_alert&) |
---|
725 | { |
---|
726 | // handler(*p_alert); |
---|
727 | } |
---|
728 | catch(std::exception& e) |
---|
729 | { |
---|
730 | // These are logged as debug because they are rarely important to act on! |
---|
731 | event_log.post(shared_ptr<EventDetail>(\ |
---|
732 | new EventStdException(event_logger::debug, e, L"alertHandler"))); |
---|
733 | } |
---|
734 | |
---|
735 | p_alert = session_.pop_alert(); |
---|
736 | |
---|
737 | boost::this_thread::interruption_point(); |
---|
738 | } |
---|
739 | |
---|
740 | boost::this_thread::sleep(pt::milliseconds(100)); |
---|
741 | |
---|
742 | } |
---|
743 | |
---|
744 | } HAL_GENERIC_FN_EXCEPTION_CATCH(L"bit_impl::alert_handler()") |
---|
745 | } |
---|
746 | |
---|
747 | } |
---|