1er commit

This commit is contained in:
2021-08-21 10:14:47 +02:00
commit a99ecf7c2d
99 changed files with 66605 additions and 0 deletions

89
src/misc/Compression.cpp Normal file
View File

@@ -0,0 +1,89 @@
#include "misc/Compression.h"
#include <zlib.h>
#include <cassert>
#define COMPRESSION_THRESHOLD 64
namespace td {
namespace utils {
unsigned long inflate(const std::string& source, std::string& dest) {
unsigned long size = dest.size();
uncompress((Bytef*)&dest[0], &size, (const Bytef*)source.c_str(), source.length());
return size;
}
unsigned long deflate(const std::string& source, std::string& dest) {
unsigned long size = source.length();
dest.resize(size);
compress((Bytef*)&dest[0], &size, (const Bytef*)source.c_str(), source.length());
dest.resize(size);
return size;
}
DataBuffer Compress(const DataBuffer& buffer) {
std::string compressedData;
DataBuffer packet;
if (buffer.GetSize() < COMPRESSION_THRESHOLD) {
// Don't compress since it's a small packet
std::uint64_t dataLength = 0;
std::uint64_t packetLength = buffer.GetSize() + sizeof(dataLength);
packet << packetLength;
packet << dataLength;
packet << buffer;
return packet;
}
deflate(buffer.ToString(), compressedData);
std::uint64_t dataLength = buffer.GetSize();
std::uint64_t packetLength = compressedData.length() + sizeof(dataLength);
packet << packetLength;
packet << dataLength;
packet << compressedData;
return packet;
}
DataBuffer Decompress(DataBuffer& buffer, std::size_t packetLength){
std::uint64_t uncompressedLength;
buffer >> uncompressedLength;
std::size_t compressedLength = packetLength - sizeof(uncompressedLength);
if (uncompressedLength == 0) {
// Uncompressed
DataBuffer ret;
buffer.ReadSome(ret, compressedLength);
return ret;
}
assert(buffer.GetReadOffset() + compressedLength <= buffer.GetSize());
std::string deflatedData;
buffer.ReadSome(deflatedData, compressedLength);
std::string inflated;
inflated.resize(uncompressedLength);
inflate(deflatedData, inflated);
assert(inflated.length() == uncompressedLength);
return DataBuffer(inflated);
}
DataBuffer Decompress(DataBuffer& buffer) {
std::uint64_t packetLength;
buffer >> packetLength;
return Decompress(buffer, packetLength);
}
} // namespace utils
} // namespace td

17
src/misc/Random.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include "misc/Random.h"
#include <random>
#include <ctime>
namespace td {
namespace utils {
void initRandomizer(){
srand(time(0));
}
std::uint64_t getRandomNumber(std::size_t max){
return rand() % max;
}
} // namespace utils
} // namespace td

37
src/misc/Time.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "misc/Time.h"
#include <chrono>
namespace td {
namespace utils {
void Timer::update(){
m_InternalTime += getTime() - m_LastTime;
if(m_InternalTime >= m_Interval){
if(m_Function != nullptr)
m_Function();
m_InternalTime %= m_Interval;
}
m_LastTime = getTime();
}
void Timer::update(std::uint64_t delta){
m_InternalTime += delta;
if(m_InternalTime >= m_Interval){
if(m_Function != nullptr)
m_Function();
m_InternalTime %= m_Interval;
}
m_LastTime = getTime();
}
void Timer::reset(){
m_InternalTime = 0;
m_LastTime = getTime();
}
std::uint64_t getTime(){
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count();
}
} // namespace utils
} // namespace td