// when can a certian ammount of data be sent? CTimerValue CPacketBufferStats::GetPacketSendTime(SLONG slSize) { CTimerValue tvNow = _pTimer->GetHighPrecisionTimer(); // calculate how much should the packet be delayed due to latency and due to bandwidth CTimerValue tvBandwidth; if (pbs_fBandwidthLimit<=0.0f) { tvBandwidth = CTimerValue(0.0); } else { tvBandwidth = CTimerValue(DOUBLE((slSize*8)/pbs_fBandwidthLimit)); } CTimerValue tvLatency; if (pbs_fLatencyLimit<=0.0f && pbs_fLatencyVariation<=0.0f) { tvLatency = CTimerValue(0.0); } else { tvLatency = CTimerValue(DOUBLE(pbs_fLatencyLimit+(pbs_fLatencyVariation*rand())/RAND_MAX)); } // time when the packet should be sent is max of CTimerValue tvStart( Max( // current time plus latency and (tvNow+tvLatency).tv_llValue, // next free point in time pbs_tvTimeNextPacketStart.tv_llValue)); // remember next free time and return it pbs_tvTimeNextPacketStart = tvStart+tvBandwidth; return pbs_tvTimeNextPacketStart; };
// get time when block of given size will be finished if started now CTimerValue CBlockBufferStats::GetBlockFinalTime(SLONG slSize) { CTimerValue tvNow = _pTimer->GetHighPrecisionTimer(); // calculate how much should block be delayed due to latency and due to bandwidth CTimerValue tvBandwidth; if (bbs_fBandwidthLimit<=0.0f) { tvBandwidth = CTimerValue(0.0); } else { tvBandwidth = CTimerValue(DOUBLE((slSize*8)/bbs_fBandwidthLimit)); } CTimerValue tvLatency; if (bbs_fLatencyLimit<=0.0f && bbs_fLatencyVariation<=0.0f) { tvLatency = CTimerValue(0.0); } else { tvLatency = CTimerValue(DOUBLE(bbs_fLatencyLimit+(bbs_fLatencyVariation*rand())/RAND_MAX)); } // start of packet receiving is later of CTimerValue tvStart( Max( // current time plus latency and (tvNow+tvLatency).tv_llValue, // next free point in time bbs_tvTimeUsed.tv_llValue)); // remember next free time and return it bbs_tvTimeUsed = tvStart+tvBandwidth; return bbs_tvTimeUsed; }