From 4d12788c536101ec661d45d18578de87fd7fe687 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 6 May 2014 22:46:24 +1000 Subject: [PATCH] Finished the server-side modifications (read more) * There seems to be something iffy with this branch * The size of SerialPacket may have changed I've implemented the accountIndex variable as best I can, but I really shouldn't code at night. I'll need to go over the changes again before merging this. --- client/scenes/in_world.cpp | 4 ++- client/scenes/lobby_menu.cpp | 2 +- server/account_data.hpp | 3 +- server/character_data.hpp | 1 - server/server_application.hpp | 4 +-- server/server_connections.cpp | 64 ++++++++++++----------------------- server/server_database.cpp | 9 ++--- 7 files changed, 35 insertions(+), 52 deletions(-) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 614da3d..1f4538b 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -63,6 +63,7 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet //TODO: add the tilesheet to the map system? tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15); + //TODO: move this into it's own function //request a sync SerialPacket packet; char buffer[PACKET_STRING_SIZE]; @@ -265,7 +266,7 @@ void InWorld::HandlePacket(SerialPacket packet) { break; //handle errors default: - throw(std::runtime_error("Unknown SerialPacket::Type encountered")); + throw(std::runtime_error(std::string() + "Unknown SerialPacket::Type encountered in InWorld: " + to_string_custom(int(packet.meta.type)))); break; } } @@ -326,6 +327,7 @@ void InWorld::HandleCharacterNew(SerialPacket packet) { } void InWorld::HandleCharacterDelete(SerialPacket packet) { + //TODO: authenticate if (playerCharacters.find(packet.characterInfo.characterIndex) == playerCharacters.end()) { throw(std::runtime_error("Cannot delete non-existant characters")); } diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index b1f8178..cd5a50d 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -230,7 +230,7 @@ void LobbyMenu::HandlePacket(SerialPacket packet) { //handle errors default: - throw(std::runtime_error("Unknown SerialPacket::Type encountered")); + throw(std::runtime_error(std::string() + "Unknown SerialPacket::Type encountered in LobbyMenu: " + to_string_custom(int(packet.meta.type)))); break; } } \ No newline at end of file diff --git a/server/account_data.hpp b/server/account_data.hpp index 220d831..f439c61 100644 --- a/server/account_data.hpp +++ b/server/account_data.hpp @@ -26,11 +26,12 @@ struct AccountData { std::string username; - //password + //TODO: password bool blackListed = false; bool whiteListed = true; int clientIndex; + int characterIndex; }; #endif diff --git a/server/character_data.hpp b/server/character_data.hpp index 3a12f36..dc94e7d 100644 --- a/server/character_data.hpp +++ b/server/character_data.hpp @@ -30,7 +30,6 @@ struct CharacterData { //metadata - int clientIndex; std::string handle; std::string avatar; diff --git a/server/server_application.hpp b/server/server_application.hpp index ea58017..d8e8589 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -77,8 +77,8 @@ private: void PumpPacket(SerialPacket); //Account management - int CreateUserAccount(std::string username, int clientIndex); - int LoadUserAccount(std::string username, int clientIndex); + int CreateUserAccount(std::string username, int clientIndex, int characterIndex); + int LoadUserAccount(std::string username, int clientIndex, int characterIndex); int SaveUserAccount(int uid); void UnloadUserAccount(int uid); void DeleteUserAccount(int uid); diff --git a/server/server_connections.cpp b/server/server_connections.cpp index 3d1387b..0d8eead 100644 --- a/server/server_connections.cpp +++ b/server/server_connections.cpp @@ -42,27 +42,28 @@ void ServerApplication::HandleBroadcastRequest(SerialPacket packet) { } void ServerApplication::HandleJoinRequest(SerialPacket packet) { + //load the user account + int accountIndex = LoadUserAccount(packet.clientInfo.username, ClientData::uidCounter, CharacterData::uidCounter); + if (accountIndex < 0) { + //TODO: send rejection packet + std::cerr << "Error: Account already loaded: " << accountIndex << std::endl; + return; + } + //create the new client ClientData newClient; newClient.address = packet.meta.srcAddress; - //load the user account - int uid = LoadUserAccount(packet.clientInfo.username, ClientData::uidCounter); - if (uid < 0) { - std::cerr << "Error: Account already loaded: " << uid << std::endl; - return; - } - //TODO: move this into the character management code //create the new character CharacterData newCharacter; - newCharacter.clientIndex = ClientData::uidCounter; newCharacter.handle = packet.clientInfo.handle; newCharacter.avatar = packet.clientInfo.avatar; //send the client their info packet.meta.type = SerialPacket::Type::JOIN_RESPONSE; packet.clientInfo.clientIndex = ClientData::uidCounter; + packet.clientInfo.accountIndex = accountIndex; packet.clientInfo.characterIndex = CharacterData::uidCounter; //bounce this packet @@ -79,11 +80,10 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) { packet.characterInfo.motion = newCharacter.motion; PumpPacket(packet); + //TODO: don't send anything to a certain client until they send the OK (the sync packet? or ignore client side?) //finished this routine - clientMap[ClientData::uidCounter] = newClient; - characterMap[CharacterData::uidCounter] = newCharacter; - ClientData::uidCounter++; - CharacterData::uidCounter++; + clientMap[ClientData::uidCounter++] = newClient; + characterMap[CharacterData::uidCounter++] = newCharacter; std::cout << "Connect, total: " << clientMap.size() << std::endl; } @@ -113,43 +113,24 @@ void ServerApplication::HandleDisconnect(SerialPacket packet) { //TODO: authenticate who is disconnecting/kicking //TODO: define the difference between unloading and deletng a character - //disconnect the specified client + //forward to the specified client char buffer[PACKET_BUFFER_SIZE]; serialize(&packet, buffer); - network.Send(&clientMap[packet.clientInfo.clientIndex].address, buffer, PACKET_BUFFER_SIZE); - clientMap.erase(packet.clientInfo.clientIndex); + network.Send(&clientMap[accountMap[packet.clientInfo.accountIndex].clientIndex].address, buffer, PACKET_BUFFER_SIZE); - //unload the client's account - //TODO: change clientIndex to accountIndex for player ID - for (auto it : accountMap) { - if (it.second.clientIndex == packet.clientInfo.clientIndex) { - UnloadUserAccount(it.first); - break; - } - } - - //prep the delete packet + //delete the client side character SerialPacket delPacket; delPacket.meta.type = SerialPacket::Type::CHARACTER_DELETE; + delPacket.characterInfo.characterIndex = accountMap[packet.clientInfo.accountIndex].characterIndex; + PumpPacket(delPacket); - //delete server and client side characters - erase_if(characterMap, [&](std::pair it) -> bool { - //find the internal characters to delete - if (it.second.clientIndex == packet.clientInfo.clientIndex) { - //send the delete characters command to all clients - delPacket.characterInfo.characterIndex = it.first; - PumpPacket(delPacket); - - //delete this characters object - return true; - } - - //don't delete this characters object - return false; - }); + //erase the in-memory stuff + clientMap.erase(accountMap[packet.clientInfo.accountIndex].clientIndex); + characterMap.erase(accountMap[packet.clientInfo.accountIndex].characterIndex); + UnloadUserAccount(packet.clientInfo.accountIndex); //finished this routine - std::cout << "Disconnect, total: " << clientMap.size() << std::endl; + std::cout << "Disconnect, total: " << accountMap.size() << std::endl; } void ServerApplication::HandleShutdown(SerialPacket packet) { @@ -160,7 +141,6 @@ void ServerApplication::HandleShutdown(SerialPacket packet) { //disconnect all clients packet.meta.type = SerialPacket::Type::DISCONNECT; - packet.clientInfo.clientIndex = -1; PumpPacket(packet); //finished this routine diff --git a/server/server_database.cpp b/server/server_database.cpp index 4f34947..87fbbdd 100644 --- a/server/server_database.cpp +++ b/server/server_database.cpp @@ -38,7 +38,7 @@ static const char* DELETE_USER_ACCOUNT = "DELETE FROM UserAccounts WHERE uid = ? //Define the methods //------------------------- -int ServerApplication::CreateUserAccount(std::string username, int clientIndex) { +int ServerApplication::CreateUserAccount(std::string username, int clientIndex, int characterIndex) { //create this user account, failing if it exists, leave this account in memory sqlite3_stmt* statement = nullptr; @@ -62,10 +62,10 @@ int ServerApplication::CreateUserAccount(std::string username, int clientIndex) sqlite3_finalize(statement); //load this account into memory - return LoadUserAccount(username, clientIndex); + return LoadUserAccount(username, clientIndex, characterIndex); } -int ServerApplication::LoadUserAccount(std::string username, int clientIndex) { +int ServerApplication::LoadUserAccount(std::string username, int clientIndex, int characterIndex) { //load this user account, failing if it is in memory, creating it if it doesn't exist sqlite3_stmt* statement = nullptr; @@ -99,6 +99,7 @@ int ServerApplication::LoadUserAccount(std::string username, int clientIndex) { newAccount.blackListed = sqlite3_column_int(statement, 2); newAccount.whiteListed = sqlite3_column_int(statement, 3); newAccount.clientIndex = clientIndex; + newAccount.characterIndex = characterIndex; //finish the routine sqlite3_finalize(statement); @@ -109,7 +110,7 @@ int ServerApplication::LoadUserAccount(std::string username, int clientIndex) { if (ret == SQLITE_DONE) { //create the non-existant account instead - return CreateUserAccount(username, clientIndex); + return CreateUserAccount(username, clientIndex, characterIndex); } throw(std::runtime_error(std::string() + "Unknown SQL error in LoadUserAccount: " + sqlite3_errmsg(database) ));