void RequestQueue::operator=(RequestQueue &rq) { PSLICE n, u = (PSLICE) 0; size_t idx; int flag = 0; if( rq_head ) _empty_slice_list(&rq_head); rq_head = rq.rq_head; rq_send = rq_head; // Reassign only the first piece represented in the queue. n = rq_head; idx = n->index; for( ; n ; u = n,n = u->next ){ if( rq.rq_send == n ) flag = 1; if( n->index != idx ) break; } if(n){ u->next = (PSLICE) 0; rq.rq_head = n; if(flag) rq.rq_send = rq.rq_head; }else{ rq.rq_head = (PSLICE) 0; rq.rq_send = rq.rq_head; } }
bool RequestQueue::Delete(bt_index_t idx) { PIECE *piece, *prev; if( !(piece = FindPiece(idx, &prev)) ) return false; if( prev ) prev->next = piece->next; else rq_head = piece->next; if( rq_send && rq_send->index == idx ) rq_send = piece->next ? piece->next->slices : (SLICE *)0; piece->next = (PIECE *)0; _empty_slice_list(&piece); return true; }
// Returns true if the piece was moved successfully. bool RequestQueue::Transfer(RequestQueue &dstq, bt_index_t idx) { bool result = true; PIECE *piece, *prev; if( !(piece = FindPiece(idx, &prev)) ) return true; if( rq_send && rq_send->index == idx ) rq_send = piece->next ? piece->next->slices : (SLICE *)0; if( prev ) prev->next = piece->next; else rq_head = piece->next; piece->next = (PIECE *)0; if( (&PENDING == &dstq && piece->count >= NSlices(piece->slices->index)) || !dstq.Append(piece) ){ _empty_slice_list(&piece); result = false; } return result; }
bt_index_t RequestQueue::Reassign(RequestQueue &dstq, const Bitfield &bf) { PIECE *piece, *prev = (PIECE *)0; bt_index_t idx = BTCONTENT.GetNPieces(); for( piece = rq_head; piece; piece = prev ? prev->next : rq_head ){ if( bf.IsSet(piece->slices->index) && !dstq.HasPiece(piece->slices->index) ){ idx = piece->slices->index; if( rq_send && rq_send->index == idx ) rq_send = piece->next ? piece->next->slices : (SLICE *)0; if( prev ) prev->next = piece->next; else rq_head = piece->next; piece->next = (PIECE *)0; if( !dstq.Append(piece) ) _empty_slice_list(&piece); else break; }else prev = piece; } return idx; }
void RequestQueue::SetHead(PSLICE ps) { if( rq_head ) _empty_slice_list(&rq_head); rq_head = ps; rq_send = rq_head; }
void RequestQueue::Empty() { if(rq_head) _empty_slice_list(&rq_head); rq_send = rq_head; }
RequestQueue::~RequestQueue() { if( rq_head ) _empty_slice_list(&rq_head); }