:returns: The current progress of the resource transfer as a *float* between 0.0 and 1.0. */
| 1354 | :returns: The current progress of the resource transfer as a *float* between 0.0 and 1.0. |
| 1355 | */ |
| 1356 | float Resource::get_progress() const { |
| 1357 | assert(_object); |
| 1358 | if (_object->_status == Type::Resource::COMPLETE |
| 1359 | && _object->_segment_index == _object->_total_segments) { |
| 1360 | return 1.0f; |
| 1361 | } |
| 1362 | |
| 1363 | double processed = 0.0; |
| 1364 | double total = 0.0; |
| 1365 | if (_object->_initiator) { |
| 1366 | if (!_object->_split) { |
| 1367 | processed = static_cast<double>(_object->_sent_parts); |
| 1368 | total = static_cast<double>(_object->_total_parts); |
| 1369 | } |
| 1370 | else { |
| 1371 | const uint16_t processed_segments = _object->_segment_index - 1; |
| 1372 | const uint32_t max_parts_per_segment = static_cast<uint32_t>( |
| 1373 | (Type::Resource::MAX_EFFICIENT_SIZE + _object->_sdu - 1) / _object->_sdu); |
| 1374 | const double current_segment_factor = (_object->_total_parts < max_parts_per_segment) |
| 1375 | ? (static_cast<double>(max_parts_per_segment) / static_cast<double>(_object->_total_parts)) |
| 1376 | : 1.0; |
| 1377 | processed = static_cast<double>(processed_segments) * max_parts_per_segment |
| 1378 | + static_cast<double>(_object->_sent_parts) * current_segment_factor; |
| 1379 | total = static_cast<double>(_object->_total_segments) * max_parts_per_segment; |
| 1380 | } |
| 1381 | } |
| 1382 | else { |
| 1383 | if (!_object->_split) { |
| 1384 | processed = static_cast<double>(_object->_received_count); |
| 1385 | total = static_cast<double>(_object->_total_parts); |
| 1386 | } |
| 1387 | else { |
| 1388 | const uint16_t processed_segments = _object->_segment_index - 1; |
| 1389 | const uint32_t max_parts_per_segment = static_cast<uint32_t>( |
| 1390 | (Type::Resource::MAX_EFFICIENT_SIZE + _object->_sdu - 1) / _object->_sdu); |
| 1391 | const double current_segment_factor = (_object->_total_parts < max_parts_per_segment) |
| 1392 | ? (static_cast<double>(max_parts_per_segment) / static_cast<double>(_object->_total_parts)) |
| 1393 | : 1.0; |
| 1394 | processed = static_cast<double>(processed_segments) * max_parts_per_segment |
| 1395 | + static_cast<double>(_object->_received_count) * current_segment_factor; |
| 1396 | total = static_cast<double>(_object->_total_segments) * max_parts_per_segment; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | if (total <= 0.0) return 0.0f; |
| 1401 | const double progress = processed / total; |
| 1402 | return static_cast<float>(progress > 1.0 ? 1.0 : progress); |
| 1403 | } |
| 1404 | |
| 1405 | float Resource::get_segment_progress() const { |
| 1406 | assert(_object); |
nothing calls this directly
no outgoing calls
no test coverage detected