Horizon Official Technical Documentation
ClientSocketMgr.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30/* This is an auto-generated file, please do not edit manually. */
31
32#ifndef HORIZON_CHAR_CLIENTSOCKETMGR_HPP
33#define HORIZON_CHAR_CLIENTSOCKETMGR_HPP
35
36#include "Server/Char/Char.hpp"
40
41#if WIN32
42 #include <windows.h>
43#elif __linux__
44 #include <sched.h>
45#endif
46
47namespace Horizon
48{
49namespace Char
50{
51
53{
54protected:
55 void on_socket_removed(std::shared_ptr<CharSocket> socket) override
56 {
57 get_resource_manager().remove<RESOURCE_PRIORITY_PRIMARY>(socket->get_socket_id());
58 }
59
60 void on_socket_added(std::shared_ptr<CharSocket> socket) override
61 {
62 get_resource_manager().add<RESOURCE_PRIORITY_PRIMARY>(socket->get_socket_id(), socket);
63 }
64public:
67 _resource_manager(PrimaryResource(RESOURCE_PRIORITY_PRIMARY, std::make_shared<s_segment_storage<uint64_t, std::shared_ptr<CharSocket>>>()))
68 {
69 }
70
71 bool start(int segment_number = 1)
72 {
74 return false;
75
76 initialize(segment_number);
77 return true;
78 }
79
80 void run() override
81 {
83 }
84
85 void update() override
86 {
87 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
89
91#if WIN32
92 DWORD cpu = GetCurrentProcessorNumber();
93 if (get_thread_cpu_id() != (int) cpu)
95#elif __linux__
96 int cpu = sched_getcpu();
97 if (get_thread_cpu_id() != cpu)
99#endif
100
102 std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
103 std::chrono::nanoseconds time_span = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
104 set_total_execution_time(time_span.count());
105 }
106
107 virtual void initialize(int segment_number = 1) override
108 {
109 _is_initialized.exchange(true);
110 set_segment_number(segment_number);
111 }
112
113 virtual void finalize() override
114 {
116 _is_finalized.exchange(true);
117 }
118
119 virtual bool is_initialized() override { return _is_initialized.load(); }
120 virtual bool is_finalized() override { return _is_finalized.load(); }
121protected:
122 std::atomic<bool> _is_initialized{false};
123 std::atomic<bool> _is_finalized{false};
124
125protected:
129public:
131};
132
136class ClientSocketMgr : public Horizon::Networking::AcceptSocketMgr<CharSocket, CharNetworkThread>
137{
139public:
141 {
142 static ClientSocketMgr instance;
143 return &instance;
144 }
145
146 bool start(boost::asio::io_context &io_context, std::string const &listen_ip, uint16_t port, uint32_t threads = 1, bool minimal = false) override;
147
148 bool stop()
149 {
150 get_sockets().clear();
151
152 for (auto i = get_thread_map().begin(); i != get_thread_map().end(); i++)
153 sChar->deregister_component(Horizon::System::RUNTIME_NETWORKING, (std::static_pointer_cast<CharNetworkThread>(i->second))->get_segment_number());
154
156 return false;
157 return true;
158 }
159
160 void update_sessions(uint64_t time)
161 {
162 auto socket_map = get_sockets();
163
164 for (auto s : socket_map) {
165 if (s.second->get_session() != nullptr)
166 s.second->get_session()->update(time);
167 }
168 }
169};
170}
171}
172#define sClientSocketMgr Horizon::Char::ClientSocketMgr::Instance()
173#endif /* HORIZON_CHAR_CLIENTSOCKETMGR_HPP */
#define sChar
Definition: Char.hpp:143
@ RESOURCE_PRIORITY_PRIMARY
Definition: Server.hpp:79
Definition: ClientSocketMgr.hpp:53
virtual bool is_finalized() override
Definition: ClientSocketMgr.hpp:120
std::atomic< bool > _is_finalized
Definition: ClientSocketMgr.hpp:123
ResourceManager _resource_manager
Definition: ClientSocketMgr.hpp:128
virtual void initialize(int segment_number=1) override
Definition: ClientSocketMgr.hpp:107
virtual void finalize() override
Definition: ClientSocketMgr.hpp:113
void update() override
Updates the network thread and schedules a recursive call to itself.
Definition: ClientSocketMgr.hpp:85
bool start(int segment_number=1)
Initializes the network thread and runs.
Definition: ClientSocketMgr.hpp:71
CharNetworkThread()
Definition: ClientSocketMgr.hpp:65
void run() override
Run the I/O Service loop within this network thread.
Definition: ClientSocketMgr.hpp:80
virtual bool is_initialized() override
Definition: ClientSocketMgr.hpp:119
void on_socket_removed(std::shared_ptr< CharSocket > socket) override
Definition: ClientSocketMgr.hpp:55
void on_socket_added(std::shared_ptr< CharSocket > socket) override
Definition: ClientSocketMgr.hpp:60
std::atomic< bool > _is_initialized
Definition: ClientSocketMgr.hpp:122
ResourceManager & get_resource_manager()
Definition: ClientSocketMgr.hpp:130
Definition: CharSocket.hpp:47
Manager of client sockets and initialization of the packet db *.
Definition: ClientSocketMgr.hpp:137
static ClientSocketMgr * Instance()
Definition: ClientSocketMgr.hpp:140
bool stop()
Definition: ClientSocketMgr.hpp:148
void update_sessions(uint64_t time)
Definition: ClientSocketMgr.hpp:160
Horizon::Networking::AcceptSocketMgr< CharSocket, CharNetworkThread > BaseSocketMgr
Definition: ClientSocketMgr.hpp:138
bool start(boost::asio::io_context &io_context, std::string const &listen_ip, uint16_t port, uint32_t threads=1, bool minimal=false) override
Initialize and start accepting connections asynchronously.
Definition: ClientSocketMgr.cpp:35
Socket Manager for Accepted Sockets.
Definition: AcceptSocketMgr.hpp:49
SocketMap & get_sockets()
Definition: AcceptSocketMgr.hpp:155
virtual bool stop_network() override
Stop the Acceptor network and clear the client socket map.
Definition: AcceptSocketMgr.hpp:91
A Network Thread object that handles a number of sockets.
Definition: NetworkThread.hpp:55
virtual void finalize()
Halts the IO Service and marks the network thread as stopped.
Definition: NetworkThread.hpp:76
virtual void update()
Updates the network thread and schedules a recursive call to itself.
Definition: NetworkThread.hpp:163
virtual void run()
Run the I/O Service loop within this network thread.
Definition: NetworkThread.hpp:143
network_thread_map & get_thread_map()
Definition: SocketMgr.hpp:155
void process_queue()
Definition: System.hpp:676
Definition: Server.hpp:192
void set_thread_cpu_id(int cpu_id)
Definition: Server.hpp:239
Horizon::System::SystemRoutineManager & get_system_routine_manager()
Definition: Server.hpp:235
void set_total_execution_time(int time)
Definition: Server.hpp:245
void calculate_and_set_cpu_load()
Definition: Server.hpp:257
void set_segment_number(int64_t segment_number)
Definition: Server.hpp:206
int get_thread_cpu_id()
Definition: Server.hpp:240
void remove(Key key)
Definition: Server.hpp:163
void add(Key key, Value value)
Definition: Server.hpp:157
Definition: Server.hpp:113
@ RUNTIME_NETWORKING
Definition: System.hpp:84
Definition: Element.hpp:7
Definition: Server.hpp:93