Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
ClientManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <poll.h>
4
5#include <string>
6#include <unordered_map>
7#include <utility>
8#include <vector>
9
12#include "network/Listener.hpp"
13
16struct PollResult {
17 std::vector<int> newConnections;
18 std::vector<int> disconnectedIds;
19 std::vector<std::pair<int, std::string>> lines;
20};
21
31 public:
32 explicit ClientManager(Listener& listener);
33
35 PollResult poll(int timeoutMs);
37 void send(int connectionId, const std::string& msg);
39 void disconnect(int connectionId);
41 Connection& getConnection(int connectionId);
42
45
46 private:
47 void _acceptNew(PollResult& result);
48 void _handleEvents(const pollfd& pfd, PollResult& result);
49 void _rebuildPollFds();
50
52 std::unordered_map<int, Connection> _connections;
53 std::unordered_map<int, int> _fdToId;
54 std::vector<pollfd> _pollFds;
55 int _nextId = 0;
56 std::vector<INetworkObserver*> _observers;
57};
Owns every client socket and runs the poll() loop.
void send(int connectionId, const std::string &msg)
Queue msg to be sent to a client (flushed during poll()).
std::unordered_map< int, Connection > _connections
void _acceptNew(PollResult &result)
Connection & getConnection(int connectionId)
Access the underlying Connection (e.g. to set its type/playerId).
void disconnect(int connectionId)
Close a client and drop its connection.
std::vector< INetworkObserver * > _observers
void _handleEvents(const pollfd &pfd, PollResult &result)
Listener & _listener
std::vector< pollfd > _pollFds
PollResult poll(int timeoutMs)
Run one poll cycle (blocks up to timeoutMs); returns this cycle's events.
std::unordered_map< int, int > _fdToId
void addNetworkObserver(INetworkObserver *observer)
Subscribe an observer to connect/disconnect/line events.
One client socket plus its read/write buffers.
Observer hooks for raw socket events (Observer pattern).
The server's listening socket.
Definition Listener.hpp:31
std::vector< int > disconnectedIds
std::vector< int > newConnections
std::vector< std::pair< int, std::string > > lines