瀏覽代碼

Fix code style consistency in lobby server project

Ivan Savenko 1 年之前
父節點
當前提交
50c1452221
共有 6 個文件被更改,包括 133 次插入148 次删除
  1. 1 1
      lobby/LobbyDatabase.cpp
  2. 1 1
      lobby/LobbyDatabase.h
  3. 10 11
      lobby/LobbyServer.cpp
  4. 1 0
      lobby/LobbyServer.h
  5. 79 92
      lobby/SQLiteConnection.cpp
  6. 41 43
      lobby/SQLiteConnection.h

+ 1 - 1
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();

+ 1 - 1
lobby/LobbyDatabase.h

@@ -77,7 +77,7 @@ public:
 	std::vector<GameRoom> getActiveGameRooms();
 	std::vector<ChatMessage> 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);
 };

+ 10 - 11
lobby/LobbyServer.cpp

@@ -21,7 +21,7 @@ void LobbyServer::sendMessage(const std::shared_ptr<NetworkConnection> & target,
 	std::string payloadString = json.toJson(true);
 
 	// FIXME: find better approach
-	uint8_t * payloadBegin = reinterpret_cast<uint8_t*>(payloadString.data());
+	uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
 	uint8_t * payloadEnd = payloadBegin + payloadString.size();
 
 	std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
@@ -35,8 +35,7 @@ void LobbyServer::onTimer()
 }
 
 void LobbyServer::onNewConnection(const std::shared_ptr<NetworkConnection> & connection)
-{
-}
+{}
 
 void LobbyServer::onDisconnected(const std::shared_ptr<NetworkConnection> & connection)
 {
@@ -48,19 +47,19 @@ void LobbyServer::onPacketReceived(const std::shared_ptr<NetworkConnection> & 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<NetworkConnection> & 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<NetworkConnection
 	reply["messageText"].String() = messageText;
 	reply["senderName"].String() = senderName;
 
-	for (auto const & connection : activeAccounts)
+	for(const auto & connection : activeAccounts)
 		sendMessage(connection.first, reply);
 }
 
@@ -102,7 +101,7 @@ void LobbyServer::receiveAuthentication(const std::shared_ptr<NetworkConnection>
 		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<NetworkConnection>
 
 void LobbyServer::receiveJoinGameRoom(const std::shared_ptr<NetworkConnection> & 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

+ 1 - 0
lobby/LobbyServer.h

@@ -41,6 +41,7 @@ class LobbyServer : public INetworkServerListener
 	void receiveSendChatMessage(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json);
 	void receiveAuthentication(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json);
 	void receiveJoinGameRoom(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json);
+
 public:
 	LobbyServer(const std::string & databasePath);
 	~LobbyServer();

+ 79 - 92
lobby/SQLiteConnection.cpp

@@ -12,111 +12,113 @@
 
 #include <sqlite3.h>
 
-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 );
-	}
+	const char * message = sqlite3_errmsg(connection);
+	throw std::runtime_error(std::string("SQLite error: ") + message);
+}
 
-	assert( result == SQLITE_OK );
+static void checkSQLiteError(sqlite3 * connection, int result)
+{
+	if(result != SQLITE_OK)
+		handleSQLiteError(connection);
 }
 
-SQLiteStatement::SQLiteStatement( SQLiteInstance & instance, sqlite3_stmt * statement ):
-	m_instance( instance ),
-	m_statement( statement )
-{ }
+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 );
+SQLiteStatement::~SQLiteStatement()
+{
+	int result = sqlite3_finalize(m_statement);
+	checkSQLiteError(m_instance.m_connection, result);
 }
 
-bool SQLiteStatement::execute( )
-{ 
-	int result = sqlite3_step( m_statement );
+bool SQLiteStatement::execute()
+{
+	int result = sqlite3_step(m_statement);
 
-	switch ( result )
+	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<int>(index), value );
-	on_sqlite_error( m_instance.m_connection, result );
+	int result = sqlite3_bind_double(m_statement, static_cast<int>(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<int>(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<int>(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<int>(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<int>( index ), value );
-	on_sqlite_error( m_instance.m_connection, result );
+	int result = sqlite3_bind_int(m_statement, static_cast<int>(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<int>( index ), value );
-	on_sqlite_error( m_instance.m_connection, result );
+	int result = sqlite3_bind_int64(m_statement, static_cast<int>(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<int>( index ), value.data(), static_cast<int>( value.size() ), SQLITE_STATIC );
-	on_sqlite_error( m_instance.m_connection, result );
+	int result = sqlite3_bind_text(m_statement, static_cast<int>(index), value.data(), static_cast<int>(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<int>( index ), value, -1, SQLITE_STATIC );
-	on_sqlite_error( m_instance.m_connection, result );
+	int result = sqlite3_bind_text(m_statement, static_cast<int>(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<int>( index ) );
+	value = sqlite3_column_double(m_statement, static_cast<int>(index));
 }
 
-void SQLiteStatement::getColumnSingle( size_t index, uint8_t & value )
+void SQLiteStatement::getColumnSingle(size_t index, uint8_t & value)
 {
-	value = static_cast<uint8_t>(sqlite3_column_int( m_statement, static_cast<int>( index ) ));
+	value = static_cast<uint8_t>(sqlite3_column_int(m_statement, static_cast<int>(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<uint16_t>(sqlite3_column_int(m_statement, static_cast<int>(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<int>( index ) );
+	value = sqlite3_column_int(m_statement, static_cast<int>(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<int>(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<int>( index ) );
+	value = sqlite3_column_int64(m_statement, static_cast<int>(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<int>(index));
-	value = reinterpret_cast<char const*>( value_raw );
+	const auto * value_raw = sqlite3_column_text(m_statement, static_cast<int>(index));
+	value = reinterpret_cast<const char *>(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<int>(index));
-	size_t blob_size = sqlite3_column_bytes(m_statement, static_cast<int>(index));
-
-	assert(blob_size < capacity);
-
-	std::copy_n(static_cast<std::byte const*>(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 );
-
-	on_sqlite_error( connection, result );
+	int result = sqlite3_open_v2(db_path.c_str(), &connection, flags, nullptr);
 
-	assert(result == SQLITE_OK);
+	if(result == SQLITE_OK)
+		return SQLiteInstancePtr(new SQLiteInstance(connection));
 
-	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(sqlite3 * connection)
+	: m_connection(connection)
+{
+}
 
-SQLiteInstance::~SQLiteInstance( )
+SQLiteInstance::~SQLiteInstance()
 {
-	int result = sqlite3_close( m_connection );
-	on_sqlite_error( m_connection, result );
+	int result = sqlite3_close(m_connection);
+	checkSQLiteError(m_connection, result);
 }
 
-SQLiteStatementPtr SQLiteInstance::prepare( std::string const & sql_text )
+SQLiteStatementPtr SQLiteInstance::prepare(const std::string & sql_text)
 {
 	sqlite3_stmt * statement;
-	int result = sqlite3_prepare_v2( m_connection, sql_text.data(), static_cast<int>( sql_text.size()), &statement, nullptr );
-	
-	on_sqlite_error( m_connection, result );
-
-	if ( result == SQLITE_OK )
-		return SQLiteStatementPtr( new SQLiteStatement( *this, statement ) );
-	sqlite3_finalize( statement );
-	return SQLiteStatementPtr( );
+	int result = sqlite3_prepare_v2(m_connection, sql_text.data(), static_cast<int>(sql_text.size()), &statement, nullptr);
+
+	if(result == SQLITE_OK)
+		return SQLiteStatementPtr(new SQLiteStatement(*this, statement));
+
+	sqlite3_finalize(statement);
+	handleSQLiteError(m_connection);
 }

+ 41 - 43
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<SQLiteInstance>;
+using SQLiteStatementPtr = std::unique_ptr<SQLiteStatement>;
 
 class SQLiteStatement : boost::noncopyable
 {
 public:
 	friend class SQLiteInstance;
 
-	bool execute( );
-	void reset( );
-	void clear( );
+	bool execute();
+	void reset();
+	void clear();
 
 	~SQLiteStatement();
 
-	template<typename ... Args >
-	void setBinds( Args const & ... args )
+	template<typename... Args>
+	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<typename ... Args >
-	void getColumns( Args & ... args )
+	template<typename... Args>
+	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 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 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);
 
 	template<typename Rep, typename Period>
-	void getColumnSingle( size_t index, std::chrono::duration<Rep, Period> & value )
+	void getColumnSingle(size_t index, std::chrono::duration<Rep, Period> & value)
 	{
 		int64_t durationValue = 0;
 		getColumnSingle(index, durationValue);
 		value = std::chrono::duration<Rep, Period>(durationValue);
 	}
 
-	SQLiteStatement( SQLiteInstance & instance, sqlite3_stmt * statement );
+	SQLiteStatement(SQLiteInstance & instance, sqlite3_stmt * statement);
 
-	template<typename T, typename ... Args >
-	void setBindSingle( size_t index, T const & arg, Args const & ... args )
+	template<typename T, typename... Args>
+	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<typename T, typename ... Args >
-	void getColumnSingle( size_t index, T & arg, Args & ... args )
+	template<typename T, typename... Args>
+	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;
 };