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_CLIENTSOCKETMGR_HPP
33#define HORIZON_CLIENTSOCKETMGR_HPP
35
36#include "Server/Auth/Auth.hpp"
42
43#if WIN32
44 #include <windows.h>
45#elif __linux__
46 #include <sched.h>
47#endif
48
49namespace Horizon
50{
51namespace Auth
52{
54{
55protected:
56 void on_socket_removed(std::shared_ptr<AuthSocket> socket) override
57 {
58 get_resource_manager().add<RESOURCE_PRIORITY_PRIMARY>(socket->get_socket_id(), socket);
59 }
60
61 void on_socket_added(std::shared_ptr<AuthSocket> socket) override
62 {
63 get_resource_manager().remove<RESOURCE_PRIORITY_PRIMARY>(socket->get_socket_id());
64 }
65public:
68 _resource_manager(PrimaryResource(RESOURCE_PRIORITY_PRIMARY, std::make_shared<s_segment_storage<uint64_t, std::shared_ptr<AuthSocket>>>()))
69 {
70 }
71
72 bool start(int segment_number = 1)
73 {
75 return false;
76
77 initialize(segment_number);
78
79 return true;
80 }
81
82 void run() override
83 {
85 }
86
87 void update() override
88 {
89 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
91
93
94#if WIN32
95 DWORD cpu = GetCurrentProcessorNumber();
96 if (get_thread_cpu_id() != (int) cpu)
98#elif __linux__
99 int cpu = sched_getcpu();
100 if (get_thread_cpu_id() != cpu)
102#endif
103
105 std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
106 std::chrono::nanoseconds time_span = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
107 set_total_execution_time(time_span.count());
108 }
109
110 virtual void initialize(int segment_number = 1) override
111 {
112 set_segment_number(segment_number);
113 _is_initialized.exchange(true);
114 }
115
116 virtual void finalize() override
117 {
119
120 _is_finalized.exchange(true);
121 }
122
123 virtual bool is_initialized() override { return _is_initialized.load(); }
124 virtual bool is_finalized() override { return _is_finalized.load(); }
125
126protected:
127 std::atomic<bool> _is_initialized{false};
128 std::atomic<bool> _is_finalized{false};
129
133public:
135};
139class ClientSocketMgr : public Networking::AcceptSocketMgr<AuthSocket, AuthNetworkThread>
140{
142public:
144 {
145 static ClientSocketMgr instance;
146 return &instance;
147 }
148
149 bool start(boost::asio::io_context &io_context, std::string const &listen_ip, uint16_t port, uint32_t threads = 1, bool minimal = false) override;
150
151 bool stop()
152 {
153 get_sockets().clear();
154
155 for (auto i = get_thread_map().begin(); i != get_thread_map().end(); i++)
156 sAuth->deregister_component(Horizon::System::RUNTIME_NETWORKING, (std::static_pointer_cast<AuthNetworkThread>(i->second))->get_segment_number());
157
159 return false;
160 return true;
161 }
162
163 void update_sessions(uint64_t time)
164 {
165 auto socket_map = get_sockets();
166
167 for (auto s : socket_map) {
168 if (s.second->get_session() != nullptr)
169 s.second->get_session()->update(time);
170 }
171 }
172};
173}
174}
175
176#define sClientSocketMgr Horizon::Auth::ClientSocketMgr::Instance()
177
178#endif /* HORIZON_CLIENTSOCKETMGR_HPP */
#define sAuth
Definition: Auth.hpp:131
@ RESOURCE_PRIORITY_PRIMARY
Definition: Server.hpp:79
Definition: ClientSocketMgr.hpp:54
bool start(int segment_number=1)
Initializes the network thread and runs.
Definition: ClientSocketMgr.hpp:72
void on_socket_added(std::shared_ptr< AuthSocket > socket) override
Definition: ClientSocketMgr.hpp:61
virtual void finalize() override
Definition: ClientSocketMgr.hpp:116
void on_socket_removed(std::shared_ptr< AuthSocket > socket) override
Definition: ClientSocketMgr.hpp:56
ResourceManager _resource_manager
Definition: ClientSocketMgr.hpp:132
void run() override
Run the I/O Service loop within this network thread.
Definition: ClientSocketMgr.hpp:82
virtual void initialize(int segment_number=1) override
Definition: ClientSocketMgr.hpp:110
ResourceManager & get_resource_manager()
Definition: ClientSocketMgr.hpp:134
std::atomic< bool > _is_initialized
Definition: ClientSocketMgr.hpp:127
AuthNetworkThread()
Definition: ClientSocketMgr.hpp:66
virtual bool is_initialized() override
Definition: ClientSocketMgr.hpp:123
std::atomic< bool > _is_finalized
Definition: ClientSocketMgr.hpp:128
void update() override
Updates the network thread and schedules a recursive call to itself.
Definition: ClientSocketMgr.hpp:87
virtual bool is_finalized() override
Definition: ClientSocketMgr.hpp:124
Definition: AuthSocket.hpp:47
Manager of client sockets and initialization of the packet db *.
Definition: ClientSocketMgr.hpp:140
bool stop()
Definition: ClientSocketMgr.hpp:151
Networking::AcceptSocketMgr< AuthSocket, AuthNetworkThread > BaseSocketMgr
Definition: ClientSocketMgr.hpp:141
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
static ClientSocketMgr * Instance()
Definition: ClientSocketMgr.hpp:143
void update_sessions(uint64_t time)
Definition: ClientSocketMgr.hpp:163
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