30 lines
913 B
C
Raw Normal View History

2019-02-10 21:46:39 -05:00
#pragma once
#include "common.h"
class RateLimiter
{
public:
RateLimiter(int requests, int delay) : requestsLeft(requests), delay(delay) {}
2019-06-03 17:58:30 -04:00
bool Request() { CreateTimerQueueTimer(DUMMY, timerQueue, [](void* This, BOOLEAN) { ((RateLimiter*)This)->requestsLeft += 1; }, this, delay, 0, 0); return --requestsLeft > 0; }
2019-02-10 21:46:39 -05:00
int delay;
private:
std::atomic<int> requestsLeft;
AutoHandle<Functor<DeleteTimerQueue>> timerQueue = CreateTimerQueue();
};
inline std::wstring StringToWideString(const std::string& text)
{
std::vector<wchar_t> buffer(text.size() + 1);
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, buffer.data(), buffer.size());
return buffer.data();
}
inline std::string WideStringToString(const std::wstring& text)
{
std::vector<char> buffer((text.size() + 1) * 4);
WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, buffer.data(), buffer.size(), nullptr, nullptr);
return buffer.data();
}