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.
This commit is contained in:
Kayne Ruse
2014-05-06 22:46:24 +10:00
parent 1beb7cbd5d
commit 4d12788c53
7 changed files with 35 additions and 52 deletions
+3 -1
View File
@@ -63,6 +63,7 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
//TODO: add the tilesheet to the map system? //TODO: add the tilesheet to the map system?
tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15); tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15);
//TODO: move this into it's own function
//request a sync //request a sync
SerialPacket packet; SerialPacket packet;
char buffer[PACKET_STRING_SIZE]; char buffer[PACKET_STRING_SIZE];
@@ -265,7 +266,7 @@ void InWorld::HandlePacket(SerialPacket packet) {
break; break;
//handle errors //handle errors
default: 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; break;
} }
} }
@@ -326,6 +327,7 @@ void InWorld::HandleCharacterNew(SerialPacket packet) {
} }
void InWorld::HandleCharacterDelete(SerialPacket packet) { void InWorld::HandleCharacterDelete(SerialPacket packet) {
//TODO: authenticate
if (playerCharacters.find(packet.characterInfo.characterIndex) == playerCharacters.end()) { if (playerCharacters.find(packet.characterInfo.characterIndex) == playerCharacters.end()) {
throw(std::runtime_error("Cannot delete non-existant characters")); throw(std::runtime_error("Cannot delete non-existant characters"));
} }
+1 -1
View File
@@ -230,7 +230,7 @@ void LobbyMenu::HandlePacket(SerialPacket packet) {
//handle errors //handle errors
default: 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; break;
} }
} }
+2 -1
View File
@@ -26,11 +26,12 @@
struct AccountData { struct AccountData {
std::string username; std::string username;
//password //TODO: password
bool blackListed = false; bool blackListed = false;
bool whiteListed = true; bool whiteListed = true;
int clientIndex; int clientIndex;
int characterIndex;
}; };
#endif #endif
-1
View File
@@ -30,7 +30,6 @@
struct CharacterData { struct CharacterData {
//metadata //metadata
int clientIndex;
std::string handle; std::string handle;
std::string avatar; std::string avatar;
+2 -2
View File
@@ -77,8 +77,8 @@ private:
void PumpPacket(SerialPacket); void PumpPacket(SerialPacket);
//Account management //Account management
int CreateUserAccount(std::string username, int clientIndex); int CreateUserAccount(std::string username, int clientIndex, int characterIndex);
int LoadUserAccount(std::string username, int clientIndex); int LoadUserAccount(std::string username, int clientIndex, int characterIndex);
int SaveUserAccount(int uid); int SaveUserAccount(int uid);
void UnloadUserAccount(int uid); void UnloadUserAccount(int uid);
void DeleteUserAccount(int uid); void DeleteUserAccount(int uid);
+22 -42
View File
@@ -42,27 +42,28 @@ void ServerApplication::HandleBroadcastRequest(SerialPacket packet) {
} }
void ServerApplication::HandleJoinRequest(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 //create the new client
ClientData newClient; ClientData newClient;
newClient.address = packet.meta.srcAddress; 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 //TODO: move this into the character management code
//create the new character //create the new character
CharacterData newCharacter; CharacterData newCharacter;
newCharacter.clientIndex = ClientData::uidCounter;
newCharacter.handle = packet.clientInfo.handle; newCharacter.handle = packet.clientInfo.handle;
newCharacter.avatar = packet.clientInfo.avatar; newCharacter.avatar = packet.clientInfo.avatar;
//send the client their info //send the client their info
packet.meta.type = SerialPacket::Type::JOIN_RESPONSE; packet.meta.type = SerialPacket::Type::JOIN_RESPONSE;
packet.clientInfo.clientIndex = ClientData::uidCounter; packet.clientInfo.clientIndex = ClientData::uidCounter;
packet.clientInfo.accountIndex = accountIndex;
packet.clientInfo.characterIndex = CharacterData::uidCounter; packet.clientInfo.characterIndex = CharacterData::uidCounter;
//bounce this packet //bounce this packet
@@ -79,11 +80,10 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) {
packet.characterInfo.motion = newCharacter.motion; packet.characterInfo.motion = newCharacter.motion;
PumpPacket(packet); 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 //finished this routine
clientMap[ClientData::uidCounter] = newClient; clientMap[ClientData::uidCounter++] = newClient;
characterMap[CharacterData::uidCounter] = newCharacter; characterMap[CharacterData::uidCounter++] = newCharacter;
ClientData::uidCounter++;
CharacterData::uidCounter++;
std::cout << "Connect, total: " << clientMap.size() << std::endl; std::cout << "Connect, total: " << clientMap.size() << std::endl;
} }
@@ -113,43 +113,24 @@ void ServerApplication::HandleDisconnect(SerialPacket packet) {
//TODO: authenticate who is disconnecting/kicking //TODO: authenticate who is disconnecting/kicking
//TODO: define the difference between unloading and deletng a character //TODO: define the difference between unloading and deletng a character
//disconnect the specified client //forward to the specified client
char buffer[PACKET_BUFFER_SIZE]; char buffer[PACKET_BUFFER_SIZE];
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(&clientMap[packet.clientInfo.clientIndex].address, buffer, PACKET_BUFFER_SIZE); network.Send(&clientMap[accountMap[packet.clientInfo.accountIndex].clientIndex].address, buffer, PACKET_BUFFER_SIZE);
clientMap.erase(packet.clientInfo.clientIndex);
//unload the client's account //delete the client side character
//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
SerialPacket delPacket; SerialPacket delPacket;
delPacket.meta.type = SerialPacket::Type::CHARACTER_DELETE; delPacket.meta.type = SerialPacket::Type::CHARACTER_DELETE;
delPacket.characterInfo.characterIndex = accountMap[packet.clientInfo.accountIndex].characterIndex;
PumpPacket(delPacket);
//delete server and client side characters //erase the in-memory stuff
erase_if(characterMap, [&](std::pair<int, CharacterData> it) -> bool { clientMap.erase(accountMap[packet.clientInfo.accountIndex].clientIndex);
//find the internal characters to delete characterMap.erase(accountMap[packet.clientInfo.accountIndex].characterIndex);
if (it.second.clientIndex == packet.clientInfo.clientIndex) { UnloadUserAccount(packet.clientInfo.accountIndex);
//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;
});
//finished this routine //finished this routine
std::cout << "Disconnect, total: " << clientMap.size() << std::endl; std::cout << "Disconnect, total: " << accountMap.size() << std::endl;
} }
void ServerApplication::HandleShutdown(SerialPacket packet) { void ServerApplication::HandleShutdown(SerialPacket packet) {
@@ -160,7 +141,6 @@ void ServerApplication::HandleShutdown(SerialPacket packet) {
//disconnect all clients //disconnect all clients
packet.meta.type = SerialPacket::Type::DISCONNECT; packet.meta.type = SerialPacket::Type::DISCONNECT;
packet.clientInfo.clientIndex = -1;
PumpPacket(packet); PumpPacket(packet);
//finished this routine //finished this routine
+5 -4
View File
@@ -38,7 +38,7 @@ static const char* DELETE_USER_ACCOUNT = "DELETE FROM UserAccounts WHERE uid = ?
//Define the methods //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 //create this user account, failing if it exists, leave this account in memory
sqlite3_stmt* statement = nullptr; sqlite3_stmt* statement = nullptr;
@@ -62,10 +62,10 @@ int ServerApplication::CreateUserAccount(std::string username, int clientIndex)
sqlite3_finalize(statement); sqlite3_finalize(statement);
//load this account into memory //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 //load this user account, failing if it is in memory, creating it if it doesn't exist
sqlite3_stmt* statement = nullptr; sqlite3_stmt* statement = nullptr;
@@ -99,6 +99,7 @@ int ServerApplication::LoadUserAccount(std::string username, int clientIndex) {
newAccount.blackListed = sqlite3_column_int(statement, 2); newAccount.blackListed = sqlite3_column_int(statement, 2);
newAccount.whiteListed = sqlite3_column_int(statement, 3); newAccount.whiteListed = sqlite3_column_int(statement, 3);
newAccount.clientIndex = clientIndex; newAccount.clientIndex = clientIndex;
newAccount.characterIndex = characterIndex;
//finish the routine //finish the routine
sqlite3_finalize(statement); sqlite3_finalize(statement);
@@ -109,7 +110,7 @@ int ServerApplication::LoadUserAccount(std::string username, int clientIndex) {
if (ret == SQLITE_DONE) { if (ret == SQLITE_DONE) {
//create the non-existant account instead //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) )); throw(std::runtime_error(std::string() + "Unknown SQL error in LoadUserAccount: " + sqlite3_errmsg(database) ));