R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ServerNetworkManager Class Reference

Manages network communication for the server with dedicated thread. More...

#include <ServerNetworkManager.hpp>

Collaboration diagram for ServerNetworkManager:
Collaboration graph

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
 

Detailed Description

Manages network communication for the server with dedicated thread.

Architecture:

NETWORK THREAD GAME THREAD (ECS)
┌──────────────────┐ ┌──────────────────┐
│ networkLoop() │ │ processMessages
│ service() │──[Queue]───>│ handlePacket │
│ push event │ │ update ECS │
└──────────────────┘ └──────────────────┘
void processMessages()
Process incoming network events from the queue.

Definition at line 32 of file ServerNetworkManager.hpp.

Member Typedef Documentation

◆ PacketHandler

using ServerNetworkManager::PacketHandler = std::function<void(HostNetworkEvent &)>

Packet handler callback type.

Parameters
eventNetwork event with packet data

Definition at line 38 of file ServerNetworkManager.hpp.

Constructor & Destructor Documentation

◆ ServerNetworkManager()

ServerNetworkManager::ServerNetworkManager ( uint16_t  port,
size_t  maxClients = 32 
)
explicit

Constructor.

Parameters
portPort to listen on
maxClientsMaximum number of clients

Definition at line 12 of file ServerNetworkManager.cpp.

◆ ~ServerNetworkManager()

ServerNetworkManager::~ServerNetworkManager ( )

Destructor - stops network thread.

Definition at line 15 of file ServerNetworkManager.cpp.

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ isRunning()

bool ServerNetworkManager::isRunning ( ) const
inline

Check if server is running.

Definition at line 80 of file ServerNetworkManager.hpp.

References _networkThread.

◆ networkThreadLoop()

void ServerNetworkManager::networkThreadLoop ( std::stop_token  stopToken)
private

Network thread main loop.

Continuously polls for network events and pushes them to the queue.

Parameters
stopTokenToken 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().

Here is the call graph for this function:

◆ processMessages()

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().

Here is the call graph for this function:

◆ setPacketHandler()

void ServerNetworkManager::setPacketHandler ( PacketHandler  handler)
inline

Set packet handler callback.

Parameters
handlerFunction to call when a packet is received

Definition at line 75 of file ServerNetworkManager.hpp.

References _packetHandler.

◆ start()

bool ServerNetworkManager::start ( )

Start the server and network thread.

Returns
true if started successfully

Definition at line 20 of file ServerNetworkManager.cpp.

References _host, _maxClients, _networkThread, _port, createAddress(), createServerHost(), LOG_ERROR, LOG_INFO, and networkThreadLoop().

Here is the call graph for this function:

◆ stop()

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().

Member Data Documentation

◆ _eventQueue

ThreadSafeQueue<HostNetworkEvent> ServerNetworkManager::_eventQueue
private

Definition at line 98 of file ServerNetworkManager.hpp.

Referenced by networkThreadLoop(), and processMessages().

◆ _host

std::unique_ptr<IHost> ServerNetworkManager::_host
private

Definition at line 94 of file ServerNetworkManager.hpp.

Referenced by networkThreadLoop(), start(), and stop().

◆ _maxClients

size_t ServerNetworkManager::_maxClients
private

Definition at line 93 of file ServerNetworkManager.hpp.

Referenced by start().

◆ _networkThread

std::jthread ServerNetworkManager::_networkThread
private

Definition at line 97 of file ServerNetworkManager.hpp.

Referenced by isRunning(), start(), and stop().

◆ _packetHandler

PacketHandler ServerNetworkManager::_packetHandler
private

Definition at line 101 of file ServerNetworkManager.hpp.

Referenced by processMessages(), and setPacketHandler().

◆ _port

uint16_t ServerNetworkManager::_port
private

Definition at line 92 of file ServerNetworkManager.hpp.

Referenced by start().


The documentation for this class was generated from the following files: