Horizon Official Technical Documentation
Horizon::Zone::ZoneServer Class Reference

#include <Zone.hpp>

+ Inheritance diagram for Horizon::Zone::ZoneServer:
+ Collaboration diagram for Horizon::Zone::ZoneServer:

Public Member Functions

 ZoneServer ()
 Zone Main server constructor. More...
 
 ~ZoneServer ()
 Zone Main server destructor. More...
 
bool read_config ()
 Read /config/zone-server.yaml. More...
 
void initialize ()
 
void finalize ()
 
void update (int64_t diff)
 
s_zone_server_configurationconfig ()
 
s_game_process_configurationgame_config ()
 
uint64_t to_uuid (uint8_t type, uint32_t uid, uint16_t uid2, uint8_t uid3)
 
void from_uuid (uint64_t unit_uuid, uint8_t &type, uint32_t &uid, uint16_t &uid2, uint8_t &uid3)
 
ClientSocketMgrget_client_socket_mgr ()
 
- Public Member Functions inherited from Horizon::Zone::ZoneKernel
 ZoneKernel (s_zone_server_configuration &config)
 
 ~ZoneKernel ()
 
void initialize ()
 
void finalize ()
 
s_zone_server_configurationconfig ()
 
void set_config (s_zone_server_configuration &config)
 
TaskSchedulergetScheduler ()
 
void verify_connected_sessions ()
 
- Public Member Functions inherited from Server
 Server ()
 
 ~Server ()
 
void parse_exec_args (const char *argv[], int argc)
 
virtual void initialize ()
 
virtual void finalize ()
 
virtual void post_initialize ()
 
virtual void post_finalize ()
 
void print_help ()
 
struct general_server_configurationgeneral_conf ()
 
bool parse_common_configs (sol::table &cfg)
 
std::shared_ptr< boost::mysql::tcp_ssl_connection > get_database_connection ()
 
bool test_database_connection ()
 
- Public Member Functions inherited from Kernel
 Kernel (general_server_configuration &config)
 
 ~Kernel ()
 
virtual void initialize ()=0
 
virtual void finalize ()=0
 
virtual void post_initialize ()=0
 
virtual void post_finalize ()=0
 
struct general_server_configurationgeneral_conf ()
 
template<typename T >
std::shared_ptr< T > get_component (std::string uuid)
 
template<typename T >
std::shared_ptr< T > get_component_of_type (Horizon::System::runtime_module_type type, int segment_number=1)
 
template<typename T >
void register_component (Horizon::System::runtime_module_type type, T &&component)
 
template<typename T >
void register_component (Horizon::System::runtime_module_type type, std::shared_ptr< T > component)
 
void deregister_component (Horizon::System::runtime_module_type type, int segment_number=1)
 
int get_registered_component_count_of_type (Horizon::System::runtime_module_type type)
 
template<typename ComponentType , std::size_t Priority, typename Key , typename Value >
int get_segment_number_for_resource (Horizon::System::runtime_module_type module_t, Key resource_key, Value resource_not_found_value)
 
int get_component_count ()
 
KernelComponentsget_components ()
 
void system_routine_queue_push (std::shared_ptr< Horizon::System::RuntimeContext > context)
 
void system_routine_queue_push (std::shared_ptr< Horizon::System::RuntimeContextChain > context)
 
void system_routine_process_queue ()
 
void system_routine_register (Horizon::System::runtime_module_type module_t, Horizon::System::runtime_synchronization_method sync_t, std::shared_ptr< Horizon::System::RuntimeContext > context)
 
Horizon::System::SystemRoutineManagerget_system_routine_manager ()
 
boost::asio::io_context & get_io_context ()
 
void set_signal_interrupt_command_line_loop (bool signal)
 
bool get_signal_interrupt_command_line_loop ()
 

Static Public Member Functions

static ZoneServergetInstance ()
 

Private Attributes

s_zone_server_configuration _zone_server_config
 
s_game_process_configuration _game_process_config
 
ClientSocketMgr _client_socket_mgr
 
boost::asio::deadline_timer _update_timer
 

Additional Inherited Members

- Protected Attributes inherited from Horizon::Zone::ZoneKernel
TaskScheduler _task_scheduler
 
s_zone_server_configuration _config
 
- Protected Attributes inherited from Server
struct general_server_configuration general_config
 
- Protected Attributes inherited from Kernel
boost::asio::io_context _io_context_global
 
KernelComponents _components
 
general_server_configuration _config
 

Constructor & Destructor Documentation

◆ ZoneServer()

ZoneServer::ZoneServer ( )

Zone Main server constructor.


105{
106}
ZoneKernel(s_zone_server_configuration &config)
Definition: Zone.cpp:42
s_zone_server_configuration _zone_server_config
Definition: Zone.hpp:239
boost::asio::deadline_timer _update_timer
Definition: Zone.hpp:242
boost::asio::io_context & get_io_context()
Definition: Server.cpp:70

◆ ~ZoneServer()

ZoneServer::~ZoneServer ( )

Zone Main server destructor.

112{
113}

Member Function Documentation

◆ config()

s_zone_server_configuration & Horizon::Zone::ZoneServer::config ( )
inline
230{ return _zone_server_config; }

References _zone_server_config.

Referenced by finalize(), initialize(), and read_config().

+ Here is the caller graph for this function:

◆ finalize()

void ZoneServer::finalize ( )
virtual

Reimplemented from Horizon::Zone::ZoneKernel.

294{
295 HLog(info) << "Server shutdown initiated ...";
296
297 if (!get_io_context().stopped())
298 get_io_context().stop();
299
300 get_component_of_type<Horizon::Zone::ZoneRuntime>(Horizon::System::RUNTIME_RUNTIME)->finalize();
302
303 for (int i = 0; i < config().max_game_logic_threads(); i++) {
304 get_component_of_type<GameLogicProcess>(Horizon::System::RUNTIME_GAMELOGIC, i + 1)->finalize();
306 }
307 for (int i = 0; i < config().max_persistence_threads(); i++) {
308 get_component_of_type<PersistenceManager>(Horizon::System::RUNTIME_PERSISTENCE, i + 1)->finalize();
310 }
311 for (int i = 0; i < config().max_script_vm_threads(); i++) {
312 get_component_of_type<ScriptManager>(Horizon::System::RUNTIME_SCRIPTVM, i + 1)->finalize();
314 }
315
317
319}
#define HLog(type)
Definition: Logger.hpp:122
void finalize()
Definition: Zone.cpp:64
s_zone_server_configuration & config()
Definition: Zone.hpp:230
void deregister_component(Horizon::System::runtime_module_type type, int segment_number=1)
Definition: Server.hpp:493
virtual void post_finalize()
Definition: Server.cpp:459
@ RUNTIME_SCRIPTVM
Definition: System.hpp:87
@ RUNTIME_GAMELOGIC
Definition: System.hpp:86
@ RUNTIME_PERSISTENCE
Definition: System.hpp:85
@ RUNTIME_RUNTIME
Definition: System.hpp:92
int max_persistence_threads()
Definition: Zone.hpp:134
int max_script_vm_threads()
Definition: Zone.hpp:137
int max_game_logic_threads()
Definition: Zone.hpp:131

References config(), Kernel::deregister_component(), Horizon::Zone::ZoneKernel::finalize(), Kernel::get_io_context(), HLog, Horizon::Zone::s_zone_server_configuration::max_game_logic_threads(), Horizon::Zone::s_zone_server_configuration::max_persistence_threads(), Horizon::Zone::s_zone_server_configuration::max_script_vm_threads(), Server::post_finalize(), Horizon::System::RUNTIME_GAMELOGIC, Horizon::System::RUNTIME_PERSISTENCE, Horizon::System::RUNTIME_RUNTIME, and Horizon::System::RUNTIME_SCRIPTVM.

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ from_uuid()

void ZoneServer::from_uuid ( uint64_t  unit_uuid,
uint8_t &  type,
uint32_t &  uid,
uint16_t &  uid2,
uint8_t &  uid3 
)
186{
187 type = (uint8_t) unit_uuid;
188 uid = (uint32_t) (unit_uuid >> 40);
189 uid2 = (uint16_t) (unit_uuid >> 8);
190 uid3 = (uint8_t) (unit_uuid >> 56);
191}
Definition: Unit.hpp:80

◆ game_config()

s_game_process_configuration & Horizon::Zone::ZoneServer::game_config ( )
inline
231{ return _game_process_config; }
s_game_process_configuration _game_process_config
Definition: Zone.hpp:240

References _game_process_config.

Referenced by initialize(), and read_config().

+ Here is the caller graph for this function:

◆ get_client_socket_mgr()

ClientSocketMgr & Horizon::Zone::ZoneServer::get_client_socket_mgr ( )
inline
236{ return _client_socket_mgr; }
ClientSocketMgr _client_socket_mgr
Definition: Zone.hpp:241

References _client_socket_mgr.

Referenced by update().

+ Here is the caller graph for this function:

◆ getInstance()

static ZoneServer * Horizon::Zone::ZoneServer::getInstance ( )
inlinestatic
219 {
220 static ZoneServer instance;
221 return &instance;
222 }
ZoneServer()
Zone Main server constructor.
Definition: Zone.cpp:103

◆ initialize()

void ZoneServer::initialize ( )
virtual

Reimplemented from Horizon::Zone::ZoneKernel.

237{
238 // Install a signal handler
239 signal(SIGINT, SignalHandler);
240#ifndef WIN32
241 signal(SIGQUIT, SignalHandler);
242#endif
243 signal(SIGTERM, SignalHandler);
244
245 // We declare this here since the responsibility of starting the network is of
246 // the Runtime level. It must remain lower than the kernel, because
247 // we're using the kernel's io_context and it will be destroyed after the destruction of
248 // the ClientSocketMgr class. This fixes the issue of dangling pointers and memory access violations.
249 sZone->get_client_socket_mgr().start(get_io_context(),
250 sZone->general_conf().get_listen_ip(),
251 general_conf().get_listen_port(),
252 config().max_network_threads(),
253 general_conf().is_test_run() && general_conf().is_test_run_with_network() == false);
254
255 // Initialize Runtime
256 register_component<Horizon::Zone::ZoneRuntime>(Horizon::System::RUNTIME_RUNTIME, std::make_shared<Horizon::Zone::ZoneRuntime>());
257 get_component_of_type<Horizon::Zone::ZoneRuntime>(Horizon::System::RUNTIME_RUNTIME)->initialize();
258
259 for (int i = 0; i < config().max_game_logic_threads(); i++) {
260 register_component(Horizon::System::RUNTIME_GAMELOGIC, std::make_shared<GameLogicProcess>(game_config()));
261 get_component_of_type<GameLogicProcess>(Horizon::System::RUNTIME_GAMELOGIC, i + 1)->initialize(i + 1);
262 }
263
264 for (int i = 0; i < config().max_script_vm_threads(); i++) {
265 register_component(Horizon::System::RUNTIME_SCRIPTVM, std::make_shared<ScriptManager>());
266 get_component_of_type<ScriptManager>(Horizon::System::RUNTIME_SCRIPTVM, i + 1)->initialize(i + 1);
267 }
268
269 for (int i = 0; i < config().max_persistence_threads(); i++) {
270 register_component(Horizon::System::RUNTIME_PERSISTENCE, std::make_shared<PersistenceManager>());
271 get_component_of_type<PersistenceManager>(Horizon::System::RUNTIME_PERSISTENCE, i + 1)->initialize(i + 1);
272 }
273
274 _update_timer.expires_from_now(boost::posix_time::microseconds(MAX_CORE_UPDATE_INTERVAL));
275 _update_timer.async_wait(boost::bind(&ZoneServer::update, this, std::time(nullptr)));
276
278
280
281 // Run the io_context until stop is called from the internal, finalizing thread.
282 // After stopping, execution will continue through the next line onwards.
283 // We actually finalize on this thread and not in any of io_context's internal threads.
284 get_io_context().run();
285 /*
286 * Core Cleanup
287 */
288 HLog(info) << "Server shutting down...";
289
291}
#define MAX_CORE_UPDATE_INTERVAL
Definition: Horizon.hpp:63
#define HLogShutdown
Definition: Logger.hpp:123
void SignalHandler(int signal_num)
Signal Handler for the Zone-Server main thread.
Definition: Zone.cpp:197
#define sZone
Definition: Zone.hpp:247
void initialize()
Definition: Zone.cpp:54
s_game_process_configuration & game_config()
Definition: Zone.hpp:231
void update(int64_t diff)
Definition: Zone.cpp:209
void register_component(Horizon::System::runtime_module_type type, T &&component)
Definition: Server.hpp:475
struct general_server_configuration & general_conf()
Definition: Server.hpp:570
virtual void post_initialize()
Definition: Server.cpp:450

References _update_timer, config(), game_config(), Server::general_conf(), Kernel::get_io_context(), HLog, HLogShutdown, Horizon::Zone::ZoneKernel::initialize(), MAX_CORE_UPDATE_INTERVAL, Horizon::Zone::s_zone_server_configuration::max_game_logic_threads(), Horizon::Zone::s_zone_server_configuration::max_persistence_threads(), Horizon::Zone::s_zone_server_configuration::max_script_vm_threads(), Server::post_initialize(), Kernel::register_component(), Horizon::System::RUNTIME_GAMELOGIC, Horizon::System::RUNTIME_PERSISTENCE, Horizon::System::RUNTIME_RUNTIME, Horizon::System::RUNTIME_SCRIPTVM, SignalHandler(), sZone, and update().

+ Here is the call graph for this function:

◆ read_config()

bool ZoneServer::read_config ( )

Read /config/zone-server.yaml.

Returns
false on failure, true on success.

Process Configuration that is common between servers.

123{
124 using namespace std::chrono;
125
126 sol::state lua;
127 std::string file_path = general_conf().get_config_file_path().string();
128
129 // Read the file. If there is an error, report it and exit.
130 try {
131 lua.script_file(file_path);
132 } catch(const std::exception &e) {
133 HLog(error) << "ZoneMain::ReadConfig: " << e.what() << ".";
134 return false;
135 }
136
137 sol::table tbl = lua["horizon_config"];
138
139 config().set_static_db_path(tbl.get_or("static_db_path", std::string("db/")));
140 HLog(info) << "Static database path set to " << config().get_static_db_path() << "";
141
142 config().set_mapcache_path(tbl.get_or("map_cache_file_path", std::string("db/maps.dat")));
143 HLog(info) << "Mapcache file name is set to " << config().get_mapcache_path() << ", it will be read while initializing maps.";
144
145 config().set_script_root_path(tbl.get_or("script_root_path", std::string("scripts/")));
146 HLog(info) << "Script root path is set to " << config().get_script_root_path() << ", it will be used for all scripting references.";
147
148 config().set_session_max_timeout(tbl.get_or("session_max_timeout", 60));
149 HLog(info) << "Session maximum timeout set to '" << config().session_max_timeout() << "'.";
150
151 config().set_max_network_threads(tbl.get_or("max_network_threads", 1));
152 HLog(info) << "Maximum network threads set to '" << config().max_network_threads() << "'.";
153
154 config().set_max_game_logic_threads(tbl.get_or("max_game_logic_threads", 1));
155 HLog(info) << "Maximum game logic threads set to '" << config().max_game_logic_threads() << "'.";
156
157 config().set_max_persistence_threads(tbl.get_or("max_persistence_threads", 1));
158 HLog(info) << "Maximum persistence threads set to '" << config().max_persistence_threads() << "'.";
159
160 config().set_max_script_vm_threads(tbl.get_or("max_script_vm_threads", 1));
161 HLog(info) << "Maximum script VM threads set to '" << config().max_script_vm_threads() << "'.";
162
163 game_config().set_natural_heal_hp_interval(tbl.get_or("natural_heal_hp_interval", 6000));
164 HLog(info) << "Natural heal HP interval set to '" << game_config().get_natural_heal_hp_interval() << "' milliseconds.";
165
166 game_config().set_natural_heal_sp_interval(tbl.get_or("natural_heal_sp_interval", 8000));
167 HLog(info) << "Natural heal SP interval set to '" << game_config().get_natural_heal_sp_interval() << "' milliseconds.";
168
172 if (!parse_common_configs(tbl))
173 return false;
174
175 HLog(info) << "Done reading server configurations from '" << file_path << "'.";
176
177 return true;
178}
bool parse_common_configs(sol::table &cfg)
Definition: Server.cpp:372
void set_natural_heal_hp_interval(int interval)
Definition: Zone.hpp:61
void set_natural_heal_sp_interval(int interval)
Definition: Zone.hpp:62
int get_natural_heal_sp_interval()
Definition: Zone.hpp:65
int get_natural_heal_hp_interval()
Definition: Zone.hpp:64
int max_network_threads()
Definition: Zone.hpp:128
void set_session_max_timeout(std::time_t timeout)
Definition: Zone.hpp:126
void set_max_script_vm_threads(int threads)
Definition: Zone.hpp:138
boost::filesystem::path & get_script_root_path()
Definition: Zone.hpp:122
std::time_t session_max_timeout()
Definition: Zone.hpp:125
void set_script_root_path(boost::filesystem::path p)
Definition: Zone.hpp:123
boost::filesystem::path & get_static_db_path()
Definition: Zone.hpp:119
boost::filesystem::path & get_mapcache_path()
Definition: Zone.hpp:116
void set_mapcache_path(boost::filesystem::path p)
Definition: Zone.hpp:117
void set_max_game_logic_threads(int threads)
Definition: Zone.hpp:132
void set_max_network_threads(int threads)
Definition: Zone.hpp:129
void set_static_db_path(boost::filesystem::path p)
Definition: Zone.hpp:120
void set_max_persistence_threads(int threads)
Definition: Zone.hpp:135
const boost::filesystem::path & get_config_file_path() const
Definition: ServerConfiguration.hpp:55

References config(), game_config(), Server::general_conf(), general_server_configuration::get_config_file_path(), Horizon::Zone::s_zone_server_configuration::get_mapcache_path(), Horizon::Zone::s_game_process_configuration::get_natural_heal_hp_interval(), Horizon::Zone::s_game_process_configuration::get_natural_heal_sp_interval(), Horizon::Zone::s_zone_server_configuration::get_script_root_path(), Horizon::Zone::s_zone_server_configuration::get_static_db_path(), HLog, Horizon::Zone::s_zone_server_configuration::max_game_logic_threads(), Horizon::Zone::s_zone_server_configuration::max_network_threads(), Horizon::Zone::s_zone_server_configuration::max_persistence_threads(), Horizon::Zone::s_zone_server_configuration::max_script_vm_threads(), Server::parse_common_configs(), Horizon::Zone::s_zone_server_configuration::session_max_timeout(), Horizon::Zone::s_zone_server_configuration::set_mapcache_path(), Horizon::Zone::s_zone_server_configuration::set_max_game_logic_threads(), Horizon::Zone::s_zone_server_configuration::set_max_network_threads(), Horizon::Zone::s_zone_server_configuration::set_max_persistence_threads(), Horizon::Zone::s_zone_server_configuration::set_max_script_vm_threads(), Horizon::Zone::s_game_process_configuration::set_natural_heal_hp_interval(), Horizon::Zone::s_game_process_configuration::set_natural_heal_sp_interval(), Horizon::Zone::s_zone_server_configuration::set_script_root_path(), Horizon::Zone::s_zone_server_configuration::set_session_max_timeout(), and Horizon::Zone::s_zone_server_configuration::set_static_db_path().

+ Here is the call graph for this function:

◆ to_uuid()

uint64_t ZoneServer::to_uuid ( uint8_t  type,
uint32_t  uid,
uint16_t  uid2,
uint8_t  uid3 
)
181{
182 return ((uint64_t)uid3 << 56 | ((uint64_t)uid2 << 8) | ((uint64_t)uid << 40) | (uint64_t) type);
183}

◆ update()

void ZoneServer::update ( int64_t  diff)

Process Packets.

210{
214 sZone->get_client_socket_mgr().manage_sockets(diff);
215 sZone->get_client_socket_mgr().update_sessions(diff);
216
217 // Process Horizon System Routine Queue.
218 sZone->system_routine_process_queue();
219
220 if (get_shutdown_stage() == SHUTDOWN_NOT_STARTED && !general_conf().is_test_run_minimal()) {
221 _update_timer.expires_from_now(boost::posix_time::microseconds(MAX_CORE_UPDATE_INTERVAL));
222 _update_timer.async_wait(boost::bind(&ZoneServer::update, this, std::time(nullptr)));
224 } else {
225 for (auto i = _components.begin(); i != _components.end(); i++) {
226 HLog(info) << "Kernel component '" << i->second.ptr->get_type_string() << " (" << i->second.segment_number << ")': " << (i->second.ptr->is_finalized() == true ? "Offline" : "Online (Shutting Down)") << " { CPU: " << i->second.ptr->get_thread_cpu_id() << " , uuid: " << i->first << " }";
227 }
228 // Stop the client socket manager here because the io_context will be stopped later.
229 // If this is stopped before the io_context, it will cause a dangling pointer.
231
232 finalize();
233 }
234}
shutdown_stages get_shutdown_stage()
Definition: Server.hpp:74
@ SHUTDOWN_NOT_STARTED
Definition: Server.hpp:64
bool stop()
Definition: ClientSocketMgr.cpp:22
TaskScheduler _task_scheduler
Definition: Zone.hpp:167
void finalize()
Definition: Zone.cpp:293
ClientSocketMgr & get_client_socket_mgr()
Definition: Zone.hpp:236
KernelComponents _components
Definition: Server.hpp:545
TaskScheduler & Update(success_t const &callback=EmptyCallback)
Update the scheduler to the current time. Calls the optional callback on successfully finish.
Definition: TaskScheduler.cpp:25

References Kernel::_components, Horizon::Zone::ZoneKernel::_task_scheduler, _update_timer, finalize(), Server::general_conf(), get_client_socket_mgr(), get_shutdown_stage(), HLog, MAX_CORE_UPDATE_INTERVAL, SHUTDOWN_NOT_STARTED, Horizon::Zone::ClientSocketMgr::stop(), sZone, update(), and TaskScheduler::Update().

Referenced by initialize(), and update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ _client_socket_mgr

ClientSocketMgr Horizon::Zone::ZoneServer::_client_socket_mgr
private

Referenced by get_client_socket_mgr().

◆ _game_process_config

s_game_process_configuration Horizon::Zone::ZoneServer::_game_process_config
private

Referenced by game_config().

◆ _update_timer

boost::asio::deadline_timer Horizon::Zone::ZoneServer::_update_timer
private

Referenced by initialize(), and update().

◆ _zone_server_config

s_zone_server_configuration Horizon::Zone::ZoneServer::_zone_server_config
private

Referenced by config().


The documentation for this class was generated from the following files: