by Hector San » Thu, 13 Jul 2006 06:59:22
You want to control ANY application or you mean your own applications?
I can't tell you how you can control other processes, but within your own
its just a matter of throttling your throughput with the socket I/O commands
or the equivalent .NET functionality of a socket I/O.
There are a few varying methodologies depending on per session per user per
service, etc. But essentially, the commonality is to calculate the "sleep"
time between socket reads and writes that will give you the bandwidth you
want.
Here is a snippet code pulled from my TSocketIO class that we used for
throttling socket bandwidths. You can take this C/C++ functionally and add
it to your own .NET code:
DWORD TSocketIO::CalculateThrottle(
const double speedk, // kbits/secs
const DWORD dwBufferSize)
{
if (speedk == 0.0) {
return 0;
}
DWORD time = 0;
DWORD bits = 8;
double rate = speedk/bits;
if (speedk > 0.0) time = (DWORD)(dwBufferSize/rate);
return time; // milli-seconds
}
int TSocketIO::SendThrottle(const char *buf,
DWORD bufsize,
int maxspeed,
DWORD flags)
{
LARGE_INTEGER start_ticks;
LARGE_INTEGER ends_ticks;
if (maxspeed > 0) QueryPerformanceCounter(&start_ticks);
int n = ::send(hSocket, (char *)buf, bufsize,flags);
if ((n > 0) && (maxspeed > 0)) {
QueryPerformanceCounter(&ends_ticks);
ends_ticks.QuadPart -= start_ticks.QuadPart;
double atime = ends_ticks.QuadPart * 1000.0 / frequency.QuadPart;
int time = (int) CalculateThrottle(maxspeed,n) - (DWORD)atime;
if (time > 0) Sleep(time);
}
return n;
}
int TSocketIO::recvthrottle(char *buf,
int bufsize,
int maxspeed,
DWORD flags /* = 0 */)
{
int n;
LARGE_INTEGER start_ticks;
LARGE_INTEGER ends_ticks;
if (maxspeed > 0) QueryPerformanceCounter(&start_ticks);
n = ::recv(hSocket, (char *)buf, bufsize, flags);
if ((n > 0) && (maxspeed > 0)) {
QueryPerformanceCounter(&ends_ticks);
ends_ticks.QuadPart -= start_ticks.QuadPart;
double atime = ends_ticks.QuadPart * 1000.0 / frequency.QuadPart;
int time = (int) CalculateThrottle(maxspeed,n) - (DWORD)atime;
if (time > 0) {
timeBeginPeriod(1);
Sleep(time);
timeEndPeriod(1);
}
}
return n;
}
In short, the above are just wrappers that will "slow down" or "throttle"
the sends and recvs by calculating a sleep based on the desired maximum
speed.
Hope this helps or what you were looking for.