diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 21a94c9..8020f86 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 2.6) -project(server) +project(server) file(GLOB_RECURSE server_src "src/*.hpp" diff --git a/server/src/main.cpp b/server/src/main.cpp index 1afc905..5786436 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -13,6 +13,13 @@ #include "crypto/bcrypt.hpp" #include "utils/bigint.hpp" #include "sock/scapesock.hpp" +#include "sock/pool.hpp" + +class User; +class Test : sosc::Pool { +protected: + void ProcessClient(User* client) override; +}; int main(int argc, char **argv) { sosc::ScapeServer server; diff --git a/server/src/sock/pool.cpp b/server/src/sock/pool.cpp deleted file mode 100644 index 423f999..0000000 --- a/server/src/sock/pool.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "pool.hpp" - diff --git a/server/src/sock/pool.hpp b/server/src/sock/pool.hpp index d149bb1..b89a6fc 100644 --- a/server/src/sock/pool.hpp +++ b/server/src/sock/pool.hpp @@ -7,29 +7,85 @@ #include namespace sosc { -template +typedef struct { + // amount of threads to start with (never close) + int initial_count = 3; + // starting limit of clients per thread + int initial_size = 3; + // amount the limit scales when threshold is passed + int size_growth = 1; + + // any values below marked -1 indicates no limit + // max amount of clients in a scaled thread + int max_size = -1; + // maximum number of threads + int max_count = -1; + // maximum number of connections in the pool + int max_total = -1; + // excess deviance from threshold necessary to rebalance + int tolerance = -1; +} poolinfo_t; + +template class Pool { public: + Pool(const poolinfo_t& info); + void Start(); + bool AddClient(T* client); + void Stop(); protected: virtual void ProcessClient(T* client) = 0; private: class Stack { public: + Stack(Pool *pool); + void Start(); + void AddClient(T* client); + int ClientCount() const; + + void Stop(); private: std::thread thread; + Pool *pool; + bool is_open; + std::list clients; std::mutex clients_mtx; - Pool *pool; - - friend class Pool; }; + poolinfo_t info; + bool is_running; std::vector stacks; - friend class Stack; }; + +template +Pool::Pool(const poolinfo_t& info) { + this->info = info; + this->is_running = false; +} + +template +void Pool::Start() { + if(this->is_running == true) + return; + + for(int i = 0; i < this->info.initial_count; ++i) { + this->stacks.push_back(Stack(this)); + this->stacks.back().Start(); + } + + this->is_running = true; +} + +template +bool Pool::AddClient(T* client) { + + + return false; +} } #endif