[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: branches/ktorrent/2.0/libktorrent/net
From: Joris Guisson <joris.guisson () gmail ! com>
Date: 2006-08-14 13:36:13
Message-ID: 1155562573.507695.19250.nullmailer () svn ! kde ! org
[Download RAW message or body]
SVN commit 572980 by guisson:
Merged up and download cap tweaks from trunk
M +11 -1 bufferedsocket.cpp
M +6 -0 bufferedsocket.h
M +59 -78 socketmonitor.cpp
M +5 -5 socketmonitor.h
--- branches/ktorrent/2.0/libktorrent/net/bufferedsocket.cpp #572979:572980
@@ -186,9 +186,19 @@
return bw;
}
+ void BufferedSocket::updateUpSpeed()
+ {
+ up_speed->update();
+ }
+
+ void BufferedSocket::updateDownSpeed()
+ {
+ down_speed->update();
+ }
+
void BufferedSocket::updateSpeeds()
{
+ up_speed->update();
down_speed->update();
- up_speed->update();
}
}
--- branches/ktorrent/2.0/libktorrent/net/bufferedsocket.h #572979:572980
@@ -108,6 +108,12 @@
float getUploadRate() const;
/// Update the speeds of the socket
+ void updateUpSpeed();
+
+ /// Update the speeds of the socket
+ void updateDownSpeed();
+
+ /// Update up and down speed
void updateSpeeds();
private:
--- branches/ktorrent/2.0/libktorrent/net/socketmonitor.cpp #572979:572980
@@ -61,9 +61,8 @@
bool isRunning() const {return running;}
};
- SocketMonitor::SocketMonitor() : mt(mt),last_selected(0),speeds_last_updated(0)
+ SocketMonitor::SocketMonitor() : mt(mt),prev_upload_time(0),prev_download_time(0)
{
- leftover_d = leftover_u;
}
@@ -102,7 +101,7 @@
if (start_thread)
{
Out(SYS_CON|LOG_DEBUG) << "Starting socketmonitor thread" << endl;
- last_selected = bt::GetCurrentTime();
+ prev_upload_time = prev_download_time = bt::GetCurrentTime();
if (!mt)
mt = new MonitorThread(this);
@@ -122,68 +121,60 @@
}
}
- void SocketMonitor::processIncomingData(QPtrList<BufferedSocket> & rbs,Uint32 now)
+ void SocketMonitor::processIncomingData(QValueList<BufferedSocket*> & rbs)
{
- Uint32 allowed = (Uint32)floor(dcap * (now - last_selected) * (1.0 / 1024.0));
- Uint32 allowance = allowed + leftover_d;
- Uint32 cnt = 0;
- QPtrList<BufferedSocket>::iterator i = rbs.begin();
- while (i != rbs.end() && allowance > 0)
+ Uint32 now = bt::GetCurrentTime();
+ Uint32 allowance = (Uint32)ceil(1.02 * dcap * (now - prev_download_time) * 0.001);
+ prev_download_time = now;
+
+ Uint32 bslot = allowance / rbs.count() + 1;
+
+ while (rbs.count() > 0 && allowance > 0)
{
- Uint32 as = (Uint32)floor(allowance / (rbs.count() - cnt));
- BufferedSocket* s = *i;
- Uint32 ret = 0;
- if (as > 0)
- ret = s->readBuffered(as);
- else
- ret = s->readBuffered(allowance);
+ Uint32 as = bslot;
+ if (as > allowance)
+ as = allowance;
+ BufferedSocket* s = rbs.first();
+ rbs.pop_front();
+
+ Uint32 ret = s->readBuffered(as);
+ if (ret == as) // if this socket did what it was supposed to do, it can have \
another go if stuff is leftover + rbs.append(s);
+
if (ret > allowance)
allowance = 0;
else
allowance -= ret;
- cnt++;
- i++;
}
-
- Uint32 pld = leftover_d;
- leftover_d = allowance;
- if (leftover_d >= pld)
- leftover_d -= pld;
- else
- leftover_d = 0;
}
- void SocketMonitor::processOutgoingData(QPtrList<BufferedSocket> & wbs,Uint32 now)
+ void SocketMonitor::processOutgoingData(QValueList<BufferedSocket*> & wbs)
{
- Uint32 allowed = (Uint32)floor(ucap * (now - last_selected) * (1.0 / 1024.0));
- Uint32 allowance = allowed + leftover_u;
- Uint32 cnt = 0;
- QPtrList<BufferedSocket>::iterator i = wbs.begin();
- while (i != wbs.end() && allowance > 0)
+ Uint32 now = bt::GetCurrentTime();
+ Uint32 allowance = (Uint32)ceil(ucap * (now - prev_upload_time) * 0.001);
+ prev_upload_time = now;
+
+ Uint32 bslot = allowance / wbs.count() + 1;
+
+ while (wbs.count() > 0 && allowance > 0)
{
- Uint32 as = (Uint32)floor(allowance / (wbs.count() - cnt));
- BufferedSocket* s = *i;
- Uint32 ret = 0;
- if (as > 0)
- ret = s->writeBuffered(as);
- else
- ret = s->writeBuffered(allowance);
+ Uint32 as = bslot;
+ if (as > allowance)
+ as = allowance;
+
+ BufferedSocket* s = wbs.first();
+ wbs.pop_front();
+
+ Uint32 ret = s->writeBuffered(as);
+ if (ret == as)
+ wbs.append(s); // it can go again if necessary
if (ret > allowance)
allowance = 0;
else
allowance -= ret;
- cnt++;
- i++;
}
-
- /* Uint32 plu = leftover_u;
- leftover_u = allowance;
- if (leftover_u >= plu)
- leftover_u -= plu;
- else*/
- leftover_u = 0;
}
void SocketMonitor::update()
@@ -192,11 +183,7 @@
fd_set fds,wfds;
FD_ZERO(&fds);
FD_ZERO(&wfds);
-
- /* bool update_speed = bt::GetCurrentTime() - speeds_last_updated >= 250;
- if (update_speed)
- speeds_last_updated = bt::GetCurrentTime();
- */
+
int max = 0;
mutex.lock();
QPtrList<BufferedSocket>::iterator itr = smap.begin();
@@ -220,14 +207,14 @@
}
mutex.unlock();
- struct timeval tv = {0,50*1000};
-
+ struct timeval tv = {0,100*1000};
+ Uint32 before = bt::GetCurrentTime(); // get the current time
if (select(max+1,&fds,&wfds,NULL,&tv) > 0)
{
Uint32 now = bt::GetCurrentTime(); // get the current time
Uint32 num_to_read = 0;
- QPtrList<BufferedSocket> rbs;
- QPtrList<BufferedSocket> wbs;
+ QValueList<BufferedSocket*> rbs;
+ QValueList<BufferedSocket*> wbs;
mutex.lock();
QPtrList<BufferedSocket>::iterator itr = smap.begin();
@@ -265,32 +252,26 @@
itr++;
}
- if (dcap > 0)
- processIncomingData(rbs,now);
+ if (dcap > 0 && rbs.count() > 0)
+ processIncomingData(rbs);
+ else
+ prev_download_time = now;
- if (ucap > 0)
- processOutgoingData(wbs,now);
-
- mutex.unlock();
- if (dcap > 0 || ucap > 0)
- {
- // sleep enough, so we don't consume to much
- if (last_selected - now < 50)
- {
- last_selected = now;
- usleep(100*1000);
- }
- else
- {
- last_selected = now;
- }
- }
+ if (ucap > 0 && wbs.count() > 0)
+ processOutgoingData(wbs);
else
- {
- last_selected = now;
- }
+ prev_upload_time = now;
+ mutex.unlock();
+ if (now - before < 100)
+ usleep(100*1000);
}
+ else
+ {
+ Uint32 now = bt::GetCurrentTime(); // get the current time
+ if (now - before < 100)
+ usleep(100*1000);
+ }
}
--- branches/ktorrent/2.0/libktorrent/net/socketmonitor.h #572979:572980
@@ -23,6 +23,7 @@
#include <qstring.h>
#include <qmutex.h>
#include <qptrlist.h>
+#include <qvaluelist.h>
#include <util/constants.h>
@@ -43,9 +44,8 @@
QMutex mutex;
MonitorThread* mt;
QPtrList<BufferedSocket> smap;
- Uint32 last_selected;
- Uint32 speeds_last_updated;
- Uint32 leftover_d,leftover_u;
+ Uint32 prev_upload_time;
+ Uint32 prev_download_time;
SocketMonitor();
public:
@@ -59,8 +59,8 @@
static void setUploadCap(Uint32 bytes_per_sec);
static SocketMonitor & instance() {return self;}
private:
- void processOutgoingData(QPtrList<BufferedSocket> & wbs,Uint32 now);
- void processIncomingData(QPtrList<BufferedSocket> & rbs,Uint32 now);
+ void processOutgoingData(QValueList<BufferedSocket*> & wbs);
+ void processIncomingData(QValueList<BufferedSocket*> & rbs);
};
}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic