From ceedfe12e2be54fbdbca403a54b288a2fa4bc413 Mon Sep 17 00:00:00 2001 From: malloc Date: Fri, 6 Jul 2018 13:32:22 -0500 Subject: [PATCH] PS 44:15 - My confusion is continually before me, and the shame of my face hath covered me --- server/src/crypto/cipher.hpp | 2 +- server/src/db/database.cpp | 10 ++++++++++ server/src/db/database.hpp | 5 ++++- server/src/hosts/master.hpp | 1 - server/src/hosts/master_intra.cpp | 24 ++++++++++++++++++++---- server/src/hosts/slave.hpp | 2 ++ server/src/sock/pool.hpp | 31 +++++++++++++++++++++++-------- 7 files changed, 60 insertions(+), 15 deletions(-) diff --git a/server/src/crypto/cipher.hpp b/server/src/crypto/cipher.hpp index aa0b8c2..2f208be 100644 --- a/server/src/crypto/cipher.hpp +++ b/server/src/crypto/cipher.hpp @@ -15,7 +15,7 @@ public: private: std::string GenerateStream(uint64_t length); - const int state_size = 256; + static const int state_size = 256; uint8_t state[state_size]; }; }} diff --git a/server/src/db/database.cpp b/server/src/db/database.cpp index 2621563..db6aad0 100644 --- a/server/src/db/database.cpp +++ b/server/src/db/database.cpp @@ -85,6 +85,16 @@ sosc::db::Query::Query() : results(this) { this->open = false; } +sosc::db::Query::Query(const Query &query) : results(this) { + this->open = false; + if(query.open) { + this->SetQuery( + sqlite3_sql(query.statement), + query.database == _ctx.mem_db ? DB_USE_MEMORY : DB_USE_HARD + ); + } +} + sosc::db::Query::Query(const std::string& query, int db) : results(this) { this->open = false; this->SetQuery(query, db); diff --git a/server/src/db/database.hpp b/server/src/db/database.hpp index 000cd94..d2abea2 100644 --- a/server/src/db/database.hpp +++ b/server/src/db/database.hpp @@ -39,7 +39,10 @@ private: class Query { public: Query(); - Query(const std::string& query, int db = DB_USE_HARD); + Query(const Query& query); + + Query& operator= (const Query&) = delete; + explicit Query(const std::string& query, int db = DB_USE_HARD); void SetQuery(const std::string& query, int db = DB_USE_HARD); void BindDouble(double value, int i); diff --git a/server/src/hosts/master.hpp b/server/src/hosts/master.hpp index 7fd2d8f..5bd0b89 100644 --- a/server/src/hosts/master.hpp +++ b/server/src/hosts/master.hpp @@ -20,7 +20,6 @@ public: private: ScapeConnection sock; - cgc::KeyExchange key; cgc::Cipher cipher; }; diff --git a/server/src/hosts/master_intra.cpp b/server/src/hosts/master_intra.cpp index af24a61..c79c1c0 100644 --- a/server/src/hosts/master_intra.cpp +++ b/server/src/hosts/master_intra.cpp @@ -62,11 +62,17 @@ sosc::MasterIntraPool::MasterIntraPool() { "SELECT MAX(`ID`) FROM `SERVER_LIST`" , DB_USE_MEMORY)); -#define QRY_SERVER_LIST_DELETE 9 +#define QRY_SERVER_LIST_MODIFY 9 + this->queries.push_back(new db::Query( + "UPDATE `SERVER_LIST` SET " + "`USERS` = ?, `MAX_USERS` = ? " + "WHERE ID = ?" + , DB_USE_MEMORY)); + +#define QRY_SERVER_LIST_DELETE 10 this->queries.push_back(new db::Query( "DELETE FROM `SERVER_LIST` WHERE `ID` = ?" , DB_USE_MEMORY)); - } void sosc::MasterIntraPool::Stop() { @@ -199,11 +205,21 @@ bool sosc::MasterIntra::StatusUpdate(sosc::Packet &pck) { if(!this->authed) return this->NotAuthorized(packetId); + db::Query* query = this->queries->at(QRY_LICENSE_VERIFY); + query->Reset(); + query->BindText(this->license, 0); + if(query->ScalarInt32() == 0) + return this->NotAuthorized(packetId); + if(!pck.Check(2, 2, 2)) return this->Close(); - - + query = this->queries->at(QRY_SERVER_LIST_MODIFY); + query->Reset(); + query->BindInt32(net::ntohv(pck[0]), 0); + query->BindInt32(net::ntohv(pck[1]), 1); + query->BindInt32(this->server_id, 2); + query->NonQuery(); return true; } diff --git a/server/src/hosts/slave.hpp b/server/src/hosts/slave.hpp index 9ec5ee2..0220a40 100644 --- a/server/src/hosts/slave.hpp +++ b/server/src/hosts/slave.hpp @@ -5,6 +5,8 @@ #include "../sock/pool.hpp" namespace sosc { +/** SLAVE -> CLIENT **/ + class SlaveClient { public: SlaveClient(const ScapeConnection& client); diff --git a/server/src/sock/pool.hpp b/server/src/sock/pool.hpp index fa9aaf8..f71d5e7 100644 --- a/server/src/sock/pool.hpp +++ b/server/src/sock/pool.hpp @@ -5,6 +5,7 @@ #include #include #include +#include "../db/database.hpp" namespace sosc { typedef struct { @@ -42,7 +43,8 @@ public: virtual void Stop(); protected: - virtual bool ProcessClient(T& client) = 0; + virtual void SetupQueries(std::vector* queries) {}; + virtual bool ProcessClient(T& client, std::vector* queries) = 0; private: bool IsStackFull(int stackCount) const; bool CanAddStack() const; @@ -63,7 +65,8 @@ private: void Stop(); private: void StackThread(); - + + std::vector queries; std::thread* thread; Pool* pool; bool is_open; @@ -75,7 +78,8 @@ private: poolinfo_t info; bool is_open; - + + std::vector queries; std::vector stacks; friend class Stack; @@ -96,7 +100,8 @@ template void Pool::Start() { if(this->is_open) return; - + + this->SetupQueries(&this->queries); for(int i = 0; i < this->info.initial_count; ++i) { this->stacks.push_back(new Stack(this)); this->stacks.back()->Start(); @@ -172,7 +177,10 @@ void Pool::Stop() { stack->Stop(); delete stack; } - + + for(auto& query : this->queries) + query.Close(); + this->stacks.clear(); this->is_open = false; } @@ -188,7 +196,10 @@ template void Pool::Stack::Start() { if(this->is_open || this->is_running) return; - + + for(auto& query : this->pool->queries) + this->queries.push_back(db::Query(query)); + this->is_open = true; this->is_running = true; this->thread = new std::thread([&]() { @@ -220,6 +231,7 @@ int Pool::Stack::ClientCount() { template void Pool::Stack::StackThread() { + while(this->is_running) { for(auto client = this->clients.begin(); client != this->clients.end(); @@ -229,7 +241,7 @@ void Pool::Stack::StackThread() { break; this->clients_mtx.lock(); - if(!this->pool->ProcessClient(*client)) + if(!this->pool->ProcessClient(*client, &this->queries)) this->clients.erase(client); this->clients_mtx.unlock(); } @@ -240,7 +252,10 @@ template void Pool::Stack::Stop() { if(!this->is_open || !this->is_running) return; - + + for(auto& query : this->queries) + query.Close(); + this->is_running = false; this->thread->join();