apps/sbc/RateLimit.h
f9701cec
 #ifndef _RateLimit_h_
 #define _RateLimit_h_
 
 #include "AmThread.h"
 #include "atomic_types.h"
 #include <sys/types.h>
 
f9564977
 class DynRateLimit
f9701cec
   : protected AmMutex
 {
   u_int32_t last_update;
   int counter;
 
   unsigned int time_base;
 
f9564977
   void update_limit(int rate, int peak);
f9701cec
 
 public:
f9564977
   // time_base_ms: milliseconds
   DynRateLimit(unsigned int time_base_ms);
f9701cec
 
f9564977
   virtual ~DynRateLimit() {}
b1b95f97
 
a22fa287
   unsigned int getTimeBase() const { return time_base; }
 
f9701cec
   /**
f9564977
    * rate: units/time_base
    * peak: units/time_base
f9701cec
    * returns true if 'size' should be dropped
    */
f9564977
   bool limit(unsigned int rate, unsigned int peak, unsigned int size);
444f4367
 
   /** Get last update timestamp (wheeltimer::wallclock ticks) */
   u_int32_t getLastUpdate() { return last_update; }
f9701cec
 };
 
f9564977
 class RateLimit
   : protected DynRateLimit
 {
   int rate;
   int peak;
 
 public:
   // time_base_ms: milliseconds
   RateLimit(unsigned int rate, unsigned int peak, unsigned int time_base_ms)
     : DynRateLimit(time_base_ms), rate(rate), peak(peak)
   {}
 
   int getRate() const { return rate; }
   int getPeak() const { return peak; }
   unsigned int getTimeBase() const { return DynRateLimit::getTimeBase(); }
 
   /**
    * returns true if 'size' should be dropped
    */
   bool limit(unsigned int size) {
     return DynRateLimit::limit(rate,peak,size);
   }
 };
 
f9701cec
 #endif