22 #include "MAPacketCache.hpp" 24 #include <MCBinaryData.hpp> 27 MAPacketCache::MAPacketCache(
unsigned int storage_size_limit) : CurrentStorageSize(0),
28 StorageSizeLimit(storage_size_limit < 100000 ? 100000 : storage_size_limit)
33 MAPacketCache::~MAPacketCache()
41 const auto Iter = Packets.find(index);
43 if (Iter != Packets.end())
45 CurrentStorageSize -= Iter->second->GetSize();
47 Iter->second = &packet;
49 Packets[
index] = &packet;
51 CurrentStorageSize += packet.
GetSize();
55 while (CurrentStorageSize > StorageSizeLimit)
57 auto Iter2 = Packets.begin();
59 CurrentStorageSize -= Iter2->second->GetSize();
69 if (index < Packets.begin()->first || index > Packets.rbegin()->first)
71 MC_WARNING(
"Packet index out of range (%d -> min. %d max. %d)", index, Packets.begin()->first,
72 Packets.rbegin()->first);
75 return Packets[
index];
79 int MAPacketCache::GetCurrentStorageSize()
const 81 return CurrentStorageSize;
85 bool MAPacketCache::UploadPacket(
int index)
94 return Upload(index, *Packet);
98 void MAPacketCache::Release()
100 for (
auto& packet : Packets)
102 delete packet.second;
105 CurrentStorageSize = 0;
#define MC_WARNING(...)
Warning macro.
Indexer< T > index(T &t)
Iterator magic for for loops to get the index while iterating through.
int GetSize() const
Get binary data size.