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?
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"));
}
+1 -1
View File
@@ -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;
}
}
+2 -1
View File
@@ -26,11 +26,12 @@
struct AccountData {
std::string username;
//password
//TODO: password
bool blackListed = false;
bool whiteListed = true;
int clientIndex;
int characterIndex;
};
#endif
-1
View File
@@ -30,7 +30,6 @@
struct CharacterData {
//metadata
int clientIndex;
std::string handle;
std::string avatar;
+2 -2
View File
@@ -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);
+22 -42
View File
@@ -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<int, CharacterData> 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
+5 -4
View File
@@ -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) ));