Moved server_util.cpp methods; created server_character_methods.cpp

This commit is contained in:
Kayne Ruse
2014-12-21 08:07:55 +11:00
parent 900f623f3b
commit dff04b5b69
6 changed files with 231 additions and 222 deletions
+5 -1
View File
@@ -106,9 +106,13 @@ private:
void HandleCharacterLoad(CharacterPacket* const);
void HandleCharacterUnload(CharacterPacket* const);
//character movement
void HandleCharacterSetRoom(CharacterPacket* const);
void HandleCharacterSetOrigin(CharacterPacket* const);
void HandleCharacterSetMotion(CharacterPacket* const);
//utility methods
void PumpPacket(SerialPacket* const);
// void PumpPacketProximity(SerialPacket* const, int roomIndex, int x, int y, int radius);
void CopyCharacterToPacket(CharacterPacket* const packet, int characterIndex);
//APIs and utilities
+176
View File
@@ -0,0 +1,176 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "server_application.hpp"
#include <iostream>
#include <sstream>
//-------------------------
//Character Management
//-------------------------
void ServerApplication::HandleCharacterCreate(CharacterPacket* const argPacket) {
int characterIndex = characterMgr.Create(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
if (characterIndex < 0) {
//build the error message
std::ostringstream msg;
msg << "Character already exists: " << argPacket->handle;
//build & send the packet
TextPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_REJECTION;
strncpy(newPacket.text, msg.str().c_str(), PACKET_STRING_SIZE);
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
return;
}
//pump this character to all clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, characterIndex);
newPacket.type = SerialPacketType::CHARACTER_CREATE;
PumpPacket(&newPacket);
}
void ServerApplication::HandleCharacterDelete(CharacterPacket* const argPacket) {
//get the user's data
AccountData* accountData = accountMgr.Get(argPacket->accountIndex);
if (!accountData) {
return;
}
ClientData* clientData = clientMgr.Get(accountData->GetClientIndex());
if (!clientData) {
return;
}
//check for fraud
if (clientData->GetAddress() != argPacket->srcAddress) {
std::cerr << "Falsified character deletion detected targeting: " << argPacket->handle << std::endl;
return;
}
//load the character into memory
int characterIndex = characterMgr.Load(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
if (characterIndex < 0) {
//build the error message
std::ostringstream msg;
msg << "Cannot delete this character";
//build & send the packet
TextPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_REJECTION;
strncpy(newPacket.text, msg.str().c_str(), PACKET_STRING_SIZE);
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
return;
}
//delete the character
characterMgr.Delete(characterIndex);
//pump character delete
CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_DELETE;
newPacket.characterIndex = characterIndex;
PumpPacket(static_cast<SerialPacket*>(&newPacket));
}
void ServerApplication::HandleCharacterLoad(CharacterPacket* const argPacket) {
int characterIndex = characterMgr.Load(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
if (characterIndex < 0) {
//build the error message
std::ostringstream msg;
if (characterIndex == -1) {
msg << "Character already loaded: ";
}
if (characterIndex == -1) {
msg << "Character name is taken: ";
}
msg << argPacket->handle;
//build & send the packet
TextPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_REJECTION;
strncpy(newPacket.text, msg.str().c_str(), PACKET_STRING_SIZE);
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
return;
}
//pump this character to all clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, characterIndex);
newPacket.type = SerialPacketType::CHARACTER_CREATE;
PumpPacket(&newPacket);
}
void ServerApplication::HandleCharacterUnload(CharacterPacket* const argPacket) {
//get the entries
CharacterData* characterData = characterMgr.Get(argPacket->characterIndex);
if (!characterData) {
return;
}
AccountData* accountData = accountMgr.Get(characterData->GetOwner());
if (!accountData) {
return; //TODO: logic_error
}
ClientData* clientData = clientMgr.Get(accountData->GetClientIndex());
if (!clientData) {
return; //TODO: logic_error
}
//check for fraud
if (clientData->GetAddress() != argPacket->srcAddress) {
std::cerr << "Falsified character unload detected targeting: uid(" << argPacket->characterIndex << ")" << std::endl;
return;
}
//unload the character
characterMgr.Unload(argPacket->characterIndex);
//pump character delete
CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_DELETE;
newPacket.characterIndex = argPacket->characterIndex;
PumpPacket(static_cast<SerialPacket*>(&newPacket));
}
//-------------------------
//character movement
//-------------------------
void ServerApplication::HandleCharacterSetRoom(CharacterPacket* const argPacket) {
//TODO
}
void ServerApplication::HandleCharacterSetOrigin(CharacterPacket* const argPacket) {
//TODO
}
void ServerApplication::HandleCharacterSetMotion(CharacterPacket* const argPacket) {
//TODO
}
-135
View File
@@ -80,138 +80,3 @@ void ServerApplication::HandleCharacterExists(CharacterPacket* const argPacket)
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
}
}
//-------------------------
//Character Management
//-------------------------
void ServerApplication::HandleCharacterCreate(CharacterPacket* const argPacket) {
int characterIndex = characterMgr.Create(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
if (characterIndex < 0) {
//build the error message
std::ostringstream msg;
msg << "Character already exists: " << argPacket->handle;
//build & send the packet
TextPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_REJECTION;
strncpy(newPacket.text, msg.str().c_str(), PACKET_STRING_SIZE);
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
return;
}
//pump this character to all clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, characterIndex);
newPacket.type = SerialPacketType::CHARACTER_CREATE;
PumpPacket(&newPacket);
}
void ServerApplication::HandleCharacterDelete(CharacterPacket* const argPacket) {
//get the user's data
AccountData* accountData = accountMgr.Get(argPacket->accountIndex);
if (!accountData) {
return;
}
ClientData* clientData = clientMgr.Get(accountData->GetClientIndex());
if (!clientData) {
return;
}
//check for fraud
if (clientData->GetAddress() != argPacket->srcAddress) {
std::cerr << "Falsified character deletion detected targeting: " << argPacket->handle << std::endl;
return;
}
//load the character into memory
int characterIndex = characterMgr.Load(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
if (characterIndex < 0) {
//build the error message
std::ostringstream msg;
msg << "Cannot delete this character";
//build & send the packet
TextPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_REJECTION;
strncpy(newPacket.text, msg.str().c_str(), PACKET_STRING_SIZE);
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
return;
}
//delete the character
characterMgr.Delete(characterIndex);
//pump character delete
CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_DELETE;
newPacket.characterIndex = characterIndex;
PumpPacket(static_cast<SerialPacket*>(&newPacket));
}
void ServerApplication::HandleCharacterLoad(CharacterPacket* const argPacket) {
int characterIndex = characterMgr.Load(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
if (characterIndex < 0) {
//build the error message
std::ostringstream msg;
if (characterIndex == -1) {
msg << "Character already loaded: ";
}
if (characterIndex == -1) {
msg << "Character name is taken: ";
}
msg << argPacket->handle;
//build & send the packet
TextPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_REJECTION;
strncpy(newPacket.text, msg.str().c_str(), PACKET_STRING_SIZE);
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
return;
}
//pump this character to all clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, characterIndex);
newPacket.type = SerialPacketType::CHARACTER_CREATE;
PumpPacket(&newPacket);
}
void ServerApplication::HandleCharacterUnload(CharacterPacket* const argPacket) {
//get the entries
CharacterData* characterData = characterMgr.Get(argPacket->characterIndex);
if (!characterData) {
return;
}
AccountData* accountData = accountMgr.Get(characterData->GetOwner());
if (!accountData) {
return; //TODO: logic_error
}
ClientData* clientData = clientMgr.Get(accountData->GetClientIndex());
if (!clientData) {
return; //TODO: logic_error
}
//check for fraud
if (clientData->GetAddress() != argPacket->srcAddress) {
std::cerr << "Falsified character unload detected targeting: uid(" << argPacket->characterIndex << ")" << std::endl;
return;
}
//unload the character
characterMgr.Unload(argPacket->characterIndex);
//pump character delete
CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_DELETE;
newPacket.characterIndex = argPacket->characterIndex;
PumpPacket(static_cast<SerialPacket*>(&newPacket));
}
+12 -13
View File
@@ -249,9 +249,9 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
break;
//server commands
case SerialPacketType::DISCONNECT_FORCED:
// case SerialPacketType::DISCONNECT_FORCED:
// HandleDisconnectForced(static_cast<ClientPacket*>(argPacket));
break;
// break;
case SerialPacketType::SHUTDOWN_REQUEST:
HandleShutdownRequest(static_cast<ClientPacket*>(argPacket));
break;
@@ -278,6 +278,16 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
HandleCharacterUnload(static_cast<CharacterPacket*>(argPacket));
break;
//character movement
case SerialPacketType::CHARACTER_SET_ROOM:
HandleCharacterSetRoom(static_cast<CharacterPacket*>(argPacket));
break;
case SerialPacketType::CHARACTER_SET_ORIGIN:
HandleCharacterSetOrigin(static_cast<CharacterPacket*>(argPacket));
break;
case SerialPacketType::CHARACTER_SET_MOTION:
HandleCharacterSetMotion(static_cast<CharacterPacket*>(argPacket));
break;
/*
case SerialPacketType::QUERY_CHARACTER_STATS:
// HandleCharacterStatsRequest(static_cast<RegionPacket*>(argPacket));
@@ -289,17 +299,6 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
// HandleCharacterStatsRequest(static_cast<RegionPacket*>(argPacket));
break;
//character movement
case SerialPacketType::CHARACTER_SET_ROOM:
// HandleCharacterUpdate(static_cast<CharacterPacket*>(argPacket));
break;
case SerialPacketType::CHARACTER_SET_ORIGIN:
// HandleCharacterUpdate(static_cast<CharacterPacket*>(argPacket));
break;
case SerialPacketType::CHARACTER_SET_MOTION:
// HandleCharacterUpdate(static_cast<CharacterPacket*>(argPacket));
break;
//enemy management
//TODO: enemy management
+38
View File
@@ -25,6 +25,18 @@
#include <iostream>
#include <sstream>
//-------------------------
//these should've come standard
//-------------------------
bool operator==(IPaddress lhs, IPaddress rhs) {
return lhs.host == rhs.host && lhs.port == rhs.port;
}
bool operator!=(IPaddress lhs, IPaddress rhs) {
return !(lhs == rhs);
}
//-------------------------
//server commands
//-------------------------
@@ -149,3 +161,29 @@ void ServerApplication::FullCharacterUnload(int index) {
return true;
});
}
//-------------------------
//utility methods
//-------------------------
void ServerApplication::PumpPacket(SerialPacket* const argPacket) {
for (auto& it : *clientMgr.GetContainer()) {
network.SendTo(it.second.GetAddress(), argPacket);
}
}
void ServerApplication::CopyCharacterToPacket(CharacterPacket* const packet, int characterIndex) {
CharacterData* character = characterMgr.Get(characterIndex);
if (!character) {
throw(std::runtime_error("Failed to copy a character to a packet"));
}
//NOTE: keep this up to date when the character changes
packet->characterIndex = characterIndex;
strncpy(packet->handle, character->GetHandle().c_str(), PACKET_STRING_SIZE);
strncpy(packet->avatar, character->GetAvatar().c_str(), PACKET_STRING_SIZE);
packet->accountIndex = character->GetOwner();
packet->roomIndex = character->GetRoomIndex();
packet->origin = character->GetOrigin();
packet->motion = character->GetMotion();
}
-73
View File
@@ -1,73 +0,0 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "server_application.hpp"
//-------------------------
//these should've come standard
//-------------------------
bool operator==(IPaddress lhs, IPaddress rhs) {
return lhs.host == rhs.host && lhs.port == rhs.port;
}
bool operator!=(IPaddress lhs, IPaddress rhs) {
return !(lhs == rhs);
}
//-------------------------
//Packet pumps
//-------------------------
void ServerApplication::PumpPacket(SerialPacket* const argPacket) {
for (auto& it : *clientMgr.GetContainer()) {
network.SendTo(it.second.GetAddress(), argPacket);
}
}
/*void ServerApplication::PumpPacketProximity(SerialPacket* const argPacket, int roomIndex, int x, int y, int radius) {
//TODO: PumpPacketProximity
//for position (roomIndex, x, y), find all characters within that distance
//find that character's owner
//find that account's client
//send the packet to that client
//NOTE: this is perhaps too complex; I write it if I need it
}*/
//-------------------------
//common copy methods
//-------------------------
void ServerApplication::CopyCharacterToPacket(CharacterPacket* const packet, int characterIndex) {
CharacterData* character = characterMgr.Get(characterIndex);
if (!character) {
throw(std::runtime_error("Failed to copy a character to a packet"));
}
//NOTE: keep this up to date when the character changes
packet->characterIndex = characterIndex;
strncpy(packet->handle, character->GetHandle().c_str(), PACKET_STRING_SIZE);
strncpy(packet->avatar, character->GetAvatar().c_str(), PACKET_STRING_SIZE);
packet->accountIndex = character->GetOwner();
packet->roomIndex = character->GetRoomIndex();
packet->origin = character->GetOrigin();
packet->motion = character->GetMotion();
}