|
R-Type
Distributed multiplayer game engine in C++
|
Manages network communication for the server with dedicated thread. More...
#include <ServerNetworkManager.hpp>

Public Types | |
| using | PacketHandler = std::function< void(HostNetworkEvent &)> |
| Packet handler callback type. | |
Public Member Functions | |
| ServerNetworkManager (uint16_t port, size_t maxClients=32) | |
| Constructor. | |
| ~ServerNetworkManager () | |
| Destructor - stops network thread. | |
| bool | start () |
| Start the server and network thread. | |
| void | stop () |
| Stop the server and network thread. | |
| void | processMessages () |
| Process incoming network events from the queue. | |
| void | setPacketHandler (PacketHandler handler) |
| Set packet handler callback. | |
| bool | isRunning () const |
| Check if server is running. | |
Private Member Functions | |
| void | networkThreadLoop (std::stop_token stopToken) |
| Network thread main loop. | |
Private Attributes | |
| uint16_t | _port |
| size_t | _maxClients |
| std::unique_ptr< IHost > | _host |
| std::jthread | _networkThread |
| ThreadSafeQueue< HostNetworkEvent > | _eventQueue |
| PacketHandler | _packetHandler |
Manages network communication for the server with dedicated thread.
Architecture:
Definition at line 32 of file ServerNetworkManager.hpp.
| using ServerNetworkManager::PacketHandler = std::function<void(HostNetworkEvent &)> |
Packet handler callback type.
| event | Network event with packet data |
Definition at line 38 of file ServerNetworkManager.hpp.
|
explicit |
Constructor.
| port | Port to listen on |
| maxClients | Maximum number of clients |
Definition at line 12 of file ServerNetworkManager.cpp.
| ServerNetworkManager::~ServerNetworkManager | ( | ) |
Destructor - stops network thread.
Definition at line 15 of file ServerNetworkManager.cpp.
References stop().

|
inline |
Check if server is running.
Definition at line 80 of file ServerNetworkManager.hpp.
References _networkThread.
|
private |
Network thread main loop.
Continuously polls for network events and pushes them to the queue.
| stopToken | Token to check for stop requests |
Definition at line 63 of file ServerNetworkManager.cpp.
References _eventQueue, _host, LOG_INFO, ThreadSafeQueue< T >::push(), and server::FrameTimer::sleepMilliseconds().
Referenced by start().

| void ServerNetworkManager::processMessages | ( | ) |
Process incoming network events from the queue.
Must be called from the game thread every frame. Processes all available events and calls the registered handlers.
Definition at line 85 of file ServerNetworkManager.cpp.
References _eventQueue, _packetHandler, CONNECT, DISCONNECT, LOG_INFO, RECEIVE, and ThreadSafeQueue< T >::tryPop().

|
inline |
Set packet handler callback.
| handler | Function to call when a packet is received |
Definition at line 75 of file ServerNetworkManager.hpp.
References _packetHandler.
| bool ServerNetworkManager::start | ( | ) |
Start the server and network thread.
Definition at line 20 of file ServerNetworkManager.cpp.
References _host, _maxClients, _networkThread, _port, createAddress(), createServerHost(), LOG_ERROR, LOG_INFO, and networkThreadLoop().

| void ServerNetworkManager::stop | ( | ) |
Stop the server and network thread.
Definition at line 44 of file ServerNetworkManager.cpp.
References _host, _networkThread, and LOG_INFO.
Referenced by ~ServerNetworkManager().
|
private |
Definition at line 98 of file ServerNetworkManager.hpp.
Referenced by networkThreadLoop(), and processMessages().
|
private |
Definition at line 94 of file ServerNetworkManager.hpp.
Referenced by networkThreadLoop(), start(), and stop().
|
private |
Definition at line 93 of file ServerNetworkManager.hpp.
Referenced by start().
|
private |
Definition at line 97 of file ServerNetworkManager.hpp.
Referenced by isRunning(), start(), and stop().
|
private |
Definition at line 101 of file ServerNetworkManager.hpp.
Referenced by processMessages(), and setPacketHandler().
|
private |
Definition at line 92 of file ServerNetworkManager.hpp.
Referenced by start().