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

« back to all changes in this revision

Viewing changes to src/protocol/peer_connection_seed.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:
39
39
#include <cstring>
40
40
#include <sstream>
41
41
 
42
 
#include "data/content.h"
 
42
#include "download/choke_manager.h"
43
43
#include "download/chunk_statistics.h"
44
44
#include "download/download_info.h"
45
45
#include "download/download_main.h"
58
58
void
59
59
PeerConnectionSeed::initialize_custom() {
60
60
//   if (m_download->content()->chunks_completed() != 0) {
61
 
//     m_up->write_bitfield(m_download->content()->bitfield()->size_bytes());
 
61
//     m_up->write_bitfield(m_download->file_list()->bitfield()->size_bytes());
62
62
 
63
63
//     m_up->buffer()->prepare_end();
64
64
//     m_up->set_position(0);
71
71
  // We assume this won't be called.
72
72
}
73
73
 
74
 
void
75
 
PeerConnectionSeed::receive_finished_chunk(__UNUSED int32_t i) {
76
 
  // We assume this won't be called.
77
 
}
78
 
 
79
74
bool
80
75
PeerConnectionSeed::receive_keepalive() {
81
76
  if (cachedTime - m_timeLastRead > rak::timer::from_seconds(240))
120
115
    return false;
121
116
 
122
117
  } else if (length > (1 << 20)) {
123
 
    throw network_error("PeerConnectionSeed::read_message() got an invalid message length.");
 
118
    throw communication_error("PeerConnectionSeed::read_message() got an invalid message length.");
124
119
  }
125
120
    
126
121
  // We do not verify the message length of those with static
137
132
 
138
133
  switch (buf->read_8()) {
139
134
  case ProtocolBase::CHOKE:
140
 
    m_down->set_choked(true);
141
 
    return true;
142
 
 
143
135
  case ProtocolBase::UNCHOKE:
144
 
    m_down->set_choked(false);
145
136
    return true;
146
137
 
147
138
  case ProtocolBase::INTERESTED:
148
 
    set_remote_interested();
 
139
    m_download->upload_choke_manager()->set_queued(this, &m_upChoke);
149
140
    return true;
150
141
 
151
142
  case ProtocolBase::NOT_INTERESTED:
152
 
    set_remote_not_interested();
 
143
    m_download->upload_choke_manager()->set_not_queued(this, &m_upChoke);
153
144
    return true;
154
145
 
155
146
  case ProtocolBase::HAVE:
163
154
    if (!m_down->can_read_request_body())
164
155
      break;
165
156
 
166
 
    if (!m_up->choked()) {
 
157
    if (!m_upChoke.choked()) {
167
158
      write_insert_poll_safe();
168
159
      read_request_piece(m_down->read_request());
169
160
 
174
165
    return true;
175
166
 
176
167
  case ProtocolBase::PIECE:
177
 
    throw network_error("Received a piece but the connection is strictly for seeding.");
 
168
    throw communication_error("Received a piece but the connection is strictly for seeding.");
178
169
 
179
170
  case ProtocolBase::CANCEL:
180
171
    if (!m_down->can_read_cancel_body())
184
175
    return true;
185
176
 
186
177
  default:
187
 
    throw network_error("Received unsupported message type.");
 
178
    throw communication_error("Received unsupported message type.");
188
179
  }
189
180
 
190
181
  // We were unsuccessfull in reading the message, need more data.
222
213
      while (read_message());
223
214
        
224
215
      if (m_down->buffer()->size_end() == read_size) {
225
 
        read_buffer_move_unused();
226
 
 
 
216
        m_down->buffer()->move_unused();
227
217
      } else {
228
 
        read_buffer_move_unused();
 
218
        m_down->buffer()->move_unused();
229
219
        return;
230
220
      }
231
221
 
254
244
    std::stringstream s;
255
245
    s << "Connection read fd(" << get_fd().get_fd() << ',' << m_down->get_state() << ',' << m_down->last_command() << ") \"" << e.what() << '"';
256
246
 
257
 
    e.set(s.str());
258
 
 
259
 
    throw;
 
247
    throw internal_error(s.str());
260
248
  }
261
249
}
262
250
 
265
253
  // No need to use delayed choke as we are a seeder.
266
254
  if (m_sendChoked && m_up->can_write_choke()) {
267
255
    m_sendChoked = false;
268
 
    m_up->write_choke(m_up->choked());
 
256
    m_up->write_choke(m_upChoke.choked());
269
257
 
270
 
    if (m_up->choked()) {
 
258
    if (m_upChoke.choked()) {
271
259
      m_download->upload_throttle()->erase(m_peerChunks.upload_throttle());
272
260
      up_chunk_release();
273
261
      m_peerChunks.upload_queue()->clear();
277
265
    }
278
266
  }
279
267
 
280
 
  if (!m_up->choked() &&
 
268
  if (!m_upChoke.choked() &&
281
269
      !m_peerChunks.upload_queue()->empty() &&
282
270
      m_up->can_write_piece())
283
271
    write_prepare_piece();
292
280
      switch (m_up->get_state()) {
293
281
      case ProtocolWrite::IDLE:
294
282
 
295
 
        // We might have buffered keepalive message or similar, but
296
 
        // 'end' should remain at the start of the buffer.
297
 
        if (m_up->buffer()->size_end() != 0)
298
 
          throw internal_error("PeerConnectionSeed::event_write() ProtocolWrite::IDLE in a wrong state.");
299
 
 
300
283
        // Fill up buffer.
301
284
        fill_write_buffer();
302
285
 
303
 
        if (m_up->buffer()->size_position() == 0) {
 
286
        if (m_up->buffer()->size_end() == 0) {
304
287
          manager->poll()->remove_write(this);
305
288
          return;
306
289
        }
307
290
 
308
291
        m_up->set_state(ProtocolWrite::MSG);
309
 
        m_up->buffer()->prepare_end();
310
292
 
311
293
      case ProtocolWrite::MSG:
312
 
        m_up->buffer()->move_position(write_stream_throws(m_up->buffer()->position(), m_up->buffer()->remaining()));
313
 
 
314
 
        if (m_up->buffer()->remaining())
 
294
        if (!m_up->buffer()->consume(write_stream_throws(m_up->buffer()->position(), m_up->buffer()->remaining())))
315
295
          return;
316
296
 
317
297
        m_up->buffer()->reset();
360
340
    std::stringstream s;
361
341
    s << "Connection write fd(" << get_fd().get_fd() << ',' << m_up->get_state() << ',' << m_up->last_command() << ") \"" << e.what() << '"';
362
342
 
363
 
    e.set(s.str());
364
 
 
365
 
    throw;
 
343
    throw internal_error(s.str());
366
344
  }
367
345
}
368
346
 
369
347
void
370
348
PeerConnectionSeed::read_have_chunk(uint32_t index) {
371
349
  if (index >= m_peerChunks.bitfield()->size_bits())
372
 
    throw network_error("Peer sent HAVE message with out-of-range index.");
 
350
    throw communication_error("Peer sent HAVE message with out-of-range index.");
373
351
 
374
352
  if (m_peerChunks.bitfield()->get(index))
375
353
    return;
376
354
 
377
 
  m_download->chunk_statistics()->received_have_chunk(&m_peerChunks, index, m_download->content()->chunk_size());
 
355
  m_download->chunk_statistics()->received_have_chunk(&m_peerChunks, index, m_download->file_list()->chunk_size());
378
356
  //m_download->chunk_selector()->received_have_chunk(&m_peerChunks, index);
379
357
 
380
358
  if (m_peerChunks.bitfield()->is_all_set())