Partial rewrite of server_application.cpp
I've also fixed some other issues along the way. However, the next step requires support for multiple rooms. Finally.
This commit is contained in:
@@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
#include "SDL/SDL_net.h"
|
#include "SDL/SDL_net.h"
|
||||||
|
|
||||||
|
#define NETWORK_VERSION 20140607
|
||||||
#define PACKET_STRING_SIZE 100
|
#define PACKET_STRING_SIZE 100
|
||||||
|
|
||||||
struct SerialPacketBase {
|
struct SerialPacketBase {
|
||||||
@@ -39,7 +40,7 @@ struct SerialPacketBase {
|
|||||||
|
|
||||||
typedef SerialPacketType Type;
|
typedef SerialPacketType Type;
|
||||||
|
|
||||||
virtual ~SerialPacketBase();
|
virtual ~SerialPacketBase() {};
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SerialPacketBase SerialPacket;
|
typedef SerialPacketBase SerialPacket;
|
||||||
|
|||||||
+10
-10
@@ -42,11 +42,11 @@ AccountManager::AccountManager() {
|
|||||||
|
|
||||||
AccountManager::~AccountManager() {
|
AccountManager::~AccountManager() {
|
||||||
for (auto& it : accountMap) {
|
for (auto& it : accountMap) {
|
||||||
SaveUserAccount(it.first);
|
SaveAccount(it.first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int AccountManager::CreateUserAccount(std::string username, int clientIndex) {
|
int AccountManager::CreateAccount(std::string username, int clientIndex) {
|
||||||
//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;
|
||||||
|
|
||||||
@@ -70,10 +70,10 @@ int AccountManager::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 LoadAccount(username, clientIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
int AccountManager::LoadUserAccount(std::string username, int clientIndex) {
|
int AccountManager::LoadAccount(std::string username, int clientIndex) {
|
||||||
//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;
|
||||||
|
|
||||||
@@ -119,13 +119,13 @@ int AccountManager::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 CreateAccount(username, clientIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw(std::runtime_error(std::string() + "Unknown SQL error in LoadUserAccount: " + sqlite3_errmsg(database) ));
|
throw(std::runtime_error(std::string() + "Unknown SQL error in LoadAccount: " + sqlite3_errmsg(database) ));
|
||||||
}
|
}
|
||||||
|
|
||||||
int AccountManager::SaveUserAccount(int uid) {
|
int AccountManager::SaveAccount(int uid) {
|
||||||
//save this user account from memory, replacing it if it exists in the database
|
//save this user account from memory, replacing it if it exists in the database
|
||||||
//DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID.
|
//DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID.
|
||||||
|
|
||||||
@@ -168,14 +168,14 @@ int AccountManager::SaveUserAccount(int uid) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AccountManager::UnloadUserAccount(int uid) {
|
void AccountManager::UnloadAccount(int uid) {
|
||||||
//save this user account, and then unload it
|
//save this user account, and then unload it
|
||||||
//NOTE: the associated characters are unloaded externally
|
//NOTE: the associated characters are unloaded externally
|
||||||
SaveUserAccount(uid);
|
SaveAccount(uid);
|
||||||
accountMap.erase(uid);
|
accountMap.erase(uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AccountManager::DeleteUserAccount(int uid) {
|
void AccountManager::DeleteAccount(int uid) {
|
||||||
//delete a user account from the database, and remove it from memory
|
//delete a user account from the database, and remove it from memory
|
||||||
//NOTE: the associated characters should be deleted externally
|
//NOTE: the associated characters should be deleted externally
|
||||||
sqlite3_stmt* statement = nullptr;
|
sqlite3_stmt* statement = nullptr;
|
||||||
|
|||||||
@@ -34,11 +34,11 @@ public:
|
|||||||
~AccountManager();
|
~AccountManager();
|
||||||
|
|
||||||
//public access methods
|
//public access methods
|
||||||
int CreateUserAccount(std::string username, int clientIndex);
|
int CreateAccount(std::string username, int clientIndex);
|
||||||
int LoadUserAccount(std::string username, int clientIndex);
|
int LoadAccount(std::string username, int clientIndex);
|
||||||
int SaveUserAccount(int uid);
|
int SaveAccount(int uid);
|
||||||
void UnloadUserAccount(int uid);
|
void UnloadAccount(int uid);
|
||||||
void DeleteUserAccount(int uid);
|
void DeleteAccount(int uid);
|
||||||
|
|
||||||
//accessors and mutators
|
//accessors and mutators
|
||||||
AccountData* GetAccount(int uid);
|
AccountData* GetAccount(int uid);
|
||||||
|
|||||||
@@ -283,6 +283,18 @@ void CharacterManager::DeleteCharacter(int uid) {
|
|||||||
characterMap.erase(uid);
|
characterMap.erase(uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CharacterManager::UnloadCharacterIf(std::function<bool(std::map<int, CharacterData>::iterator)> f) {
|
||||||
|
//save this character, then unload it if the parameter returns true
|
||||||
|
for (std::map<int, CharacterData>::iterator it = characterMap.begin(); it != characterMap.end(); /* EMPTY */ ) {
|
||||||
|
if (f(it)) {
|
||||||
|
SaveCharacter(it->first);
|
||||||
|
it = characterMap.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Define the accessors and mutators
|
//Define the accessors and mutators
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include "sqlite3/sqlite3.h"
|
#include "sqlite3/sqlite3.h"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
class CharacterManager {
|
class CharacterManager {
|
||||||
public:
|
public:
|
||||||
@@ -40,6 +41,8 @@ public:
|
|||||||
void UnloadCharacter(int uid);
|
void UnloadCharacter(int uid);
|
||||||
void DeleteCharacter(int uid);
|
void DeleteCharacter(int uid);
|
||||||
|
|
||||||
|
void UnloadCharacterIf(std::function<bool(std::map<int, CharacterData>::iterator)> f);
|
||||||
|
|
||||||
//accessors and mutators
|
//accessors and mutators
|
||||||
CharacterData* GetCharacter(int uid);
|
CharacterData* GetCharacter(int uid);
|
||||||
std::map<int, CharacterData>* GetContainer();
|
std::map<int, CharacterData>* GetContainer();
|
||||||
|
|||||||
+88
-104
@@ -106,7 +106,7 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
|
|
||||||
std::cout << "Internal sizes:" << std::endl;
|
std::cout << "Internal sizes:" << std::endl;
|
||||||
std::cout << "\tTile Size: " << sizeof(Region::type_t) << std::endl;
|
std::cout << "\tTile Size: " << sizeof(Region::type_t) << std::endl;
|
||||||
std::cout << "\tRegion Format: " << REGION_WIDTH << ", " << REGION_HEIGHT << ", " << REGION_DEPTH << << std::endl;
|
std::cout << "\tRegion Format: " << REGION_WIDTH << ", " << REGION_HEIGHT << ", " << REGION_DEPTH << std::endl;
|
||||||
std::cout << "\tRegion Content Footprint: " << REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) << std::endl;
|
std::cout << "\tRegion Content Footprint: " << REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) << std::endl;
|
||||||
std::cout << "\tPACKET_BUFFER_SIZE (max size): " << PACKET_BUFFER_SIZE << std::endl;
|
std::cout << "\tPACKET_BUFFER_SIZE (max size): " << PACKET_BUFFER_SIZE << std::endl;
|
||||||
|
|
||||||
@@ -122,7 +122,7 @@ void ServerApplication::Proc() {
|
|||||||
while(running) {
|
while(running) {
|
||||||
//suck in the waiting packets & process them
|
//suck in the waiting packets & process them
|
||||||
while(network.Receive(&packet)) {
|
while(network.Receive(&packet)) {
|
||||||
HandlePacket(packet);
|
HandlePacket(&packet);
|
||||||
}
|
}
|
||||||
//update the internals
|
//update the internals
|
||||||
//TODO: update the internals i.e. player positions
|
//TODO: update the internals i.e. player positions
|
||||||
@@ -148,25 +148,25 @@ void ServerApplication::Quit() {
|
|||||||
//handle incoming traffic
|
//handle incoming traffic
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void ServerApplication::HandlePacket(SerialPacket packet) {
|
void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
|
||||||
switch(packet.meta.type) {
|
switch(argPacket->type) {
|
||||||
//basic connections
|
//basic connections
|
||||||
case SerialPacketType::BROADCAST_REQUEST:
|
case SerialPacketType::BROADCAST_REQUEST:
|
||||||
HandleBroadcastRequest(packet);
|
HandleBroadcastRequest(dynamic_cast<SerialPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
case SerialPacketType::JOIN_REQUEST:
|
case SerialPacketType::JOIN_REQUEST:
|
||||||
HandleJoinRequest(packet);
|
HandleJoinRequest(dynamic_cast<ClientPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
case SerialPacketType::DISCONNECT:
|
case SerialPacketType::DISCONNECT:
|
||||||
HandleDisconnect(packet);
|
HandleDisconnect(dynamic_cast<ClientPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
case SerialPacketType::SHUTDOWN:
|
case SerialPacketType::SHUTDOWN:
|
||||||
HandleShutdown(packet);
|
HandleShutdown(dynamic_cast<SerialPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//map management
|
//map management
|
||||||
case SerialPacketType::REGION_REQUEST:
|
case SerialPacketType::REGION_REQUEST:
|
||||||
HandleRegionRequest(packet);
|
HandleRegionRequest(dynamic_cast<RegionPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//combat management
|
//combat management
|
||||||
@@ -174,14 +174,14 @@ void ServerApplication::HandlePacket(SerialPacket packet) {
|
|||||||
|
|
||||||
//character management
|
//character management
|
||||||
case SerialPacketType::CHARACTER_NEW:
|
case SerialPacketType::CHARACTER_NEW:
|
||||||
HandleCharacterNew(packet);
|
HandleCharacterNew(dynamic_cast<SerialPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
case SerialPacketType::CHARACTER_DELETE:
|
case SerialPacketType::CHARACTER_DELETE:
|
||||||
HandleCharacterDelete(packet);
|
HandleCharacterDelete(dynamic_cast<SerialPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
case SerialPacketType::CHARACTER_UPDATE:
|
case SerialPacketType::CHARACTER_UPDATE:
|
||||||
case SerialPacketType::CHARACTER_STATS_REQUEST: //TODO: ?
|
case SerialPacketType::CHARACTER_STATS_REQUEST: //TODO: ?
|
||||||
HandleCharacterUpdate(packet);
|
HandleCharacterUpdate(dynamic_cast<SerialPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//enemy management
|
//enemy management
|
||||||
@@ -189,12 +189,12 @@ void ServerApplication::HandlePacket(SerialPacket packet) {
|
|||||||
|
|
||||||
//mismanagement
|
//mismanagement
|
||||||
case SerialPacketType::SYNCHRONIZE:
|
case SerialPacketType::SYNCHRONIZE:
|
||||||
HandleSynchronize(packet);
|
HandleSynchronize(dynamic_cast<SerialPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//handle errors
|
//handle errors
|
||||||
default:
|
default:
|
||||||
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in the server: " + to_string_custom(int(packet.type))));
|
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in the server: " + to_string_custom(int(argPacket->type))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,129 +203,82 @@ void ServerApplication::HandlePacket(SerialPacket packet) {
|
|||||||
//basic connections
|
//basic connections
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void ServerApplication::HandleBroadcastRequest(SerialPacket packet) {
|
void ServerApplication::HandleBroadcastRequest(SerialPacket* const argPacket) {
|
||||||
//pack the server's data
|
//send the server's data
|
||||||
packet.meta.type = SerialPacket::Type::BROADCAST_RESPONSE;
|
ServerPacket newPacket;
|
||||||
packet.serverInfo.networkVersion = NETWORK_VERSION;
|
|
||||||
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
|
|
||||||
packet.serverInfo.playerCount = characterMap.size();
|
|
||||||
|
|
||||||
//bounce this packet
|
newPacket.type = SerialPacketType::BROADCAST_RESPONSE;
|
||||||
network.SendTo(&packet.meta.srcAddress, &packet);
|
snprintf(newPacket.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
|
||||||
|
newPacket.playerCount = characterMgr.GetContainer()->size();
|
||||||
|
newPacket.version = NETWORK_VERSION;
|
||||||
|
|
||||||
|
network.SendTo(&argPacket->srcAddress, dynamic_cast<SerialPacket*>(&newPacket));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::HandleJoinRequest(SerialPacket packet) {
|
void ServerApplication::HandleJoinRequest(ClientPacket* const argPacket) {
|
||||||
//create the new client
|
//create the new client
|
||||||
ClientData newClient;
|
ClientData newClient;
|
||||||
newClient.address = packet.meta.srcAddress;
|
newClient.address = argPacket->srcAddress;
|
||||||
|
|
||||||
//load the user account
|
//load the user account
|
||||||
int accountIndex = LoadUserAccount(packet.clientInfo.username, clientUID);
|
//TODO: handle passwords
|
||||||
|
int accountIndex = accountMgr.LoadAccount(argPacket->username, clientUID);
|
||||||
if (accountIndex < 0) {
|
if (accountIndex < 0) {
|
||||||
//TODO: send rejection packet
|
//TODO: send rejection packet
|
||||||
std::cerr << "Error: Account already loaded: " << accountIndex << std::endl;
|
std::cerr << "Error: Account already loaded: " << accountIndex << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//load the new character
|
|
||||||
int characterIndex = LoadCharacter(accountIndex, packet.clientInfo.handle, packet.clientInfo.avatar);
|
|
||||||
if (characterIndex < 0) {
|
|
||||||
//TODO: send rejection packet
|
|
||||||
std::cerr << "Error: Character already loaded: " << characterIndex << std::endl;
|
|
||||||
UnloadUserAccount(accountIndex);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//send the client their info
|
//send the client their info
|
||||||
packet.meta.type = SerialPacket::Type::JOIN_RESPONSE;
|
ClientPacket newPacket;
|
||||||
packet.clientInfo.clientIndex = clientUID;
|
newPacket.type = SerialPacketType::JOIN_RESPONSE;
|
||||||
packet.clientInfo.accountIndex = accountIndex;
|
newPacket.clientIndex = clientUID;
|
||||||
packet.clientInfo.characterIndex = characterIndex;
|
newPacket.accountIndex = accountIndex;
|
||||||
|
|
||||||
//bounce this packet
|
network.SendTo(&newClient.address, dynamic_cast<SerialPacket*>(&newPacket));
|
||||||
network.SendTo(&newClient.address, &packet);
|
|
||||||
|
|
||||||
//reference to prevent multiple lookups
|
|
||||||
//TODO: I need a way to pack structures unto packets more easily
|
|
||||||
//NOTE: this chunk of code is similar to HandleSynchronize
|
|
||||||
CharacterData& character = characterMap[characterIndex];
|
|
||||||
|
|
||||||
//send the new character to all clients
|
|
||||||
packet.meta.type = SerialPacket::Type::CHARACTER_NEW;
|
|
||||||
packet.characterInfo.characterIndex = characterIndex;
|
|
||||||
strncpy(packet.characterInfo.handle, character.handle.c_str(), PACKET_STRING_SIZE);
|
|
||||||
strncpy(packet.characterInfo.avatar, character.avatar.c_str(), PACKET_STRING_SIZE);
|
|
||||||
packet.characterInfo.mapIndex = character.mapIndex;
|
|
||||||
packet.characterInfo.origin = character.origin;
|
|
||||||
packet.characterInfo.motion = character.motion;
|
|
||||||
packet.characterInfo.stats = character.stats;
|
|
||||||
|
|
||||||
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[clientUID++] = newClient;
|
clientMap[clientUID++] = newClient;
|
||||||
std::cout << "Connect, total: " << clientMap.size() << std::endl;
|
std::cout << "New connection, " << clientMap.size() << " clients and " << accountMgr.GetContainer()->size() << " accounts total" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::HandleSynchronize(SerialPacket packet) {
|
void ServerApplication::HandleDisconnect(ClientPacket* const argPacket) {
|
||||||
//TODO: compensate for large distances
|
|
||||||
|
|
||||||
//send all the server's data to this client
|
|
||||||
SerialPacket newPacket;
|
|
||||||
|
|
||||||
//characters
|
|
||||||
newPacket.meta.type = SerialPacket::Type::CHARACTER_UPDATE;
|
|
||||||
for (auto& it : characterMap) {
|
|
||||||
//TODO: update this for the expanded CharacterData structure
|
|
||||||
newPacket.characterInfo.characterIndex = it.first;
|
|
||||||
snprintf(newPacket.characterInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
|
|
||||||
snprintf(newPacket.characterInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
|
|
||||||
newPacket.characterInfo.mapIndex = it.second.mapIndex;
|
|
||||||
newPacket.characterInfo.origin = it.second.origin;
|
|
||||||
newPacket.characterInfo.motion = it.second.motion;
|
|
||||||
newPacket.characterInfo.stats = it.second.stats;
|
|
||||||
|
|
||||||
network.SendTo(&clientMap[packet.clientInfo.clientIndex].address, &newPacket);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ServerApplication::HandleDisconnect(SerialPacket packet) {
|
|
||||||
//TODO: authenticate who is disconnecting/kicking
|
//TODO: authenticate who is disconnecting/kicking
|
||||||
|
|
||||||
//forward to the specified client
|
//forward to the specified client
|
||||||
network.SendTo(&clientMap[accountMap[packet.clientInfo.accountIndex].clientIndex].address, &packet);
|
network.SendTo(
|
||||||
|
&clientMap[ accountMgr.GetAccount(argPacket->accountIndex)->clientIndex ].address,
|
||||||
|
dynamic_cast<SerialPacket*>(argPacket)
|
||||||
|
);
|
||||||
|
|
||||||
//unload client and server-side characters
|
//save and unload this account's characters
|
||||||
for (std::map<int, CharacterData>::iterator it = characterMap.begin(); it != characterMap.end(); /* EMPTY */ ) {
|
//pump the unload message to all remaining clients
|
||||||
if (it->second.owner == packet.clientInfo.accountIndex) {
|
characterMgr.UnloadCharacterIf([&](std::map<int, CharacterData>::iterator it) -> bool {
|
||||||
|
if (argPacket->accountIndex == it->second.owner) {
|
||||||
PumpCharacterUnload(it->first);
|
PumpCharacterUnload(it->first);
|
||||||
SaveCharacter(it->first);
|
return true;
|
||||||
it = characterMap.erase(it); //efficient
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
//erase the in-memory stuff
|
//erase the in-memory stuff
|
||||||
clientMap.erase(accountMap[packet.clientInfo.accountIndex].clientIndex);
|
clientMap.erase(accountMgr.GetAccount(argPacket->accountIndex)->clientIndex);
|
||||||
UnloadUserAccount(packet.clientInfo.accountIndex);
|
accountMgr.UnloadAccount(argPacket->accountIndex);
|
||||||
|
|
||||||
//finished this routine
|
//finished this routine
|
||||||
std::cout << "Disconnect, total: " << clientMap.size() << std::endl;
|
std::cout << "Disconnection, " << clientMap.size() << " clients and " << accountMgr.GetContainer()->size() << " accounts total" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::HandleShutdown(SerialPacket packet) {
|
void ServerApplication::HandleShutdown(SerialPacket* const argPacket) {
|
||||||
//TODO: authenticate who is shutting the server down
|
//TODO: authenticate who is shutting the server down
|
||||||
|
|
||||||
//end the server
|
//end the server
|
||||||
running = false;
|
running = false;
|
||||||
|
|
||||||
//disconnect all clients
|
//disconnect all clients
|
||||||
packet.meta.type = SerialPacket::Type::DISCONNECT;
|
SerialPacket newPacket;
|
||||||
PumpPacket(packet);
|
newPacket.type = SerialPacketType::DISCONNECT;
|
||||||
|
PumpPacket(&newPacket);
|
||||||
|
|
||||||
//finished this routine
|
//finished this routine
|
||||||
std::cout << "Shutdown signal accepted" << std::endl;
|
std::cout << "Shutdown signal accepted" << std::endl;
|
||||||
@@ -335,13 +288,18 @@ void ServerApplication::HandleShutdown(SerialPacket packet) {
|
|||||||
//map management
|
//map management
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void ServerApplication::HandleRegionRequest(SerialPacket packet) {
|
void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
|
||||||
//TODO: this should be moved elsewhere
|
RegionPacket newPacket;
|
||||||
packet.meta.type = SerialPacket::Type::REGION_CONTENT;
|
|
||||||
packet.regionInfo.region = regionPager.GetRegion(packet.regionInfo.x, packet.regionInfo.y);
|
newPacket.type = SerialPacketType::REGION_CONTENT;
|
||||||
|
newPacket.roomIndex = argPacket->roomIndex;
|
||||||
|
newPacket.x = argPacket->x;
|
||||||
|
newPacket.y = argPacket->y;
|
||||||
|
|
||||||
|
newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y);
|
||||||
|
|
||||||
//send the content
|
//send the content
|
||||||
network.SendTo(&packet.meta.srcAddress, &packet);
|
network.SendTo(&argPacket->srcAddress, dynamic_cast<SerialPacket*>(argPacket));
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -381,6 +339,32 @@ void ServerApplication::HandleCharacterUpdate(SerialPacket packet) {
|
|||||||
|
|
||||||
//TODO: enemy management
|
//TODO: enemy management
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//mismanagement
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void ServerApplication::HandleSynchronize(SerialPacket packet) {
|
||||||
|
//TODO: compensate for large distances
|
||||||
|
|
||||||
|
//send all the server's data to this client
|
||||||
|
SerialPacket newPacket;
|
||||||
|
|
||||||
|
//characters
|
||||||
|
newPacket.meta.type = SerialPacket::Type::CHARACTER_UPDATE;
|
||||||
|
for (auto& it : characterMap) {
|
||||||
|
//TODO: update this for the expanded CharacterData structure
|
||||||
|
newPacket.characterInfo.characterIndex = it.first;
|
||||||
|
snprintf(newPacket.characterInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
|
||||||
|
snprintf(newPacket.characterInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
|
||||||
|
newPacket.characterInfo.mapIndex = it.second.mapIndex;
|
||||||
|
newPacket.characterInfo.origin = it.second.origin;
|
||||||
|
newPacket.characterInfo.motion = it.second.motion;
|
||||||
|
newPacket.characterInfo.stats = it.second.stats;
|
||||||
|
|
||||||
|
network.SendTo(&clientMap[packet.clientInfo.clientIndex].address, &newPacket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//utility methods
|
//utility methods
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|||||||
@@ -61,34 +61,34 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
//handle incoming traffic
|
//handle incoming traffic
|
||||||
void HandlePacket(SerialPacket);
|
void HandlePacket(SerialPacket* const);
|
||||||
|
|
||||||
//basic connections
|
//basic connections
|
||||||
void HandleBroadcastRequest(SerialPacket);
|
void HandleBroadcastRequest(SerialPacket* const);
|
||||||
void HandleJoinRequest(SerialPacket);
|
void HandleJoinRequest(ClientPacket* const);
|
||||||
void HandleDisconnect(SerialPacket);
|
void HandleDisconnect(ClientPacket* const);
|
||||||
void HandleShutdown(SerialPacket);
|
void HandleShutdown(SerialPacket* const);
|
||||||
|
|
||||||
//map management
|
//map management
|
||||||
void HandleRegionRequest(SerialPacket);
|
void HandleRegionRequest(RegionPacket* const);
|
||||||
|
|
||||||
//combat management
|
//combat management
|
||||||
//TODO: combat management
|
//TODO: combat management
|
||||||
|
|
||||||
//character management
|
//character management
|
||||||
void HandleCharacterNew(SerialPacket);
|
void HandleCharacterNew(SerialPacket* const);
|
||||||
void HandleCharacterDelete(SerialPacket);
|
void HandleCharacterDelete(SerialPacket* const);
|
||||||
void HandleCharacterUpdate(SerialPacket);
|
void HandleCharacterUpdate(SerialPacket* const);
|
||||||
|
|
||||||
//enemy management
|
//enemy management
|
||||||
//TODO: enemy management
|
//TODO: enemy management
|
||||||
|
|
||||||
//mismanagement
|
//mismanagement
|
||||||
void HandleSynchronize(SerialPacket);
|
void HandleSynchronize(SerialPacket* const);
|
||||||
|
|
||||||
//utility methods
|
//utility methods
|
||||||
//TODO: a function that only sends to characters in a certain proximity
|
//TODO: a function that only sends to characters in a certain proximity
|
||||||
void PumpPacket(SerialPacket);
|
void PumpPacket(SerialPacket* const);
|
||||||
void PumpCharacterUnload(int uid);
|
void PumpCharacterUnload(int uid);
|
||||||
|
|
||||||
//APIs and utilities
|
//APIs and utilities
|
||||||
|
|||||||
Reference in New Issue
Block a user