| 329 | } |
| 330 | |
| 331 | void web_peer_connection::write_request(peer_request const& r) |
| 332 | { |
| 333 | INVARIANT_CHECK; |
| 334 | |
| 335 | std::shared_ptr<torrent> t = associated_torrent().lock(); |
| 336 | TORRENT_ASSERT(t); |
| 337 | |
| 338 | TORRENT_ASSERT(t->valid_metadata()); |
| 339 | |
| 340 | torrent_info const& info = t->torrent_file(); |
| 341 | peer_request req = r; |
| 342 | |
| 343 | std::string request; |
| 344 | request.reserve(400); |
| 345 | |
| 346 | int size = r.length; |
| 347 | const int block_size = t->block_size(); |
| 348 | const int piece_size = info.piece_length(); |
| 349 | peer_request pr{}; |
| 350 | |
| 351 | piece_index_t cur_piece = r.piece; |
| 352 | int cur_start = r.start; |
| 353 | |
| 354 | while (size > 0) |
| 355 | { |
| 356 | pr.piece = cur_piece; |
| 357 | pr.start = cur_start; |
| 358 | pr.length = std::min(std::min(block_size, size), info.piece_size_for_req(pr.piece) - pr.start); |
| 359 | TORRENT_ASSERT(validate_piece_request(pr)); |
| 360 | m_requests.push_back(pr); |
| 361 | |
| 362 | if (m_web->restart_request == m_requests.front()) |
| 363 | { |
| 364 | TORRENT_ASSERT(int(m_piece.size()) == m_received_in_piece); |
| 365 | TORRENT_ASSERT(m_piece.empty()); |
| 366 | m_piece = std::move(m_web->restart_piece); |
| 367 | peer_request const& front = m_requests.front(); |
| 368 | TORRENT_ASSERT(front.length > int(m_piece.size())); |
| 369 | |
| 370 | #ifndef TORRENT_DISABLE_LOGGING |
| 371 | peer_log(peer_log_alert::info, "RESTART_DATA", |
| 372 | "data: %d req: (%d, %d) size: %d" |
| 373 | , int(m_piece.size()), static_cast<int>(front.piece), front.start |
| 374 | , front.start + front.length - 1); |
| 375 | #else |
| 376 | TORRENT_UNUSED(front); |
| 377 | #endif |
| 378 | |
| 379 | req.start += int(m_piece.size()); |
| 380 | req.length -= int(m_piece.size()); |
| 381 | |
| 382 | // just to keep the accounting straight for the upper layer. |
| 383 | // it doesn't know we just re-wrote the request |
| 384 | incoming_piece_fragment(int(m_piece.size())); |
| 385 | m_web->restart_request.piece = piece_index_t(-1); |
| 386 | |
| 387 | TORRENT_ASSERT(int(m_piece.size()) == m_received_in_piece); |
| 388 | } |
nothing calls this directly
no test coverage detected