From cdb96726df0f383c20bc83448a4e2427317371c0 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Sat, 7 Sep 2013 12:23:53 -0400 Subject: updateAndCheckMulticastBalance and friends --- node/BandwidthAccount.hpp | 57 +++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 34 deletions(-) (limited to 'node/BandwidthAccount.hpp') diff --git a/node/BandwidthAccount.hpp b/node/BandwidthAccount.hpp index 12c303e4..927037f8 100644 --- a/node/BandwidthAccount.hpp +++ b/node/BandwidthAccount.hpp @@ -28,16 +28,14 @@ #ifndef _ZT_BWACCOUNT_HPP #define _ZT_BWACCOUNT_HPP +#include #include +#include + #include "Constants.hpp" #include "Utils.hpp" -#ifdef __WINDOWS__ -#define fmin(a,b) (((a) <= (b)) ? (a) : (b)) -#define fmax(a,b) (((a) >= (b)) ? (a) : (b)) -#endif - namespace ZeroTier { /** @@ -56,27 +54,6 @@ namespace ZeroTier { class BandwidthAccount { public: - /** - * Rate of balance accrual and min/max - */ - struct Accrual - { - /** - * Rate of balance accrual in bytes per second - */ - double bytesPerSecond; - - /** - * Maximum balance that can ever be accrued (should be > 0.0) - */ - double maxBalance; - - /** - * Minimum balance, or maximum allowable "debt" (should be <= 0.0) - */ - double minBalance; - }; - /** * Create an uninitialized account * @@ -88,43 +65,55 @@ public: * Create and initialize * * @param preload Initial balance to place in account + * @param minb Minimum allowed balance (or maximum debt) (<= 0) + * @param maxb Maximum allowed balance (> 0) + * @param acc Rate of accrual in bytes per second */ - BandwidthAccount(double preload) + BandwidthAccount(int32_t preload,int32_t minb,int32_t maxb,int32_t acc) throw() { - init(preload); + init(preload,minb,maxb,acc); } /** * Initialize or re-initialize account * * @param preload Initial balance to place in account + * @param minb Minimum allowed balance (or maximum debt) (<= 0) + * @param maxb Maximum allowed balance (> 0) + * @param acc Rate of accrual in bytes per second */ - inline void init(double preload) + inline void init(int32_t preload,int32_t minb,int32_t maxb,int32_t acc) throw() { _lastTime = Utils::nowf(); _balance = preload; + _minBalance = minb; + _maxBalance = maxb; + _accrual = acc; } /** * Update balance by accruing and then deducting * - * @param ar Current rate of accrual * @param deduct Amount to deduct, or 0.0 to just update * @return New balance with deduction applied */ - inline double update(const Accrual &ar,double deduct) + inline int32_t update(int32_t deduct) throw() { double lt = _lastTime; - double now = _lastTime = Utils::nowf(); - return (_balance = fmax(ar.minBalance,fmin(ar.maxBalance,(_balance + (ar.bytesPerSecond * (now - lt))) - deduct))); + double now = Utils::nowf(); + _lastTime = now; + return (_balance = std::max(_minBalance,std::min(_maxBalance,(int32_t)round(((double)_balance) + (((double)_accrual) * (now - lt))) - deduct))); } private: double _lastTime; - double _balance; + int32_t _balance; + int32_t _minBalance; + int32_t _maxBalance; + int32_t _accrual; }; } // namespace ZeroTier -- cgit v1.2.3