From 50c14522213aa72470f60754be4f6ed91fea4466 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Fri, 29 Dec 2023 13:19:04 +0200 Subject: [PATCH] Fix code style consistency in lobby server project --- lobby/LobbyDatabase.cpp | 2 +- lobby/LobbyDatabase.h | 2 +- lobby/LobbyServer.cpp | 21 +++-- lobby/LobbyServer.h | 1 + lobby/SQLiteConnection.cpp | 175 +++++++++++++++++-------------------- lobby/SQLiteConnection.h | 82 +++++++++-------- 6 files changed, 134 insertions(+), 149 deletions(-) diff --git a/lobby/LobbyDatabase.cpp b/lobby/LobbyDatabase.cpp index 59712b4dd..673f33048 100644 --- a/lobby/LobbyDatabase.cpp +++ b/lobby/LobbyDatabase.cpp @@ -56,7 +56,7 @@ LobbyDatabase::LobbyDatabase(const std::string & databasePath) { database = SQLiteInstance::open(databasePath, true); - if (!database) + if(!database) throw std::runtime_error("Failed to open SQLite database!"); initializeDatabase(); diff --git a/lobby/LobbyDatabase.h b/lobby/LobbyDatabase.h index bd1ccc52c..fccea96f2 100644 --- a/lobby/LobbyDatabase.h +++ b/lobby/LobbyDatabase.h @@ -77,7 +77,7 @@ public: std::vector getActiveGameRooms(); std::vector getRecentMessageHistory(); - bool checkAccessCookie(const std::string & accountName,const std::string & accessCookieUUID); + bool checkAccessCookie(const std::string & accountName, const std::string & accessCookieUUID); bool isPlayerInGameRoom(const std::string & accountName); bool isAccountNameAvailable(const std::string & accountName); }; diff --git a/lobby/LobbyServer.cpp b/lobby/LobbyServer.cpp index 543ba7fe5..35c64aa39 100644 --- a/lobby/LobbyServer.cpp +++ b/lobby/LobbyServer.cpp @@ -21,7 +21,7 @@ void LobbyServer::sendMessage(const std::shared_ptr & target, std::string payloadString = json.toJson(true); // FIXME: find better approach - uint8_t * payloadBegin = reinterpret_cast(payloadString.data()); + uint8_t * payloadBegin = reinterpret_cast(payloadString.data()); uint8_t * payloadEnd = payloadBegin + payloadString.size(); std::vector payloadBuffer(payloadBegin, payloadEnd); @@ -35,8 +35,7 @@ void LobbyServer::onTimer() } void LobbyServer::onNewConnection(const std::shared_ptr & connection) -{ -} +{} void LobbyServer::onDisconnected(const std::shared_ptr & connection) { @@ -48,19 +47,19 @@ void LobbyServer::onPacketReceived(const std::shared_ptr & co // FIXME: find better approach JsonNode json(message.data(), message.size()); - if (json["type"].String() == "sendChatMessage") + if(json["type"].String() == "sendChatMessage") return receiveSendChatMessage(connection, json); - if (json["type"].String() == "authentication") + if(json["type"].String() == "authentication") return receiveAuthentication(connection, json); - if (json["type"].String() == "joinGameRoom") + if(json["type"].String() == "joinGameRoom") return receiveJoinGameRoom(connection, json); } void LobbyServer::receiveSendChatMessage(const std::shared_ptr & connection, const JsonNode & json) { - if (activeAccounts.count(connection) == 0) + if(activeAccounts.count(connection) == 0) return; // unauthenticated std::string senderName = activeAccounts[connection].accountName; @@ -73,7 +72,7 @@ void LobbyServer::receiveSendChatMessage(const std::shared_ptr JsonNode reply; reply["type"].String() = "chatHistory"; - for (auto const & message : boost::adaptors::reverse(history)) + for(const auto & message : boost::adaptors::reverse(history)) { JsonNode jsonEntry; @@ -119,12 +118,12 @@ void LobbyServer::receiveAuthentication(const std::shared_ptr void LobbyServer::receiveJoinGameRoom(const std::shared_ptr & connection, const JsonNode & json) { - if (activeAccounts.count(connection) == 0) + if(activeAccounts.count(connection) == 0) return; // unauthenticated std::string senderName = activeAccounts[connection].accountName; - if (database->isPlayerInGameRoom(senderName)) + if(database->isPlayerInGameRoom(senderName)) return; // only 1 room per player allowed // TODO: roomType: private, public diff --git a/lobby/LobbyServer.h b/lobby/LobbyServer.h index bd4081b74..c9f278349 100644 --- a/lobby/LobbyServer.h +++ b/lobby/LobbyServer.h @@ -41,6 +41,7 @@ class LobbyServer : public INetworkServerListener void receiveSendChatMessage(const std::shared_ptr & connection, const JsonNode & json); void receiveAuthentication(const std::shared_ptr & connection, const JsonNode & json); void receiveJoinGameRoom(const std::shared_ptr & connection, const JsonNode & json); + public: LobbyServer(const std::string & databasePath); ~LobbyServer(); diff --git a/lobby/SQLiteConnection.cpp b/lobby/SQLiteConnection.cpp index 16cffb950..40fd62d86 100644 --- a/lobby/SQLiteConnection.cpp +++ b/lobby/SQLiteConnection.cpp @@ -12,111 +12,113 @@ #include -static void on_sqlite_error( sqlite3 * connection, [[maybe_unused]] int result ) +[[noreturn]] static void handleSQLiteError(sqlite3 * connection) { - if ( result != SQLITE_OK ) - { - const char * message = sqlite3_errmsg( connection ); - printf( "sqlite error: %s\n", message ); - } - - assert( result == SQLITE_OK ); + const char * message = sqlite3_errmsg(connection); + throw std::runtime_error(std::string("SQLite error: ") + message); } -SQLiteStatement::SQLiteStatement( SQLiteInstance & instance, sqlite3_stmt * statement ): - m_instance( instance ), - m_statement( statement ) -{ } - -SQLiteStatement::~SQLiteStatement( ) -{ - int result = sqlite3_finalize( m_statement ); - on_sqlite_error( m_instance.m_connection, result ); +static void checkSQLiteError(sqlite3 * connection, int result) +{ + if(result != SQLITE_OK) + handleSQLiteError(connection); } -bool SQLiteStatement::execute( ) -{ - int result = sqlite3_step( m_statement ); +SQLiteStatement::SQLiteStatement(SQLiteInstance & instance, sqlite3_stmt * statement) + : m_instance(instance) + , m_statement(statement) +{ +} - switch ( result ) +SQLiteStatement::~SQLiteStatement() +{ + int result = sqlite3_finalize(m_statement); + checkSQLiteError(m_instance.m_connection, result); +} + +bool SQLiteStatement::execute() +{ + int result = sqlite3_step(m_statement); + + switch(result) { case SQLITE_DONE: return false; case SQLITE_ROW: return true; default: - on_sqlite_error( m_instance.m_connection, result ); + checkSQLiteError(m_instance.m_connection, result); return false; } } -void SQLiteStatement::reset( ) +void SQLiteStatement::reset() { - int result = sqlite3_reset( m_statement ); - on_sqlite_error( m_instance.m_connection, result ); + int result = sqlite3_reset(m_statement); + checkSQLiteError(m_instance.m_connection, result); } void SQLiteStatement::clear() { int result = sqlite3_clear_bindings(m_statement); - on_sqlite_error(m_instance.m_connection, result); + checkSQLiteError(m_instance.m_connection, result); } -void SQLiteStatement::setBindSingle( size_t index, double const & value ) +void SQLiteStatement::setBindSingle(size_t index, const double & value) { - int result = sqlite3_bind_double( m_statement, static_cast(index), value ); - on_sqlite_error( m_instance.m_connection, result ); + int result = sqlite3_bind_double(m_statement, static_cast(index), value); + checkSQLiteError(m_instance.m_connection, result); } -void SQLiteStatement::setBindSingle(size_t index, uint8_t const & value) +void SQLiteStatement::setBindSingle(size_t index, const uint8_t & value) { int result = sqlite3_bind_int(m_statement, static_cast(index), value); - on_sqlite_error(m_instance.m_connection, result); + checkSQLiteError(m_instance.m_connection, result); } -void SQLiteStatement::setBindSingle(size_t index, uint16_t const & value) +void SQLiteStatement::setBindSingle(size_t index, const uint16_t & value) { int result = sqlite3_bind_int(m_statement, static_cast(index), value); - on_sqlite_error(m_instance.m_connection, result); + checkSQLiteError(m_instance.m_connection, result); } -void SQLiteStatement::setBindSingle(size_t index, uint32_t const & value) +void SQLiteStatement::setBindSingle(size_t index, const uint32_t & value) { int result = sqlite3_bind_int(m_statement, static_cast(index), value); - on_sqlite_error(m_instance.m_connection, result); + checkSQLiteError(m_instance.m_connection, result); } -void SQLiteStatement::setBindSingle( size_t index, int32_t const & value ) +void SQLiteStatement::setBindSingle(size_t index, const int32_t & value) { - int result = sqlite3_bind_int( m_statement, static_cast( index ), value ); - on_sqlite_error( m_instance.m_connection, result ); + int result = sqlite3_bind_int(m_statement, static_cast(index), value); + checkSQLiteError(m_instance.m_connection, result); } -void SQLiteStatement::setBindSingle( size_t index, int64_t const & value ) +void SQLiteStatement::setBindSingle(size_t index, const int64_t & value) { - int result = sqlite3_bind_int64( m_statement, static_cast( index ), value ); - on_sqlite_error( m_instance.m_connection, result ); + int result = sqlite3_bind_int64(m_statement, static_cast(index), value); + checkSQLiteError(m_instance.m_connection, result); } -void SQLiteStatement::setBindSingle( size_t index, std::string const & value ) +void SQLiteStatement::setBindSingle(size_t index, const std::string & value) { - int result = sqlite3_bind_text( m_statement, static_cast( index ), value.data(), static_cast( value.size() ), SQLITE_STATIC ); - on_sqlite_error( m_instance.m_connection, result ); + int result = sqlite3_bind_text(m_statement, static_cast(index), value.data(), static_cast(value.size()), SQLITE_STATIC); + checkSQLiteError(m_instance.m_connection, result); } -void SQLiteStatement::setBindSingle( size_t index, char const * value ) +void SQLiteStatement::setBindSingle(size_t index, const char * value) { - int result = sqlite3_bind_text( m_statement, static_cast( index ), value, -1, SQLITE_STATIC ); - on_sqlite_error( m_instance.m_connection, result ); + int result = sqlite3_bind_text(m_statement, static_cast(index), value, -1, SQLITE_STATIC); + checkSQLiteError(m_instance.m_connection, result); } -void SQLiteStatement::getColumnSingle( size_t index, double & value ) +void SQLiteStatement::getColumnSingle(size_t index, double & value) { - value = sqlite3_column_double( m_statement, static_cast( index ) ); + value = sqlite3_column_double(m_statement, static_cast(index)); } -void SQLiteStatement::getColumnSingle( size_t index, uint8_t & value ) +void SQLiteStatement::getColumnSingle(size_t index, uint8_t & value) { - value = static_cast(sqlite3_column_int( m_statement, static_cast( index ) )); + value = static_cast(sqlite3_column_int(m_statement, static_cast(index))); } void SQLiteStatement::getColumnSingle(size_t index, uint16_t & value) @@ -124,9 +126,9 @@ void SQLiteStatement::getColumnSingle(size_t index, uint16_t & value) value = static_cast(sqlite3_column_int(m_statement, static_cast(index))); } -void SQLiteStatement::getColumnSingle( size_t index, int32_t & value ) +void SQLiteStatement::getColumnSingle(size_t index, int32_t & value) { - value = sqlite3_column_int( m_statement, static_cast( index ) ); + value = sqlite3_column_int(m_statement, static_cast(index)); } void SQLiteStatement::getColumnSingle(size_t index, uint32_t & value) @@ -134,65 +136,50 @@ void SQLiteStatement::getColumnSingle(size_t index, uint32_t & value) value = sqlite3_column_int(m_statement, static_cast(index)); } -void SQLiteStatement::getColumnSingle( size_t index, int64_t & value ) +void SQLiteStatement::getColumnSingle(size_t index, int64_t & value) { - value = sqlite3_column_int64( m_statement, static_cast( index ) ); + value = sqlite3_column_int64(m_statement, static_cast(index)); } -void SQLiteStatement::getColumnSingle( size_t index, std::string & value ) +void SQLiteStatement::getColumnSingle(size_t index, std::string & value) { - auto value_raw = sqlite3_column_text(m_statement, static_cast(index)); - value = reinterpret_cast( value_raw ); + const auto * value_raw = sqlite3_column_text(m_statement, static_cast(index)); + value = reinterpret_cast(value_raw); } -void SQLiteStatement::getColumnBlob(size_t index, std::byte * value, [[maybe_unused]] size_t capacity) -{ - auto * blob_data = sqlite3_column_blob(m_statement, static_cast(index)); - size_t blob_size = sqlite3_column_bytes(m_statement, static_cast(index)); - - assert(blob_size < capacity); - - std::copy_n(static_cast(blob_data), blob_size, value); - value[blob_size] = std::byte(0); -} - -SQLiteInstancePtr SQLiteInstance::open( std::string const & db_path, bool allow_write) +SQLiteInstancePtr SQLiteInstance::open(const std::string & db_path, bool allow_write) { int flags = allow_write ? (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) : SQLITE_OPEN_READONLY; sqlite3 * connection; - int result = sqlite3_open_v2( db_path.c_str( ), &connection, flags, nullptr ); + int result = sqlite3_open_v2(db_path.c_str(), &connection, flags, nullptr); - on_sqlite_error( connection, result ); + if(result == SQLITE_OK) + return SQLiteInstancePtr(new SQLiteInstance(connection)); - assert(result == SQLITE_OK); - - if ( result == SQLITE_OK ) - return SQLiteInstancePtr( new SQLiteInstance( connection ) ); - - sqlite3_close( connection ); - return SQLiteInstancePtr( ); + sqlite3_close(connection); + handleSQLiteError(connection); } -SQLiteInstance::SQLiteInstance( sqlite3 * connection ): - m_connection( connection ) -{ } - -SQLiteInstance::~SQLiteInstance( ) +SQLiteInstance::SQLiteInstance(sqlite3 * connection) + : m_connection(connection) { - int result = sqlite3_close( m_connection ); - on_sqlite_error( m_connection, result ); } -SQLiteStatementPtr SQLiteInstance::prepare( std::string const & sql_text ) +SQLiteInstance::~SQLiteInstance() +{ + int result = sqlite3_close(m_connection); + checkSQLiteError(m_connection, result); +} + +SQLiteStatementPtr SQLiteInstance::prepare(const std::string & sql_text) { sqlite3_stmt * statement; - int result = sqlite3_prepare_v2( m_connection, sql_text.data(), static_cast( sql_text.size()), &statement, nullptr ); - - on_sqlite_error( m_connection, result ); + int result = sqlite3_prepare_v2(m_connection, sql_text.data(), static_cast(sql_text.size()), &statement, nullptr); - if ( result == SQLITE_OK ) - return SQLiteStatementPtr( new SQLiteStatement( *this, statement ) ); - sqlite3_finalize( statement ); - return SQLiteStatementPtr( ); + if(result == SQLITE_OK) + return SQLiteStatementPtr(new SQLiteStatement(*this, statement)); + + sqlite3_finalize(statement); + handleSQLiteError(m_connection); } diff --git a/lobby/SQLiteConnection.h b/lobby/SQLiteConnection.h index 5eca8bf70..d80c426b7 100644 --- a/lobby/SQLiteConnection.h +++ b/lobby/SQLiteConnection.h @@ -15,76 +15,74 @@ typedef struct sqlite3_stmt sqlite3_stmt; class SQLiteInstance; class SQLiteStatement; -using SQLiteInstancePtr = std::unique_ptr< SQLiteInstance >; -using SQLiteStatementPtr = std::unique_ptr< SQLiteStatement >; +using SQLiteInstancePtr = std::unique_ptr; +using SQLiteStatementPtr = std::unique_ptr; class SQLiteStatement : boost::noncopyable { public: friend class SQLiteInstance; - bool execute( ); - void reset( ); - void clear( ); + bool execute(); + void reset(); + void clear(); ~SQLiteStatement(); - template - void setBinds( Args const & ... args ) + template + void setBinds(const Args &... args) { - setBindSingle( 1, args... ); // The leftmost SQL parameter has an index of 1 + setBindSingle(1, args...); // The leftmost SQL parameter has an index of 1 } - template - void getColumns( Args & ... args ) + template + void getColumns(Args &... args) { - getColumnSingle( 0, args... ); // The leftmost column of the result set has the index 0 + getColumnSingle(0, args...); // The leftmost column of the result set has the index 0 } private: - void setBindSingle( size_t index, double const & value ); - void setBindSingle( size_t index, uint8_t const & value ); - void setBindSingle( size_t index, uint16_t const & value ); - void setBindSingle( size_t index, uint32_t const & value ); - void setBindSingle( size_t index, int32_t const & value ); - void setBindSingle( size_t index, int64_t const & value ); - void setBindSingle( size_t index, std::string const & value ); - void setBindSingle( size_t index, char const * value ); + void setBindSingle(size_t index, const double & value); + void setBindSingle(size_t index, const uint8_t & value); + void setBindSingle(size_t index, const uint16_t & value); + void setBindSingle(size_t index, const uint32_t & value); + void setBindSingle(size_t index, const int32_t & value); + void setBindSingle(size_t index, const int64_t & value); + void setBindSingle(size_t index, const std::string & value); + void setBindSingle(size_t index, const char * value); - void getColumnSingle( size_t index, double & value ); - void getColumnSingle( size_t index, uint8_t & value ); - void getColumnSingle( size_t index, uint16_t & value ); - void getColumnSingle( size_t index, uint32_t & value ); - void getColumnSingle( size_t index, int32_t & value ); - void getColumnSingle( size_t index, int64_t & value ); - void getColumnSingle( size_t index, std::string & value ); + void getColumnSingle(size_t index, double & value); + void getColumnSingle(size_t index, uint8_t & value); + void getColumnSingle(size_t index, uint16_t & value); + void getColumnSingle(size_t index, uint32_t & value); + void getColumnSingle(size_t index, int32_t & value); + void getColumnSingle(size_t index, int64_t & value); + void getColumnSingle(size_t index, std::string & value); template - void getColumnSingle( size_t index, std::chrono::duration & value ) + void getColumnSingle(size_t index, std::chrono::duration & value) { int64_t durationValue = 0; getColumnSingle(index, durationValue); value = std::chrono::duration(durationValue); } - SQLiteStatement( SQLiteInstance & instance, sqlite3_stmt * statement ); + SQLiteStatement(SQLiteInstance & instance, sqlite3_stmt * statement); - template - void setBindSingle( size_t index, T const & arg, Args const & ... args ) + template + void setBindSingle(size_t index, T const & arg, const Args &... args) { - setBindSingle( index, arg ); - setBindSingle( index + 1, args... ); + setBindSingle(index, arg); + setBindSingle(index + 1, args...); } - template - void getColumnSingle( size_t index, T & arg, Args & ... args ) + template + void getColumnSingle(size_t index, T & arg, Args &... args) { - getColumnSingle( index, arg ); - getColumnSingle( index + 1, args... ); + getColumnSingle(index, arg); + getColumnSingle(index + 1, args...); } - void getColumnBlob(size_t index, std::byte * value, size_t capacity); - SQLiteInstance & m_instance; sqlite3_stmt * m_statement; }; @@ -94,14 +92,14 @@ class SQLiteInstance : boost::noncopyable public: friend class SQLiteStatement; - static SQLiteInstancePtr open(std::string const & db_path, bool allow_write ); + static SQLiteInstancePtr open(const std::string & db_path, bool allow_write); - ~SQLiteInstance( ); + ~SQLiteInstance(); - SQLiteStatementPtr prepare( std::string const & statement ); + SQLiteStatementPtr prepare(const std::string & statement); private: - SQLiteInstance( sqlite3 * connection ); + SQLiteInstance(sqlite3 * connection); sqlite3 * m_connection; };