diff --git a/server/src/main.cpp b/server/src/main.cpp index 4d41a44..73dcea5 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -18,7 +18,7 @@ class User; class Test : sosc::Pool { protected: - void ProcessClient(User* client) override; + bool ProcessClient(User* client) override; }; int main(int argc, char **argv) { diff --git a/server/src/sock/pool.hpp b/server/src/sock/pool.hpp index 1f9d805..04ca9d9 100644 --- a/server/src/sock/pool.hpp +++ b/server/src/sock/pool.hpp @@ -42,6 +42,9 @@ public: protected: virtual bool ProcessClient(T* client) = 0; private: + bool IsStackFull(int stackCount) const; + bool CanAddStack() const; + class Stack { public: Stack(Pool* pool); @@ -93,11 +96,51 @@ void Pool::Start() { this->is_running = true; } +template +bool Pool::IsStackFull(int stackCount) const { + poolinfo_t *info = &this->info; + return info->max_size != -1 + && stackCount < + info->initial_size + + (info->size_growth + * (this->stacks.size() - info->initial_count)) + && stackCount < info->max_size; +} + +template +bool Pool::CanAddStack() const { + return this->info.max_count == -1 + || this->stacks.size() < this->info.max_count; +} + template bool Pool::AddClient(T* client) { - // TODO + if(!this->is_running) + return; - return false; + if(this->info.max_total != -1) + if(this->ClientCount() >= this->info.max_total) + return false; + + int lowestCount = -1; + Stack* lowestStack = nullptr; + for(auto i = this->stacks.begin(); i != this->stacks.end(); ++i) { + int thisCount; + if((thisCount = i->ClientCount()) > lowestCount) { + lowestCount = thisCount; + lowestStack = &(*i); + } + } + + if(lowestStack != nullptr && !this->IsStackFull(lowestCount)) + lowestStack->AddClient(client); + else if(this->CanAddStack()) { + this->stacks.push_back(Stack(this)); + this->stacks.back().AddClient(client); + } else + return false; + + return true; } template