R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ServerNetworkManager.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by mael on 08/12/2025.
4** File description:
5** ServerNetworkManager.hpp
6*/
7
8#pragma once
9
10#include <functional>
11#include <memory>
12#include <thread>
14#include "IHost.hpp"
15#include "NetworkFactory.hpp"
16#include "ThreadSafeQueue.hpp"
17
33 public:
38 using PacketHandler = std::function<void(HostNetworkEvent &)>;
39
45 explicit ServerNetworkManager(uint16_t port, size_t maxClients = 32);
46
51
56 bool start();
57
61 void stop();
62
69 void processMessages();
70
75 void setPacketHandler(PacketHandler handler) { _packetHandler = handler; }
76
80 bool isRunning() const { return _networkThread.joinable(); }
81
82 private:
90 void networkThreadLoop(std::stop_token stopToken);
91
92 uint16_t _port;
94 std::unique_ptr<IHost> _host;
95
96 // Multi-threading components
97 std::jthread _networkThread;
99
100 // Callback
102};
Manages network communication for the server with dedicated thread.
~ServerNetworkManager()
Destructor - stops network thread.
bool isRunning() const
Check if server is running.
void setPacketHandler(PacketHandler handler)
Set packet handler callback.
ThreadSafeQueue< HostNetworkEvent > _eventQueue
void processMessages()
Process incoming network events from the queue.
std::unique_ptr< IHost > _host
void stop()
Stop the server and network thread.
bool start()
Start the server and network thread.
void networkThreadLoop(std::stop_token stopToken)
Network thread main loop.
std::function< void(HostNetworkEvent &)> PacketHandler
Packet handler callback type.
Thread-safe queue for inter-thread communication.
Represents a network event (connection, disconnection, or received data).
Definition IHost.hpp:32