Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
ClientManager Class Reference

Owns every client socket and runs the poll() loop. More...

#include <ClientManager.hpp>

+ Collaboration diagram for ClientManager:

Public Member Functions

 ClientManager (Listener &listener)
 
PollResult poll (int timeoutMs)
 Run one poll cycle (blocks up to timeoutMs); returns this cycle's events.
 
void send (int connectionId, const std::string &msg)
 Queue msg to be sent to a client (flushed during poll()).
 
void disconnect (int connectionId)
 Close a client and drop its connection.
 
ConnectiongetConnection (int connectionId)
 Access the underlying Connection (e.g. to set its type/playerId).
 
void addNetworkObserver (INetworkObserver *observer)
 Subscribe an observer to connect/disconnect/line events.
 

Private Member Functions

void _acceptNew (PollResult &result)
 
void _handleEvents (const pollfd &pfd, PollResult &result)
 
void _rebuildPollFds ()
 

Private Attributes

Listener_listener
 
std::unordered_map< int, Connection_connections
 
std::unordered_map< int, int > _fdToId
 
std::vector< pollfd > _pollFds
 
int _nextId = 0
 
std::vector< INetworkObserver * > _observers
 

Detailed Description

Owns every client socket and runs the poll() loop.

One poll() call watches all sockets at once: it accepts new clients, reads the ready ones, splits incoming bytes into '
' lines, and flushes pending writes. Clients are addressed by a stable id, not the raw fd. Observers are told about all traffic.

Definition at line 30 of file ClientManager.hpp.

Constructor & Destructor Documentation

◆ ClientManager()

ClientManager::ClientManager ( Listener listener)
explicit

Definition at line 9 of file ClientManager.cpp.

Member Function Documentation

◆ _acceptNew()

void ClientManager::_acceptNew ( PollResult result)
private

Definition at line 38 of file ClientManager.cpp.

References _connections, _listener, _nextId, _observers, Listener::accept(), and PollResult::newConnections.

Referenced by poll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _handleEvents()

void ClientManager::_handleEvents ( const pollfd &  pfd,
PollResult result 
)
private

Definition at line 49 of file ClientManager.cpp.

References _connections, _fdToId, _observers, PollResult::disconnectedIds, PollResult::lines, and n().

Referenced by poll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _rebuildPollFds()

void ClientManager::_rebuildPollFds ( )
private

Definition at line 11 of file ClientManager.cpp.

References _connections, _fdToId, _listener, _pollFds, Listener::fd(), and id().

Referenced by poll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addNetworkObserver()

void ClientManager::addNetworkObserver ( INetworkObserver observer)

Subscribe an observer to connect/disconnect/line events.

Definition at line 100 of file ClientManager.cpp.

References _observers.

Referenced by Server::Server().

+ Here is the caller graph for this function:

◆ disconnect()

void ClientManager::disconnect ( int  connectionId)

Close a client and drop its connection.

Definition at line 83 of file ClientManager.cpp.

References _connections, and _observers.

Referenced by Server::_handleGameOver(), HandshakeHandler::_reject(), and Server::run().

+ Here is the caller graph for this function:

◆ getConnection()

◆ poll()

PollResult ClientManager::poll ( int  timeoutMs)

Run one poll cycle (blocks up to timeoutMs); returns this cycle's events.

Definition at line 23 of file ClientManager.cpp.

References _acceptNew(), _handleEvents(), _pollFds, _rebuildPollFds(), and poll().

Referenced by poll(), and Server::run().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ send()

Member Data Documentation

◆ _connections

std::unordered_map<int, Connection> ClientManager::_connections
private

◆ _fdToId

std::unordered_map<int, int> ClientManager::_fdToId
private

Definition at line 53 of file ClientManager.hpp.

Referenced by _handleEvents(), and _rebuildPollFds().

◆ _listener

Listener& ClientManager::_listener
private

Definition at line 51 of file ClientManager.hpp.

Referenced by _acceptNew(), and _rebuildPollFds().

◆ _nextId

int ClientManager::_nextId = 0
private

Definition at line 55 of file ClientManager.hpp.

Referenced by _acceptNew().

◆ _observers

std::vector<INetworkObserver*> ClientManager::_observers
private

Definition at line 56 of file ClientManager.hpp.

Referenced by _acceptNew(), _handleEvents(), addNetworkObserver(), disconnect(), and send().

◆ _pollFds

std::vector<pollfd> ClientManager::_pollFds
private

Definition at line 54 of file ClientManager.hpp.

Referenced by _rebuildPollFds(), and poll().


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