/*
	ZNetBandwidthMeter.hpp
	Author: Patrick Baggett	<ptbaggett@762studios.com>
	Created: 7/10/2013

	Purpose:

	** NOT PART OF PUBLIC SDK **
	This class is not part of the public SDK; its fields and methods are not present
	in the documentation and cannot be guaranteed in future revisions.
	** NOT PART OF PUBLIC SDK **
	
	Bandwidth metering using a simple token bucket algorithm. A single value is
	metered, so a incoming / outgoing each need an instance.

	License:
	
	Copyright 2013, 762 Studios
*/

#pragma once

#ifndef _ZNETBANDWIDTHMETER_HPP
#define _ZNETBANDWIDTHMETER_HPP


#include <pstdint.h>

class ZNetBandwidthMeter
{
	public:
		/* Sets the new bandwidth limit */
		void SetLimit(uint32_t newLimit);
	
		/* Resets the meter for reuse. Takes the current time */
		void Reset(uint64_t newStartTime);
		
		/* Try to allocate a given number of bytes */
		bool TryAllocate(uint32_t bytes);

		/* Update the token bucket */
		void Update(uint64_t newTime);

		/* Get the bandwidth limit */
		uint32_t GetLimit() const { return limit; }
		
		/* Get the available instantaneous bandwidth */
		uint32_t GetAvailable() const { return tokens; }
		
	private: //Should be POD basically
		uint64_t lastTime;	//The last time this was updated
		uint32_t limit;		//The bandwidth limit, in bytes per second
		uint32_t tokens;	//The available bandwidth within this 1 second timeframe
};

#endif

