• Create Account

#Actualpapalazaru

Posted 28 September 2012 - 07:48 PM

Just a note, a very crude and simple way to cap bandwidth is to use a bucket monitoring your packet being sent, and payback some of the bandwidth every once in a while. It's lightweight, and reasonably accurate.

namespace utils
{
static double current_time()
{
LARGE_INTEGER frec;
LARGE_INTEGER ticks;
if( !QueryPerformanceFrequency(&frec) ||
!QueryPerformanceCounter(&ticks))
return 0.0;
}
};
class BandwidthThrottler
{
public:
BandwidthThrottler(size_t bits_alloc)
: m_bits_alloc(bits_alloc)
, m_bits_avail(bits_alloc)
, m_timestamp(utils::current_time())
{}
//------------------------------------------------------
// update once in a while
//------------------------------------------------------
void update()
{
// check time since last update.
double  time_now  = utils::current_time();   // time now, taken from high performance counter.
double  time_elapsed = time_now - m_timestamp;   // time since last update.
time_elapsed   = max(min(time_elapsed, 1.0), 0.0); // clamp in the range [0.0, 1.0] in seconds.
m_timestamp	= time_now;	   // timestamp that update.

// calculate bit rate available, after paying back a bit of bandwidth.
size_t  bits_payback = (size_t)((double)m_bits_alloc * (time_elapsed / 1.0)); // how much bandwidth we have to pay back since last update,
// given the maximum number of bits we can send over 1.0 seconds.
m_bits_avail = min(m_bits_avail + bits_payback, m_bits_alloc);	 // how much bandwidth we will have available.
}
//------------------------------------------------------
// a packet has been sent, so consume a bit of bandwidth
// so the packet size, plus the size of a UDP header in bits.
// limit to zero if we consume more than we have available.
//------------------------------------------------------
void consume_bits(size_t bits)
{
m_bits_avail -= min(m_bits_avail, bits);
}

size_t get_bits_available() const { return m_bits_avail; }
size_t get_bits_allocated() const { return m_bits_alloc; }
void set_bits_allocated(size_t bits_alloc) { m_bits_alloc = bits_alloc; }
private:
size_t m_bits_avail; // the bitrate cap we want to use.
size_t m_bits_alloc; // the number of bits available for transmission.
double m_timestamp; // the timestamp of the last update.
};


#4papalazaru

Posted 28 September 2012 - 07:45 PM

Just a note, a very crude and simple way to cap bandwidth is to use a bucket, and payback some of the bandwidth every once in a while. It's lightweight, and reasonably accurate.

namespace utils
{
static double current_time()
{
LARGE_INTEGER frec;
LARGE_INTEGER ticks;
if( !QueryPerformanceFrequency(&frec) ||
!QueryPerformanceCounter(&ticks))
return 0.0;
}
};
class BandwidthThrottler
{
public:
BandwidthThrottler(size_t bits_alloc)
: m_bits_alloc(bits_alloc)
, m_bits_avail(bits_alloc)
, m_timestamp(utils::current_time())
{}
//------------------------------------------------------
// update once in a while
//------------------------------------------------------
void update()
{
// check time since last update.
double  time_now  = utils::current_time();   // time now, taken from high performance counter.
double  time_elapsed = time_now - m_timestamp;   // time since last update.
time_elapsed   = max(min(time_elapsed, 1.0), 0.0); // clamp in the range [0.0, 1.0] in seconds.
m_timestamp	= time_now;	   // timestamp that update.

// calculate bit rate available, after paying back a bit of bandwidth.
size_t  bits_payback = (size_t)((double)m_bits_alloc * (time_elapsed / 1.0)); // how much bandwidth we have to pay back since last update,
// given the maximum number of bits we can send over 1.0 seconds.
m_bits_avail = min(m_bits_avail + bits_payback, m_bits_alloc);	 // how much bandwidth we will have available.
}
//------------------------------------------------------
// a packet has been sent, so consume a bit of bandwidth
// so the packet size, plus the size of a UDP header in bits.
// limit to zero if we consume more than we have available.
//------------------------------------------------------
void consume_bits(size_t bits)
{
m_bits_avail -= min(m_bits_avail, bits);
}

size_t get_bits_available() const { return m_bits_avail; }
size_t get_bits_allocated() const { return m_bits_alloc; }
void set_bits_allocated(size_t bits_alloc) { m_bits_alloc = bits_alloc; }
private:
size_t m_bits_avail; // the bitrate cap we want to use.
size_t m_bits_alloc; // the number of bits available for transmission.
double m_timestamp; // the timestamp of the last update.
};


#3papalazaru

Posted 28 September 2012 - 07:44 PM

Just a note, a very crude and simple way to cap bandwidth is to use a bucket, and payback some of the bandwidth every once in a while. It's lightweight, and reasonably accurate.

namespace utils
{
static double current_time()
{
LARGE_INTEGER frec;
LARGE_INTEGER ticks;
if( !QueryPerformanceFrequency(&frec) ||
!QueryPerformanceCounter(&ticks))
return 0.0;
}
};
class BandwidthThrottler
{
public:
BandwidthThrottler(size_t bits_alloc)
: m_bits_alloc(bits_alloc)
, m_bits_avail(bits_alloc)
, m_timestamp(utils::current_time())
{}
//------------------------------------------------------
// update once in a while
//------------------------------------------------------
void update()
{
// check time since last update.
double  time_now  = utils::current_time();   // time now, taken from high performance counter.
double  time_elapsed = time_now - m_timestamp;   // time since last update.
time_elapsed   = max(min(time_elapsed, 1.0), 0.0); // clamp in the range [0.0, 1.0] in seconds.
m_timestamp	= time_now;	   // timestamp that update.

// calculate bit rate available, after paying back a bit of bandwidth.
size_t  bits_payback = (size_t)((double)m_bits_alloc * (time_elapsed / 1.0)); // how much bandwidth we have to pay back since last update,
// given the maximum number of bits we can send over 1.0 seconds.
m_bits_avail = min(m_bits_avail + bits_payback, m_bits_alloc);	 // how much bandwidth we will have available.}
//------------------------------------------------------
// a packet has been sent, so consume a bit of bandwidth
// so the packet size, plus the size of a UDP header in bits.
// limit to zero if we consume more than we have available.
//------------------------------------------------------
void consume_bits(size_t bits)
{
m_bits_avail -= min(m_bits_avail, bits);
}

size_t get_bits_available() const { return m_bits_avail; }
size_t get_bits_allocated() const { return m_bits_alloc; }
void set_bits_allocated(size_t bits_alloc) { m_bits_alloc = bits_alloc; }
private:
size_t m_bits_avail; // the bitrate cap we want to use.
size_t m_bits_alloc; // the number of bits available for transmission.
double m_timestamp; // the timestamp of the last update.
};


#2papalazaru

Posted 28 September 2012 - 07:43 PM

Just a note, a very crude and simple way to cap bandwidth is to use a bucket, and payback some of the bandwidth every once in a while. It's lightweight, and reasonably accurate.

namespace utils
{
static double current_time()
{
LARGE_INTEGER frec;
LARGE_INTEGER ticks;
if( !QueryPerformanceFrequency(&frec) ||
!QueryPerformanceCounter(&ticks))
return 0.0;
}
};
class BandwidthThrottler
{
public:
BandwidthThrottler(size_t bits_alloc)
: m_bits_alloc(bits_alloc)
, m_bits_avail(bits_alloc)
, m_timestamp(utils::current_time())
{}
//------------------------------------------------------
// update once in a while
//------------------------------------------------------
void update()
{
// check time since last update.
double  time_now  = utils::current_time();   // time now, taken from high performance counter.
double  time_elapsed = time_now - m_timestamp;   // time since last update.
time_elapsed   = max(min(time_elapsed, 1.0), 0.0); // clamp in the range [0.0, 1.0] in seconds.
m_timestamp	= time_now;	   // timestamp that update.

// calculate bit rate available, after paying back a bit of bandwidth.
size_t  bits_payback = (size_t)((double)m_bits_alloc * (time_elapsed / 1.0)); // how much bandwidth we have to pay back since last update, given the maximum bit rate.
m_bits_avail = min(m_bits_avail + bits_payback, m_bits_alloc);	 // how much bandwidth we will have available.
}
//------------------------------------------------------
// a packet has been sent, so consume a bit of bandwidth
// so the packet size, plus the size of a UDP header in bits.
// limit to zero if we consume more than we have available.
//------------------------------------------------------
void consume_bits(size_t bits)
{
m_bits_avail -= min(m_bits_avail, bits);
}

size_t get_bits_available() const { return m_bits_avail; }
size_t get_bits_allocated() const { return m_bits_alloc; }
void set_bits_allocated(size_t bits_alloc) { m_bits_alloc = bits_alloc; }
private:
size_t m_bits_avail; // the bitrate cap we want to use.
size_t m_bits_alloc; // the number of bits available for transmission.
double m_timestamp; // the timestamp of the last update.
};


#1papalazaru

Posted 28 September 2012 - 07:42 PM

Just a note, a very crude and simple way to cap bandwidth is to use a bucket, and payback some of the bandwidth every once in a while. It's lightweight, and reasonably accurate.

namespace utils
{
static double current_time()
{
LARGE_INTEGER frec;
LARGE_INTEGER ticks;
if( !QueryPerformanceFrequency(&frec) ||
!QueryPerformanceCounter(&ticks))
return 0.0;
}
};
class BandwidthThrottler
{
public:
BandwidthThrottler(size_t bits_alloc)
: m_bits_alloc(bits_alloc)
, m_bits_avail(bits_alloc)
, m_timestamp(utils::current_time())
{}
//------------------------------------------------------
// update once in a while
//------------------------------------------------------
void update()
{
// check time since last update.
double  time_now  = utils::current_time();   // time now, taken from high performance counter.
double  time_elapsed = time_now - m_timestamp;   // time since last update.
time_elapsed   = max(min(time_elapsed, 1.0), 0.0); // clamp in the range [0.0, 1.0] in seconds.
m_timestamp	= time_now;	   // timestamp that update.

// calculate bit rate available, after paying back a bit of bandwidth.
size_t  bits_payback = (size_t)((double)m_bits_alloc * (time_elapsed / 1.0)); // how much bandwidth we have to pay back since last update, maximum of one second (since we measure bits per second bitrate).
m_bits_avail = min(m_bits_avail + bits_payback, m_bits_alloc);	 // how much bandwidth we will have available.
}
//------------------------------------------------------
// a packet has been sent, so consume a bit of bandwidth
// so the packet size, plus the size of a UDP header in bits.
// limit to zero if we consume more than we have available.
//------------------------------------------------------
void consume_bits(size_t bits)
{
m_bits_avail -= min(m_bits_avail, bits);
}

size_t get_bits_available() const { return m_bits_avail; }
size_t get_bits_allocated() const { return m_bits_alloc; }
void set_bits_allocated(size_t bits_alloc) { m_bits_alloc = bits_alloc; }
private:
size_t m_bits_avail; // the bitrate cap we want to use.
size_t m_bits_alloc; // the number of bits available for transmission.
double m_timestamp; // the timestamp of the last update.
};


PARTNERS