30#ifndef HORIZON_NETWORKING_NETWORKTHREAD_HPP
31#define HORIZON_NETWORKING_NETWORKTHREAD_HPP
39#include <boost/asio/ip/tcp.hpp>
40#include <boost/asio/deadline_timer.hpp>
42using boost::asio::ip::tcp;
53template <
class SocketType>
85 }
catch (std::system_error &error) {
86 HLog(error) <<
"Error joining network thread2: " << error.what();
94 virtual bool start(
int segment_number = 1)
145 _update_timer.expires_from_now(boost::posix_time::microseconds(500));
165 _update_timer.expires_from_now(boost::posix_time::microseconds(500));
173 [
this] (std::shared_ptr<SocketType> sock)
175 if (!sock->update() || is_finalizing()) {
178 sock->close_socket();
180 on_socket_removed(sock);
184 HLog(info) <<
"Socket closed in networking thread " << (void *) (_thread.get()) <<
" (Connections: " << _connections <<
")";
195 HLog(info) <<
"Network thread " << (
void *) (
_thread.get()) <<
" has been finalized.";
211 std::lock_guard<std::mutex> lock(_new_socket_queue_lock);
213 if (_new_socket_queue.empty())
216 for (std::shared_ptr<SocketType> sock : _new_socket_queue) {
217 if (sock->is_open()) {
218 _active_sockets.push_back(sock);
222 on_socket_added(sock);
226 HLog(trace) <<
"A new socket has been added to network thread " << (
void *) (_thread.get()) <<
" (Connections: " << _connections <<
")";
230 _new_socket_queue.clear();
236 int _segment_number{1};
#define HLog(type)
Definition: Logger.hpp:122
A Network Thread object that handles a number of sockets.
Definition: NetworkThread.hpp:55
virtual void on_socket_added(std::shared_ptr< SocketType > sock)=0
std::atomic< int32_t > _connections
Definition: NetworkThread.hpp:237
void join()
Definition: NetworkThread.hpp:81
SocketContainer _new_socket_queue
Definition: NetworkThread.hpp:242
SocketContainer _active_sockets
Definition: NetworkThread.hpp:234
std::vector< std::shared_ptr< SocketType > > SocketContainer
Definition: NetworkThread.hpp:56
virtual void on_socket_removed(std::shared_ptr< SocketType > sock)=0
std::mutex _new_socket_queue_lock
Definition: NetworkThread.hpp:244
boost::asio::deadline_timer _update_timer
Definition: NetworkThread.hpp:247
std::shared_ptr< tcp::socket > get_new_socket()
Gets a socket for a new connection.
Definition: NetworkThread.hpp:123
virtual bool start(int segment_number=1)
Initializes the network thread and runs.
Definition: NetworkThread.hpp:94
NetworkThread()
Definition: NetworkThread.hpp:58
std::atomic< bool > _finalizing
Definition: NetworkThread.hpp:238
virtual void finalize()
Halts the IO Service and marks the network thread as stopped.
Definition: NetworkThread.hpp:76
boost::asio::io_context _io_context
Definition: NetworkThread.hpp:246
virtual ~NetworkThread()
Destructor of the network thread, performs a clean network finalisation routine before deleting.
Definition: NetworkThread.hpp:68
virtual void update()
Updates the network thread and schedules a recursive call to itself.
Definition: NetworkThread.hpp:163
virtual void add_new_sockets()
Processess the new socket queue.
Definition: NetworkThread.hpp:206
int32_t connection_count() const
Gets the total number of network connections or sockets handled by this network thread.
Definition: NetworkThread.hpp:130
virtual void add_socket(std::shared_ptr< SocketType > sock)
Adds a new socket to a queue that is processed frequently within this network thread.
Definition: NetworkThread.hpp:110
virtual void run()
Run the I/O Service loop within this network thread.
Definition: NetworkThread.hpp:143
std::unique_ptr< std::thread > _thread
Definition: NetworkThread.hpp:240
int _segment_number
Definition: NetworkThread.hpp:236
bool is_finalizing()
Issues the status of network thread whether it is finalizing or not.
Definition: NetworkThread.hpp:136
Definition: Element.hpp:7