Merged server_internals.cpp and network_handlers.cpp into
server_application.cpp
This commit is contained in:
@@ -20,10 +20,8 @@
|
|||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#include "character_data.hpp"
|
#include "character_data.hpp"
|
||||||
#include "client_data.hpp"
|
|
||||||
#include "combat_data.hpp"
|
#include "combat_data.hpp"
|
||||||
#include "enemy_data.hpp"
|
#include "enemy_data.hpp"
|
||||||
#include "room_data.hpp"
|
|
||||||
#include "statistics.hpp"
|
#include "statistics.hpp"
|
||||||
|
|
||||||
/* DOCS: Sanity check, read more
|
/* DOCS: Sanity check, read more
|
||||||
|
|||||||
@@ -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,11 +21,199 @@
|
|||||||
*/
|
*/
|
||||||
#include "server_application.hpp"
|
#include "server_application.hpp"
|
||||||
|
|
||||||
|
#include "sql_utility.hpp"
|
||||||
|
#include "serial.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Handle various network input
|
//Define the public members
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void ServerApplication::Init(int argc, char** argv) {
|
||||||
|
//NOTE: I might need to rearrange the init process so that lua & SQL can interact with the map system as needed.
|
||||||
|
std::cout << "Beginning startup" << std::endl;
|
||||||
|
|
||||||
|
//initial setup
|
||||||
|
config.Load("rsc\\config.cfg");
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Initialize the APIs
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
//Init SDL
|
||||||
|
if (SDL_Init(0)) {
|
||||||
|
throw(std::runtime_error("Failed to initialize SDL"));
|
||||||
|
}
|
||||||
|
std::cout << "Initialized SDL" << std::endl;
|
||||||
|
|
||||||
|
//Init SDL_net
|
||||||
|
if (SDLNet_Init()) {
|
||||||
|
throw(std::runtime_error("Failed to initialize SDL_net"));
|
||||||
|
}
|
||||||
|
network.Open(config.Int("server.port"));
|
||||||
|
std::cout << "Initialized SDL_net" << std::endl;
|
||||||
|
|
||||||
|
//Init SQL
|
||||||
|
int ret = sqlite3_open_v2(config["server.dbname"].c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr);
|
||||||
|
if (ret != SQLITE_OK || !database) {
|
||||||
|
throw(std::runtime_error(std::string() + "Failed to initialize SQL: " + sqlite3_errmsg(database) ));
|
||||||
|
}
|
||||||
|
std::cout << "Initialized SQL" << std::endl;
|
||||||
|
|
||||||
|
//Init lua
|
||||||
|
luaState = luaL_newstate();
|
||||||
|
if (!luaState) {
|
||||||
|
throw(std::runtime_error("Failed to initialize lua"));
|
||||||
|
}
|
||||||
|
luaL_openlibs(luaState);
|
||||||
|
std::cout << "Initialized lua" << std::endl;
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Setup the objects
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
//setup the map object
|
||||||
|
regionPager.GetAllocator()->SetLuaState(luaState);
|
||||||
|
regionPager.GetFormat()->SetLuaState(luaState);
|
||||||
|
regionPager.GetFormat()->SetSaveDir(config["dir.maps"] + config["map.savename"]);
|
||||||
|
std::cout << "Prepared the map system" << std::endl;
|
||||||
|
|
||||||
|
//push the pager onto the lua registry
|
||||||
|
lua_pushstring(luaState, "pager");
|
||||||
|
lua_pushlightuserdata(luaState, reinterpret_cast<void*>(®ionPager));
|
||||||
|
lua_settable(luaState, LUA_REGISTRYINDEX);
|
||||||
|
std::cout << "Registered the map system in lua" << std::endl;
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Run the startup scripts
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
//setup the database
|
||||||
|
if (runSQLScript(database, config["dir.scripts"] + "setup_server.sql")) {
|
||||||
|
throw(std::runtime_error("Failed to initialize SQL's setup script"));
|
||||||
|
}
|
||||||
|
std::cout << "Completed SQL's setup script" << std::endl;
|
||||||
|
|
||||||
|
//run lua's startup script
|
||||||
|
if (luaL_dofile(luaState, (config["dir.scripts"] + "setup_server.lua").c_str())) {
|
||||||
|
throw(std::runtime_error(std::string() + "Failed to initialize lua's setup script: " + lua_tostring(luaState, -1) ));
|
||||||
|
}
|
||||||
|
std::cout << "Completed lua's setup script" << std::endl;
|
||||||
|
|
||||||
|
//debug output
|
||||||
|
std::cout << "Internal sizes:" << std::endl;
|
||||||
|
std::cout << "\tsizeof(SerialPacket): " << sizeof(SerialPacket) << std::endl;
|
||||||
|
std::cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << std::endl;
|
||||||
|
|
||||||
|
//finalize the startup
|
||||||
|
std::cout << "Startup completed successfully" << std::endl;
|
||||||
|
|
||||||
|
//debugging
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerApplication::Proc() {
|
||||||
|
SerialPacket packet;
|
||||||
|
while(running) {
|
||||||
|
//suck in the waiting packets & process them
|
||||||
|
while(network.Receive(&packet)) {
|
||||||
|
HandlePacket(packet);
|
||||||
|
}
|
||||||
|
//update the internals
|
||||||
|
//TODO: update the internals i.e. player positions
|
||||||
|
//give the computer a break
|
||||||
|
SDL_Delay(10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerApplication::Quit() {
|
||||||
|
std::cout << "Shutting down" << std::endl;
|
||||||
|
|
||||||
|
//save the server state
|
||||||
|
for (auto& it : accountMap) {
|
||||||
|
SaveUserAccount(it.first);
|
||||||
|
}
|
||||||
|
for (auto& it : characterMap) {
|
||||||
|
SaveCharacter(it.first);
|
||||||
|
}
|
||||||
|
|
||||||
|
//empty the members
|
||||||
|
accountMap.clear();
|
||||||
|
characterMap.clear();
|
||||||
|
regionPager.UnloadAll();
|
||||||
|
|
||||||
|
//APIs
|
||||||
|
lua_close(luaState);
|
||||||
|
sqlite3_close_v2(database);
|
||||||
|
network.Close();
|
||||||
|
SDLNet_Quit();
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
std::cout << "Shutdown finished" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Define the network switch
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void ServerApplication::HandlePacket(SerialPacket packet) {
|
||||||
|
switch(packet.meta.type) {
|
||||||
|
//basic connections
|
||||||
|
case SerialPacketType::BROADCAST_REQUEST:
|
||||||
|
HandleBroadcastRequest(packet);
|
||||||
|
break;
|
||||||
|
case SerialPacketType::JOIN_REQUEST:
|
||||||
|
HandleJoinRequest(packet);
|
||||||
|
break;
|
||||||
|
case SerialPacketType::DISCONNECT:
|
||||||
|
HandleDisconnect(packet);
|
||||||
|
break;
|
||||||
|
case SerialPacketType::SHUTDOWN:
|
||||||
|
HandleShutdown(packet);
|
||||||
|
break;
|
||||||
|
|
||||||
|
//map management
|
||||||
|
case SerialPacketType::REGION_REQUEST:
|
||||||
|
HandleRegionRequest(packet);
|
||||||
|
break;
|
||||||
|
|
||||||
|
//combat management
|
||||||
|
//TODO: combat management
|
||||||
|
|
||||||
|
//character management
|
||||||
|
case SerialPacketType::CHARACTER_NEW:
|
||||||
|
HandleCharacterNew(packet);
|
||||||
|
break;
|
||||||
|
case SerialPacketType::CHARACTER_DELETE:
|
||||||
|
HandleCharacterDelete(packet);
|
||||||
|
break;
|
||||||
|
case SerialPacketType::CHARACTER_UPDATE:
|
||||||
|
HandleCharacterUpdate(packet);
|
||||||
|
break;
|
||||||
|
case SerialPacketType::CHARACTER_STATS_REQUEST:
|
||||||
|
HandleCharacterUpdate(packet);
|
||||||
|
break;
|
||||||
|
|
||||||
|
//enemy management
|
||||||
|
//TODO: enemy management
|
||||||
|
|
||||||
|
//mismanagement
|
||||||
|
case SerialPacketType::SYNCHRONIZE:
|
||||||
|
HandleSynchronize(packet);
|
||||||
|
break;
|
||||||
|
|
||||||
|
//handle errors
|
||||||
|
default:
|
||||||
|
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in the server: " + to_string_custom(int(packet.type))));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Define the network handlers
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void ServerApplication::HandleBroadcastRequest(SerialPacket packet) {
|
void ServerApplication::HandleBroadcastRequest(SerialPacket packet) {
|
||||||
@@ -191,4 +379,11 @@ void ServerApplication::PumpCharacterUnload(int uid) {
|
|||||||
delPacket.meta.type = SerialPacket::Type::CHARACTER_DELETE;
|
delPacket.meta.type = SerialPacket::Type::CHARACTER_DELETE;
|
||||||
delPacket.characterInfo.characterIndex = uid;
|
delPacket.characterInfo.characterIndex = uid;
|
||||||
PumpPacket(delPacket);
|
PumpPacket(delPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Define the utility methods
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
//TODO: utility methods
|
||||||
|
|
||||||
@@ -78,10 +78,8 @@ private:
|
|||||||
void PumpPacket(SerialPacket);
|
void PumpPacket(SerialPacket);
|
||||||
void PumpCharacterUnload(int uid);
|
void PumpCharacterUnload(int uid);
|
||||||
|
|
||||||
//Account management
|
//TODO: Account management
|
||||||
|
//TODO: character management
|
||||||
//character management
|
|
||||||
|
|
||||||
//TODO: combat management
|
//TODO: combat management
|
||||||
|
|
||||||
//APIs
|
//APIs
|
||||||
@@ -91,7 +89,6 @@ private:
|
|||||||
|
|
||||||
//server tables
|
//server tables
|
||||||
std::map<int, ClientData> clientMap;
|
std::map<int, ClientData> clientMap;
|
||||||
std::map<int, CharacterData> characterMap;
|
|
||||||
std::map<int, CombatData> combatMap;
|
std::map<int, CombatData> combatMap;
|
||||||
std::map<int, EnemyData> enemyMap;
|
std::map<int, EnemyData> enemyMap;
|
||||||
|
|
||||||
|
|||||||
@@ -1,190 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
*
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
*
|
|
||||||
* 3. This notice may not be removed or altered from any source
|
|
||||||
* distribution.
|
|
||||||
*/
|
|
||||||
#include "server_application.hpp"
|
|
||||||
|
|
||||||
#include "sql_utility.hpp"
|
|
||||||
#include "serial.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Define the public members
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void ServerApplication::Init(int argc, char** argv) {
|
|
||||||
//NOTE: I might need to rearrange the init process so that lua & SQL can interact with the map system as needed.
|
|
||||||
std::cout << "Beginning startup" << std::endl;
|
|
||||||
|
|
||||||
//initial setup
|
|
||||||
config.Load("rsc\\config.cfg");
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Initialize the APIs
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
//Init SDL
|
|
||||||
if (SDL_Init(0)) {
|
|
||||||
throw(std::runtime_error("Failed to initialize SDL"));
|
|
||||||
}
|
|
||||||
std::cout << "Initialized SDL" << std::endl;
|
|
||||||
|
|
||||||
//Init SDL_net
|
|
||||||
if (SDLNet_Init()) {
|
|
||||||
throw(std::runtime_error("Failed to initialize SDL_net"));
|
|
||||||
}
|
|
||||||
network.Open(config.Int("server.port"));
|
|
||||||
std::cout << "Initialized SDL_net" << std::endl;
|
|
||||||
|
|
||||||
//Init SQL
|
|
||||||
int ret = sqlite3_open_v2(config["server.dbname"].c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr);
|
|
||||||
if (ret != SQLITE_OK || !database) {
|
|
||||||
throw(std::runtime_error(std::string() + "Failed to initialize SQL: " + sqlite3_errmsg(database) ));
|
|
||||||
}
|
|
||||||
std::cout << "Initialized SQL" << std::endl;
|
|
||||||
|
|
||||||
//Init lua
|
|
||||||
luaState = luaL_newstate();
|
|
||||||
if (!luaState) {
|
|
||||||
throw(std::runtime_error("Failed to initialize lua"));
|
|
||||||
}
|
|
||||||
luaL_openlibs(luaState);
|
|
||||||
std::cout << "Initialized lua" << std::endl;
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Setup the objects
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
//setup the map object
|
|
||||||
regionPager.GetAllocator()->SetLuaState(luaState);
|
|
||||||
regionPager.GetFormat()->SetLuaState(luaState);
|
|
||||||
regionPager.GetFormat()->SetSaveDir(config["dir.maps"] + config["map.savename"]);
|
|
||||||
std::cout << "Prepared the map system" << std::endl;
|
|
||||||
|
|
||||||
//push the pager onto the lua registry
|
|
||||||
lua_pushstring(luaState, "pager");
|
|
||||||
lua_pushlightuserdata(luaState, reinterpret_cast<void*>(®ionPager));
|
|
||||||
lua_settable(luaState, LUA_REGISTRYINDEX);
|
|
||||||
std::cout << "Registered the map system in lua" << std::endl;
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Run the startup scripts
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
//setup the database
|
|
||||||
if (runSQLScript(database, config["dir.scripts"] + "setup_server.sql")) {
|
|
||||||
throw(std::runtime_error("Failed to initialize SQL's setup script"));
|
|
||||||
}
|
|
||||||
std::cout << "Completed SQL's setup script" << std::endl;
|
|
||||||
|
|
||||||
//run lua's startup script
|
|
||||||
if (luaL_dofile(luaState, (config["dir.scripts"] + "setup_server.lua").c_str())) {
|
|
||||||
throw(std::runtime_error(std::string() + "Failed to initialize lua's setup script: " + lua_tostring(luaState, -1) ));
|
|
||||||
}
|
|
||||||
std::cout << "Completed lua's setup script" << std::endl;
|
|
||||||
|
|
||||||
//debug output
|
|
||||||
std::cout << "Internal sizes:" << std::endl;
|
|
||||||
std::cout << "\tsizeof(SerialPacket): " << sizeof(SerialPacket) << std::endl;
|
|
||||||
std::cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << std::endl;
|
|
||||||
|
|
||||||
//finalize the startup
|
|
||||||
std::cout << "Startup completed successfully" << std::endl;
|
|
||||||
|
|
||||||
//debugging
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void ServerApplication::Proc() {
|
|
||||||
SerialPacket packet;
|
|
||||||
while(running) {
|
|
||||||
//suck in the waiting packets & process them
|
|
||||||
while(network.Receive(&packet)) {
|
|
||||||
HandlePacket(packet);
|
|
||||||
}
|
|
||||||
//update the internals
|
|
||||||
//TODO: update the internals i.e. player positions
|
|
||||||
//give the computer a break
|
|
||||||
SDL_Delay(10);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ServerApplication::Quit() {
|
|
||||||
std::cout << "Shutting down" << std::endl;
|
|
||||||
|
|
||||||
//save the server state
|
|
||||||
for (auto& it : accountMap) {
|
|
||||||
SaveUserAccount(it.first);
|
|
||||||
}
|
|
||||||
for (auto& it : characterMap) {
|
|
||||||
SaveCharacter(it.first);
|
|
||||||
}
|
|
||||||
|
|
||||||
//empty the members
|
|
||||||
accountMap.clear();
|
|
||||||
characterMap.clear();
|
|
||||||
regionPager.UnloadAll();
|
|
||||||
|
|
||||||
//APIs
|
|
||||||
lua_close(luaState);
|
|
||||||
sqlite3_close_v2(database);
|
|
||||||
network.Close();
|
|
||||||
SDLNet_Quit();
|
|
||||||
SDL_Quit();
|
|
||||||
|
|
||||||
std::cout << "Shutdown finished" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Define the uber switch
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void ServerApplication::HandlePacket(SerialPacket packet) {
|
|
||||||
switch(packet.meta.type) {
|
|
||||||
case SerialPacket::Type::BROADCAST_REQUEST:
|
|
||||||
HandleBroadcastRequest(packet);
|
|
||||||
break;
|
|
||||||
case SerialPacket::Type::JOIN_REQUEST:
|
|
||||||
HandleJoinRequest(packet);
|
|
||||||
break;
|
|
||||||
case SerialPacket::Type::SYNCHRONIZE:
|
|
||||||
HandleSynchronize(packet);
|
|
||||||
break;
|
|
||||||
case SerialPacket::Type::DISCONNECT:
|
|
||||||
HandleDisconnect(packet);
|
|
||||||
break;
|
|
||||||
case SerialPacket::Type::SHUTDOWN:
|
|
||||||
HandleShutdown(packet);
|
|
||||||
break;
|
|
||||||
case SerialPacket::Type::CHARACTER_UPDATE:
|
|
||||||
HandleCharacterUpdate(packet);
|
|
||||||
break;
|
|
||||||
case SerialPacket::Type::REGION_REQUEST:
|
|
||||||
HandleRegionRequest(packet);
|
|
||||||
break;
|
|
||||||
//handle errors
|
|
||||||
default:
|
|
||||||
throw(std::runtime_error("Unknown SerialPacketType encountered"));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user