Bladeren bron

Simplified code

Ivan Savenko 1 jaar geleden
bovenliggende
commit
c12558bf8a
5 gewijzigde bestanden met toevoegingen van 30 en 37 verwijderingen
  1. 9 1
      client/CServerHandler.cpp
  2. 0 1
      lobby/LobbyDefines.h
  3. 14 17
      lobby/LobbyServer.cpp
  4. 6 17
      lobby/LobbyServer.h
  5. 1 1
      lobby/SQLiteConnection.h

+ 9 - 1
client/CServerHandler.cpp

@@ -129,7 +129,15 @@ public:
 CServerHandler::~CServerHandler()
 {
 	networkHandler->stop();
-	threadNetwork->join();
+	try
+	{
+		threadNetwork->join();
+	}
+	catch (const std::runtime_error & e)
+	{
+		logGlobal->error("Failed to shut down network thread! Reason: %s", e.what());
+		assert(false);
+	}
 }
 
 CServerHandler::CServerHandler()

+ 0 - 1
lobby/LobbyDefines.h

@@ -13,7 +13,6 @@ struct LobbyAccount
 {
 	std::string accountID;
 	std::string displayName;
-	//std::string status;
 };
 
 struct LobbyGameRoom

+ 14 - 17
lobby/LobbyServer.cpp

@@ -43,7 +43,7 @@ std::string LobbyServer::sanitizeChatMessage(const std::string & inputString) co
 NetworkConnectionPtr LobbyServer::findAccount(const std::string & accountID) const
 {
 	for(const auto & account : activeAccounts)
-		if(account.second.accountID == accountID)
+		if(account.second == accountID)
 			return account.first;
 
 	return nullptr;
@@ -52,7 +52,7 @@ NetworkConnectionPtr LobbyServer::findAccount(const std::string & accountID) con
 NetworkConnectionPtr LobbyServer::findGameRoom(const std::string & gameRoomID) const
 {
 	for(const auto & account : activeGameRooms)
-		if(account.second.roomID == gameRoomID)
+		if(account.second == gameRoomID)
 			return account.first;
 
 	return nullptr;
@@ -215,10 +215,10 @@ void LobbyServer::onNewConnection(const NetworkConnectionPtr & connection)
 void LobbyServer::onDisconnected(const NetworkConnectionPtr & connection)
 {
 	if (activeAccounts.count(connection))
-		database->setAccountOnline(activeAccounts.at(connection).accountID, false);
+		database->setAccountOnline(activeAccounts.at(connection), false);
 
 	if (activeGameRooms.count(connection))
-		database->setGameRoomStatus(activeGameRooms.at(connection).roomID, LobbyRoomState::CLOSED);
+		database->setGameRoomStatus(activeGameRooms.at(connection), LobbyRoomState::CLOSED);
 
 	// NOTE: lost connection can be in only one of these lists (or in none of them)
 	// calling on all possible containers since calling std::map::erase() with non-existing key is legal
@@ -252,7 +252,7 @@ void LobbyServer::onPacketReceived(const NetworkConnectionPtr & connection, cons
 	// communication messages from vcmiclient
 	if(activeAccounts.count(connection))
 	{
-		std::string accountName = activeAccounts.at(connection).accountID;
+		std::string accountName = activeAccounts.at(connection);
 		logGlobal->info("%s: Received message of type %s", accountName, messageType);
 
 		if(messageType == "sendChatMessage")
@@ -277,7 +277,7 @@ void LobbyServer::onPacketReceived(const NetworkConnectionPtr & connection, cons
 	// communication messages from vcmiserver
 	if(activeGameRooms.count(connection))
 	{
-		std::string roomName = activeGameRooms.at(connection).roomID;
+		std::string roomName = activeGameRooms.at(connection);
 		logGlobal->info("%s: Received message of type %s", roomName, messageType);
 
 		if(messageType == "leaveGameRoom")
@@ -313,7 +313,7 @@ void LobbyServer::onPacketReceived(const NetworkConnectionPtr & connection, cons
 
 void LobbyServer::receiveSendChatMessage(const NetworkConnectionPtr & connection, const JsonNode & json)
 {
-	std::string accountID = activeAccounts[connection].accountID;
+	std::string accountID = activeAccounts[connection];
 	std::string messageText = json["messageText"].String();
 	std::string messageTextClean = sanitizeChatMessage(messageText);
 	std::string displayName = database->getAccountDisplayName(accountID);
@@ -369,10 +369,7 @@ void LobbyServer::receiveClientLogin(const NetworkConnectionPtr & connection, co
 
 	std::string displayName = database->getAccountDisplayName(accountID);
 
-	activeAccounts[connection].accountID = accountID;
-	activeAccounts[connection].displayName = displayName;
-	activeAccounts[connection].version = version;
-	activeAccounts[connection].language = language;
+	activeAccounts[connection] = accountID;
 
 	sendLoginSuccess(connection, accountCookie, displayName);
 	sendChatHistory(connection, database->getRecentMessageHistory());
@@ -399,7 +396,7 @@ void LobbyServer::receiveServerLogin(const NetworkConnectionPtr & connection, co
 	else
 	{
 		database->insertGameRoom(gameRoomID, accountID);
-		activeGameRooms[connection].roomID = gameRoomID;
+		activeGameRooms[connection] = gameRoomID;
 		sendLoginSuccess(connection, accountCookie, {});
 		broadcastActiveGameRooms();
 	}
@@ -474,7 +471,7 @@ void LobbyServer::receiveServerProxyLogin(const NetworkConnectionPtr & connectio
 void LobbyServer::receiveOpenGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json)
 {
 	std::string hostAccountID = json["hostAccountID"].String();
-	std::string accountID = activeAccounts[connection].accountID;
+	std::string accountID = activeAccounts[connection];
 
 	if(database->isPlayerInGameRoom(accountID))
 		return sendOperationFailed(connection, "Player already in the room!");
@@ -503,7 +500,7 @@ void LobbyServer::receiveOpenGameRoom(const NetworkConnectionPtr & connection, c
 void LobbyServer::receiveJoinGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json)
 {
 	std::string gameRoomID = json["gameRoomID"].String();
-	std::string accountID = activeAccounts[connection].accountID;
+	std::string accountID = activeAccounts[connection];
 
 	if(database->isPlayerInGameRoom(accountID))
 		return sendOperationFailed(connection, "Player already in the room!");
@@ -537,7 +534,7 @@ void LobbyServer::receiveJoinGameRoom(const NetworkConnectionPtr & connection, c
 void LobbyServer::receiveLeaveGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json)
 {
 	std::string gameRoomID = json["gameRoomID"].String();
-	std::string senderName = activeAccounts[connection].accountID;
+	std::string senderName = activeAccounts[connection];
 
 	if(!database->isPlayerInGameRoom(senderName, gameRoomID))
 		return sendOperationFailed(connection, "You are not in the room!");
@@ -547,7 +544,7 @@ void LobbyServer::receiveLeaveGameRoom(const NetworkConnectionPtr & connection,
 
 void LobbyServer::receiveSendInvite(const NetworkConnectionPtr & connection, const JsonNode & json)
 {
-	std::string senderName = activeAccounts[connection].accountID;
+	std::string senderName = activeAccounts[connection];
 	std::string accountID = json["accountID"].String();
 	std::string gameRoomID = database->getAccountGameRoom(senderName);
 
@@ -571,7 +568,7 @@ void LobbyServer::receiveSendInvite(const NetworkConnectionPtr & connection, con
 
 void LobbyServer::receiveDeclineInvite(const NetworkConnectionPtr & connection, const JsonNode & json)
 {
-	std::string accountID = activeAccounts[connection].accountID;
+	std::string accountID = activeAccounts[connection];
 	std::string gameRoomID = json["gameRoomID"].String();
 
 	if(database->getAccountInviteStatus(accountID, gameRoomID) != LobbyInviteStatus::INVITED)

+ 6 - 17
lobby/LobbyServer.h

@@ -18,38 +18,27 @@ VCMI_LIB_NAMESPACE_END
 
 class LobbyDatabase;
 
-class LobbyServer : public INetworkServerListener
+class LobbyServer final : public INetworkServerListener
 {
-	struct AccountState
-	{
-		std::string accountID;
-		std::string displayName;
-		std::string version;
-		std::string language;
-	};
-	struct GameRoomState
-	{
-		std::string roomID;
-	};
 	struct AwaitingProxyState
 	{
 		std::string accountID;
 		std::string roomID;
-		std::weak_ptr<INetworkConnection> accountConnection;
-		std::weak_ptr<INetworkConnection> roomConnection;
+		NetworkConnectionWeakPtr accountConnection;
+		NetworkConnectionWeakPtr roomConnection;
 	};
 
 	/// list of connected proxies. All messages received from (key) will be redirected to (value) connection
-	std::map<NetworkConnectionPtr, std::weak_ptr<INetworkConnection>> activeProxies;
+	std::map<NetworkConnectionPtr, NetworkConnectionWeakPtr> activeProxies;
 
 	/// list of half-established proxies from server that are still waiting for client to connect
 	std::vector<AwaitingProxyState> awaitingProxies;
 
 	/// list of logged in accounts (vcmiclient's)
-	std::map<NetworkConnectionPtr, AccountState> activeAccounts;
+	std::map<NetworkConnectionPtr, std::string> activeAccounts;
 
 	/// list of currently logged in game rooms (vcmiserver's)
-	std::map<NetworkConnectionPtr, GameRoomState> activeGameRooms;
+	std::map<NetworkConnectionPtr, std::string> activeGameRooms;
 
 	std::unique_ptr<LobbyDatabase> database;
 	std::unique_ptr<INetworkHandler> networkHandler;

+ 1 - 1
lobby/SQLiteConnection.h

@@ -109,7 +109,7 @@ public:
 	SQLiteStatementPtr prepare(const std::string & statement);
 
 private:
-	SQLiteInstance(sqlite3 * connection);
+	explicit SQLiteInstance(sqlite3 * connection);
 
 	sqlite3 * m_connection;
 };