~ubuntu-branches/debian/experimental/libtorrent/experimental

« back to all changes in this revision

Viewing changes to src/data/hash_torrent.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Rivas
  • Date: 2007-03-31 10:31:05 UTC
  • mto: (4.1.4 gutsy) (6.2.1 squeeze) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20070331103105-jzpp1rml6ud0ff75
Tags: upstream-0.11.4
ImportĀ upstreamĀ versionĀ 0.11.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
#include "config.h"
38
38
 
 
39
#include "data/chunk_list.h"
39
40
#include "torrent/exceptions.h"
40
 
#include "data/chunk_list.h"
 
41
 
41
42
#include "hash_torrent.h"
42
43
#include "hash_queue.h"
 
44
#include "globals.h"
43
45
 
44
46
namespace torrent {
45
47
 
48
50
  m_outstanding(-1),
49
51
  m_errno(0),
50
52
 
51
 
  m_chunkList(c),
52
 
  m_queue(NULL) {
 
53
  m_chunkList(c) {
53
54
}
54
55
 
55
56
bool
57
58
  if (m_position == m_chunkList->size())
58
59
    return true;
59
60
 
60
 
  if (!is_checking()) {
61
 
    if (m_position > 0 || m_queue == NULL || m_chunkList == NULL || m_chunkList->empty())
62
 
      throw internal_error("HashTorrent::start() call failed.");
63
 
 
64
 
    m_outstanding = 0;
65
 
  }
66
 
 
67
 
  // This doesn't really handle paused hashing properly... Do we set
68
 
  // m_outstanding to -1 when stopping?
 
61
  if (m_position > 0 || m_chunkList->empty())
 
62
    throw internal_error("HashTorrent::start() call failed.");
 
63
 
 
64
  m_outstanding = 0;
69
65
 
70
66
  queue(tryQuick);
71
67
  return m_position == m_chunkList->size();
99
95
 
100
96
void
101
97
HashTorrent::receive_chunkdone() {
102
 
  if (m_outstanding == -1)
103
 
    throw internal_error("HashTorrent::receive_chunkdone() m_outstanding < 0.");
 
98
  if (m_outstanding <= 0)
 
99
    throw internal_error("HashTorrent::receive_chunkdone() m_outstanding <= 0.");
104
100
 
105
101
  // m_signalChunk will always point to
106
102
  // DownloadMain::receive_hash_done, so it will take care of cleanup.
107
103
  //
108
104
  // Make sure we call chunkdone before torrentDone has a chance to
109
105
  // trigger.
110
 
//   m_slotChunkDone(handle, hash);
111
 
  m_outstanding--;
112
 
 
113
 
  // Don't add more when we've stopped. Use some better condition than
114
 
  // m_outstanding. This code is ugly... needs a refactoring, a
115
 
  // seperate flag for active and allow pause or clearing the state.
116
 
  if (m_outstanding >= 0)
117
 
    queue(false);
 
106
  m_outstanding--;
 
107
 
 
108
  queue(false);
 
109
}
 
110
 
 
111
// Mark unsuccessful checks so that if we have just stopped the
 
112
// hash checker it will ensure those pieces get rechecked upon
 
113
// restart.
 
114
void
 
115
HashTorrent::receive_chunk_cleared(uint32_t index) {
 
116
  if (m_outstanding <= 0)
 
117
    throw internal_error("HashTorrent::receive_chunk_cleared() m_outstanding < 0.");
 
118
  
 
119
  if (m_ranges.has(index))
 
120
    throw internal_error("HashTorrent::receive_chunk_cleared() m_ranges.has(index).");
 
121
 
 
122
  m_outstanding--;
 
123
  m_ranges.insert(index, index + 1);
118
124
}
119
125
 
120
126
void
138
144
 
139
145
    // Need to do increment later if we're going to support resume
140
146
    // hashing a quick hashed torrent.
141
 
    ChunkHandle handle = m_chunkList->get(m_position++, false);
 
147
    ChunkHandle handle = m_chunkList->get(m_position, false);
142
148
 
143
149
    if (quick) {
144
150
      // We're not actually interested in doing any hashing, so just
156
162
      if (handle.error_number().is_valid())
157
163
        return;
158
164
 
 
165
      m_position++;
159
166
      continue;
160
167
 
161
168
    } else {
169
176
        // We wait for all the outstanding chunks to be checked before
170
177
        // borking completely, else low-memory devices might not be able
171
178
        // to finish the hash check.
172
 
        if (m_outstanding != 0) {
173
 
          m_position--;
 
179
        if (m_outstanding != 0)
174
180
          return;
175
 
        }
176
181
 
177
182
        // The rest of the outstanding chunks get ignored by
178
183
        // DownloadWrapper::receive_hash_done. Obsolete.
185
190
        return;
186
191
      }
187
192
 
 
193
      m_position++;
 
194
 
188
195
      // Missing file, skip the hash check.
189
196
      if (!handle.is_valid())
190
197
        continue;
191
198
 
192
 
      m_slotCheckChunk(handle);
 
199
      m_slotCheck(handle);
193
200
      m_outstanding++;
194
201
    }
195
202
  }