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_ZONE_CLIENTSOCKETMGR_HPP
33#define HORIZON_ZONE_CLIENTSOCKETMGR_HPP
34
39
40#if WIN32
41 #include <windows.h>
42#elif __linux__
43 #include <sched.h>
44#endif
45
46namespace Horizon
47{
48namespace Zone
49{
50
52{
53protected:
54 void on_socket_removed(std::shared_ptr<ZoneSocket> socket) override
55 {
56 get_resource_manager().add<RESOURCE_PRIORITY_PRIMARY>(socket->get_socket_id(), socket);
57 }
58
59 void on_socket_added(std::shared_ptr<ZoneSocket> socket) override
60 {
61 get_resource_manager().remove<RESOURCE_PRIORITY_PRIMARY>(socket->get_socket_id());
62 }
63public:
65
66 bool start(int segment_number = 1) override
67 {
69 return false;
70
71 initialize(segment_number);
72 return true;
73 }
74
75 void run() override
76 {
78 }
79
80 // This routine is overriden from base NetworkThread::update
81 void update() override
82 {
83 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
85
87
88#if WIN32
89 DWORD cpu = GetCurrentProcessorNumber();
90 if (get_thread_cpu_id() != (int) cpu)
92#elif __linux__
93 int cpu = sched_getcpu();
94 if (get_thread_cpu_id() != cpu)
96#endif
98 std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
99 std::chrono::nanoseconds time_span = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
100 set_total_execution_time(time_span.count());
101 }
102
103 virtual void initialize(int segment_number = 1) override
104 {
105 set_segment_number(segment_number);
106
107 _is_initialized.exchange(true);
108 }
109
110 virtual void finalize() override
111 {
113 _is_finalized.exchange(true);
114 }
115
116 virtual bool is_initialized() override { return _is_initialized.load(); }
117 virtual bool is_finalized() override { return _is_finalized.load(); }
118protected:
119 std::atomic<bool> _is_initialized{false};
120 std::atomic<bool> _is_finalized{false};
121
125
127};
128
129
133class ClientSocketMgr : public Horizon::Networking::AcceptSocketMgr<ZoneSocket, ZoneNetworkThread>
134{
136public:
137 bool start(boost::asio::io_context &io_context, std::string const &listen_ip, uint16_t port, uint32_t threads = 1, bool minimal = false);
138
139 bool stop();
140
141 void update_sessions(uint64_t time)
142 {
143 auto socket_map = get_sockets();
144
145 for (auto s : socket_map) {
146 if (s.second->get_session() != nullptr)
147 s.second->get_session()->update(time);
148 }
149 }
150};
151}
152}
153
154#endif /* HORIZON_ZONE_CLIENTSOCKETMGR_HPP */
@ RESOURCE_PRIORITY_PRIMARY
Definition: Server.hpp:79
Socket Manager for Accepted Sockets.
Definition: AcceptSocketMgr.hpp:49
SocketMap & get_sockets()
Definition: AcceptSocketMgr.hpp:155
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
void process_queue()
Definition: System.hpp:676
Manager of client sockets.
Definition: ClientSocketMgr.hpp:134
bool stop()
Definition: ClientSocketMgr.cpp:22
bool start(boost::asio::io_context &io_context, std::string const &listen_ip, uint16_t port, uint32_t threads=1, bool minimal=false)
Initialize and start accepting connections asynchronously.
Definition: ClientSocketMgr.cpp:10
void update_sessions(uint64_t time)
Definition: ClientSocketMgr.hpp:141
Horizon::Networking::AcceptSocketMgr< ZoneSocket, ZoneNetworkThread > BaseSocketMgr
Definition: ClientSocketMgr.hpp:135
Definition: ClientSocketMgr.hpp:52
void update() override
Updates the network thread and schedules a recursive call to itself.
Definition: ClientSocketMgr.hpp:81
virtual void initialize(int segment_number=1) override
Definition: ClientSocketMgr.hpp:103
virtual bool is_finalized() override
Definition: ClientSocketMgr.hpp:117
bool start(int segment_number=1) override
Initializes the network thread and runs.
Definition: ClientSocketMgr.hpp:66
void run() override
Run the I/O Service loop within this network thread.
Definition: ClientSocketMgr.hpp:75
std::atomic< bool > _is_finalized
Definition: ClientSocketMgr.hpp:120
virtual bool is_initialized() override
Definition: ClientSocketMgr.hpp:116
ZoneNetworkThread()
Definition: ClientSocketMgr.cpp:4
void on_socket_added(std::shared_ptr< ZoneSocket > socket) override
Definition: ClientSocketMgr.hpp:59
void on_socket_removed(std::shared_ptr< ZoneSocket > socket) override
Definition: ClientSocketMgr.hpp:54
std::atomic< bool > _is_initialized
Definition: ClientSocketMgr.hpp:119
virtual void finalize() override
Definition: ClientSocketMgr.hpp:110
ResourceManager & get_resource_manager()
Definition: ClientSocketMgr.hpp:126
ResourceManager _resource_manager
Definition: ClientSocketMgr.hpp:124
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
Definition: Element.hpp:7