Compare commits

..

13 Commits

Author SHA1 Message Date
Kayne Ruse 59963a05f3 Tried, failed. I give up 2014-08-31 04:29:40 +10:00
Kayne Ruse 7d4d7817f2 I've adjusted the naming conventions for the serial code (read more)
It looks like this whole branch is fucking useless, considering that I'll
need to reimplement that massive switch statement again just to determine
which overwritten method to use. I might as well not have bothered.
2014-08-31 02:58:47 +10:00
Kayne Ruse 1c1b1e0a1f I'm trying something to fix this stupid segfault 2014-08-30 22:27:52 +10:00
Kayne Ruse 895671e30f Fixed reference error 2014-08-30 21:36:25 +10:00
Kayne Ruse cfe82c0625 BUG: Receive() is failing 2014-08-28 22:28:37 +10:00
Kayne Ruse b5ca9dc729 Updated the client to use the packet methods 2014-08-28 22:05:21 +10:00
Kayne Ruse 164247de4f Updated the server to use the packet methods 2014-08-28 21:48:29 +10:00
Kayne Ruse ac799bc583 Finished tedious encapsulation of the packet classes 2014-08-27 20:35:04 +10:00
Kayne Ruse b8bd5f9cea Changed the internal serialization conventions 2014-08-27 16:04:14 +10:00
Kayne Ruse 4b5194918b Encapsulated SerialPacket, and made adjustments to accomodate it 2014-08-27 15:35:04 +10:00
Kayne Ruse 426c3a52c2 Made a few more adjustments, the file structure should be correct now 2014-08-27 14:57:33 +10:00
Kayne Ruse 16b2a60373 Renamed serial source files 2014-08-27 14:52:36 +10:00
Kayne Ruse 6cdc3080a2 Moved packet and serial files into the same directory 2014-08-27 14:38:13 +10:00
26 changed files with 404 additions and 350 deletions
-1
View File
@@ -21,7 +21,6 @@
*/
#include "client_application.hpp"
#include "serial_packet.hpp"
#include "config_utility.hpp"
#include <stdexcept>
+42 -42
View File
@@ -269,7 +269,7 @@ void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
//-------------------------
void InWorld::HandlePacket(SerialPacket* const argPacket) {
switch(argPacket->type) {
switch(argPacket->GetType()) {
case SerialPacketType::DISCONNECT:
HandleDisconnect(argPacket);
break;
@@ -287,7 +287,7 @@ void InWorld::HandlePacket(SerialPacket* const argPacket) {
break;
//handle errors
default:
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InWorld: " + to_string_custom(static_cast<int>(argPacket->type)) ));
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InWorld: " + to_string_custom(static_cast<int>(argPacket->GetType())) ));
break;
}
}
@@ -297,21 +297,21 @@ void InWorld::HandleDisconnect(SerialPacket* const argPacket) {
}
void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) {
if (characterMap.find(argPacket->characterIndex) != characterMap.end()) {
if (characterMap.find(argPacket->GetCharacterIndex()) != characterMap.end()) {
throw(std::runtime_error("Cannot create duplicate characters"));
}
//create the character object
Character& newCharacter = characterMap[argPacket->characterIndex];
Character& newCharacter = characterMap[argPacket->GetCharacterIndex()];
//fill out the character's members
newCharacter.SetHandle(argPacket->handle);
newCharacter.SetAvatar(argPacket->avatar);
newCharacter.SetHandle(argPacket->GetHandle());
newCharacter.SetAvatar(argPacket->GetAvatar());
newCharacter.GetSprite()->LoadSurface(ConfigUtility::GetSingleton()["dir.sprites"] + newCharacter.GetAvatar(), 4, 4);
newCharacter.SetOrigin(argPacket->origin);
newCharacter.SetMotion(argPacket->motion);
newCharacter.SetOrigin(argPacket->GetOrigin());
newCharacter.SetMotion(argPacket->GetMotion());
newCharacter.SetBounds({
CHARACTER_BOUNDS_X,
CHARACTER_BOUNDS_Y,
@@ -319,14 +319,14 @@ void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) {
CHARACTER_BOUNDS_HEIGHT
});
(*newCharacter.GetStats()) = argPacket->stats;
*newCharacter.GetStats() = *argPacket->GetStatistics();
//bookkeeping code
newCharacter.CorrectSprite();
//catch this client's player object
if (argPacket->accountIndex == accountIndex && !localCharacter) {
characterIndex = argPacket->characterIndex;
if (argPacket->GetAccountIndex() == accountIndex && !localCharacter) {
characterIndex = argPacket->GetCharacterIndex();
localCharacter = &newCharacter;
//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)
//catch this client's player object
if (argPacket->characterIndex == characterIndex) {
if (argPacket->GetCharacterIndex() == characterIndex) {
characterIndex = -1;
localCharacter = nullptr;
}
characterMap.erase(argPacket->characterIndex);
characterMap.erase(argPacket->GetCharacterIndex());
}
void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) {
if (characterMap.find(argPacket->characterIndex) == characterMap.end()) {
if (characterMap.find(argPacket->GetCharacterIndex()) == characterMap.end()) {
std::cout << "Warning: HandleCharacterUpdate() is passing to HandleCharacterNew()" << std::endl;
HandleCharacterNew(argPacket);
return;
}
Character& character = characterMap[argPacket->characterIndex];
Character& character = characterMap[argPacket->GetCharacterIndex()];
//other characters moving
if (argPacket->characterIndex != characterIndex) {
character.SetOrigin(argPacket->origin);
character.SetMotion(argPacket->motion);
if (argPacket->GetCharacterIndex() != characterIndex) {
character.SetOrigin(argPacket->GetOrigin());
character.SetMotion(argPacket->GetMotion());
character.CorrectSprite();
}
}
void InWorld::HandleRegionContent(RegionPacket* const argPacket) {
//replace existing regions
regionPager.UnloadRegion(argPacket->x, argPacket->y);
regionPager.PushRegion(argPacket->region);
regionPager.UnloadRegion(argPacket->GetX(), argPacket->GetY());
regionPager.PushRegion(argPacket->GetRegion());
//clean up after the serial code
delete argPacket->region;
argPacket->region = nullptr;
delete argPacket->GetRegion();
argPacket->SetRegion(nullptr);
}
//-------------------------
@@ -387,9 +387,9 @@ void InWorld::RequestSynchronize() {
ClientPacket newPacket;
//request a sync
newPacket.type = SerialPacketType::SYNCHRONIZE;
newPacket.clientIndex = clientIndex;
newPacket.accountIndex = accountIndex;
newPacket.SetType(SerialPacketType::SYNCHRONIZE);
newPacket.SetClientIndex(clientIndex);
newPacket.SetAccountIndex(accountIndex);
//TODO: location, range for sync request
@@ -400,15 +400,15 @@ void InWorld::SendPlayerUpdate() {
CharacterPacket newPacket;
//pack the packet
newPacket.type = SerialPacketType::CHARACTER_UPDATE;
newPacket.SetType(SerialPacketType::CHARACTER_UPDATE);
newPacket.characterIndex = characterIndex;
newPacket.SetCharacterIndex(characterIndex);
//NOTE: omitting the handle and avatar here
newPacket.accountIndex = accountIndex;
newPacket.roomIndex = 0; //TODO: room index
newPacket.origin = localCharacter->GetOrigin();
newPacket.motion = localCharacter->GetMotion();
newPacket.stats = *localCharacter->GetStats();
newPacket.SetAccountIndex(accountIndex);
newPacket.SetRoomIndex(0); //TODO: room index
newPacket.SetOrigin(localCharacter->GetOrigin());
newPacket.SetMotion(localCharacter->GetMotion());
*newPacket.GetStatistics() = *localCharacter->GetStats();
//TODO: gameplay components: equipment, items, buffs, debuffs
@@ -419,9 +419,9 @@ void InWorld::RequestDisconnect() {
ClientPacket newPacket;
//send a disconnect request
newPacket.type = SerialPacketType::DISCONNECT;
newPacket.clientIndex = clientIndex;
newPacket.accountIndex = accountIndex;
newPacket.SetType(SerialPacketType::DISCONNECT);
newPacket.SetClientIndex(clientIndex);
newPacket.SetAccountIndex(accountIndex);
network.SendTo(Channels::SERVER, &newPacket);
}
@@ -430,9 +430,9 @@ void InWorld::RequestShutDown() {
ClientPacket newPacket;
//send a shutdown request
newPacket.type = SerialPacketType::SHUTDOWN;
newPacket.clientIndex = clientIndex;
newPacket.accountIndex = accountIndex;
newPacket.SetType(SerialPacketType::SHUTDOWN);
newPacket.SetClientIndex(clientIndex);
newPacket.SetAccountIndex(accountIndex);
network.SendTo(Channels::SERVER, &newPacket);
}
@@ -441,10 +441,10 @@ void InWorld::RequestRegion(int roomIndex, int x, int y) {
RegionPacket packet;
//pack the region's data
packet.type = SerialPacketType::REGION_REQUEST;
packet.roomIndex = roomIndex;
packet.x = x;
packet.y = y;
packet.SetType(SerialPacketType::REGION_REQUEST);
packet.SetRoomIndex(roomIndex);
packet.SetX(x);
packet.SetY(y);
network.SendTo(Channels::SERVER, &packet);
}
-1
View File
@@ -27,7 +27,6 @@
//networking
#include "udp_network_utility.hpp"
#include "serial_packet.hpp"
//graphics
#include "image.hpp"
+17 -17
View File
@@ -185,7 +185,7 @@ void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) {
//-------------------------
void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
switch(argPacket->type) {
switch(argPacket->GetType()) {
case SerialPacketType::BROADCAST_RESPONSE:
HandleBroadcastResponse(static_cast<ServerPacket*>(argPacket));
break;
@@ -194,7 +194,7 @@ void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
break;
//handle errors
default:
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in LobbyMenu: " + to_string_custom(static_cast<int>(argPacket->type)) ));
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in LobbyMenu: " + to_string_custom(static_cast<int>(argPacket->GetType())) ));
break;
}
}
@@ -202,10 +202,10 @@ void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
//extract the data
ServerInformation server;
server.address = argPacket->srcAddress;
server.name = argPacket->name;
server.playerCount = argPacket->playerCount;
server.version = argPacket->version;
server.address = argPacket->GetAddress();
server.name = argPacket->GetName();
server.playerCount = argPacket->GetPlayerCount();
server.version = argPacket->GetVersion();
//Checking compatibility
server.compatible = server.version == NETWORK_VERSION;
@@ -215,17 +215,17 @@ void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
}
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
clientIndex = argPacket->clientIndex;
accountIndex = argPacket->accountIndex;
network.Bind(&argPacket->srcAddress, Channels::SERVER);
clientIndex = argPacket->GetClientIndex();
accountIndex = argPacket->GetAccountIndex();
network.Bind(argPacket->GetAddressPtr(), Channels::SERVER);
SetNextScene(SceneList::INWORLD);
//send this player's character info
CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_NEW;
strncpy(newPacket.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE);
strncpy(newPacket.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE);
newPacket.accountIndex = accountIndex;
newPacket.SetType(SerialPacketType::CHARACTER_NEW);
newPacket.SetHandle(config["client.handle"].c_str());
newPacket.SetAvatar(config["client.avatar"].c_str());
newPacket.SetAccountIndex(accountIndex);
network.SendTo(Channels::SERVER, &newPacket);
}
@@ -235,8 +235,8 @@ void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
void LobbyMenu::SendBroadcastRequest() {
//broadcast to the network, or a specific server
ClientPacket packet;
packet.type = SerialPacketType::BROADCAST_REQUEST;
ServerPacket packet;
packet.SetType(SerialPacketType::BROADCAST_REQUEST);
network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet);
//reset the server list
@@ -247,8 +247,8 @@ void LobbyMenu::SendBroadcastRequest() {
void LobbyMenu::SendJoinRequest() {
//pack the packet
ClientPacket packet;
packet.type = SerialPacketType::JOIN_REQUEST;
strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE);
packet.SetType(SerialPacketType::JOIN_REQUEST);
packet.SetUsername(config["client.username"].c_str());
//join the selected server
network.SendTo(&selection->address, &packet);
-1
View File
@@ -31,7 +31,6 @@
//utilities
#include "config_utility.hpp"
#include "udp_network_utility.hpp"
#include "serial_packet.hpp"
//client
#include "base_scene.hpp"
@@ -23,52 +23,50 @@
#include "serial_utility.hpp"
#include "serial_statistics.hpp"
void serializeCharacter(void* buffer, CharacterPacket* packet) {
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
void CharacterPacket::Serialize(void* buffer) {
serializeCopy(&buffer, &type, sizeof(SerialPacketType));
//identify the character
serialCopy(&buffer, &packet->characterIndex, sizeof(int));
serialCopy(&buffer, packet->handle, PACKET_STRING_SIZE);
serialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE);
serializeCopy(&buffer, &characterIndex, sizeof(int));
serializeCopy(&buffer, handle, PACKET_STRING_SIZE);
serializeCopy(&buffer, avatar, PACKET_STRING_SIZE);
//the owner
serialCopy(&buffer, &packet->accountIndex, sizeof(int));
serializeCopy(&buffer, &accountIndex, sizeof(int));
//location
serialCopy(&buffer, &packet->roomIndex, sizeof(int));
serialCopy(&buffer, &packet->origin.x, sizeof(double));
serialCopy(&buffer, &packet->origin.y, sizeof(double));
serialCopy(&buffer, &packet->motion.x, sizeof(double));
serialCopy(&buffer, &packet->motion.y, sizeof(double));
serializeCopy(&buffer, &roomIndex, sizeof(int));
serializeCopy(&buffer, &origin.x, sizeof(double));
serializeCopy(&buffer, &origin.y, sizeof(double));
serializeCopy(&buffer, &motion.x, sizeof(double));
serializeCopy(&buffer, &motion.y, sizeof(double));
//stats structure
serializeStatistics(&buffer, &packet->stats);
serializeCopyStatistics(&buffer, &stats);
//TODO: gameplay components: equipment, items, buffs, debuffs
}
void deserializeCharacter(void* buffer, CharacterPacket* packet) {
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
void CharacterPacket::Deserialize(void* buffer) {
deserializeCopy(&buffer, &type, sizeof(SerialPacketType));
//identify the character
deserialCopy(&buffer, &packet->characterIndex, sizeof(int));
deserialCopy(&buffer, packet->handle, PACKET_STRING_SIZE);
deserialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE);
deserializeCopy(&buffer, &characterIndex, sizeof(int));
deserializeCopy(&buffer, handle, PACKET_STRING_SIZE);
deserializeCopy(&buffer, avatar, PACKET_STRING_SIZE);
//the owner
deserialCopy(&buffer, &packet->accountIndex, sizeof(int));
deserializeCopy(&buffer, &accountIndex, sizeof(int));
//location
deserialCopy(&buffer, &packet->roomIndex, sizeof(int));
deserialCopy(&buffer, &packet->origin.x, sizeof(double));
deserialCopy(&buffer, &packet->origin.y, sizeof(double));
deserialCopy(&buffer, &packet->motion.x, sizeof(double));
deserialCopy(&buffer, &packet->motion.y, sizeof(double));
deserializeCopy(&buffer, &roomIndex, sizeof(int));
deserializeCopy(&buffer, &origin.x, sizeof(double));
deserializeCopy(&buffer, &origin.y, sizeof(double));
deserializeCopy(&buffer, &motion.x, sizeof(double));
deserializeCopy(&buffer, &motion.y, sizeof(double));
//stats structure
deserializeStatistics(&buffer, &packet->stats);
deserializeCopyStatistics(&buffer, &stats);
//TODO: gameplay components: equipment, items, buffs, debuffs
}
@@ -27,11 +27,48 @@
#include "vector2.hpp"
#include "statistics.hpp"
struct CharacterPacket : SerialPacketBase {
#include <cstring>
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
int characterIndex;
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
char handle[PACKET_STRING_SIZE+1];
char avatar[PACKET_STRING_SIZE+1];
//the owner
int accountIndex;
@@ -47,7 +84,4 @@ struct CharacterPacket : SerialPacketBase {
//TODO: gameplay components: equipment, items, buffs, debuffs
};
void serializeCharacter(void* buffer, CharacterPacket* packet);
void deserializeCharacter(void* buffer, CharacterPacket* packet);
#endif
+10 -10
View File
@@ -23,18 +23,18 @@
#include "serial_utility.hpp"
void serializeClient(void* buffer, ClientPacket* packet) {
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
void ClientPacket::Serialize(void* buffer) {
serializeCopy(&buffer, &type, sizeof(SerialPacketType));
serialCopy(&buffer, &packet->clientIndex, sizeof(int));
serialCopy(&buffer, &packet->accountIndex, sizeof(int));
serialCopy(&buffer, packet->username, PACKET_STRING_SIZE);
serializeCopy(&buffer, &clientIndex, sizeof(int));
serializeCopy(&buffer, &accountIndex, sizeof(int));
serializeCopy(&buffer, username, PACKET_STRING_SIZE);
}
void deserializeClient(void* buffer, ClientPacket* packet) {
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
void ClientPacket::Deserialize(void* buffer) {
deserializeCopy(&buffer, &type, sizeof(SerialPacketType));
deserialCopy(&buffer, &packet->clientIndex, sizeof(int));
deserialCopy(&buffer, &packet->accountIndex, sizeof(int));
deserialCopy(&buffer, packet->username, PACKET_STRING_SIZE);
deserializeCopy(&buffer, &clientIndex, sizeof(int));
deserializeCopy(&buffer, &accountIndex, sizeof(int));
deserializeCopy(&buffer, username, PACKET_STRING_SIZE);
}
+23 -5
View File
@@ -24,13 +24,31 @@
#include "serial_packet_base.hpp"
struct ClientPacket : SerialPacketBase {
#include <cstring>
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 accountIndex;
char username[PACKET_STRING_SIZE];
char username[PACKET_STRING_SIZE+1];
// char password[PACKET_STRING_SIZE]; //hashed, not currently used
};
void serializeClient(void* buffer, ClientPacket* packet);
void deserializeClient(void* buffer, ClientPacket* packet);
#endif
+17 -17
View File
@@ -23,15 +23,15 @@
#include "serial_utility.hpp"
void serializeRegion(void* buffer, RegionPacket* packet) {
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
void RegionPacket::Serialize(void* buffer) {
serializeCopy(&buffer, &type, sizeof(SerialPacketType));
//format
serialCopy(&buffer, &packet->roomIndex, sizeof(int));
serialCopy(&buffer, &packet->x, sizeof(int));
serialCopy(&buffer, &packet->y, sizeof(int));
serializeCopy(&buffer, &roomIndex, sizeof(int));
serializeCopy(&buffer, &x, sizeof(int));
serializeCopy(&buffer, &y, sizeof(int));
if (packet->type != SerialPacketType::REGION_CONTENT) {
if (type != SerialPacketType::REGION_CONTENT) {
return;
}
@@ -39,41 +39,41 @@ void serializeRegion(void* buffer, RegionPacket* packet) {
for (int i = 0; i < REGION_WIDTH; i++) {
for (int j = 0; j < REGION_HEIGHT; j++) {
for (int k = 0; k < REGION_DEPTH; k++) {
*reinterpret_cast<Region::type_t*>(buffer) = packet->region->GetTile(i, j, k);
*reinterpret_cast<Region::type_t*>(buffer) = region->GetTile(i, j, k);
buffer = reinterpret_cast<char*>(buffer) + sizeof(Region::type_t);
}
}
}
//solids
serialCopy(&buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
serializeCopy(&buffer, region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
}
void deserializeRegion(void* buffer, RegionPacket* packet) {
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
void RegionPacket::Deserialize(void* buffer) {
deserializeCopy(&buffer, &type, sizeof(SerialPacketType));
//format
deserialCopy(&buffer, &packet->roomIndex, sizeof(int));
deserialCopy(&buffer, &packet->x, sizeof(int));
deserialCopy(&buffer, &packet->y, sizeof(int));
deserializeCopy(&buffer, &roomIndex, sizeof(int));
deserializeCopy(&buffer, &x, sizeof(int));
deserializeCopy(&buffer, &y, sizeof(int));
if (packet->type != SerialPacketType::REGION_CONTENT) {
if (type != SerialPacketType::REGION_CONTENT) {
return;
}
//an object to work on
packet->region = new Region(packet->x, packet->y);
region = new Region(x, y);
//tiles
for (int i = 0; i < REGION_WIDTH; i++) {
for (int j = 0; j < REGION_HEIGHT; j++) {
for (int k = 0; k < REGION_DEPTH; k++) {
packet->region->SetTile(i, j, k, *reinterpret_cast<Region::type_t*>(buffer));
region->SetTile(i, j, k, *reinterpret_cast<Region::type_t*>(buffer));
buffer = reinterpret_cast<char*>(buffer) + sizeof(Region::type_t);
}
}
}
//solids
deserialCopy(&buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
deserializeCopy(&buffer, region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
}
+23 -4
View File
@@ -27,13 +27,35 @@
#include "region.hpp"
#include <cmath>
#include <cstring>
//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_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0);
constexpr int REGION_METADATA_FOOTPRINT = sizeof(int) * 3;
struct RegionPacket : SerialPacketBase {
class RegionPacket : public 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
int roomIndex;
int x, y;
@@ -42,7 +64,4 @@ struct RegionPacket : SerialPacketBase {
Region* region;
};
void serializeRegion(void* buffer, RegionPacket* packet);
void deserializeRegion(void* buffer, RegionPacket* packet);
#endif
@@ -1,4 +1,4 @@
/* Copyright: (c) Kayne Ruse 2014
/* Copyright: (c) Kayne Ruse 2013, 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
@@ -21,4 +21,4 @@
*/
#include "serial_packet_base.hpp"
//sanity check
//NOTE: This is a sanity check
@@ -26,14 +26,28 @@
#include "SDL/SDL_net.h"
//The packets use a char array for string storage
constexpr int PACKET_STRING_SIZE = 100;
struct SerialPacketBase {
//members
class SerialPacketBase {
public:
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;
IPaddress srcAddress;
virtual ~SerialPacketBase() {};
};
#endif
@@ -1,64 +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 "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));
}
@@ -1,30 +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.
*/
#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"
void serializeServer(void* buffer, ServerPacket* packet) {
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
void ServerPacket::Serialize(void* buffer) {
serializeCopy(&buffer, &type, sizeof(SerialPacketType));
//identify the server
serialCopy(&buffer, packet->name, PACKET_STRING_SIZE);
serialCopy(&buffer, &packet->playerCount, sizeof(int));
serialCopy(&buffer, &packet->version, sizeof(int));
serializeCopy(&buffer, name, PACKET_STRING_SIZE);
serializeCopy(&buffer, &playerCount, sizeof(int));
serializeCopy(&buffer, &version, sizeof(int));
}
void deserializeServer(void* buffer, ServerPacket* packet) {
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
void ServerPacket::Deserialize(void* buffer) {
deserializeCopy(&buffer, &type, sizeof(SerialPacketType));
//identify the server
deserialCopy(&buffer, packet->name, PACKET_STRING_SIZE);
deserialCopy(&buffer, &packet->playerCount, sizeof(int));
deserialCopy(&buffer, &packet->version, sizeof(int));
deserializeCopy(&buffer, name, PACKET_STRING_SIZE);
deserializeCopy(&buffer, &playerCount, sizeof(int));
deserializeCopy(&buffer, &version, sizeof(int));
}
+21 -5
View File
@@ -24,14 +24,30 @@
#include "serial_packet_base.hpp"
struct ServerPacket : SerialPacketBase {
#include <cstring>
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
char name[PACKET_STRING_SIZE];
char name[PACKET_STRING_SIZE+1];
int playerCount;
int version;
};
void serializeServer(void* buffer, ServerPacket* packet);
void deserializeServer(void* buffer, ServerPacket* packet);
#endif
+7 -2
View File
@@ -22,6 +22,10 @@
#ifndef 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 "character_packet.hpp"
#include "client_packet.hpp"
@@ -34,14 +38,15 @@ typedef SerialPacketBase SerialPacket;
//DOCS: NETWORK_VERSION is used to discern compatible servers and clients
constexpr int NETWORK_VERSION = 20140831;
union MaxPacket {
//_MaxPacket Should not be used
union _MaxPacket {
CharacterPacket a;
ClientPacket b;
RegionPacket c;
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: SerialPacketType::REGION_CONTENT is currently the largest packet type
+68 -29
View File
@@ -21,33 +21,22 @@
*/
#include "serial_utility.hpp"
//packet types
#include "character_packet.hpp"
#include "serial_packet_type.hpp"
#include "server_packet.hpp"
#include "client_packet.hpp"
#include "region_packet.hpp"
#include "server_packet.hpp"
#include "character_packet.hpp"
#include <cstring>
//raw memory copy
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) {
void serializePacket(SerialPacketBase* packet, void* data) {
switch(packet->GetType()) {
case SerialPacketType::PING:
case SerialPacketType::PONG:
case SerialPacketType::BROADCAST_REQUEST:
case SerialPacketType::BROADCAST_RESPONSE:
serializeServer(buffer, static_cast<ServerPacket*>(packet));
static_cast<ServerPacket*>(packet)->Serialize(data);
break;
case SerialPacketType::JOIN_REQUEST:
case SerialPacketType::JOIN_RESPONSE:
@@ -55,11 +44,11 @@ void serializePacket(void* buffer, SerialPacketBase* packet) {
case SerialPacketType::SYNCHRONIZE:
case SerialPacketType::DISCONNECT:
case SerialPacketType::SHUTDOWN:
serializeClient(buffer, static_cast<ClientPacket*>(packet));
static_cast<ClientPacket*>(packet)->Serialize(data);
break;
case SerialPacketType::REGION_REQUEST:
case SerialPacketType::REGION_CONTENT:
serializeRegion(buffer, static_cast<RegionPacket*>(packet));
static_cast<RegionPacket*>(packet)->Serialize(data);
break;
case SerialPacketType::CHARACTER_NEW:
case SerialPacketType::CHARACTER_DELETE:
@@ -67,22 +56,22 @@ void serializePacket(void* buffer, SerialPacketBase* packet) {
case SerialPacketType::CHARACTER_STATS_REQUEST:
case SerialPacketType::CHARACTER_STATS_RESPONSE:
case SerialPacketType::CHARACTER_REJECTION:
serializeCharacter(buffer, static_cast<CharacterPacket*>(packet));
static_cast<CharacterPacket*>(packet)->Serialize(data);
break;
}
}
void deserializePacket(void* buffer, SerialPacketBase* packet) {
//find the type, so that you can actually deserialize the packet!
void deserializePacket(SerialPacketBase* packet, void* data) {
//get the type
SerialPacketType type;
memcpy(&type, buffer, sizeof(SerialPacketType));
memcpy(&type, data, sizeof(SerialPacketType));
switch(type) {
case SerialPacketType::PING:
case SerialPacketType::PONG:
case SerialPacketType::BROADCAST_REQUEST:
case SerialPacketType::BROADCAST_RESPONSE:
deserializeServer(buffer, static_cast<ServerPacket*>(packet));
static_cast<ServerPacket*>(packet)->Deserialize(data);
break;
case SerialPacketType::JOIN_REQUEST:
case SerialPacketType::JOIN_RESPONSE:
@@ -90,11 +79,11 @@ void deserializePacket(void* buffer, SerialPacketBase* packet) {
case SerialPacketType::SYNCHRONIZE:
case SerialPacketType::DISCONNECT:
case SerialPacketType::SHUTDOWN:
deserializeClient(buffer, static_cast<ClientPacket*>(packet));
static_cast<ClientPacket*>(packet)->Deserialize(data);
break;
case SerialPacketType::REGION_REQUEST:
case SerialPacketType::REGION_CONTENT:
deserializeRegion(buffer, static_cast<RegionPacket*>(packet));
static_cast<RegionPacket*>(packet)->Deserialize(data);
break;
case SerialPacketType::CHARACTER_NEW:
case SerialPacketType::CHARACTER_DELETE:
@@ -102,7 +91,57 @@ void deserializePacket(void* buffer, SerialPacketBase* packet) {
case SerialPacketType::CHARACTER_STATS_REQUEST:
case SerialPacketType::CHARACTER_STATS_RESPONSE:
case SerialPacketType::CHARACTER_REJECTION:
deserializeCharacter(buffer, static_cast<CharacterPacket*>(packet));
static_cast<CharacterPacket*>(packet)->Deserialize(data);
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));
}
+12 -7
View File
@@ -24,14 +24,19 @@
#include "serial_packet_base.hpp"
#include <cstring>
#include "statistics.hpp"
//raw memory copy
void serialCopy(void** buffer, void* data, int size);
void deserialCopy(void** buffer, void* data, int size);
//NOTE: The naming conventions here are fucking terrible
//primary functions
void serializePacket(void* buffer, SerialPacketBase* packet);
void deserializePacket(void* buffer, SerialPacketBase* packet);
//BUGFIX: There's really no way to escape this :(
void serializePacket(SerialPacketBase* packet, void* data);
void deserializePacket(SerialPacketBase* packet, void* data);
//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
+15 -13
View File
@@ -21,13 +21,12 @@
*/
#include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "serial_utility.hpp"
#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
//NOTE: don't confuse SerialPacketBase with UDPpacket
//NOTE: don't confuse SerialPacket with UDPpacket
void UDPNetworkUtility::Open(int port) {
socket = SDLNet_UDP_Open(port);
@@ -153,10 +152,10 @@ int UDPNetworkUtility::Receive() {
}
//-------------------------
//send a SerialPacketBase
//send a SerialPacket
//-------------------------
int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacketBase* serialPacket) {
int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* serialPacket) {
IPaddress add;
if (SDLNet_ResolveHost(&add, ip, port) == -1) {
throw(std::runtime_error("Failed to resolve a host"));
@@ -165,9 +164,9 @@ int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacketBase* serial
SendTo(&add, serialPacket);
}
int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacketBase* serialPacket) {
int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) {
memset(packet->data, 0, packet->maxlen);
serializePacket(packet->data, serialPacket);
serializePacket(serialPacket, packet->data);
packet->len = PACKET_BUFFER_SIZE;
packet->address = *add;
@@ -180,9 +179,9 @@ int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacketBase* serialPacket) {
return ret;
}
int UDPNetworkUtility::SendTo(int channel, SerialPacketBase* serialPacket) {
int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) {
memset(packet->data, 0, packet->maxlen);
serializePacket(packet->data, serialPacket);
serializePacket(serialPacket, packet->data);
packet->len = PACKET_BUFFER_SIZE;
int ret = SDLNet_UDP_Send(socket, channel, packet);
@@ -194,9 +193,9 @@ int UDPNetworkUtility::SendTo(int channel, SerialPacketBase* serialPacket) {
return ret;
}
int UDPNetworkUtility::SendToAllChannels(SerialPacketBase* serialPacket) {
int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) {
memset(packet->data, 0, packet->maxlen);
serializePacket(packet->data, serialPacket);
serializePacket(serialPacket, packet->data);
packet->len = PACKET_BUFFER_SIZE;
int sent = 0;
@@ -211,11 +210,14 @@ int UDPNetworkUtility::SendToAllChannels(SerialPacketBase* serialPacket) {
return sent;
}
int UDPNetworkUtility::Receive(SerialPacketBase* serialPacket) {
int UDPNetworkUtility::Receive(SerialPacket* serialPacket) {
memset(packet->data, 0, packet->maxlen);
int ret = SDLNet_UDP_Recv(socket, packet);
deserializePacket(packet->data, serialPacket);
serialPacket->srcAddress = packet->address;
if (ret > 0) {
//BUG: This simply fails
deserializePacket(serialPacket, packet->data);
serialPacket->srcAddress = packet->address;
}
if (ret < 0) {
throw(std::runtime_error("Unknown network error occured"));
+7 -7
View File
@@ -23,7 +23,7 @@
#define UDPNETWORKUTILITY_HPP_
//common
#include "serial_packet_base.hpp"
#include "serial_packet.hpp"
#include "singleton.hpp"
//APIs
@@ -50,12 +50,12 @@ public:
int SendToAllChannels(void* data, int len);
int Receive();
//send a SerialPacketBase
int SendTo(const char* ip, int port, SerialPacketBase* serialPacket);
int SendTo(IPaddress* add, SerialPacketBase* serialPacket);
int SendTo(int channel, SerialPacketBase* serialPacket);
int SendToAllChannels(SerialPacketBase* serialPacket);
int Receive(SerialPacketBase* serialPacket);
//send a SerialPacket
int SendTo(const char* ip, int port, SerialPacket* serialPacket);
int SendTo(IPaddress* add, SerialPacket* serialPacket);
int SendTo(int channel, SerialPacket* serialPacket);
int SendToAllChannels(SerialPacket* serialPacket);
int Receive(SerialPacket* serialPacket);
//accessors
UDPpacket* GetPacket() const {
-1
View File
@@ -30,7 +30,6 @@
//common utilities
#include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "config_utility.hpp"
#include "singleton.hpp"
+4 -2
View File
@@ -21,6 +21,8 @@
*/
#include "server_application.hpp"
#include "serial_packet.hpp"
//utility functions
#include "sql_utility.hpp"
#include "utility.hpp"
@@ -176,7 +178,7 @@ void ServerApplication::Quit() {
//-------------------------
void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
switch(argPacket->type) {
switch(argPacket->GetType()) {
//basic connections
case SerialPacketType::BROADCAST_REQUEST:
HandleBroadcastRequest(static_cast<SerialPacket*>(argPacket));
@@ -222,7 +224,7 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
//handle errors
default: {
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));
}
break;
+43 -43
View File
@@ -31,22 +31,22 @@ void ServerApplication::HandleBroadcastRequest(SerialPacket* const argPacket) {
//send the server's data
ServerPacket newPacket;
newPacket.type = SerialPacketType::BROADCAST_RESPONSE;
strncpy(newPacket.name, config["server.name"].c_str(), PACKET_STRING_SIZE);
newPacket.playerCount = characterMgr.GetContainer()->size();
newPacket.version = NETWORK_VERSION;
newPacket.SetType(SerialPacketType::BROADCAST_RESPONSE);
newPacket.SetName(config["server.name"].c_str());
newPacket.SetPlayerCount(characterMgr.GetContainer()->size());
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) {
//create the new client
ClientData newClient;
newClient.address = argPacket->srcAddress;
newClient.address = argPacket->GetAddress();
//load the user account
//TODO: handle passwords
int accountIndex = accountMgr.LoadAccount(argPacket->username, clientIndex);
int accountIndex = accountMgr.LoadAccount(argPacket->GetUsername(), clientIndex);
if (accountIndex < 0) {
//TODO: send rejection packet
std::cerr << "Error: Account already loaded: " << accountIndex << std::endl;
@@ -55,9 +55,9 @@ void ServerApplication::HandleJoinRequest(ClientPacket* const argPacket) {
//send the client their info
ClientPacket newPacket;
newPacket.type = SerialPacketType::JOIN_RESPONSE;
newPacket.clientIndex = clientIndex;
newPacket.accountIndex = accountIndex;
newPacket.SetType(SerialPacketType::JOIN_RESPONSE);
newPacket.SetClientIndex(clientIndex);
newPacket.SetAccountIndex(accountIndex);
network.SendTo(&newClient.address, static_cast<SerialPacket*>(&newPacket));
@@ -80,14 +80,14 @@ void ServerApplication::HandleDisconnect(ClientPacket* const argPacket) {
//forward to the specified client
network.SendTo(
&clientMap[ accountMgr.GetAccount(argPacket->accountIndex)->GetClientIndex() ].address,
&clientMap[ accountMgr.GetAccount(argPacket->GetAccountIndex())->GetClientIndex() ].address,
static_cast<SerialPacket*>(argPacket)
);
//save and unload this account's characters
//pump the unload message to all remaining clients
characterMgr.UnloadCharacterIf([&](std::map<int, CharacterData>::iterator it) -> bool {
if (argPacket->accountIndex == it->second.GetOwner()) {
if (argPacket->GetAccountIndex() == it->second.GetOwner()) {
PumpCharacterUnload(it->first);
return true;
}
@@ -95,8 +95,8 @@ void ServerApplication::HandleDisconnect(ClientPacket* const argPacket) {
});
//erase the in-memory stuff
clientMap.erase(accountMgr.GetAccount(argPacket->accountIndex)->GetClientIndex());
accountMgr.UnloadAccount(argPacket->accountIndex);
clientMap.erase(accountMgr.GetAccount(argPacket->GetAccountIndex())->GetClientIndex());
accountMgr.UnloadAccount(argPacket->GetAccountIndex());
//finished this routine
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;
//disconnect all clients
SerialPacket newPacket;
newPacket.type = SerialPacketType::DISCONNECT;
ServerPacket newPacket;
newPacket.SetType(SerialPacketType::DISCONNECT);
PumpPacket(&newPacket);
//finished this routine
@@ -130,15 +130,15 @@ void ServerApplication::HandleShutdown(SerialPacket* const argPacket) {
void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
RegionPacket newPacket;
newPacket.type = SerialPacketType::REGION_CONTENT;
newPacket.roomIndex = argPacket->roomIndex;
newPacket.x = argPacket->x;
newPacket.y = argPacket->y;
newPacket.SetType(SerialPacketType::REGION_CONTENT);
newPacket.SetRoomIndex(argPacket->GetRoomIndex());
newPacket.SetX(argPacket->GetX());
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
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) {
//BUG: #27 Characters can be created with an invalid account index
//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) {
//TODO: rejection packet
@@ -164,7 +164,7 @@ void ServerApplication::HandleCharacterNew(CharacterPacket* const argPacket) {
//send this new character to all clients
CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_NEW;
newPacket.SetType(SerialPacketType::CHARACTER_NEW);
CopyCharacterToPacket(&newPacket, characterIndex);
PumpPacket(&newPacket);
}
@@ -173,7 +173,7 @@ void ServerApplication::HandleCharacterDelete(CharacterPacket* const argPacket)
//NOTE: Disconnecting only unloads a character, this explicitly deletes it
//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 (characterIndex == -2) {
@@ -197,7 +197,7 @@ void ServerApplication::HandleCharacterDelete(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
if (!character) {
@@ -208,11 +208,11 @@ void ServerApplication::HandleCharacterUpdate(CharacterPacket* const argPacket)
}
//accept client-side logic
character->SetRoomIndex(argPacket->roomIndex);
character->SetOrigin(argPacket->origin);
character->SetMotion(argPacket->motion);
character->SetRoomIndex(argPacket->GetRoomIndex());
character->SetOrigin(argPacket->GetOrigin());
character->SetMotion(argPacket->GetMotion());
*character->GetBaseStats() = argPacket->stats;
*character->GetBaseStats() = *argPacket->GetStatistics();
//TODO: gameplay components: equipment, items, buffs, debuffs
@@ -228,14 +228,14 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) {
//NOTE: I quite dislike this function
//send all of the server's data to this client
ClientData& client = clientMap[argPacket->clientIndex];
ClientData& client = clientMap[argPacket->GetClientIndex()];
//send all characters
CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_UPDATE;
newPacket.SetType(SerialPacketType::CHARACTER_UPDATE);
for (auto& it : *characterMgr.GetContainer()) {
newPacket.characterIndex = it.first;
newPacket.SetCharacterIndex(it.first);
CopyCharacterToPacket(&newPacket, it.first);
network.SendTo(&client.address, static_cast<SerialPacket*>(&newPacket));
}
@@ -258,8 +258,8 @@ void ServerApplication::PumpPacket(SerialPacket* const argPacket) {
void ServerApplication::PumpCharacterUnload(int uid) {
//delete the client-side character(s)
CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_DELETE;
newPacket.characterIndex = uid;
newPacket.SetType(SerialPacketType::CHARACTER_DELETE);
newPacket.SetCharacterIndex(uid);
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
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();
packet->stats = *character->GetBaseStats();
packet->SetCharacterIndex(characterIndex);
packet->SetHandle(character->GetHandle().c_str());
packet->SetAvatar(character->GetAvatar().c_str());
packet->SetAccountIndex(character->GetOwner());
packet->SetRoomIndex(character->GetRoomIndex());
packet->SetOrigin(character->GetOrigin());
packet->SetMotion(character->GetMotion());
*packet->GetStatistics() = *character->GetBaseStats();
}
+1 -1
View File
@@ -1,4 +1,4 @@
TODO: Sort out the *_cast<> stuff
TODO: Reduce the verbosity of the network packets
TODO: encapsulate the data structures
TODO: Ping-pong and keep alive system
TODO: Move the statistics into their own SQL table, instead of duplicating the structure a dozen times