* Decides the (minimum) amount the socket needs to send to prevent timeout. * * @author SlugFiller */
| 666 | * @author SlugFiller |
| 667 | */ |
| 668 | uint32 CEMSocket::GetNeededBytes() |
| 669 | { |
| 670 | uint64 sendgap; |
| 671 | |
| 672 | uint64 timetotal; |
| 673 | uint64 timeleft; |
| 674 | uint64 sizeleft, sizetotal; |
| 675 | |
| 676 | { |
| 677 | std::lock_guard<std::mutex> lock(m_sendLocker); |
| 678 | |
| 679 | if (byConnected == ES_DISCONNECTED) { |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | if (!((sendbuffer && !m_currentPacket_is_controlpacket) || !m_standard_queue.empty())) { |
| 684 | // No standard packet to send. Even if data needs to be sent to prevent timeout, there's nothing to send. |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | if (((sendbuffer && !m_currentPacket_is_controlpacket)) && !m_control_queue.empty()) |
| 689 | m_bAccelerateUpload = true; // We might be trying to send a block request, accelerate packet |
| 690 | |
| 691 | uint64 now = ::GetTickCount64(); |
| 692 | sendgap = now - lastCalledSend; |
| 693 | |
| 694 | timetotal = m_bAccelerateUpload?45000:90000; |
| 695 | timeleft = now - lastFinishedStandard; |
| 696 | if (sendbuffer && !m_currentPacket_is_controlpacket) { |
| 697 | sizeleft = sendblen-sent; |
| 698 | sizetotal = sendblen; |
| 699 | } else { |
| 700 | sizeleft = sizetotal = m_standard_queue.front().packet->GetRealPacketSize(); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | if (timeleft >= timetotal) |
| 705 | return sizeleft; |
| 706 | timeleft = timetotal-timeleft; |
| 707 | if (timeleft*sizetotal >= timetotal*sizeleft) { |
| 708 | // don't use 'GetTimeOut' here in case the timeout value is high, |
| 709 | if (sendgap > SEC2MS(20)) |
| 710 | return 1; // Don't let the socket itself time out - Might happen when switching from spread(non-focus) slot to trickle slot |
| 711 | return 0; |
| 712 | } |
| 713 | uint64 decval = timeleft*sizetotal/timetotal; |
| 714 | if (!decval) |
| 715 | return sizeleft; |
| 716 | if (decval < sizeleft) |
| 717 | return sizeleft-decval+1; // Round up |
| 718 | else |
| 719 | return 1; |
| 720 | } |
| 721 | |
| 722 | |
| 723 | /** |
no test coverage detected