Compare commits

..

9 Commits

Author SHA1 Message Date
Kayne Ruse 6ca62db16d Merged network changes from branch 'develop', read more
After several days of trying, I decided to discard changes to the networking system:

> discard-encapsulated-packets

Instead, I've moved the packets and specialized serial functions into the same directory, and renamed them accordingly. The serialization code is only accessed by UDPNetworkUtility.

I've also removed the combat and enemy code from the network system, and reduced the number of packet types in SerialPacketType. These changes should make it easier to add more features in the future.
2014-08-31 15:35:35 +10:00
Kayne Ruse 5536bf366d Implemented the serialization switch statement 2014-08-31 15:35:14 +10:00
Kayne Ruse 094efad728 Rearranged the packet and serial code to make more sense
This mostly just reimplements the best parts of the discarded branch:

> discard-encapsulated-packets

There may still be some work needed.
2014-08-31 13:24:53 +10:00
Kayne Ruse f77aec6dd7 Merge branch 'develop' 2014-08-27 14:01:46 +10:00
Kayne Ruse d0cc5521da Merge branch 'develop' 2014-08-19 04:19:21 +10:00
Kayne Ruse e56a3d121c Merge branch 'develop' 2014-08-13 08:24:08 +10:00
Kayne Ruse 07885cca1b Merge branch 'develop' into master (read more)
Following a rather educational "game jam" using components of Tortuga, I've
made a significant number of revisions.

A brief summary:

* The entirety of the map system now have lua APIs
* The ConfigUtility handles recusive config files
* Collisions are functional
* I've added a singleton template base class, which is used by the apps
* I've decided that focusing on the engine's stability is important
* I need to push through the generated TODO list
2014-08-04 01:32:21 +10:00
Kayne Ruse 0077d501ac Merge branch 'develop' 2014-07-08 16:28:07 +10:00
Kayne Ruse ac1098fa86 Merge branch 'develop', Major API expansion 2014-06-24 02:46:39 +10:00
26 changed files with 350 additions and 404 deletions
+1
View File
@@ -21,6 +21,7 @@
*/ */
#include "client_application.hpp" #include "client_application.hpp"
#include "serial_packet.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
#include <stdexcept> #include <stdexcept>
+42 -42
View File
@@ -269,7 +269,7 @@ void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
//------------------------- //-------------------------
void InWorld::HandlePacket(SerialPacket* const argPacket) { void InWorld::HandlePacket(SerialPacket* const argPacket) {
switch(argPacket->GetType()) { switch(argPacket->type) {
case SerialPacketType::DISCONNECT: case SerialPacketType::DISCONNECT:
HandleDisconnect(argPacket); HandleDisconnect(argPacket);
break; break;
@@ -287,7 +287,7 @@ void InWorld::HandlePacket(SerialPacket* const argPacket) {
break; break;
//handle errors //handle errors
default: default:
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InWorld: " + to_string_custom(static_cast<int>(argPacket->GetType())) )); throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InWorld: " + to_string_custom(static_cast<int>(argPacket->type)) ));
break; break;
} }
} }
@@ -297,21 +297,21 @@ void InWorld::HandleDisconnect(SerialPacket* const argPacket) {
} }
void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) { void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) {
if (characterMap.find(argPacket->GetCharacterIndex()) != characterMap.end()) { if (characterMap.find(argPacket->characterIndex) != characterMap.end()) {
throw(std::runtime_error("Cannot create duplicate characters")); throw(std::runtime_error("Cannot create duplicate characters"));
} }
//create the character object //create the character object
Character& newCharacter = characterMap[argPacket->GetCharacterIndex()]; Character& newCharacter = characterMap[argPacket->characterIndex];
//fill out the character's members //fill out the character's members
newCharacter.SetHandle(argPacket->GetHandle()); newCharacter.SetHandle(argPacket->handle);
newCharacter.SetAvatar(argPacket->GetAvatar()); newCharacter.SetAvatar(argPacket->avatar);
newCharacter.GetSprite()->LoadSurface(ConfigUtility::GetSingleton()["dir.sprites"] + newCharacter.GetAvatar(), 4, 4); newCharacter.GetSprite()->LoadSurface(ConfigUtility::GetSingleton()["dir.sprites"] + newCharacter.GetAvatar(), 4, 4);
newCharacter.SetOrigin(argPacket->GetOrigin()); newCharacter.SetOrigin(argPacket->origin);
newCharacter.SetMotion(argPacket->GetMotion()); newCharacter.SetMotion(argPacket->motion);
newCharacter.SetBounds({ newCharacter.SetBounds({
CHARACTER_BOUNDS_X, CHARACTER_BOUNDS_X,
CHARACTER_BOUNDS_Y, CHARACTER_BOUNDS_Y,
@@ -319,14 +319,14 @@ void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) {
CHARACTER_BOUNDS_HEIGHT CHARACTER_BOUNDS_HEIGHT
}); });
*newCharacter.GetStats() = *argPacket->GetStatistics(); (*newCharacter.GetStats()) = argPacket->stats;
//bookkeeping code //bookkeeping code
newCharacter.CorrectSprite(); newCharacter.CorrectSprite();
//catch this client's player object //catch this client's player object
if (argPacket->GetAccountIndex() == accountIndex && !localCharacter) { if (argPacket->accountIndex == accountIndex && !localCharacter) {
characterIndex = argPacket->GetCharacterIndex(); characterIndex = argPacket->characterIndex;
localCharacter = &newCharacter; localCharacter = &newCharacter;
//setup the camera //setup the camera
@@ -344,39 +344,39 @@ void InWorld::HandleCharacterDelete(CharacterPacket* const argPacket) {
//TODO: authenticate when own character is being deleted (linked to a TODO in the server) //TODO: authenticate when own character is being deleted (linked to a TODO in the server)
//catch this client's player object //catch this client's player object
if (argPacket->GetCharacterIndex() == characterIndex) { if (argPacket->characterIndex == characterIndex) {
characterIndex = -1; characterIndex = -1;
localCharacter = nullptr; localCharacter = nullptr;
} }
characterMap.erase(argPacket->GetCharacterIndex()); characterMap.erase(argPacket->characterIndex);
} }
void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) { void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) {
if (characterMap.find(argPacket->GetCharacterIndex()) == characterMap.end()) { if (characterMap.find(argPacket->characterIndex) == characterMap.end()) {
std::cout << "Warning: HandleCharacterUpdate() is passing to HandleCharacterNew()" << std::endl; std::cout << "Warning: HandleCharacterUpdate() is passing to HandleCharacterNew()" << std::endl;
HandleCharacterNew(argPacket); HandleCharacterNew(argPacket);
return; return;
} }
Character& character = characterMap[argPacket->GetCharacterIndex()]; Character& character = characterMap[argPacket->characterIndex];
//other characters moving //other characters moving
if (argPacket->GetCharacterIndex() != characterIndex) { if (argPacket->characterIndex != characterIndex) {
character.SetOrigin(argPacket->GetOrigin()); character.SetOrigin(argPacket->origin);
character.SetMotion(argPacket->GetMotion()); character.SetMotion(argPacket->motion);
character.CorrectSprite(); character.CorrectSprite();
} }
} }
void InWorld::HandleRegionContent(RegionPacket* const argPacket) { void InWorld::HandleRegionContent(RegionPacket* const argPacket) {
//replace existing regions //replace existing regions
regionPager.UnloadRegion(argPacket->GetX(), argPacket->GetY()); regionPager.UnloadRegion(argPacket->x, argPacket->y);
regionPager.PushRegion(argPacket->GetRegion()); regionPager.PushRegion(argPacket->region);
//clean up after the serial code //clean up after the serial code
delete argPacket->GetRegion(); delete argPacket->region;
argPacket->SetRegion(nullptr); argPacket->region = nullptr;
} }
//------------------------- //-------------------------
@@ -387,9 +387,9 @@ void InWorld::RequestSynchronize() {
ClientPacket newPacket; ClientPacket newPacket;
//request a sync //request a sync
newPacket.SetType(SerialPacketType::SYNCHRONIZE); newPacket.type = SerialPacketType::SYNCHRONIZE;
newPacket.SetClientIndex(clientIndex); newPacket.clientIndex = clientIndex;
newPacket.SetAccountIndex(accountIndex); newPacket.accountIndex = accountIndex;
//TODO: location, range for sync request //TODO: location, range for sync request
@@ -400,15 +400,15 @@ void InWorld::SendPlayerUpdate() {
CharacterPacket newPacket; CharacterPacket newPacket;
//pack the packet //pack the packet
newPacket.SetType(SerialPacketType::CHARACTER_UPDATE); newPacket.type = SerialPacketType::CHARACTER_UPDATE;
newPacket.SetCharacterIndex(characterIndex); newPacket.characterIndex = characterIndex;
//NOTE: omitting the handle and avatar here //NOTE: omitting the handle and avatar here
newPacket.SetAccountIndex(accountIndex); newPacket.accountIndex = accountIndex;
newPacket.SetRoomIndex(0); //TODO: room index newPacket.roomIndex = 0; //TODO: room index
newPacket.SetOrigin(localCharacter->GetOrigin()); newPacket.origin = localCharacter->GetOrigin();
newPacket.SetMotion(localCharacter->GetMotion()); newPacket.motion = localCharacter->GetMotion();
*newPacket.GetStatistics() = *localCharacter->GetStats(); newPacket.stats = *localCharacter->GetStats();
//TODO: gameplay components: equipment, items, buffs, debuffs //TODO: gameplay components: equipment, items, buffs, debuffs
@@ -419,9 +419,9 @@ void InWorld::RequestDisconnect() {
ClientPacket newPacket; ClientPacket newPacket;
//send a disconnect request //send a disconnect request
newPacket.SetType(SerialPacketType::DISCONNECT); newPacket.type = SerialPacketType::DISCONNECT;
newPacket.SetClientIndex(clientIndex); newPacket.clientIndex = clientIndex;
newPacket.SetAccountIndex(accountIndex); newPacket.accountIndex = accountIndex;
network.SendTo(Channels::SERVER, &newPacket); network.SendTo(Channels::SERVER, &newPacket);
} }
@@ -430,9 +430,9 @@ void InWorld::RequestShutDown() {
ClientPacket newPacket; ClientPacket newPacket;
//send a shutdown request //send a shutdown request
newPacket.SetType(SerialPacketType::SHUTDOWN); newPacket.type = SerialPacketType::SHUTDOWN;
newPacket.SetClientIndex(clientIndex); newPacket.clientIndex = clientIndex;
newPacket.SetAccountIndex(accountIndex); newPacket.accountIndex = accountIndex;
network.SendTo(Channels::SERVER, &newPacket); network.SendTo(Channels::SERVER, &newPacket);
} }
@@ -441,10 +441,10 @@ void InWorld::RequestRegion(int roomIndex, int x, int y) {
RegionPacket packet; RegionPacket packet;
//pack the region's data //pack the region's data
packet.SetType(SerialPacketType::REGION_REQUEST); packet.type = SerialPacketType::REGION_REQUEST;
packet.SetRoomIndex(roomIndex); packet.roomIndex = roomIndex;
packet.SetX(x); packet.x = x;
packet.SetY(y); packet.y = y;
network.SendTo(Channels::SERVER, &packet); network.SendTo(Channels::SERVER, &packet);
} }
+1
View File
@@ -27,6 +27,7 @@
//networking //networking
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial_packet.hpp"
//graphics //graphics
#include "image.hpp" #include "image.hpp"
+17 -17
View File
@@ -185,7 +185,7 @@ void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) {
//------------------------- //-------------------------
void LobbyMenu::HandlePacket(SerialPacket* const argPacket) { void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
switch(argPacket->GetType()) { switch(argPacket->type) {
case SerialPacketType::BROADCAST_RESPONSE: case SerialPacketType::BROADCAST_RESPONSE:
HandleBroadcastResponse(static_cast<ServerPacket*>(argPacket)); HandleBroadcastResponse(static_cast<ServerPacket*>(argPacket));
break; break;
@@ -194,7 +194,7 @@ void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
break; break;
//handle errors //handle errors
default: default:
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in LobbyMenu: " + to_string_custom(static_cast<int>(argPacket->GetType())) )); throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in LobbyMenu: " + to_string_custom(static_cast<int>(argPacket->type)) ));
break; break;
} }
} }
@@ -202,10 +202,10 @@ void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) { void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
//extract the data //extract the data
ServerInformation server; ServerInformation server;
server.address = argPacket->GetAddress(); server.address = argPacket->srcAddress;
server.name = argPacket->GetName(); server.name = argPacket->name;
server.playerCount = argPacket->GetPlayerCount(); server.playerCount = argPacket->playerCount;
server.version = argPacket->GetVersion(); server.version = argPacket->version;
//Checking compatibility //Checking compatibility
server.compatible = server.version == NETWORK_VERSION; server.compatible = server.version == NETWORK_VERSION;
@@ -215,17 +215,17 @@ void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
} }
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) { void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
clientIndex = argPacket->GetClientIndex(); clientIndex = argPacket->clientIndex;
accountIndex = argPacket->GetAccountIndex(); accountIndex = argPacket->accountIndex;
network.Bind(argPacket->GetAddressPtr(), Channels::SERVER); network.Bind(&argPacket->srcAddress, Channels::SERVER);
SetNextScene(SceneList::INWORLD); SetNextScene(SceneList::INWORLD);
//send this player's character info //send this player's character info
CharacterPacket newPacket; CharacterPacket newPacket;
newPacket.SetType(SerialPacketType::CHARACTER_NEW); newPacket.type = SerialPacketType::CHARACTER_NEW;
newPacket.SetHandle(config["client.handle"].c_str()); strncpy(newPacket.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE);
newPacket.SetAvatar(config["client.avatar"].c_str()); strncpy(newPacket.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE);
newPacket.SetAccountIndex(accountIndex); newPacket.accountIndex = accountIndex;
network.SendTo(Channels::SERVER, &newPacket); network.SendTo(Channels::SERVER, &newPacket);
} }
@@ -235,8 +235,8 @@ void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
void LobbyMenu::SendBroadcastRequest() { void LobbyMenu::SendBroadcastRequest() {
//broadcast to the network, or a specific server //broadcast to the network, or a specific server
ServerPacket packet; ClientPacket packet;
packet.SetType(SerialPacketType::BROADCAST_REQUEST); packet.type = SerialPacketType::BROADCAST_REQUEST;
network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet);
//reset the server list //reset the server list
@@ -247,8 +247,8 @@ void LobbyMenu::SendBroadcastRequest() {
void LobbyMenu::SendJoinRequest() { void LobbyMenu::SendJoinRequest() {
//pack the packet //pack the packet
ClientPacket packet; ClientPacket packet;
packet.SetType(SerialPacketType::JOIN_REQUEST); packet.type = SerialPacketType::JOIN_REQUEST;
packet.SetUsername(config["client.username"].c_str()); strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE);
//join the selected server //join the selected server
network.SendTo(&selection->address, &packet); network.SendTo(&selection->address, &packet);
+1
View File
@@ -31,6 +31,7 @@
//utilities //utilities
#include "config_utility.hpp" #include "config_utility.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial_packet.hpp"
//client //client
#include "base_scene.hpp" #include "base_scene.hpp"
@@ -23,50 +23,52 @@
#include "serial_utility.hpp" #include "serial_utility.hpp"
void CharacterPacket::Serialize(void* buffer) { #include "serial_statistics.hpp"
serializeCopy(&buffer, &type, sizeof(SerialPacketType));
void serializeCharacter(void* buffer, CharacterPacket* packet) {
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
//identify the character //identify the character
serializeCopy(&buffer, &characterIndex, sizeof(int)); serialCopy(&buffer, &packet->characterIndex, sizeof(int));
serializeCopy(&buffer, handle, PACKET_STRING_SIZE); serialCopy(&buffer, packet->handle, PACKET_STRING_SIZE);
serializeCopy(&buffer, avatar, PACKET_STRING_SIZE); serialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE);
//the owner //the owner
serializeCopy(&buffer, &accountIndex, sizeof(int)); serialCopy(&buffer, &packet->accountIndex, sizeof(int));
//location //location
serializeCopy(&buffer, &roomIndex, sizeof(int)); serialCopy(&buffer, &packet->roomIndex, sizeof(int));
serializeCopy(&buffer, &origin.x, sizeof(double)); serialCopy(&buffer, &packet->origin.x, sizeof(double));
serializeCopy(&buffer, &origin.y, sizeof(double)); serialCopy(&buffer, &packet->origin.y, sizeof(double));
serializeCopy(&buffer, &motion.x, sizeof(double)); serialCopy(&buffer, &packet->motion.x, sizeof(double));
serializeCopy(&buffer, &motion.y, sizeof(double)); serialCopy(&buffer, &packet->motion.y, sizeof(double));
//stats structure //stats structure
serializeCopyStatistics(&buffer, &stats); serializeStatistics(&buffer, &packet->stats);
//TODO: gameplay components: equipment, items, buffs, debuffs //TODO: gameplay components: equipment, items, buffs, debuffs
} }
void CharacterPacket::Deserialize(void* buffer) { void deserializeCharacter(void* buffer, CharacterPacket* packet) {
deserializeCopy(&buffer, &type, sizeof(SerialPacketType)); deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
//identify the character //identify the character
deserializeCopy(&buffer, &characterIndex, sizeof(int)); deserialCopy(&buffer, &packet->characterIndex, sizeof(int));
deserializeCopy(&buffer, handle, PACKET_STRING_SIZE); deserialCopy(&buffer, packet->handle, PACKET_STRING_SIZE);
deserializeCopy(&buffer, avatar, PACKET_STRING_SIZE); deserialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE);
//the owner //the owner
deserializeCopy(&buffer, &accountIndex, sizeof(int)); deserialCopy(&buffer, &packet->accountIndex, sizeof(int));
//location //location
deserializeCopy(&buffer, &roomIndex, sizeof(int)); deserialCopy(&buffer, &packet->roomIndex, sizeof(int));
deserializeCopy(&buffer, &origin.x, sizeof(double)); deserialCopy(&buffer, &packet->origin.x, sizeof(double));
deserializeCopy(&buffer, &origin.y, sizeof(double)); deserialCopy(&buffer, &packet->origin.y, sizeof(double));
deserializeCopy(&buffer, &motion.x, sizeof(double)); deserialCopy(&buffer, &packet->motion.x, sizeof(double));
deserializeCopy(&buffer, &motion.y, sizeof(double)); deserialCopy(&buffer, &packet->motion.y, sizeof(double));
//stats structure //stats structure
deserializeCopyStatistics(&buffer, &stats); deserializeStatistics(&buffer, &packet->stats);
//TODO: gameplay components: equipment, items, buffs, debuffs //TODO: gameplay components: equipment, items, buffs, debuffs
} }
@@ -27,48 +27,11 @@
#include "vector2.hpp" #include "vector2.hpp"
#include "statistics.hpp" #include "statistics.hpp"
#include <cstring> struct CharacterPacket : SerialPacketBase {
class CharacterPacket : public SerialPacketBase {
public:
CharacterPacket() {}
~CharacterPacket() {}
//indentity
int SetCharacterIndex(int i) { return characterIndex = i; }
const char* SetHandle(const char* s)
{ return strncpy(handle, s, PACKET_STRING_SIZE); }
const char* SetAvatar(const char* s)
{ return strncpy(handle, s, PACKET_STRING_SIZE); }
int SetAccountIndex(int i) { return accountIndex = i; }
int GetCharacterIndex() { return characterIndex; }
const char* GetHandle() { return handle; }
const char* GetAvatar() { return avatar; }
int GetAccountIndex() { return accountIndex; }
//location
int SetRoomIndex(int i) { return roomIndex = i; }
Vector2 SetOrigin(Vector2 v) { return origin = v; }
Vector2 SetMotion(Vector2 v) { return motion = v; }
int GetRoomIndex() { return roomIndex; }
Vector2 GetOrigin() { return origin; }
Vector2 GetMotion() { return motion; }
//gameplay
Statistics* GetStatistics() { return &stats; }
virtual void Serialize(void* buffer) override;
virtual void Deserialize(void* buffer) override;
private:
//identify the character //identify the character
int characterIndex; int characterIndex;
char handle[PACKET_STRING_SIZE+1]; char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE+1]; char avatar[PACKET_STRING_SIZE];
//the owner //the owner
int accountIndex; int accountIndex;
@@ -84,4 +47,7 @@ private:
//TODO: gameplay components: equipment, items, buffs, debuffs //TODO: gameplay components: equipment, items, buffs, debuffs
}; };
void serializeCharacter(void* buffer, CharacterPacket* packet);
void deserializeCharacter(void* buffer, CharacterPacket* packet);
#endif #endif
+10 -10
View File
@@ -23,18 +23,18 @@
#include "serial_utility.hpp" #include "serial_utility.hpp"
void ClientPacket::Serialize(void* buffer) { void serializeClient(void* buffer, ClientPacket* packet) {
serializeCopy(&buffer, &type, sizeof(SerialPacketType)); serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
serializeCopy(&buffer, &clientIndex, sizeof(int)); serialCopy(&buffer, &packet->clientIndex, sizeof(int));
serializeCopy(&buffer, &accountIndex, sizeof(int)); serialCopy(&buffer, &packet->accountIndex, sizeof(int));
serializeCopy(&buffer, username, PACKET_STRING_SIZE); serialCopy(&buffer, packet->username, PACKET_STRING_SIZE);
} }
void ClientPacket::Deserialize(void* buffer) { void deserializeClient(void* buffer, ClientPacket* packet) {
deserializeCopy(&buffer, &type, sizeof(SerialPacketType)); deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
deserializeCopy(&buffer, &clientIndex, sizeof(int)); deserialCopy(&buffer, &packet->clientIndex, sizeof(int));
deserializeCopy(&buffer, &accountIndex, sizeof(int)); deserialCopy(&buffer, &packet->accountIndex, sizeof(int));
deserializeCopy(&buffer, username, PACKET_STRING_SIZE); deserialCopy(&buffer, packet->username, PACKET_STRING_SIZE);
} }
+5 -23
View File
@@ -24,31 +24,13 @@
#include "serial_packet_base.hpp" #include "serial_packet_base.hpp"
#include <cstring> struct ClientPacket : SerialPacketBase {
class ClientPacket : public SerialPacketBase {
public:
ClientPacket() {}
~ClientPacket() {}
//accessors & mutators
int SetClientIndex(int i) { return clientIndex = i; }
int SetAccountIndex(int i) { return accountIndex = i; }
const char* SetUsername(const char* s)
{ return strncpy(username, s, PACKET_STRING_SIZE); }
int GetClientIndex() { return clientIndex; }
int GetAccountIndex() { return accountIndex; }
const char* GetUsername() { return username; }
virtual void Serialize(void* buffer) override;
virtual void Deserialize(void* buffer) override;
private:
int clientIndex; int clientIndex;
int accountIndex; int accountIndex;
char username[PACKET_STRING_SIZE+1]; char username[PACKET_STRING_SIZE];
// char password[PACKET_STRING_SIZE]; //hashed, not currently used
}; };
void serializeClient(void* buffer, ClientPacket* packet);
void deserializeClient(void* buffer, ClientPacket* packet);
#endif #endif
+17 -17
View File
@@ -23,15 +23,15 @@
#include "serial_utility.hpp" #include "serial_utility.hpp"
void RegionPacket::Serialize(void* buffer) { void serializeRegion(void* buffer, RegionPacket* packet) {
serializeCopy(&buffer, &type, sizeof(SerialPacketType)); serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
//format //format
serializeCopy(&buffer, &roomIndex, sizeof(int)); serialCopy(&buffer, &packet->roomIndex, sizeof(int));
serializeCopy(&buffer, &x, sizeof(int)); serialCopy(&buffer, &packet->x, sizeof(int));
serializeCopy(&buffer, &y, sizeof(int)); serialCopy(&buffer, &packet->y, sizeof(int));
if (type != SerialPacketType::REGION_CONTENT) { if (packet->type != SerialPacketType::REGION_CONTENT) {
return; return;
} }
@@ -39,41 +39,41 @@ void RegionPacket::Serialize(void* buffer) {
for (int i = 0; i < REGION_WIDTH; i++) { for (int i = 0; i < REGION_WIDTH; i++) {
for (int j = 0; j < REGION_HEIGHT; j++) { for (int j = 0; j < REGION_HEIGHT; j++) {
for (int k = 0; k < REGION_DEPTH; k++) { for (int k = 0; k < REGION_DEPTH; k++) {
*reinterpret_cast<Region::type_t*>(buffer) = region->GetTile(i, j, k); *reinterpret_cast<Region::type_t*>(buffer) = packet->region->GetTile(i, j, k);
buffer = reinterpret_cast<char*>(buffer) + sizeof(Region::type_t); buffer = reinterpret_cast<char*>(buffer) + sizeof(Region::type_t);
} }
} }
} }
//solids //solids
serializeCopy(&buffer, region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); serialCopy(&buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
} }
void RegionPacket::Deserialize(void* buffer) { void deserializeRegion(void* buffer, RegionPacket* packet) {
deserializeCopy(&buffer, &type, sizeof(SerialPacketType)); deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
//format //format
deserializeCopy(&buffer, &roomIndex, sizeof(int)); deserialCopy(&buffer, &packet->roomIndex, sizeof(int));
deserializeCopy(&buffer, &x, sizeof(int)); deserialCopy(&buffer, &packet->x, sizeof(int));
deserializeCopy(&buffer, &y, sizeof(int)); deserialCopy(&buffer, &packet->y, sizeof(int));
if (type != SerialPacketType::REGION_CONTENT) { if (packet->type != SerialPacketType::REGION_CONTENT) {
return; return;
} }
//an object to work on //an object to work on
region = new Region(x, y); packet->region = new Region(packet->x, packet->y);
//tiles //tiles
for (int i = 0; i < REGION_WIDTH; i++) { for (int i = 0; i < REGION_WIDTH; i++) {
for (int j = 0; j < REGION_HEIGHT; j++) { for (int j = 0; j < REGION_HEIGHT; j++) {
for (int k = 0; k < REGION_DEPTH; k++) { for (int k = 0; k < REGION_DEPTH; k++) {
region->SetTile(i, j, k, *reinterpret_cast<Region::type_t*>(buffer)); packet->region->SetTile(i, j, k, *reinterpret_cast<Region::type_t*>(buffer));
buffer = reinterpret_cast<char*>(buffer) + sizeof(Region::type_t); buffer = reinterpret_cast<char*>(buffer) + sizeof(Region::type_t);
} }
} }
} }
//solids //solids
deserializeCopy(&buffer, region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); deserialCopy(&buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
} }
+4 -23
View File
@@ -27,35 +27,13 @@
#include "region.hpp" #include "region.hpp"
#include <cmath> #include <cmath>
#include <cstring>
//define the memory footprint for the region's members //define the memory footprint for the region's members
constexpr int REGION_TILE_FOOTPRINT = sizeof(Region::type_t) * REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH; constexpr int REGION_TILE_FOOTPRINT = sizeof(Region::type_t) * REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH;
constexpr int REGION_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0); constexpr int REGION_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0);
constexpr int REGION_METADATA_FOOTPRINT = sizeof(int) * 3; constexpr int REGION_METADATA_FOOTPRINT = sizeof(int) * 3;
class RegionPacket : public SerialPacketBase { struct RegionPacket : SerialPacketBase {
public:
RegionPacket() {}
~RegionPacket() {}
//location
int SetRoomIndex(int i) { return roomIndex = i; }
int SetX(int i) { return x = i; }
int SetY(int i) { return y = i; }
int GetRoomIndex() { return roomIndex; }
int GetX() { return x; }
int GetY() { return y; }
//the region itself
Region* SetRegion(Region* r) { return region = r; }
Region* GetRegion() { return region; }
virtual void Serialize(void* buffer) override;
virtual void Deserialize(void* buffer) override;
private:
//location/identify the region //location/identify the region
int roomIndex; int roomIndex;
int x, y; int x, y;
@@ -64,4 +42,7 @@ private:
Region* region; Region* region;
}; };
void serializeRegion(void* buffer, RegionPacket* packet);
void deserializeRegion(void* buffer, RegionPacket* packet);
#endif #endif
@@ -1,4 +1,4 @@
/* Copyright: (c) Kayne Ruse 2013, 2014 /* Copyright: (c) Kayne Ruse 2014
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
@@ -21,4 +21,4 @@
*/ */
#include "serial_packet_base.hpp" #include "serial_packet_base.hpp"
//NOTE: This is a sanity check //sanity check
@@ -26,28 +26,14 @@
#include "SDL/SDL_net.h" #include "SDL/SDL_net.h"
//The packets use a char array for string storage
constexpr int PACKET_STRING_SIZE = 100; constexpr int PACKET_STRING_SIZE = 100;
class SerialPacketBase { struct SerialPacketBase {
public: //members
SerialPacketType SetType(SerialPacketType t) { return type = t; }
SerialPacketType GetType() { return type; }
IPaddress GetAddress() { return srcAddress; }
IPaddress* GetAddressPtr() { return &srcAddress; }
SerialPacketBase() {};
virtual ~SerialPacketBase() {};
virtual void Serialize(void* buffer) = 0;
virtual void Deserialize(void* buffer) = 0;
protected:
friend class UDPNetworkUtility;
SerialPacketType type; SerialPacketType type;
IPaddress srcAddress; IPaddress srcAddress;
virtual ~SerialPacketBase() {};
}; };
#endif #endif
@@ -0,0 +1,64 @@
/* 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 "serial_statistics.hpp"
#include "serial_utility.hpp"
void serializeStatistics(void** buffer, Statistics* stats) {
//integers
serialCopy(buffer, &stats->level, sizeof(int));
serialCopy(buffer, &stats->exp, sizeof(int));
serialCopy(buffer, &stats->maxHP, sizeof(int));
serialCopy(buffer, &stats->health, sizeof(int));
serialCopy(buffer, &stats->maxMP, sizeof(int));
serialCopy(buffer, &stats->mana, sizeof(int));
serialCopy(buffer, &stats->attack, sizeof(int));
serialCopy(buffer, &stats->defence, sizeof(int));
serialCopy(buffer, &stats->intelligence, sizeof(int));
serialCopy(buffer, &stats->resistance, sizeof(int));
serialCopy(buffer, &stats->speed, sizeof(int));
//floats
serialCopy(buffer, &stats->accuracy, sizeof(float));
serialCopy(buffer, &stats->evasion, sizeof(float));
serialCopy(buffer, &stats->luck, sizeof(float));
}
void deserializeStatistics(void** buffer, Statistics* stats) {
//integers
deserialCopy(buffer, &stats->level, sizeof(int));
deserialCopy(buffer, &stats->exp, sizeof(int));
deserialCopy(buffer, &stats->maxHP, sizeof(int));
deserialCopy(buffer, &stats->health, sizeof(int));
deserialCopy(buffer, &stats->maxMP, sizeof(int));
deserialCopy(buffer, &stats->mana, sizeof(int));
deserialCopy(buffer, &stats->attack, sizeof(int));
deserialCopy(buffer, &stats->defence, sizeof(int));
deserialCopy(buffer, &stats->intelligence, sizeof(int));
deserialCopy(buffer, &stats->resistance, sizeof(int));
deserialCopy(buffer, &stats->speed, sizeof(int));
//floats
deserialCopy(buffer, &stats->accuracy, sizeof(float));
deserialCopy(buffer, &stats->evasion, sizeof(float));
deserialCopy(buffer, &stats->luck, sizeof(float));
}
@@ -0,0 +1,30 @@
/* 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.
*/
#ifndef SERIALSTATISTICS_HPP_
#define SERIALSTATISTICS_HPP_
#include "statistics.hpp"
void serializeStatistics(void** buffer, Statistics* stats);
void deserializeStatistics(void** buffer, Statistics* stats);
#endif
+10 -10
View File
@@ -23,20 +23,20 @@
#include "serial_utility.hpp" #include "serial_utility.hpp"
void ServerPacket::Serialize(void* buffer) { void serializeServer(void* buffer, ServerPacket* packet) {
serializeCopy(&buffer, &type, sizeof(SerialPacketType)); serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
//identify the server //identify the server
serializeCopy(&buffer, name, PACKET_STRING_SIZE); serialCopy(&buffer, packet->name, PACKET_STRING_SIZE);
serializeCopy(&buffer, &playerCount, sizeof(int)); serialCopy(&buffer, &packet->playerCount, sizeof(int));
serializeCopy(&buffer, &version, sizeof(int)); serialCopy(&buffer, &packet->version, sizeof(int));
} }
void ServerPacket::Deserialize(void* buffer) { void deserializeServer(void* buffer, ServerPacket* packet) {
deserializeCopy(&buffer, &type, sizeof(SerialPacketType)); deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
//identify the server //identify the server
deserializeCopy(&buffer, name, PACKET_STRING_SIZE); deserialCopy(&buffer, packet->name, PACKET_STRING_SIZE);
deserializeCopy(&buffer, &playerCount, sizeof(int)); deserialCopy(&buffer, &packet->playerCount, sizeof(int));
deserializeCopy(&buffer, &version, sizeof(int)); deserialCopy(&buffer, &packet->version, sizeof(int));
} }
+5 -21
View File
@@ -24,30 +24,14 @@
#include "serial_packet_base.hpp" #include "serial_packet_base.hpp"
#include <cstring> struct ServerPacket : SerialPacketBase {
class ServerPacket : public SerialPacketBase {
public:
ServerPacket() {}
~ServerPacket() {}
const char* SetName(const char* s)
{ return strncpy(name, s, PACKET_STRING_SIZE); }
int SetPlayerCount(int i) { return playerCount = i; }
int SetVersion(int i) { return version = i; }
const char* GetName() { return name; }
int GetPlayerCount() { return playerCount; }
int GetVersion() { return version; }
virtual void Serialize(void* buffer) override;
virtual void Deserialize(void* buffer) override;
private:
//identify the server //identify the server
char name[PACKET_STRING_SIZE+1]; char name[PACKET_STRING_SIZE];
int playerCount; int playerCount;
int version; int version;
}; };
void serializeServer(void* buffer, ServerPacket* packet);
void deserializeServer(void* buffer, ServerPacket* packet);
#endif #endif
+2 -7
View File
@@ -22,10 +22,6 @@
#ifndef SERIALPACKET_HPP_ #ifndef SERIALPACKET_HPP_
#define SERIALPACKET_HPP_ #define SERIALPACKET_HPP_
/* DOCS: serial_packet.hpp is used to define a number of required constants.
* These are used extensively by the server and client
*/
#include "serial_packet_base.hpp" #include "serial_packet_base.hpp"
#include "character_packet.hpp" #include "character_packet.hpp"
#include "client_packet.hpp" #include "client_packet.hpp"
@@ -38,15 +34,14 @@ typedef SerialPacketBase SerialPacket;
//DOCS: NETWORK_VERSION is used to discern compatible servers and clients //DOCS: NETWORK_VERSION is used to discern compatible servers and clients
constexpr int NETWORK_VERSION = 20140831; constexpr int NETWORK_VERSION = 20140831;
//_MaxPacket Should not be used union MaxPacket {
union _MaxPacket {
CharacterPacket a; CharacterPacket a;
ClientPacket b; ClientPacket b;
RegionPacket c; RegionPacket c;
ServerPacket d; ServerPacket d;
}; };
constexpr int MAX_PACKET_SIZE = sizeof(_MaxPacket); constexpr int MAX_PACKET_SIZE = sizeof(MaxPacket);
/* DOCS: PACKET_BUFFER_SIZE is the memory required to store serialized data /* DOCS: PACKET_BUFFER_SIZE is the memory required to store serialized data
* DOCS: SerialPacketType::REGION_CONTENT is currently the largest packet type * DOCS: SerialPacketType::REGION_CONTENT is currently the largest packet type
+28 -67
View File
@@ -21,22 +21,33 @@
*/ */
#include "serial_utility.hpp" #include "serial_utility.hpp"
#include "serial_packet_type.hpp" //packet types
#include "character_packet.hpp"
#include "server_packet.hpp"
#include "client_packet.hpp" #include "client_packet.hpp"
#include "region_packet.hpp" #include "region_packet.hpp"
#include "character_packet.hpp" #include "server_packet.hpp"
#include <cstring> #include <cstring>
void serializePacket(SerialPacketBase* packet, void* data) { //raw memory copy
switch(packet->GetType()) { void serialCopy(void** buffer, void* data, int size) {
memcpy(*buffer, data, size);
*buffer = reinterpret_cast<char*>(*buffer) + size;
}
void deserialCopy(void** buffer, void* data, int size) {
memcpy(data, *buffer, size);
*buffer = reinterpret_cast<char*>(*buffer) + size;
}
//main switch functions
void serializePacket(void* buffer, SerialPacketBase* packet) {
switch(packet->type) {
case SerialPacketType::PING: case SerialPacketType::PING:
case SerialPacketType::PONG: case SerialPacketType::PONG:
case SerialPacketType::BROADCAST_REQUEST: case SerialPacketType::BROADCAST_REQUEST:
case SerialPacketType::BROADCAST_RESPONSE: case SerialPacketType::BROADCAST_RESPONSE:
static_cast<ServerPacket*>(packet)->Serialize(data); serializeServer(buffer, static_cast<ServerPacket*>(packet));
break; break;
case SerialPacketType::JOIN_REQUEST: case SerialPacketType::JOIN_REQUEST:
case SerialPacketType::JOIN_RESPONSE: case SerialPacketType::JOIN_RESPONSE:
@@ -44,11 +55,11 @@ void serializePacket(SerialPacketBase* packet, void* data) {
case SerialPacketType::SYNCHRONIZE: case SerialPacketType::SYNCHRONIZE:
case SerialPacketType::DISCONNECT: case SerialPacketType::DISCONNECT:
case SerialPacketType::SHUTDOWN: case SerialPacketType::SHUTDOWN:
static_cast<ClientPacket*>(packet)->Serialize(data); serializeClient(buffer, static_cast<ClientPacket*>(packet));
break; break;
case SerialPacketType::REGION_REQUEST: case SerialPacketType::REGION_REQUEST:
case SerialPacketType::REGION_CONTENT: case SerialPacketType::REGION_CONTENT:
static_cast<RegionPacket*>(packet)->Serialize(data); serializeRegion(buffer, static_cast<RegionPacket*>(packet));
break; break;
case SerialPacketType::CHARACTER_NEW: case SerialPacketType::CHARACTER_NEW:
case SerialPacketType::CHARACTER_DELETE: case SerialPacketType::CHARACTER_DELETE:
@@ -56,22 +67,22 @@ void serializePacket(SerialPacketBase* packet, void* data) {
case SerialPacketType::CHARACTER_STATS_REQUEST: case SerialPacketType::CHARACTER_STATS_REQUEST:
case SerialPacketType::CHARACTER_STATS_RESPONSE: case SerialPacketType::CHARACTER_STATS_RESPONSE:
case SerialPacketType::CHARACTER_REJECTION: case SerialPacketType::CHARACTER_REJECTION:
static_cast<CharacterPacket*>(packet)->Serialize(data); serializeCharacter(buffer, static_cast<CharacterPacket*>(packet));
break; break;
} }
} }
void deserializePacket(SerialPacketBase* packet, void* data) { void deserializePacket(void* buffer, SerialPacketBase* packet) {
//get the type //find the type, so that you can actually deserialize the packet!
SerialPacketType type; SerialPacketType type;
memcpy(&type, data, sizeof(SerialPacketType)); memcpy(&type, buffer, sizeof(SerialPacketType));
switch(type) { switch(type) {
case SerialPacketType::PING: case SerialPacketType::PING:
case SerialPacketType::PONG: case SerialPacketType::PONG:
case SerialPacketType::BROADCAST_REQUEST: case SerialPacketType::BROADCAST_REQUEST:
case SerialPacketType::BROADCAST_RESPONSE: case SerialPacketType::BROADCAST_RESPONSE:
static_cast<ServerPacket*>(packet)->Deserialize(data); deserializeServer(buffer, static_cast<ServerPacket*>(packet));
break; break;
case SerialPacketType::JOIN_REQUEST: case SerialPacketType::JOIN_REQUEST:
case SerialPacketType::JOIN_RESPONSE: case SerialPacketType::JOIN_RESPONSE:
@@ -79,11 +90,11 @@ void deserializePacket(SerialPacketBase* packet, void* data) {
case SerialPacketType::SYNCHRONIZE: case SerialPacketType::SYNCHRONIZE:
case SerialPacketType::DISCONNECT: case SerialPacketType::DISCONNECT:
case SerialPacketType::SHUTDOWN: case SerialPacketType::SHUTDOWN:
static_cast<ClientPacket*>(packet)->Deserialize(data); deserializeClient(buffer, static_cast<ClientPacket*>(packet));
break; break;
case SerialPacketType::REGION_REQUEST: case SerialPacketType::REGION_REQUEST:
case SerialPacketType::REGION_CONTENT: case SerialPacketType::REGION_CONTENT:
static_cast<RegionPacket*>(packet)->Deserialize(data); deserializeRegion(buffer, static_cast<RegionPacket*>(packet));
break; break;
case SerialPacketType::CHARACTER_NEW: case SerialPacketType::CHARACTER_NEW:
case SerialPacketType::CHARACTER_DELETE: case SerialPacketType::CHARACTER_DELETE:
@@ -91,57 +102,7 @@ void deserializePacket(SerialPacketBase* packet, void* data) {
case SerialPacketType::CHARACTER_STATS_REQUEST: case SerialPacketType::CHARACTER_STATS_REQUEST:
case SerialPacketType::CHARACTER_STATS_RESPONSE: case SerialPacketType::CHARACTER_STATS_RESPONSE:
case SerialPacketType::CHARACTER_REJECTION: case SerialPacketType::CHARACTER_REJECTION:
static_cast<CharacterPacket*>(packet)->Deserialize(data); deserializeCharacter(buffer, static_cast<CharacterPacket*>(packet));
break; break;
} }
} }
void serializeCopy(void** bufferHead, void* data, int size) {
memcpy(*bufferHead, data, size);
(*bufferHead) = static_cast<void*>(static_cast<char*>(*bufferHead) + size);
}
void deserializeCopy(void** bufferHead, void* data, int size) {
memcpy(data, *bufferHead, size);
(*bufferHead) = static_cast<void*>(static_cast<char*>(*bufferHead) + size);
}
void serializeCopyStatistics(void** bufferHead, Statistics* stats) {
//integers
serializeCopy(bufferHead, &stats->level, sizeof(int));
serializeCopy(bufferHead, &stats->exp, sizeof(int));
serializeCopy(bufferHead, &stats->maxHP, sizeof(int));
serializeCopy(bufferHead, &stats->health, sizeof(int));
serializeCopy(bufferHead, &stats->maxMP, sizeof(int));
serializeCopy(bufferHead, &stats->mana, sizeof(int));
serializeCopy(bufferHead, &stats->attack, sizeof(int));
serializeCopy(bufferHead, &stats->defence, sizeof(int));
serializeCopy(bufferHead, &stats->intelligence, sizeof(int));
serializeCopy(bufferHead, &stats->resistance, sizeof(int));
serializeCopy(bufferHead, &stats->speed, sizeof(int));
//floats
serializeCopy(bufferHead, &stats->accuracy, sizeof(float));
serializeCopy(bufferHead, &stats->evasion, sizeof(float));
serializeCopy(bufferHead, &stats->luck, sizeof(float));
}
void deserializeCopyStatistics(void** bufferHead, Statistics* stats) {
//integers
deserializeCopy(bufferHead, &stats->level, sizeof(int));
deserializeCopy(bufferHead, &stats->exp, sizeof(int));
deserializeCopy(bufferHead, &stats->maxHP, sizeof(int));
deserializeCopy(bufferHead, &stats->health, sizeof(int));
deserializeCopy(bufferHead, &stats->maxMP, sizeof(int));
deserializeCopy(bufferHead, &stats->mana, sizeof(int));
deserializeCopy(bufferHead, &stats->attack, sizeof(int));
deserializeCopy(bufferHead, &stats->defence, sizeof(int));
deserializeCopy(bufferHead, &stats->intelligence, sizeof(int));
deserializeCopy(bufferHead, &stats->resistance, sizeof(int));
deserializeCopy(bufferHead, &stats->speed, sizeof(int));
//floats
deserializeCopy(bufferHead, &stats->accuracy, sizeof(float));
deserializeCopy(bufferHead, &stats->evasion, sizeof(float));
deserializeCopy(bufferHead, &stats->luck, sizeof(float));
}
+7 -12
View File
@@ -24,19 +24,14 @@
#include "serial_packet_base.hpp" #include "serial_packet_base.hpp"
#include "statistics.hpp" #include <cstring>
//NOTE: The naming conventions here are fucking terrible //raw memory copy
void serialCopy(void** buffer, void* data, int size);
void deserialCopy(void** buffer, void* data, int size);
//BUGFIX: There's really no way to escape this :( //primary functions
void serializePacket(SerialPacketBase* packet, void* data); void serializePacket(void* buffer, SerialPacketBase* packet);
void deserializePacket(SerialPacketBase* packet, void* data); void deserializePacket(void* buffer, SerialPacketBase* packet);
//raw memcpy
void serializeCopy(void** bufferHead, void* data, int size);
void deserializeCopy(void** bufferHead, void* data, int size);
void serializeCopyStatistics(void** bufferHead, Statistics* stats);
void deserializeCopyStatistics(void** bufferHead, Statistics* stats);
#endif #endif
+13 -15
View File
@@ -21,12 +21,13 @@
*/ */
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "serial_utility.hpp" #include "serial_utility.hpp"
#include <stdexcept> #include <stdexcept>
//BUGFIX: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network //BUGFIX: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network
//NOTE: don't confuse SerialPacket with UDPpacket //NOTE: don't confuse SerialPacketBase with UDPpacket
void UDPNetworkUtility::Open(int port) { void UDPNetworkUtility::Open(int port) {
socket = SDLNet_UDP_Open(port); socket = SDLNet_UDP_Open(port);
@@ -152,10 +153,10 @@ int UDPNetworkUtility::Receive() {
} }
//------------------------- //-------------------------
//send a SerialPacket //send a SerialPacketBase
//------------------------- //-------------------------
int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* serialPacket) { int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacketBase* serialPacket) {
IPaddress add; IPaddress add;
if (SDLNet_ResolveHost(&add, ip, port) == -1) { if (SDLNet_ResolveHost(&add, ip, port) == -1) {
throw(std::runtime_error("Failed to resolve a host")); throw(std::runtime_error("Failed to resolve a host"));
@@ -164,9 +165,9 @@ int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* serialPack
SendTo(&add, serialPacket); SendTo(&add, serialPacket);
} }
int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) { int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacketBase* serialPacket) {
memset(packet->data, 0, packet->maxlen); memset(packet->data, 0, packet->maxlen);
serializePacket(serialPacket, packet->data); serializePacket(packet->data, serialPacket);
packet->len = PACKET_BUFFER_SIZE; packet->len = PACKET_BUFFER_SIZE;
packet->address = *add; packet->address = *add;
@@ -179,9 +180,9 @@ int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) {
return ret; return ret;
} }
int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) { int UDPNetworkUtility::SendTo(int channel, SerialPacketBase* serialPacket) {
memset(packet->data, 0, packet->maxlen); memset(packet->data, 0, packet->maxlen);
serializePacket(serialPacket, packet->data); serializePacket(packet->data, serialPacket);
packet->len = PACKET_BUFFER_SIZE; packet->len = PACKET_BUFFER_SIZE;
int ret = SDLNet_UDP_Send(socket, channel, packet); int ret = SDLNet_UDP_Send(socket, channel, packet);
@@ -193,9 +194,9 @@ int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) {
return ret; return ret;
} }
int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) { int UDPNetworkUtility::SendToAllChannels(SerialPacketBase* serialPacket) {
memset(packet->data, 0, packet->maxlen); memset(packet->data, 0, packet->maxlen);
serializePacket(serialPacket, packet->data); serializePacket(packet->data, serialPacket);
packet->len = PACKET_BUFFER_SIZE; packet->len = PACKET_BUFFER_SIZE;
int sent = 0; int sent = 0;
@@ -210,14 +211,11 @@ int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) {
return sent; return sent;
} }
int UDPNetworkUtility::Receive(SerialPacket* serialPacket) { int UDPNetworkUtility::Receive(SerialPacketBase* serialPacket) {
memset(packet->data, 0, packet->maxlen); memset(packet->data, 0, packet->maxlen);
int ret = SDLNet_UDP_Recv(socket, packet); int ret = SDLNet_UDP_Recv(socket, packet);
if (ret > 0) { deserializePacket(packet->data, serialPacket);
//BUG: This simply fails serialPacket->srcAddress = packet->address;
deserializePacket(serialPacket, packet->data);
serialPacket->srcAddress = packet->address;
}
if (ret < 0) { if (ret < 0) {
throw(std::runtime_error("Unknown network error occured")); throw(std::runtime_error("Unknown network error occured"));
+7 -7
View File
@@ -23,7 +23,7 @@
#define UDPNETWORKUTILITY_HPP_ #define UDPNETWORKUTILITY_HPP_
//common //common
#include "serial_packet.hpp" #include "serial_packet_base.hpp"
#include "singleton.hpp" #include "singleton.hpp"
//APIs //APIs
@@ -50,12 +50,12 @@ public:
int SendToAllChannels(void* data, int len); int SendToAllChannels(void* data, int len);
int Receive(); int Receive();
//send a SerialPacket //send a SerialPacketBase
int SendTo(const char* ip, int port, SerialPacket* serialPacket); int SendTo(const char* ip, int port, SerialPacketBase* serialPacket);
int SendTo(IPaddress* add, SerialPacket* serialPacket); int SendTo(IPaddress* add, SerialPacketBase* serialPacket);
int SendTo(int channel, SerialPacket* serialPacket); int SendTo(int channel, SerialPacketBase* serialPacket);
int SendToAllChannels(SerialPacket* serialPacket); int SendToAllChannels(SerialPacketBase* serialPacket);
int Receive(SerialPacket* serialPacket); int Receive(SerialPacketBase* serialPacket);
//accessors //accessors
UDPpacket* GetPacket() const { UDPpacket* GetPacket() const {
+1
View File
@@ -30,6 +30,7 @@
//common utilities //common utilities
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
#include "singleton.hpp" #include "singleton.hpp"
+2 -4
View File
@@ -21,8 +21,6 @@
*/ */
#include "server_application.hpp" #include "server_application.hpp"
#include "serial_packet.hpp"
//utility functions //utility functions
#include "sql_utility.hpp" #include "sql_utility.hpp"
#include "utility.hpp" #include "utility.hpp"
@@ -178,7 +176,7 @@ void ServerApplication::Quit() {
//------------------------- //-------------------------
void ServerApplication::HandlePacket(SerialPacket* const argPacket) { void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
switch(argPacket->GetType()) { switch(argPacket->type) {
//basic connections //basic connections
case SerialPacketType::BROADCAST_REQUEST: case SerialPacketType::BROADCAST_REQUEST:
HandleBroadcastRequest(static_cast<SerialPacket*>(argPacket)); HandleBroadcastRequest(static_cast<SerialPacket*>(argPacket));
@@ -224,7 +222,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->GetType())); msg += to_string_custom(static_cast<int>(argPacket->type));
throw(std::runtime_error(msg)); throw(std::runtime_error(msg));
} }
break; break;
+43 -43
View File
@@ -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.SetType(SerialPacketType::BROADCAST_RESPONSE); newPacket.type = SerialPacketType::BROADCAST_RESPONSE;
newPacket.SetName(config["server.name"].c_str()); strncpy(newPacket.name, config["server.name"].c_str(), PACKET_STRING_SIZE);
newPacket.SetPlayerCount(characterMgr.GetContainer()->size()); newPacket.playerCount = characterMgr.GetContainer()->size();
newPacket.SetVersion(NETWORK_VERSION); newPacket.version = NETWORK_VERSION;
network.SendTo(argPacket->GetAddressPtr(), static_cast<SerialPacket*>(&newPacket)); network.SendTo(&argPacket->srcAddress, 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->GetAddress(); newClient.address = argPacket->srcAddress;
//load the user account //load the user account
//TODO: handle passwords //TODO: handle passwords
int accountIndex = accountMgr.LoadAccount(argPacket->GetUsername(), clientIndex); int accountIndex = accountMgr.LoadAccount(argPacket->username, 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.SetType(SerialPacketType::JOIN_RESPONSE); newPacket.type = SerialPacketType::JOIN_RESPONSE;
newPacket.SetClientIndex(clientIndex); newPacket.clientIndex = clientIndex;
newPacket.SetAccountIndex(accountIndex); newPacket.accountIndex = 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->GetAccountIndex())->GetClientIndex() ].address, &clientMap[ accountMgr.GetAccount(argPacket->accountIndex)->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->GetAccountIndex() == it->second.GetOwner()) { if (argPacket->accountIndex == 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->GetAccountIndex())->GetClientIndex()); clientMap.erase(accountMgr.GetAccount(argPacket->accountIndex)->GetClientIndex());
accountMgr.UnloadAccount(argPacket->GetAccountIndex()); accountMgr.UnloadAccount(argPacket->accountIndex);
//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
ServerPacket newPacket; SerialPacket newPacket;
newPacket.SetType(SerialPacketType::DISCONNECT); newPacket.type = 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.SetType(SerialPacketType::REGION_CONTENT); newPacket.type = SerialPacketType::REGION_CONTENT;
newPacket.SetRoomIndex(argPacket->GetRoomIndex()); newPacket.roomIndex = argPacket->roomIndex;
newPacket.SetX(argPacket->GetX()); newPacket.x = argPacket->x;
newPacket.SetY(argPacket->GetY()); newPacket.y = argPacket->y;
newPacket.SetRegion(roomMgr.GetRoom(argPacket->GetRoomIndex())->GetPager()->GetRegion(argPacket->GetX(), argPacket->GetY() )); newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y);
//send the content //send the content
network.SendTo(argPacket->GetAddressPtr(), static_cast<SerialPacket*>(&newPacket)); network.SendTo(&argPacket->srcAddress, 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->GetAccountIndex(), argPacket->GetHandle(), argPacket->GetAvatar()); int characterIndex = characterMgr.LoadCharacter(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
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.SetType(SerialPacketType::CHARACTER_NEW); newPacket.type = 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->GetAccountIndex(), argPacket->GetHandle(), argPacket->GetAvatar()); int characterIndex = characterMgr.LoadCharacter(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
//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->GetCharacterIndex()); CharacterData* character = characterMgr.GetCharacter(argPacket->characterIndex);
//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->GetRoomIndex()); character->SetRoomIndex(argPacket->roomIndex);
character->SetOrigin(argPacket->GetOrigin()); character->SetOrigin(argPacket->origin);
character->SetMotion(argPacket->GetMotion()); character->SetMotion(argPacket->motion);
*character->GetBaseStats() = *argPacket->GetStatistics(); *character->GetBaseStats() = argPacket->stats;
//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->GetClientIndex()]; ClientData& client = clientMap[argPacket->clientIndex];
//send all characters //send all characters
CharacterPacket newPacket; CharacterPacket newPacket;
newPacket.SetType(SerialPacketType::CHARACTER_UPDATE); newPacket.type = SerialPacketType::CHARACTER_UPDATE;
for (auto& it : *characterMgr.GetContainer()) { for (auto& it : *characterMgr.GetContainer()) {
newPacket.SetCharacterIndex(it.first); newPacket.characterIndex = 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.SetType(SerialPacketType::CHARACTER_DELETE); newPacket.type = SerialPacketType::CHARACTER_DELETE;
newPacket.SetCharacterIndex(uid); newPacket.characterIndex = 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->SetCharacterIndex(characterIndex); packet->characterIndex = characterIndex;
packet->SetHandle(character->GetHandle().c_str()); strncpy(packet->handle, character->GetHandle().c_str(), PACKET_STRING_SIZE);
packet->SetAvatar(character->GetAvatar().c_str()); strncpy(packet->avatar, character->GetAvatar().c_str(), PACKET_STRING_SIZE);
packet->SetAccountIndex(character->GetOwner()); packet->accountIndex = character->GetOwner();
packet->SetRoomIndex(character->GetRoomIndex()); packet->roomIndex = character->GetRoomIndex();
packet->SetOrigin(character->GetOrigin()); packet->origin = character->GetOrigin();
packet->SetMotion(character->GetMotion()); packet->motion = character->GetMotion();
*packet->GetStatistics() = *character->GetBaseStats(); packet->stats = *character->GetBaseStats();
} }
+1 -1
View File
@@ -1,4 +1,4 @@
TODO: Reduce the verbosity of the network packets TODO: Sort out the *_cast<> stuff
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