Began reworking the network code

This commit devides SerialPacket into a series of different structures,
all decended from a common base class.

Using a union was not a good idea.
This commit is contained in:
Kayne Ruse
2014-06-04 20:46:05 +10:00
parent 0a03535ecb
commit da60fa8f94
17 changed files with 505 additions and 201 deletions
@@ -0,0 +1,53 @@
/* 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
* 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 CHARACTERPACKET_HPP_
#define CHARACTERPACKET_HPP_
#include "serial_packet_base.hpp"
#include "vector2.hpp"
#include "statistics.hpp"
struct CharacterPacket : SerialPacketBase {
//identify the character
int characterIndex;
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
//the owner
int accountIndex;
//location
int roomIndex;
Vector2 origin;
Vector2 motion;
//gameplay
Statistics stats;
//TODO: equipment
//TODO: items
//TODO: buffs
//TODO: debuffs
};
#endif
+34
View File
@@ -0,0 +1,34 @@
/* 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
* 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 CLIENTPACKET_HPP_
#define CLIENTPACKET_HPP_
#include "serial_packet_base.hpp"
struct ClientPacket : SerialPacketBase {
int clientIndex;
int accountIndex;
char username[PACKET_STRING_SIZE];
char password[PACKET_STRING_SIZE]; //hashed, not currently used
};
#endif
+46
View File
@@ -0,0 +1,46 @@
/* 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
* 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 COMBATPACKET_HPP_
#define COMBATPACKET_HPP_
#include "serial_packet_base.hpp"
#include "combat_data.hpp"
struct CombatPacket : SerialPacketBase {
//identify the combat instance
int combatIndex;
int difficulty;
CombatData::Terrain terrainType;
//combatants
int characterArray[COMBAT_MAX_CHARACTERS];
int enemyArray[COMBAT_MAX_ENEMIES];
//location
int mapIndex;
Vector2 origin;
//TODO: rewards
};
#endif
+39
View File
@@ -0,0 +1,39 @@
/* 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
* 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 ENEMYPACKET_HPP_
#define ENEMYPACKET_HPP_
#include "serial_packet_base.hpp"
struct EnemyPacket : SerialPacketBase {
//identify the enemy
int enemyIndex;
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
//gameplay
Statistics stats;
//TODO: rewards
};
#endif
+37
View File
@@ -0,0 +1,37 @@
#config
INCLUDES+=. ../../gameplay ../../map ../../utilities
LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
#source
CXXSRC=$(wildcard *.cpp)
#objects
OBJDIR=obj
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
#output
OUTDIR=../../..
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
#targets
all: $(OBJ) $(OUT)
ar -crs $(OUT) $(OBJ)
$(OBJ): | $(OBJDIR)
$(OUT): | $(OUTDIR)
$(OBJDIR):
mkdir $(OBJDIR)
$(OUTDIR):
mkdir $(OUTDIR)
$(OBJDIR)/%.o: %.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
$(RM) *.o *.a *.exe
rebuild: clean all
+38
View File
@@ -0,0 +1,38 @@
/* 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
* 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 REGIONPACKET_HPP_
#define REGIONPACKET_HPP_
#include "serial_packet_base.hpp"
#include "region.hpp"
struct RegionPacket : SerialPacketBase {
//location/identify the region
int roomIndex;
int x, y;
//the data
Region* region;
};
#endif
+28
View File
@@ -0,0 +1,28 @@
/* 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
* 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_packet.hpp"
/* DOCS: Sanity check, read more
* Since most/all of the files in this directory are header files, I've created
* this source file as a "sanity check", to ensure that the above header files
* are written correctly via make.
*/
+34
View File
@@ -0,0 +1,34 @@
/* 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
* 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 SERIALPACKET_HPP_
#define SERIALPACKET_HPP_
#include "character_packet.hpp"
#include "client_packet.hpp"
#include "combat_packet.hpp"
#include "enemy_packet.hpp"
#include "region_packet.hpp"
#include "server_packet.hpp"
//NOTE: SerialPacket is defined in serial_packet_base.hpp
#endif
@@ -0,0 +1,46 @@
/* 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
* 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 SERIALPACKETBASE_HPP_
#define SERIALPACKETBASE_HPP_
#ifndef SERIALPACKET_HPP_
#error Cannot include this file without 'serial_packet.hpp'
#endif
#include "serial_packet_type.hpp"
#include "SDL/SDL_net.h"
#define PACKET_STRING_SIZE 100
struct SerialPacketBase {
//members
SerialPacketType type;
IPaddress srcAddress;
typedef SerialPacketType Type;
};
typedef SerialPacketBase* SerialPacket;
typedef SerialPacketType PacketType;
#endif
@@ -0,0 +1,98 @@
/* 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
* 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 SERIALPACKETTYPE_HPP_
#define SERIALPACKETTYPE_HPP_
enum class SerialPacketType {
//default: there is something wrong
NONE = 0,
//keep alive
PING,
PONG,
//searching for a server to join
BROADCAST_REQUEST,
BROADCAST_RESPONSE,
BROADCAST_REJECTION,
//try to join the server
JOIN_REQUEST,
JOIN_RESPONSE,
JOIN_REJECTION,
//mass update
SYNCHRONIZE,
//disconnect from the server
DISCONNECT,
//shut down the server
SHUTDOWN,
//map data
REGION_REQUEST,
REGION_CONTENT,
REGION_REJECTION,
//combat data
COMBAT_NEW,
COMBAT_DELETE,
COMBAT_UPDATE,
COMBAT_ENTER_REQUEST,
COMBAT_ENTER_RESPONSE,
COMBAT_EXIT_REQUEST,
COMBAT_EXIT_RESPONSE,
//TODO: COMBAT info
COMBAT_REJECTION,
//character data
CHARACTER_NEW,
CHARACTER_DELETE,
CHARACTER_UPDATE,
CHARACTER_STATS_REQUEST,
CHARACTER_STATS_RESPONSE,
CHARACTER_REJECTION,
//enemy data
ENEMY_NEW,
ENEMY_DELETE,
ENEMY_UPDATE,
ENEMY_STATS_REQUEST,
ENEMY_STATS_RESPONSE,
ENEMY_REJECTION,
//more packet types go here
//not used
LAST,
};
#endif
+34
View File
@@ -0,0 +1,34 @@
/* 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
* 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 SERVERPACKET_HPP_
#define SERVERPACKET_HPP_
#include "serial_packet_base.hpp"
struct ServerPacket : SerialPacketBase {
//identify the server
char name[PACKET_STRING_SIZE];
int playerCount;
int version;
};
#endif
+4 -4
View File
@@ -103,8 +103,8 @@ void serializeCombat(SerialPacket* packet, char* buffer) {
SERIALIZE(buffer, &packet->combatInfo.terrainType, sizeof(CombatData::Terrain));
//arrays
SERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTER_COUNT);
SERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMY_COUNT);
SERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTERS);
SERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMIES);
//position
SERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int));
@@ -247,8 +247,8 @@ void deserializeCombat(SerialPacket* packet, char* buffer) {
DESERIALIZE(buffer, &packet->combatInfo.terrainType, sizeof(CombatData::Terrain));
//arrays
DESERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTER_COUNT);
DESERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMY_COUNT);
DESERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTERS);
DESERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMIES);
//position
DESERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int));
-188
View File
@@ -1,188 +0,0 @@
/* 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
* 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 SERIALPACKET_HPP_
#define SERIALPACKET_HPP_
#include "vector2.hpp"
#include "region.hpp"
#include "statistics.hpp"
#include "combat_data.hpp"
#include "SDL/SDL_net.h"
#define NETWORK_VERSION 20140601
#define PACKET_STRING_SIZE 100
//TODO: would it be possible to serialize structures directly?
union SerialPacket {
//types of packets
enum class Type {
//default: there is something wrong
NONE = 0,
//keep alive
PING,
PONG,
//searching for a server to join
BROADCAST_REQUEST,
BROADCAST_RESPONSE,
BROADCAST_REJECTION,
//try to join the server
JOIN_REQUEST,
JOIN_RESPONSE,
JOIN_REJECTION,
//mass update
SYNCHRONIZE,
//disconnect from the server
DISCONNECT,
//shut down the server
SHUTDOWN,
//map data
REGION_REQUEST,
REGION_CONTENT,
REGION_REJECTION,
//combat data
COMBAT_NEW,
COMBAT_DELETE,
COMBAT_UPDATE,
COMBAT_ENTER_REQUEST,
COMBAT_ENTER_RESPONSE,
COMBAT_EXIT_REQUEST,
COMBAT_EXIT_RESPONSE,
//TODO: COMBAT info
COMBAT_REJECTION,
//character data
CHARACTER_NEW,
CHARACTER_DELETE,
CHARACTER_UPDATE,
CHARACTER_STATS_REQUEST,
CHARACTER_STATS_RESPONSE,
CHARACTER_REJECTION,
//enemy data
ENEMY_NEW,
ENEMY_DELETE,
ENEMY_UPDATE,
ENEMY_STATS_REQUEST,
ENEMY_STATS_RESPONSE,
ENEMY_REJECTION,
//more packet types go here
//not used
LAST,
};
//metadata on the packet itself
struct Metadata {
Type type;
IPaddress srcAddress;
}meta;
//info about the server
struct ServerInformation {
Metadata meta;
int networkVersion;
char name[PACKET_STRING_SIZE];
int playerCount;
}serverInfo;
//info about the client
struct ClientInformation {
Metadata meta;
int clientIndex;
int accountIndex;
int characterIndex;
char username[PACKET_STRING_SIZE];
//TODO: password
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
}clientInfo;
//info about a region
struct RegionInformation {
Metadata meta;
int mapIndex;
int x, y;
Region* region;
}regionInfo;
//info about a combat scenario
struct CombatInformation {
Metadata meta;
int combatIndex;
int difficulty;
CombatData::Terrain terrainType;
int characterArray[COMBAT_MAX_CHARACTER_COUNT];
int enemyArray[COMBAT_MAX_ENEMY_COUNT];
int mapIndex;
Vector2 origin;
//TODO: rewards
}combatInfo;
//info about a character
struct CharacterInformation {
Metadata meta;
int clientIndex;
int accountIndex;
int characterIndex;
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
int mapIndex;
Vector2 origin;
Vector2 motion;
Statistics stats;
}characterInfo;
//info about an enemy
struct EnemyInformation {
Metadata meta;
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
Statistics stats;
//TODO: rewards
}enemyInfo;
//defaults
SerialPacket() {
meta.type = Type::NONE;
meta.srcAddress = {0,0};
}
};
#endif