Horizon Official Technical Documentation
Horizon::Char::CharServer Class Reference

#include <Char.hpp>

+ Inheritance diagram for Horizon::Char::CharServer:
+ Collaboration diagram for Horizon::Char::CharServer:

Classes

struct  character_server_configuration
 

Public Member Functions

 CharServer ()
 Char Constructor. More...
 
 ~CharServer ()
 Char Destructor. More...
 
bool read_config ()
 Read /config/char-server.yaml. More...
 
void initialize_cli_commands ()
 
void initialize () override
 
void finalize () override
 
character_server_configurationconfig ()
 
TaskSchedulergetScheduler ()
 
void update (uint64_t time)
 
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 CharServergetInstance ()
 

Private Types

enum  character_server_type : uint16_t {
  CHAR_SERVER_TYPE_NORMAL , CHAR_SERVER_TYPE_MAINTENANCE , CHAR_SERVER_TYPE_OVER18 , CHAR_SERVER_TYPE_PAYING ,
  CHAR_SERVER_TYPE_FREE2PLAY , CHAR_SERVER_TYPE_MAX
}
 

Private Attributes

character_server_configuration _char_server_config
 
TaskScheduler _task_scheduler
 
boost::asio::deadline_timer _update_timer
 

Additional Inherited Members

- 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
 

Member Enumeration Documentation

◆ character_server_type

Enumerator
CHAR_SERVER_TYPE_NORMAL 
CHAR_SERVER_TYPE_MAINTENANCE 
CHAR_SERVER_TYPE_OVER18 
CHAR_SERVER_TYPE_PAYING 
CHAR_SERVER_TYPE_FREE2PLAY 
CHAR_SERVER_TYPE_MAX 
45{
52};
@ CHAR_SERVER_TYPE_OVER18
Definition: Char.hpp:48
@ CHAR_SERVER_TYPE_PAYING
Definition: Char.hpp:49
@ CHAR_SERVER_TYPE_MAX
Definition: Char.hpp:51
@ CHAR_SERVER_TYPE_MAINTENANCE
Definition: Char.hpp:47
@ CHAR_SERVER_TYPE_NORMAL
Definition: Char.hpp:46
@ CHAR_SERVER_TYPE_FREE2PLAY
Definition: Char.hpp:50

Constructor & Destructor Documentation

◆ CharServer()

CharServer::CharServer ( )

Char Constructor.

50{
51 //
52}
boost::asio::deadline_timer _update_timer
Definition: Char.hpp:138
boost::asio::io_context & get_io_context()
Definition: Server.cpp:70
Server()
Definition: Server.cpp:300

◆ ~CharServer()

CharServer::~CharServer ( )

Char Destructor.

58{
59 //
60}

Member Function Documentation

◆ config()

character_server_configuration & Horizon::Char::CharServer::config ( )
inline
127{ return _char_server_config; }
character_server_configuration _char_server_config
Definition: Char.hpp:136

References _char_server_config.

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

+ Here is the caller graph for this function:

◆ finalize()

void CharServer::finalize ( )
overridevirtual

Server shutdown routine begins here...

Reimplemented from Server.

306{
307 HLog(info) << "Shutdown process initiated...";
308
309 if (!get_io_context().stopped())
310 get_io_context().stop();
311
316
318}
#define HLog(type)
Definition: Logger.hpp:122
virtual void post_finalize()
Definition: Server.cpp:459
virtual void finalize()
Definition: Server.cpp:439

References Server::finalize(), Kernel::get_io_context(), HLog, and Server::post_finalize().

Referenced by update().

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

◆ getInstance()

static CharServer * Horizon::Char::CharServer::getInstance ( )
inlinestatic
116 {
117 static CharServer instance;
118 return &instance;
119 }
CharServer()
Char Constructor.
Definition: Char.cpp:48

◆ getScheduler()

TaskScheduler & Horizon::Char::CharServer::getScheduler ( )
inline
129{ return _task_scheduler; }
TaskScheduler _task_scheduler
Definition: Char.hpp:137

References _task_scheduler.

Referenced by initialize(), and update().

+ Here is the caller graph for this function:

◆ initialize()

void CharServer::initialize ( )
overridevirtual

Reimplemented from Server.

265{
266 /* Core Signal Handler */
267 signal(SIGINT, SignalHandler);
268 signal(SIGTERM, SignalHandler);
269#ifndef WIN32
270 signal(SIGQUIT, SignalHandler);
271#endif
272
273 /* Start Character Network */
275 general_conf().get_listen_ip(),
276 general_conf().get_listen_port(),
277 config().max_network_threads(),
278 general_conf().is_test_run() && general_conf().is_test_run_with_network() == false);
279
281
282 getScheduler().Schedule(Seconds(60), [this] (TaskContext context) {
284 context.Repeat();
285 });
286
287 _update_timer.expires_from_now(boost::posix_time::seconds(0));
288 _update_timer.async_wait(std::bind(&CharServer::update, this, std::time(nullptr)));
289
291
292 // Run the io_context until stop is called from the internal, finalizing thread.
293 // After stopping, execution will continue through the next line onwards.
294 // We actually finalize on this thread and not in any of io_context's internal threads.
295 get_io_context().run();
296
297 /* Core Cleanup */
298 HLog(info) << "Server shutting down...";
299
301
303}
#define sClientSocketMgr
Definition: ClientSocketMgr.hpp:176
void SignalHandler(int signal_num)
Signal handler for the Char-Server main thread.
Definition: Char.cpp:193
#define HLogShutdown
Definition: Logger.hpp:123
@ SHUTDOWN_CLEANUP_COMPLETE
Definition: Server.hpp:66
void set_shutdown_stage(shutdown_stages new_stage)
Definition: Server.hpp:75
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition: TaskScheduler.hpp:34
TaskScheduler & getScheduler()
Definition: Char.hpp:129
character_server_configuration & config()
Definition: Char.hpp:127
void verify_connected_sessions()
Definition: Char.cpp:205
void update(uint64_t time)
Definition: Char.cpp:236
virtual void initialize()
Definition: Server.cpp:425
struct general_server_configuration & general_conf()
Definition: Server.hpp:570
virtual void post_initialize()
Definition: Server.cpp:450
Definition: TaskScheduler.hpp:487
TaskContext & Repeat(std::chrono::duration< _Rep, _Period > const &duration)
Repeats the event and sets a new duration. std::chrono::seconds(5) for example. This will consume the...
Definition: TaskScheduler.hpp:560
TaskScheduler & Schedule(std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Sch...
Definition: TaskScheduler.hpp:278

References _update_timer, config(), Server::general_conf(), Kernel::get_io_context(), getScheduler(), HLog, HLogShutdown, Server::initialize(), Server::post_initialize(), TaskContext::Repeat(), TaskScheduler::Schedule(), sClientSocketMgr, set_shutdown_stage(), SHUTDOWN_CLEANUP_COMPLETE, SignalHandler(), update(), and verify_connected_sessions().

+ Here is the call graph for this function:

◆ initialize_cli_commands()

void CharServer::initialize_cli_commands ( )
185{
186}

◆ read_config()

bool CharServer::read_config ( )

Read /config/char-server.yaml.

Returns
true on success false on failure.

Process Configuration that is common between servers.

70{
71 sol::state lua;
72 std::string file_path = general_conf().get_config_file_path().string();
73
74 // Read the file. If there is an error, report it and exit.
75 try {
76 lua.script_file(file_path);
77 } catch(const std::exception &e) {
78 HLog(error) << "Char::ReadConfig: " << e.what();
79 return false;
80 }
81
82 sol::table tbl = lua["horizon_config"];
83
84 try {
85 sol::table chtbl = tbl.get<sol::table>("new_character");
86 config().set_start_map(chtbl.get_or("start_map", std::string("new_1-1")));
87 config().set_start_x(chtbl.get_or("start_x", 53));
88 config().set_start_y(chtbl.get_or("start_y", 111));
89 config().set_start_zeny(chtbl.get_or("start_zeny", 0));
90
91 sol::optional<sol::table> maybe_chitbl = chtbl.get<sol::optional<sol::table>>("start_items");
92
93 if (maybe_chitbl) {
94 sol::table chitbl = maybe_chitbl.value();
95 chitbl.for_each([this](sol::object const &key, sol::object const &value) {
96 config().add_start_item(std::make_pair(key.as<int>(), value.as<int>()));
97 });
98 if (chitbl.size() < 1) {
99 config().add_start_item(std::make_pair(1201, 1));
100 config().add_start_item(std::make_pair(2301, 1));
101 }
102 }
103 } catch (sol::error &err) {
104 HLog(error) << "Error for setting in new_character:" << err.what();
105 }
106
107 try {
108 int char_deletion_time = tbl.get_or("character_deletion_time", 86400);
109 config().set_character_deletion_time(char_deletion_time);
110 HLog(info) << "Character deletion wait period : '" << char_deletion_time << "'.";
111 } catch (sol::error &err) {
112 HLog(error) << "Error for setting character_deletion_time:" << err.what();
113 }
114
115 try {
116 bool char_hard_delete = tbl.get_or("character_hard_delete", false);
117 config().set_char_hard_delete(char_hard_delete);
118 HLog(info) << "Character hard delete (permanently deleted) : '" << (char_hard_delete ? "True" : "False") << "'.";
119 } catch (sol::error &err) {
120 HLog(error) << "Error for setting character_hard_delete:" << err.what();
121 }
122
123 try {
124 sol::table zone_tbl = tbl.get<sol::table>("zone_server");
125
126 std::string ip_addr = zone_tbl.get_or("ip_address", std::string("127.0.0.1"));
127 int32_t port = zone_tbl.get_or("port", 5121);
128
129 config().set_zone_server_ip(ip_addr);
131 HLog(info) << "Zone Server set to '" << config().zone_server_ip() << "' port: " << config().zone_server_port() << ".";
132 } catch (sol::error &err) {
133 HLog(error) << "Error for setting zone_server:" << err.what();
134 }
135
136 try {
137 int32_t pincode_expiry = tbl.get<int32_t>("pincode_expiry");
138 config().set_pincode_expiry(pincode_expiry);
139 HLog(info) << "Pincode expiry to '" << config().pincode_expiry() << "'.";
140 } catch (sol::error &err) {
141 HLog(error) << "Error for setting pincode_expiry:" << err.what();
142 }
143
144 try {
145 int32_t pincode_max_retry = tbl.get<uint32_t>("pincode_max_retry");
146 config().set_pincode_max_retry(pincode_max_retry);
147 HLog(info) << "Pincode max retry to '" << config().pincode_max_retry() << "'.";
148 } catch (sol::error &err) {
149 HLog(error) << "Error for setting pincode_max_retry:" << err.what();
150 }
151
152 try {
153 int32_t session_max_timeout = tbl.get_or("session_max_timeout", 60);
154 config().set_session_max_timeout(session_max_timeout);
155 HLog(info) << "Session maximum timeout set to '" << config().session_max_timeout() << "',"
156 << " sessions surpassing this time limit will be disconnected.";
157 } catch (sol::error &err) {
158 HLog(error) << "Error for setting session_max_timeout:" << err.what();
159 }
160
161 // Max Network Threads
162 try {
163 int32_t max_network_threads = tbl.get_or("max_network_threads", 1);
164 config().set_max_network_threads(max_network_threads);
165 HLog(info) << "Max Network Threads set to '" << config().max_network_threads() << "'.";
166 } catch (sol::error &err) {
167 HLog(error) << "Error for setting max_network_threads:" << err.what();
168 }
169
173 if (!parse_common_configs(tbl))
174 return false;
175
176 HLog(info) << "Done reading server configurations from '" << file_path << "'.";
177
178 return true;
179}
bool parse_common_configs(sol::table &cfg)
Definition: Server.cpp:372
void set_zone_server_port(int16_t port)
Definition: Char.hpp:80
void set_pincode_expiry(int32_t pincode_expiry)
Definition: Char.hpp:86
void set_pincode_max_retry(int32_t retry)
Definition: Char.hpp:89
std::time_t session_max_timeout()
Definition: Char.hpp:91
void set_char_hard_delete(bool d)
Definition: Char.hpp:83
void set_start_y(int16_t y)
Definition: Char.hpp:64
void set_start_zeny(int32_t zeny)
Definition: Char.hpp:67
void add_start_item(std::pair< int32_t, int32_t > item)
Definition: Char.hpp:69
int16_t zone_server_port() const
Definition: Char.hpp:79
void set_character_deletion_time(time_t time)
Definition: Char.hpp:74
void set_zone_server_ip(std::string const &ip)
Definition: Char.hpp:77
void set_session_max_timeout(std::time_t timeout)
Definition: Char.hpp:92
const std::string & zone_server_ip() const
Definition: Char.hpp:76
void set_start_map(std::string const &map)
Definition: Char.hpp:58
void set_max_network_threads(int threads)
Definition: Char.hpp:95
void set_start_x(int16_t x)
Definition: Char.hpp:61
const boost::filesystem::path & get_config_file_path() const
Definition: ServerConfiguration.hpp:55

References Horizon::Char::CharServer::character_server_configuration::add_start_item(), config(), Server::general_conf(), general_server_configuration::get_config_file_path(), HLog, Horizon::Char::CharServer::character_server_configuration::max_network_threads(), Server::parse_common_configs(), Horizon::Char::CharServer::character_server_configuration::pincode_expiry(), Horizon::Char::CharServer::character_server_configuration::pincode_max_retry(), Horizon::Char::CharServer::character_server_configuration::session_max_timeout(), Horizon::Char::CharServer::character_server_configuration::set_char_hard_delete(), Horizon::Char::CharServer::character_server_configuration::set_character_deletion_time(), Horizon::Char::CharServer::character_server_configuration::set_max_network_threads(), Horizon::Char::CharServer::character_server_configuration::set_pincode_expiry(), Horizon::Char::CharServer::character_server_configuration::set_pincode_max_retry(), Horizon::Char::CharServer::character_server_configuration::set_session_max_timeout(), Horizon::Char::CharServer::character_server_configuration::set_start_map(), Horizon::Char::CharServer::character_server_configuration::set_start_x(), Horizon::Char::CharServer::character_server_configuration::set_start_y(), Horizon::Char::CharServer::character_server_configuration::set_start_zeny(), Horizon::Char::CharServer::character_server_configuration::set_zone_server_ip(), Horizon::Char::CharServer::character_server_configuration::set_zone_server_port(), Horizon::Char::CharServer::character_server_configuration::zone_server_ip(), and Horizon::Char::CharServer::character_server_configuration::zone_server_port().

+ Here is the call graph for this function:

◆ update()

void CharServer::update ( uint64_t  time)

Cancel all pending tasks.

Stop all networks

237{
239
240 sClientSocketMgr->manage_sockets(time);
241 sClientSocketMgr->update_sessions(time);
242
243 if (get_shutdown_stage() == SHUTDOWN_NOT_STARTED && !general_conf().is_test_run_minimal()) {
244 _update_timer.expires_from_now(boost::posix_time::microseconds(MAX_CORE_UPDATE_INTERVAL));
245 _update_timer.async_wait(std::bind(&CharServer::update, this, std::time(nullptr)));
246 } else {
247 for (auto i = _components.begin(); i != _components.end(); i++) {
248 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 << " }";
249 }
254
258 sClientSocketMgr->stop();
259
260 finalize();
261 }
262}
#define MAX_CORE_UPDATE_INTERVAL
Definition: Horizon.hpp:63
shutdown_stages get_shutdown_stage()
Definition: Server.hpp:74
@ SHUTDOWN_NOT_STARTED
Definition: Server.hpp:64
void finalize() override
Definition: Char.cpp:305
KernelComponents _components
Definition: Server.hpp:545
TaskScheduler & CancelAll()
Cancels all tasks. Never call this from within a task context! Use TaskContext::CancelAll instead!
Definition: TaskScheduler.cpp:43
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, _update_timer, TaskScheduler::CancelAll(), finalize(), Server::general_conf(), get_shutdown_stage(), getScheduler(), HLog, MAX_CORE_UPDATE_INTERVAL, sClientSocketMgr, SHUTDOWN_NOT_STARTED, TaskScheduler::Update(), and update().

Referenced by initialize(), and update().

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

◆ verify_connected_sessions()

void CharServer::verify_connected_sessions ( )
206{
207 try {
208 std::shared_ptr<boost::mysql::tcp_ssl_connection> conn = sChar->get_database_connection();
209 boost::mysql::statement stmt = conn->prepare_statement("DELETE FROM `session_data` WHERE `current_server` = ? AND `last_update` < ?");
210 auto b = stmt.bind("C", std::time(nullptr) - config().session_max_timeout());
211 boost::mysql::results results;
212 conn->execute(b, results);
213
214 stmt = conn->prepare_statement("SELECT COUNT(`game_account_id`) FROM `session_data` WHERE `current_server` = ?");
215 auto b2 = stmt.bind("C");
216 conn->execute(b2, results);
217
218 if (results.rows().empty()) {
219 HLog(info) << "There are no connected session(s).";
220 return;
221 }
222
223 int32_t count = results.rows()[0][0].as_int64();
224
225 HLog(info) << count << " connected session(s).";
226
227 }
228 catch (boost::mysql::error_with_diagnostics &error) {
229 HLog(error) << error.what();
230 }
231 catch (std::exception& error) {
232 HLog(error) << error.what();
233 }
234}
#define sChar
Definition: Char.hpp:143
size_t count(GridTypeListContainer< SPECIFIC_TYPE > const &elements, SPECIFIC_TYPE *)
Definition: GridReferenceContainer.hpp:100

References config(), GridTypeListIterator::count(), HLog, and sChar.

Referenced by initialize().

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

Member Data Documentation

◆ _char_server_config

character_server_configuration Horizon::Char::CharServer::_char_server_config
private

Referenced by config().

◆ _task_scheduler

TaskScheduler Horizon::Char::CharServer::_task_scheduler
private

Referenced by getScheduler().

◆ _update_timer

boost::asio::deadline_timer Horizon::Char::CharServer::_update_timer
private

Referenced by initialize(), and update().


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