Hi,
Im trying to calculate download/upload speed based on total data downloaded/uploaded.
The problem with my current method is that it sometimes displays a really large value. Heres the code im using, its called after every send/recv, xBandwidthInfo is a global struct used to keep track of the totals/rates.
Code:
void CalculateRates()
{
xBandwidthInfo *m_pInfo = (xBandwidthInfo *)lpvMem;
int nTimeDelta = timeGetTime() - m_pInfo->nLastTime;
if (nTimeDelta == 0)
nTimeDelta = 1;
if (abs(nTimeDelta) >= 1000) // reset every second
{
m_pInfo->nLastDownload = m_pInfo->nTotalDownload;
m_pInfo->nLastUpload = m_pInfo->nTotalUpload;
m_pInfo->nLastTime = timeGetTime();
}
uint nCurrentDownload = m_pInfo->nTotalDownload - m_pInfo->nLastDownload;
uint nCurrentUpload = m_pInfo->nTotalUpload - m_pInfo->nLastUpload;
float fTime = 1000.0f / nTimeDelta;
fTime = abs(fTime);
m_pInfo->fDownloadRate = (float)nCurrentDownload * fTime;
m_pInfo->fUploadRate = (float)nCurrentUpload * fTime;
}