From ea535bca2bcdcd4203c7f0a7f2dd472fa14be097 Mon Sep 17 00:00:00 2001
From: Arvid Norberg
Example code to pause and save resume data for all torrents and wait for the alerts:
-int num_resume_data = 0; +extern int outstanding_resume_data; // global counter of outstanding resume data std::vector<torrent_handle> handles = ses.get_torrents(); ses.pause(); for (std::vector<torrent_handle>::iterator i = handles.begin(); @@ -3110,12 +3110,13 @@ for (std::vector<torrent_handle>::iterator i = handles.begin(); if (!h.is_valid()) continue; torrent_status s = h.status(); if (!s.has_metadata) continue; + if (!s.need_save_resume_data()) continue; h.save_resume_data(); - ++num_resume_data; + ++outstanding_resume_data; } -while (num_resume_data > 0) +while (outstanding_resume_data > 0) { alert const* a = ses.wait_for_alert(seconds(10)); @@ -3127,7 +3128,7 @@ while (num_resume_data > 0) if (alert_cast<save_resume_data_failed_alert>(a)) { process_alert(a); - --num_resume_data; + --outstanding_resume_data; continue; } @@ -3143,9 +3144,18 @@ while (num_resume_data > 0) / (h.get_torrent_info().name() + ".fastresume"), std::ios_base::binary); out.unsetf(std::ios_base::skipws); bencode(std::ostream_iterator<char>(out), *rd->resume_data); - --num_resume_data; + --outstanding_resume_data; }+
Note
+Note how outstanding_resume_data is a global counter in this example. +This is deliberate, otherwise there is a race condition for torrents that +was just asked to save their resume data, they posted the alert, but it has +not been received yet. Those torrents would report that they don't need to +save resume data again, and skipped by the initial loop, and thwart the counter +otherwise.
+Note
+A torrent's resume data is considered saved as soon as the alert +is posted. It is important to make sure this alert is received and handled +in order for this function to be meaningful.
+This is a debug alert that is generated by an incoming invalid piece request. -ìp is the address of the peer and the request is the actual incoming +Ïp is the address of the peer and the request is the actual incoming request from the peer.
struct invalid_request_alert: peer_alert @@ -7754,13 +7770,13 @@ std::string error_code_to_string(boost::system::error_code const& ec) static const char const* swedish[] = { "inget fel", - "en fil i torrenten kolliderar med en fil från en annan torrent", + "en fil i torrenten kolliderar med en fil frÂn en annan torrent", "hash check misslyckades", - "torrent filen är inte en dictionary", - "'info'-nyckeln saknas eller är korrupt i torrentfilen", - "'info'-fältet är inte en dictionary", - "'piece length' fältet saknas eller är korrupt i torrentfilen", - "torrentfilen saknar namnfältet", + "torrent filen ‰r inte en dictionary", + "'info'-nyckeln saknas eller ‰r korrupt i torrentfilen", + "'info'-f‰ltet ‰r inte en dictionary", + "'piece length' f‰ltet saknas eller ‰r korrupt i torrentfilen", + "torrentfilen saknar namnf‰ltet", "ogiltigt namn i torrentfilen (kan vara en attack)", // ... more strings here };