Updated the server to use the packet methods
This commit is contained in:
@@ -34,7 +34,8 @@ public:
|
|||||||
SerialPacketType SetType(SerialPacketType t) { return type = t; }
|
SerialPacketType SetType(SerialPacketType t) { return type = t; }
|
||||||
SerialPacketType GetType() { return type; }
|
SerialPacketType GetType() { return type; }
|
||||||
|
|
||||||
IPaddress GetSourceAddress() { return srcAddress; }
|
IPaddress GetAddress() { return srcAddress; }
|
||||||
|
IPaddress* GetAddressPtr() { return &srcAddress; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend class UDPNetworkUtility;
|
friend class UDPNetworkUtility;
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
#config
|
#config
|
||||||
INCLUDES+=. accounts characters rooms ../common/debugging ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities
|
INCLUDES+=. accounts characters rooms ../common/debugging ../common/gameplay ../common/map ../common/network ../common/network/packet_types ../common/utilities
|
||||||
LIBS+=server.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3
|
LIBS+=server.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
#include "server_application.hpp"
|
#include "server_application.hpp"
|
||||||
|
|
||||||
//for PACKET_BUFFER_SIZE
|
#include "serial_packet.hpp"
|
||||||
#include "serial.hpp"
|
|
||||||
|
|
||||||
//utility functions
|
//utility functions
|
||||||
#include "sql_utility.hpp"
|
#include "sql_utility.hpp"
|
||||||
@@ -179,7 +178,7 @@ void ServerApplication::Quit() {
|
|||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
|
void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
|
||||||
switch(argPacket->type) {
|
switch(argPacket->GetType()) {
|
||||||
//basic connections
|
//basic connections
|
||||||
case SerialPacketType::BROADCAST_REQUEST:
|
case SerialPacketType::BROADCAST_REQUEST:
|
||||||
HandleBroadcastRequest(static_cast<SerialPacket*>(argPacket));
|
HandleBroadcastRequest(static_cast<SerialPacket*>(argPacket));
|
||||||
@@ -225,7 +224,7 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
|
|||||||
//handle errors
|
//handle errors
|
||||||
default: {
|
default: {
|
||||||
std::string msg = "Unknown SerialPacketType encountered in the server: ";
|
std::string msg = "Unknown SerialPacketType encountered in the server: ";
|
||||||
msg += to_string_custom(static_cast<int>(argPacket->type));
|
msg += to_string_custom(static_cast<int>(argPacket->GetType()));
|
||||||
throw(std::runtime_error(msg));
|
throw(std::runtime_error(msg));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
+43
-43
@@ -31,22 +31,22 @@ void ServerApplication::HandleBroadcastRequest(SerialPacket* const argPacket) {
|
|||||||
//send the server's data
|
//send the server's data
|
||||||
ServerPacket newPacket;
|
ServerPacket newPacket;
|
||||||
|
|
||||||
newPacket.type = SerialPacketType::BROADCAST_RESPONSE;
|
newPacket.SetType(SerialPacketType::BROADCAST_RESPONSE);
|
||||||
strncpy(newPacket.name, config["server.name"].c_str(), PACKET_STRING_SIZE);
|
newPacket.SetName(config["server.name"].c_str());
|
||||||
newPacket.playerCount = characterMgr.GetContainer()->size();
|
newPacket.SetPlayerCount(characterMgr.GetContainer()->size());
|
||||||
newPacket.version = NETWORK_VERSION;
|
newPacket.SetVersion(NETWORK_VERSION);
|
||||||
|
|
||||||
network.SendTo(&argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
network.SendTo(argPacket->GetAddressPtr(), static_cast<SerialPacket*>(&newPacket));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::HandleJoinRequest(ClientPacket* const argPacket) {
|
void ServerApplication::HandleJoinRequest(ClientPacket* const argPacket) {
|
||||||
//create the new client
|
//create the new client
|
||||||
ClientData newClient;
|
ClientData newClient;
|
||||||
newClient.address = argPacket->srcAddress;
|
newClient.address = argPacket->GetAddress();
|
||||||
|
|
||||||
//load the user account
|
//load the user account
|
||||||
//TODO: handle passwords
|
//TODO: handle passwords
|
||||||
int accountIndex = accountMgr.LoadAccount(argPacket->username, clientIndex);
|
int accountIndex = accountMgr.LoadAccount(argPacket->GetUsername(), clientIndex);
|
||||||
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;
|
||||||
@@ -55,9 +55,9 @@ void ServerApplication::HandleJoinRequest(ClientPacket* const argPacket) {
|
|||||||
|
|
||||||
//send the client their info
|
//send the client their info
|
||||||
ClientPacket newPacket;
|
ClientPacket newPacket;
|
||||||
newPacket.type = SerialPacketType::JOIN_RESPONSE;
|
newPacket.SetType(SerialPacketType::JOIN_RESPONSE);
|
||||||
newPacket.clientIndex = clientIndex;
|
newPacket.SetClientIndex(clientIndex);
|
||||||
newPacket.accountIndex = accountIndex;
|
newPacket.SetAccountIndex(accountIndex);
|
||||||
|
|
||||||
network.SendTo(&newClient.address, static_cast<SerialPacket*>(&newPacket));
|
network.SendTo(&newClient.address, static_cast<SerialPacket*>(&newPacket));
|
||||||
|
|
||||||
@@ -80,14 +80,14 @@ void ServerApplication::HandleDisconnect(ClientPacket* const argPacket) {
|
|||||||
|
|
||||||
//forward to the specified client
|
//forward to the specified client
|
||||||
network.SendTo(
|
network.SendTo(
|
||||||
&clientMap[ accountMgr.GetAccount(argPacket->accountIndex)->GetClientIndex() ].address,
|
&clientMap[ accountMgr.GetAccount(argPacket->GetAccountIndex())->GetClientIndex() ].address,
|
||||||
static_cast<SerialPacket*>(argPacket)
|
static_cast<SerialPacket*>(argPacket)
|
||||||
);
|
);
|
||||||
|
|
||||||
//save and unload this account's characters
|
//save and unload this account's characters
|
||||||
//pump the unload message to all remaining clients
|
//pump the unload message to all remaining clients
|
||||||
characterMgr.UnloadCharacterIf([&](std::map<int, CharacterData>::iterator it) -> bool {
|
characterMgr.UnloadCharacterIf([&](std::map<int, CharacterData>::iterator it) -> bool {
|
||||||
if (argPacket->accountIndex == it->second.GetOwner()) {
|
if (argPacket->GetAccountIndex() == it->second.GetOwner()) {
|
||||||
PumpCharacterUnload(it->first);
|
PumpCharacterUnload(it->first);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -95,8 +95,8 @@ void ServerApplication::HandleDisconnect(ClientPacket* const argPacket) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//erase the in-memory stuff
|
//erase the in-memory stuff
|
||||||
clientMap.erase(accountMgr.GetAccount(argPacket->accountIndex)->GetClientIndex());
|
clientMap.erase(accountMgr.GetAccount(argPacket->GetAccountIndex())->GetClientIndex());
|
||||||
accountMgr.UnloadAccount(argPacket->accountIndex);
|
accountMgr.UnloadAccount(argPacket->GetAccountIndex());
|
||||||
|
|
||||||
//finished this routine
|
//finished this routine
|
||||||
std::cout << "Disconnection, " << clientMap.size() << " clients and " << accountMgr.GetContainer()->size() << " accounts total" << std::endl;
|
std::cout << "Disconnection, " << clientMap.size() << " clients and " << accountMgr.GetContainer()->size() << " accounts total" << std::endl;
|
||||||
@@ -115,8 +115,8 @@ void ServerApplication::HandleShutdown(SerialPacket* const argPacket) {
|
|||||||
running = false;
|
running = false;
|
||||||
|
|
||||||
//disconnect all clients
|
//disconnect all clients
|
||||||
SerialPacket newPacket;
|
ServerPacket newPacket;
|
||||||
newPacket.type = SerialPacketType::DISCONNECT;
|
newPacket.SetType(SerialPacketType::DISCONNECT);
|
||||||
PumpPacket(&newPacket);
|
PumpPacket(&newPacket);
|
||||||
|
|
||||||
//finished this routine
|
//finished this routine
|
||||||
@@ -130,15 +130,15 @@ void ServerApplication::HandleShutdown(SerialPacket* const argPacket) {
|
|||||||
void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
|
void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
|
||||||
RegionPacket newPacket;
|
RegionPacket newPacket;
|
||||||
|
|
||||||
newPacket.type = SerialPacketType::REGION_CONTENT;
|
newPacket.SetType(SerialPacketType::REGION_CONTENT);
|
||||||
newPacket.roomIndex = argPacket->roomIndex;
|
newPacket.SetRoomIndex(argPacket->GetRoomIndex());
|
||||||
newPacket.x = argPacket->x;
|
newPacket.SetX(argPacket->GetX());
|
||||||
newPacket.y = argPacket->y;
|
newPacket.SetY(argPacket->GetY());
|
||||||
|
|
||||||
newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y);
|
newPacket.SetRegion(roomMgr.GetRoom(argPacket->GetRoomIndex())->GetPager()->GetRegion(argPacket->GetX(), argPacket->GetY() ));
|
||||||
|
|
||||||
//send the content
|
//send the content
|
||||||
network.SendTo(&argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
network.SendTo(argPacket->GetAddressPtr(), static_cast<SerialPacket*>(&newPacket));
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -148,7 +148,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
|
|||||||
void ServerApplication::HandleCharacterNew(CharacterPacket* const argPacket) {
|
void ServerApplication::HandleCharacterNew(CharacterPacket* const argPacket) {
|
||||||
//BUG: #27 Characters can be created with an invalid account index
|
//BUG: #27 Characters can be created with an invalid account index
|
||||||
//NOTE: misnomer, try to load the character first
|
//NOTE: misnomer, try to load the character first
|
||||||
int characterIndex = characterMgr.LoadCharacter(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
|
int characterIndex = characterMgr.LoadCharacter(argPacket->GetAccountIndex(), argPacket->GetHandle(), argPacket->GetAvatar());
|
||||||
|
|
||||||
if (characterIndex == -1) {
|
if (characterIndex == -1) {
|
||||||
//TODO: rejection packet
|
//TODO: rejection packet
|
||||||
@@ -164,7 +164,7 @@ void ServerApplication::HandleCharacterNew(CharacterPacket* const argPacket) {
|
|||||||
|
|
||||||
//send this new character to all clients
|
//send this new character to all clients
|
||||||
CharacterPacket newPacket;
|
CharacterPacket newPacket;
|
||||||
newPacket.type = SerialPacketType::CHARACTER_NEW;
|
newPacket.SetType(SerialPacketType::CHARACTER_NEW);
|
||||||
CopyCharacterToPacket(&newPacket, characterIndex);
|
CopyCharacterToPacket(&newPacket, characterIndex);
|
||||||
PumpPacket(&newPacket);
|
PumpPacket(&newPacket);
|
||||||
}
|
}
|
||||||
@@ -173,7 +173,7 @@ void ServerApplication::HandleCharacterDelete(CharacterPacket* const argPacket)
|
|||||||
//NOTE: Disconnecting only unloads a character, this explicitly deletes it
|
//NOTE: Disconnecting only unloads a character, this explicitly deletes it
|
||||||
|
|
||||||
//Authenticate the owner is doing this
|
//Authenticate the owner is doing this
|
||||||
int characterIndex = characterMgr.LoadCharacter(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
|
int characterIndex = characterMgr.LoadCharacter(argPacket->GetAccountIndex(), argPacket->GetHandle(), argPacket->GetAvatar());
|
||||||
|
|
||||||
//if this is not your character
|
//if this is not your character
|
||||||
if (characterIndex == -2) {
|
if (characterIndex == -2) {
|
||||||
@@ -197,7 +197,7 @@ void ServerApplication::HandleCharacterDelete(CharacterPacket* const argPacket)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::HandleCharacterUpdate(CharacterPacket* const argPacket) {
|
void ServerApplication::HandleCharacterUpdate(CharacterPacket* const argPacket) {
|
||||||
CharacterData* character = characterMgr.GetCharacter(argPacket->characterIndex);
|
CharacterData* character = characterMgr.GetCharacter(argPacket->GetCharacterIndex());
|
||||||
|
|
||||||
//make a new character if this one doesn't exist
|
//make a new character if this one doesn't exist
|
||||||
if (!character) {
|
if (!character) {
|
||||||
@@ -208,11 +208,11 @@ void ServerApplication::HandleCharacterUpdate(CharacterPacket* const argPacket)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//accept client-side logic
|
//accept client-side logic
|
||||||
character->SetRoomIndex(argPacket->roomIndex);
|
character->SetRoomIndex(argPacket->GetRoomIndex());
|
||||||
character->SetOrigin(argPacket->origin);
|
character->SetOrigin(argPacket->GetOrigin());
|
||||||
character->SetMotion(argPacket->motion);
|
character->SetMotion(argPacket->GetMotion());
|
||||||
|
|
||||||
*character->GetBaseStats() = argPacket->stats;
|
*character->GetBaseStats() = *argPacket->GetStatistics();
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs
|
//TODO: gameplay components: equipment, items, buffs, debuffs
|
||||||
|
|
||||||
@@ -228,14 +228,14 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) {
|
|||||||
//NOTE: I quite dislike this function
|
//NOTE: I quite dislike this function
|
||||||
|
|
||||||
//send all of the server's data to this client
|
//send all of the server's data to this client
|
||||||
ClientData& client = clientMap[argPacket->clientIndex];
|
ClientData& client = clientMap[argPacket->GetClientIndex()];
|
||||||
|
|
||||||
//send all characters
|
//send all characters
|
||||||
CharacterPacket newPacket;
|
CharacterPacket newPacket;
|
||||||
newPacket.type = SerialPacketType::CHARACTER_UPDATE;
|
newPacket.SetType(SerialPacketType::CHARACTER_UPDATE);
|
||||||
|
|
||||||
for (auto& it : *characterMgr.GetContainer()) {
|
for (auto& it : *characterMgr.GetContainer()) {
|
||||||
newPacket.characterIndex = it.first;
|
newPacket.SetCharacterIndex(it.first);
|
||||||
CopyCharacterToPacket(&newPacket, it.first);
|
CopyCharacterToPacket(&newPacket, it.first);
|
||||||
network.SendTo(&client.address, static_cast<SerialPacket*>(&newPacket));
|
network.SendTo(&client.address, static_cast<SerialPacket*>(&newPacket));
|
||||||
}
|
}
|
||||||
@@ -258,8 +258,8 @@ void ServerApplication::PumpPacket(SerialPacket* const argPacket) {
|
|||||||
void ServerApplication::PumpCharacterUnload(int uid) {
|
void ServerApplication::PumpCharacterUnload(int uid) {
|
||||||
//delete the client-side character(s)
|
//delete the client-side character(s)
|
||||||
CharacterPacket newPacket;
|
CharacterPacket newPacket;
|
||||||
newPacket.type = SerialPacketType::CHARACTER_DELETE;
|
newPacket.SetType(SerialPacketType::CHARACTER_DELETE);
|
||||||
newPacket.characterIndex = uid;
|
newPacket.SetCharacterIndex(uid);
|
||||||
PumpPacket(static_cast<SerialPacket*>(&newPacket));
|
PumpPacket(static_cast<SerialPacket*>(&newPacket));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,12 +270,12 @@ void ServerApplication::CopyCharacterToPacket(CharacterPacket* const packet, int
|
|||||||
}
|
}
|
||||||
|
|
||||||
//TODO: keep this up to date when the character changes
|
//TODO: keep this up to date when the character changes
|
||||||
packet->characterIndex = characterIndex;
|
packet->SetCharacterIndex(characterIndex);
|
||||||
strncpy(packet->handle, character->GetHandle().c_str(), PACKET_STRING_SIZE);
|
packet->SetHandle(character->GetHandle().c_str());
|
||||||
strncpy(packet->avatar, character->GetAvatar().c_str(), PACKET_STRING_SIZE);
|
packet->SetAvatar(character->GetAvatar().c_str());
|
||||||
packet->accountIndex = character->GetOwner();
|
packet->SetAccountIndex(character->GetOwner());
|
||||||
packet->roomIndex = character->GetRoomIndex();
|
packet->SetRoomIndex(character->GetRoomIndex());
|
||||||
packet->origin = character->GetOrigin();
|
packet->SetOrigin(character->GetOrigin());
|
||||||
packet->motion = character->GetMotion();
|
packet->SetMotion(character->GetMotion());
|
||||||
packet->stats = *character->GetBaseStats();
|
*packet->GetStatistics() = *character->GetBaseStats();
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
TODO: Reduce the verbosity of the network packets
|
||||||
TODO: encapsulate the data structures
|
TODO: encapsulate the data structures
|
||||||
TODO: Ping-pong and keep alive system
|
TODO: Ping-pong and keep alive system
|
||||||
TODO: Move the statistics into their own SQL table, instead of duplicating the structure a dozen times
|
TODO: Move the statistics into their own SQL table, instead of duplicating the structure a dozen times
|
||||||
|
|||||||
Reference in New Issue
Block a user