Changed a massive swap statement for bounds checks

Instead of using a massive block of case statements in serial_utility.cpp,
I've added FORMAT_* tags to SerialPacketType as a way to destinguish
between type values, at least internally. I can't believe I missed this
for so long.

I've also added a placeholder for the network API, as I was working on
that when I ran into this problem.
This commit is contained in:
Kayne Ruse
2015-02-21 23:46:50 +11:00
parent ddedc06e47
commit 46df0f17b7
7 changed files with 155 additions and 117 deletions
+1 -1
View File
@@ -34,7 +34,7 @@
typedef SerialPacketBase SerialPacket; 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 = 20150214; constexpr int NETWORK_VERSION = 20150221;
union MaxPacket { union MaxPacket {
CharacterPacket a; CharacterPacket a;
+35 -1
View File
@@ -25,6 +25,7 @@
/* DOCS: The headers indicate what packet type is used for each message /* DOCS: The headers indicate what packet type is used for each message
* different messages under the same header will carry different amounts of * different messages under the same header will carry different amounts of
* valid data, but it will still be carried in that packet's format. * valid data, but it will still be carried in that packet's format.
* FORMAT_* is for internal use, deviding the different format bounds.
*/ */
enum class SerialPacketType { enum class SerialPacketType {
@@ -36,6 +37,8 @@ enum class SerialPacketType {
// name, player count, version // name, player count, version
//------------------------- //-------------------------
FORMAT_SERVER,
//heartbeat //heartbeat
PING, PING,
PONG, PONG,
@@ -44,11 +47,15 @@ enum class SerialPacketType {
BROADCAST_REQUEST, BROADCAST_REQUEST,
BROADCAST_RESPONSE, BROADCAST_RESPONSE,
FORMAT_END_SERVER,
//------------------------- //-------------------------
//ClientPacket //ClientPacket
// client index, account index, username // client index, account index, username
//------------------------- //-------------------------
FORMAT_CLIENT,
//Connecting to a server as a client //Connecting to a server as a client
JOIN_REQUEST, JOIN_REQUEST,
JOIN_RESPONSE, JOIN_RESPONSE,
@@ -69,15 +76,21 @@ enum class SerialPacketType {
//shut down the server //shut down the server
ADMIN_SHUTDOWN_REQUEST, ADMIN_SHUTDOWN_REQUEST,
FORMAT_END_CLIENT,
//------------------------- //-------------------------
//RegionPacket //RegionPacket
// room index, x, y, raw data // room index, x, y, raw data
//------------------------- //-------------------------
FORMAT_REGION,
//map data //map data
REGION_REQUEST, REGION_REQUEST,
REGION_CONTENT, REGION_CONTENT,
FORMAT_END_REGION,
//------------------------- //-------------------------
//CharacterPacket //CharacterPacket
// character index, // character index,
@@ -86,6 +99,11 @@ enum class SerialPacketType {
// room index, origin, motion // room index, origin, motion
//------------------------- //-------------------------
FORMAT_CHARACTER,
//full data update
CHARACTER_UPDATE,
//character management //character management
CHARACTER_CREATE, CHARACTER_CREATE,
CHARACTER_DELETE, CHARACTER_DELETE,
@@ -97,7 +115,7 @@ enum class SerialPacketType {
QUERY_CHARACTER_STATS, QUERY_CHARACTER_STATS,
QUERY_CHARACTER_LOCATION, QUERY_CHARACTER_LOCATION,
//set the info in the server //actions taken
CHARACTER_MOVEMENT, CHARACTER_MOVEMENT,
CHARACTER_ATTACK, CHARACTER_ATTACK,
CHARACTER_DAMAGE, CHARACTER_DAMAGE,
@@ -105,6 +123,8 @@ enum class SerialPacketType {
//admin control //admin control
// ADMIN_SET_CHARACTER_ORIGIN, // ADMIN_SET_CHARACTER_ORIGIN,
FORMAT_END_CHARACTER,
//------------------------- //-------------------------
//MonsterPacket //MonsterPacket
// monster index, // monster index,
@@ -113,22 +133,34 @@ enum class SerialPacketType {
// room index, origin, motion // room index, origin, motion
//------------------------- //-------------------------
FORMAT_MONSTER,
//full data update
MONSTER_UPDATE,
//character management
MONSTER_CREATE, MONSTER_CREATE,
MONSTER_DELETE, MONSTER_DELETE,
//find out info from the server
QUERY_MONSTER_EXISTS, QUERY_MONSTER_EXISTS,
QUERY_MONSTER_STATS, QUERY_MONSTER_STATS,
QUERY_MONSTER_LOCATION, QUERY_MONSTER_LOCATION,
//actions taken
MONSTER_MOVEMENT, MONSTER_MOVEMENT,
MONSTER_ATTACK, MONSTER_ATTACK,
MONSTER_DAMAGE, MONSTER_DAMAGE,
FORMAT_END_MONSTER,
//------------------------- //-------------------------
//TextPacket //TextPacket
// name, text // name, text
//------------------------- //-------------------------
FORMAT_TEXT,
//general speech //general speech
TEXT_BROADCAST, TEXT_BROADCAST,
TEXT_SPEECH, TEXT_SPEECH,
@@ -143,6 +175,8 @@ enum class SerialPacketType {
SHUTDOWN_REJECTION, SHUTDOWN_REJECTION,
QUERY_REJECTION, QUERY_REJECTION,
FORMAT_END_TEXT,
//------------------------- //-------------------------
//not used //not used
//------------------------- //-------------------------
+35 -102
View File
@@ -31,6 +31,9 @@
#include <cstring> #include <cstring>
//macros
#define BOUNDS(type, lower, upper) ((type) > (lower) && (type) < (upper))
//raw memory copy //raw memory copy
void serialCopy(void** buffer, void* data, int size) { void serialCopy(void** buffer, void* data, int size) {
memcpy(*buffer, data, size); memcpy(*buffer, data, size);
@@ -46,63 +49,28 @@ void deserialCopy(void** buffer, void* data, int size) {
//main switch functions //main switch functions
void serializePacket(void* buffer, SerialPacketBase* packet) { void serializePacket(void* buffer, SerialPacketBase* packet) {
switch(packet->type) { if (BOUNDS(packet->type, SerialPacketType::FORMAT_SERVER, SerialPacketType::FORMAT_END_SERVER)) {
case SerialPacketType::PING:
case SerialPacketType::PONG:
case SerialPacketType::BROADCAST_REQUEST:
case SerialPacketType::BROADCAST_RESPONSE:
serializeServer(buffer, static_cast<ServerPacket*>(packet)); serializeServer(buffer, static_cast<ServerPacket*>(packet));
break; }
case SerialPacketType::JOIN_REQUEST:
case SerialPacketType::JOIN_RESPONSE: if (BOUNDS(packet->type, SerialPacketType::FORMAT_CLIENT, SerialPacketType::FORMAT_END_CLIENT)) {
case SerialPacketType::DISCONNECT_REQUEST:
case SerialPacketType::DISCONNECT_RESPONSE:
case SerialPacketType::ADMIN_DISCONNECT_FORCED:
case SerialPacketType::LOGIN_REQUEST:
case SerialPacketType::LOGIN_RESPONSE:
case SerialPacketType::LOGOUT_REQUEST:
case SerialPacketType::LOGOUT_RESPONSE:
case SerialPacketType::ADMIN_SHUTDOWN_REQUEST:
serializeClient(buffer, static_cast<ClientPacket*>(packet)); serializeClient(buffer, static_cast<ClientPacket*>(packet));
break; }
case SerialPacketType::REGION_REQUEST:
case SerialPacketType::REGION_CONTENT: if (BOUNDS(packet->type, SerialPacketType::FORMAT_REGION, SerialPacketType::FORMAT_END_REGION)) {
serializeRegion(buffer, static_cast<RegionPacket*>(packet)); serializeRegion(buffer, static_cast<RegionPacket*>(packet));
break; }
case SerialPacketType::CHARACTER_CREATE:
case SerialPacketType::CHARACTER_DELETE: if (BOUNDS(packet->type, SerialPacketType::FORMAT_CHARACTER, SerialPacketType::FORMAT_END_CHARACTER)) {
case SerialPacketType::CHARACTER_LOAD:
case SerialPacketType::CHARACTER_UNLOAD:
case SerialPacketType::QUERY_CHARACTER_EXISTS:
case SerialPacketType::QUERY_CHARACTER_STATS:
case SerialPacketType::QUERY_CHARACTER_LOCATION:
case SerialPacketType::CHARACTER_MOVEMENT:
case SerialPacketType::CHARACTER_ATTACK:
case SerialPacketType::CHARACTER_DAMAGE:
serializeCharacter(buffer, static_cast<CharacterPacket*>(packet)); serializeCharacter(buffer, static_cast<CharacterPacket*>(packet));
break; }
case SerialPacketType::MONSTER_CREATE:
case SerialPacketType::MONSTER_DELETE: if (BOUNDS(packet->type, SerialPacketType::FORMAT_MONSTER, SerialPacketType::FORMAT_END_MONSTER)) {
case SerialPacketType::QUERY_MONSTER_EXISTS:
case SerialPacketType::QUERY_MONSTER_STATS:
case SerialPacketType::QUERY_MONSTER_LOCATION:
case SerialPacketType::MONSTER_MOVEMENT:
case SerialPacketType::MONSTER_ATTACK:
case SerialPacketType::MONSTER_DAMAGE:
serializeMonster(buffer, static_cast<MonsterPacket*>(packet)); serializeMonster(buffer, static_cast<MonsterPacket*>(packet));
break; }
case SerialPacketType::TEXT_BROADCAST:
case SerialPacketType::TEXT_SPEECH: if (BOUNDS(packet->type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) {
case SerialPacketType::TEXT_WHISPER:
case SerialPacketType::JOIN_REJECTION:
case SerialPacketType::LOGIN_REJECTION:
case SerialPacketType::REGION_REJECTION:
case SerialPacketType::CHARACTER_REJECTION:
case SerialPacketType::MONSTER_REJECTION:
case SerialPacketType::SHUTDOWN_REJECTION:
case SerialPacketType::QUERY_REJECTION:
serializeText(buffer, static_cast<TextPacket*>(packet)); serializeText(buffer, static_cast<TextPacket*>(packet));
break;
} }
} }
@@ -111,62 +79,27 @@ void deserializePacket(void* buffer, SerialPacketBase* packet) {
SerialPacketType type; SerialPacketType type;
memcpy(&type, buffer, sizeof(SerialPacketType)); memcpy(&type, buffer, sizeof(SerialPacketType));
switch(type) { if (BOUNDS(type, SerialPacketType::FORMAT_SERVER, SerialPacketType::FORMAT_END_SERVER)) {
case SerialPacketType::PING:
case SerialPacketType::PONG:
case SerialPacketType::BROADCAST_REQUEST:
case SerialPacketType::BROADCAST_RESPONSE:
deserializeServer(buffer, static_cast<ServerPacket*>(packet)); deserializeServer(buffer, static_cast<ServerPacket*>(packet));
break; }
case SerialPacketType::JOIN_REQUEST:
case SerialPacketType::JOIN_RESPONSE: if (BOUNDS(type, SerialPacketType::FORMAT_CLIENT, SerialPacketType::FORMAT_END_CLIENT)) {
case SerialPacketType::DISCONNECT_REQUEST:
case SerialPacketType::DISCONNECT_RESPONSE:
case SerialPacketType::ADMIN_DISCONNECT_FORCED:
case SerialPacketType::LOGIN_REQUEST:
case SerialPacketType::LOGIN_RESPONSE:
case SerialPacketType::LOGOUT_REQUEST:
case SerialPacketType::LOGOUT_RESPONSE:
case SerialPacketType::ADMIN_SHUTDOWN_REQUEST:
deserializeClient(buffer, static_cast<ClientPacket*>(packet)); deserializeClient(buffer, static_cast<ClientPacket*>(packet));
break; }
case SerialPacketType::REGION_REQUEST:
case SerialPacketType::REGION_CONTENT: if (BOUNDS(type, SerialPacketType::FORMAT_REGION, SerialPacketType::FORMAT_END_REGION)) {
deserializeRegion(buffer, static_cast<RegionPacket*>(packet)); deserializeRegion(buffer, static_cast<RegionPacket*>(packet));
break; }
case SerialPacketType::CHARACTER_CREATE:
case SerialPacketType::CHARACTER_DELETE: if (BOUNDS(type, SerialPacketType::FORMAT_CHARACTER, SerialPacketType::FORMAT_END_CHARACTER)) {
case SerialPacketType::CHARACTER_LOAD:
case SerialPacketType::CHARACTER_UNLOAD:
case SerialPacketType::QUERY_CHARACTER_EXISTS:
case SerialPacketType::QUERY_CHARACTER_STATS:
case SerialPacketType::QUERY_CHARACTER_LOCATION:
case SerialPacketType::CHARACTER_MOVEMENT:
case SerialPacketType::CHARACTER_ATTACK:
case SerialPacketType::CHARACTER_DAMAGE:
deserializeCharacter(buffer, static_cast<CharacterPacket*>(packet)); deserializeCharacter(buffer, static_cast<CharacterPacket*>(packet));
break; }
case SerialPacketType::MONSTER_CREATE:
case SerialPacketType::MONSTER_DELETE: if (BOUNDS(type, SerialPacketType::FORMAT_MONSTER, SerialPacketType::FORMAT_END_MONSTER)) {
case SerialPacketType::QUERY_MONSTER_EXISTS:
case SerialPacketType::QUERY_MONSTER_STATS:
case SerialPacketType::QUERY_MONSTER_LOCATION:
case SerialPacketType::MONSTER_MOVEMENT:
case SerialPacketType::MONSTER_ATTACK:
case SerialPacketType::MONSTER_DAMAGE:
deserializeMonster(buffer, static_cast<MonsterPacket*>(packet)); deserializeMonster(buffer, static_cast<MonsterPacket*>(packet));
break; }
case SerialPacketType::TEXT_BROADCAST:
case SerialPacketType::TEXT_SPEECH: if (BOUNDS(type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) {
case SerialPacketType::TEXT_WHISPER:
case SerialPacketType::JOIN_REJECTION:
case SerialPacketType::LOGIN_REJECTION:
case SerialPacketType::REGION_REJECTION:
case SerialPacketType::CHARACTER_REJECTION:
case SerialPacketType::MONSTER_REJECTION:
case SerialPacketType::SHUTDOWN_REJECTION:
case SerialPacketType::QUERY_REJECTION:
deserializeText(buffer, static_cast<TextPacket*>(packet)); deserializeText(buffer, static_cast<TextPacket*>(packet));
break;
} }
} }
+2
View File
@@ -40,6 +40,7 @@
#include "character_system_api.hpp" #include "character_system_api.hpp"
#include "map_system_api.hpp" #include "map_system_api.hpp"
#include "monster_system_api.hpp" #include "monster_system_api.hpp"
#include "network_api.hpp"
#include "room_system_api.hpp" #include "room_system_api.hpp"
#include "waypoint_system_api.hpp" #include "waypoint_system_api.hpp"
@@ -65,6 +66,7 @@ static const luaL_Reg preloadedlibs[] = {
{TORTUGA_CHARACTER_SYSTEM_API, openCharacterSystemAPI}, {TORTUGA_CHARACTER_SYSTEM_API, openCharacterSystemAPI},
{TORTUGA_MAP_SYSTEM_API, openMapSystemAPI}, {TORTUGA_MAP_SYSTEM_API, openMapSystemAPI},
{TORTUGA_MONSTER_SYSTEM_API, openMonsterSystemAPI}, {TORTUGA_MONSTER_SYSTEM_API, openMonsterSystemAPI},
{TORTUGA_NETWORK_API, openNetworkAPI},
{TORTUGA_ROOM_SYSTEM_API, openRoomSystemAPI}, {TORTUGA_ROOM_SYSTEM_API, openRoomSystemAPI},
{TORTUGA_WAYPOINT_SYSTEM_API, openWaypointSystemAPI}, {TORTUGA_WAYPOINT_SYSTEM_API, openWaypointSystemAPI},
{NULL, NULL} {NULL, NULL}
+37
View File
@@ -0,0 +1,37 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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 "network_api.hpp"
static int pumpCharacterUpdate(lua_State* L) {
return 0;
}
static const luaL_Reg networkLib[] = {
{"PumpCharacterUpdate", pumpCharacterUpdate},
{nullptr, nullptr}
};
LUAMOD_API int openNetworkAPI(lua_State* L) {
luaL_newlib(L, networkLib);
return 1;
}
+30
View File
@@ -0,0 +1,30 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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 NETWORKAPI_HPP_
#define NETWORKAPI_HPP_
#include "lua.hpp"
#define TORTUGA_NETWORK_API "network"
LUAMOD_API int openNetworkAPI(lua_State* L);
#endif
+3 -1
View File
@@ -1,3 +1,6 @@
TODO: upgrade to lua 5.3
TODO: Split config.cfg in two, one for the server and the client
TODO: In need of script APIs (list) TODO: In need of script APIs (list)
* Characters * Characters
@@ -9,7 +12,6 @@ TODO: Account passwords (list)
TODO: Features TODO: Features
* Make sure login errors are sent to the client * Make sure login errors are sent to the client
* Split config.cfg in two, one for the server and the client
* Add the "home" parameter to the server's config file * Add the "home" parameter to the server's config file
* Waypoints, with positions and trigger zones (collision areas) for doors, monster spawns, etc. * Waypoints, with positions and trigger zones (collision areas) for doors, monster spawns, etc.
* Fix shoddy movement * Fix shoddy movement