Skip to content

Class Engine::Network::UdpServer

ClassList > Engine > Network > UdpServer

Multi-threaded high-performance UDP Game Server built on ENet. More...

  • #include <UdpServer.hpp>

Public Functions

Type Name
void BroadcastPacket (const uint8_t * data, size_t size, bool reliable=true, uint8_t channel=0)
Broadcasts a packet to all connected peers. Thread-safe.
void BroadcastPacket (const ByteWriter & writer, bool reliable=true, uint8_t channel=0)
void DisconnectPeer (ENetPeer * peer, uint32_t data=0)
Disconnects a specific peer cleanly.
size_t GetConnectedCount () const
uint16_t GetPort () noexcept const
bool IsRunning () noexcept const
void PollEvents (const std::function< void(const NetworkEvent &)> & handler)
Drains all queued incoming network events from the I/O thread to the main thread.
void SendPacket (ENetPeer * peer, const uint8_t * data, size_t size, bool reliable=true, uint8_t channel=0)
Sends a packet to a specific connected peer. Thread-safe.
void SendPacket (ENetPeer * peer, const ByteWriter & writer, bool reliable=true, uint8_t channel=0)
bool Start (uint16_t port, size_t maxClients=32, size_t channels=2, bool multiThreaded=true)
Starts the UDP server on the specified port.
void Stop ()
Stops the server, disconnects all peers, and joins the I/O thread.
UdpServer ()
UdpServer (const UdpServer &) = delete
UdpServer & operator= (const UdpServer &) = delete
virtual ~UdpServer ()

Protected Attributes

Type Name
std::queue< NetworkEvent > incomingEvents_
std::thread ioThread_
std::atomic< bool > isRunning_ = {false}
bool multiThreaded_ = {true}
uint16_t port_ = {0}
std::mutex queueMutex_
std::mutex sendMutex_
ENetHost * serverHost_ = {nullptr}

Protected Functions

Type Name
void NetworkIoLoop ()
void ProcessEnetEvents ()

Detailed Description

Runs network I/O asynchronously on a dedicated worker thread, buffering network events in a thread-safe queue. The game simulation loop can poll events without ever blocking frame execution or risking packet loss.

Public Functions Documentation

function BroadcastPacket [½]

Broadcasts a packet to all connected peers. Thread-safe.

void Engine::Network::UdpServer::BroadcastPacket (
    const uint8_t * data,
    size_t size,
    bool reliable=true,
    uint8_t channel=0
) 


function BroadcastPacket [2/2]

void Engine::Network::UdpServer::BroadcastPacket (
    const ByteWriter & writer,
    bool reliable=true,
    uint8_t channel=0
) 

function DisconnectPeer

Disconnects a specific peer cleanly.

void Engine::Network::UdpServer::DisconnectPeer (
    ENetPeer * peer,
    uint32_t data=0
) 


function GetConnectedCount

size_t Engine::Network::UdpServer::GetConnectedCount () const

function GetPort

inline uint16_t Engine::Network::UdpServer::GetPort () noexcept const

function IsRunning

inline bool Engine::Network::UdpServer::IsRunning () noexcept const

function PollEvents

Drains all queued incoming network events from the I/O thread to the main thread.

void Engine::Network::UdpServer::PollEvents (
    const std::function< void(const NetworkEvent &)> & handler
) 

Parameters:

  • handler Callback invoked for each received event.

function SendPacket [½]

Sends a packet to a specific connected peer. Thread-safe.

void Engine::Network::UdpServer::SendPacket (
    ENetPeer * peer,
    const uint8_t * data,
    size_t size,
    bool reliable=true,
    uint8_t channel=0
) 


function SendPacket [2/2]

void Engine::Network::UdpServer::SendPacket (
    ENetPeer * peer,
    const ByteWriter & writer,
    bool reliable=true,
    uint8_t channel=0
) 

function Start

Starts the UDP server on the specified port.

bool Engine::Network::UdpServer::Start (
    uint16_t port,
    size_t maxClients=32,
    size_t channels=2,
    bool multiThreaded=true
) 

Parameters:

  • port UDP port to listen on.
  • maxClients Maximum concurrent client connections.
  • channels Number of ENet channels.
  • multiThreaded When true, spawns a background I/O thread for network polling.

Returns:

True if bind and startup succeeded.


function Stop

Stops the server, disconnects all peers, and joins the I/O thread.

void Engine::Network::UdpServer::Stop () 


function UdpServer [½]

Engine::Network::UdpServer::UdpServer () 

function UdpServer [2/2]

Engine::Network::UdpServer::UdpServer (
    const UdpServer &
) = delete

function operator=

UdpServer & Engine::Network::UdpServer::operator= (
    const UdpServer &
) = delete

function ~UdpServer

virtual Engine::Network::UdpServer::~UdpServer () 

Protected Attributes Documentation

variable incomingEvents_

std::queue<NetworkEvent> Engine::Network::UdpServer::incomingEvents_;

variable ioThread_

std::thread Engine::Network::UdpServer::ioThread_;

variable isRunning_

std::atomic<bool> Engine::Network::UdpServer::isRunning_;

variable multiThreaded_

bool Engine::Network::UdpServer::multiThreaded_;

variable port_

uint16_t Engine::Network::UdpServer::port_;

variable queueMutex_

std::mutex Engine::Network::UdpServer::queueMutex_;

variable sendMutex_

std::mutex Engine::Network::UdpServer::sendMutex_;

variable serverHost_

ENetHost* Engine::Network::UdpServer::serverHost_;

Protected Functions Documentation

function NetworkIoLoop

void Engine::Network::UdpServer::NetworkIoLoop () 

function ProcessEnetEvents

void Engine::Network::UdpServer::ProcessEnetEvents () 


The documentation for this class was generated from the following file /home/runner/work/r-type/r-type/Engine/include/Network/UdpServer.hpp