VOX
A little voxel engine
Loading...
Searching...
No Matches
Server.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "define.hpp"
4#include <unordered_map>
5#include <exception>
6#include <atomic>
7
8#include "logger.hpp"
9#include "ServerSocket.hpp"
10#include "Connection.hpp"
11#include "Poller.hpp"
12#include "PacketFactory.hpp"
13#include "ThreadSafeQueue.hpp"
15
32class Server
33{
34public:
35 static constexpr int PING_TIMEOUT_MS = 5000;
36 Server (int port);
37 ~Server();
38
39 Server(const Server& other) = delete;
40 Server& operator=(const Server& other) = delete;
41 Server(Server&& other) = delete;
42 Server& operator=(Server&& other) = delete;
43
44 void runOnce(int timeout_ms);
45
46 enum flags : uint8_t
47 {
48 ALL = 1,
49 ALLEXCEPT = 1 << 1,
50 ASYNC = 1 << 2,
51 };
52
56 struct sendInfo
57 {
62 std::shared_ptr<IPacket> packet;
63
68 uint8_t flag;
69
77 uint64_t id = 0;
78 };
79 void send(const sendInfo & info);
80
86 void ping(uint64_t id);
87
93 void disconnect(uint64_t id);
94
100 ThreadSafePacketQueue & getIncomingPackets() { return m_incoming_packets; }
101
102 class ClientDisconnected : public std::exception
103 {
104 public:
105 ClientDisconnected(uint64_t id) : m_id(id) {}
106 virtual const char* what() const throw()
107 {
108 return "Client disconnected";
109 }
110 uint64_t id() const { return m_id; }
111 private:
112 uint64_t m_id;
113 };
114
119 std::unordered_map<uint64_t, std::chrono::time_point<std::chrono::high_resolution_clock>> m_pings;
120
121private:
122
123 Poller m_poller;
124 ServerSocket m_server_socket;
125 PacketFactory & m_packet_factory;
126 ThreadSafePacketQueue m_incoming_packets;
127 ThreadSafeQueue<sendInfo> m_outgoing_packets;
128 std::unordered_map<uint64_t, std::shared_ptr<Connection>> m_connections;
129 std::mutex m_connections_mutex;
130
131 uint64_t m_ids_counter;
132
133 uint64_t get_new_id();
134
135 int readData(Connection & connection, uint64_t id);
136 int sendData(Connection & connection, uint64_t id);
137 void emptyOutgoingPackets();
138 void emptyOldPings();
139
140 void sendPacket(std::shared_ptr<IPacket> packet, const uint64_t & id);
141 void sendAllExcept(std::shared_ptr<IPacket> packet, const uint64_t & except_id);
142 void sendAll(std::shared_ptr<IPacket> packet);
143};
a bufferised and thread safe RAII wrapper for a socket representing a connection.
Definition: Connection.hpp:29
Definition: PacketFactory.hpp:8
Definition: Poller.hpp:13
a class that represents a server socket. you can accept connections from clients.
Definition: ServerSocket.hpp:16
Definition: Server.hpp:103
virtual const char * what() const
Definition: Server.hpp:106
uint64_t id() const
Definition: Server.hpp:110
ClientDisconnected(uint64_t id)
Definition: Server.hpp:105
The server side interface with the network module.
Definition: Server.hpp:33
void send(const sendInfo &info)
Definition: Server.cpp:185
std::unordered_map< uint64_t, std::chrono::time_point< std::chrono::high_resolution_clock > > m_pings
map used to store the time a ping was sent to a client
Definition: Server.hpp:119
void disconnect(uint64_t id)
disconnect a client
Definition: Server.cpp:151
static constexpr int PING_TIMEOUT_MS
Definition: Server.hpp:35
~Server()
Definition: Server.cpp:10
flags
Definition: Server.hpp:47
@ ASYNC
Definition: Server.hpp:50
@ ALLEXCEPT
Definition: Server.hpp:49
@ ALL
Definition: Server.hpp:48
ThreadSafePacketQueue & getIncomingPackets()
Get a ref to the list of packet that have been received.
Definition: Server.hpp:100
Server & operator=(Server &&other)=delete
Server(Server &&other)=delete
Server & operator=(const Server &other)=delete
Server(const Server &other)=delete
void ping(uint64_t id)
send a ping packet to a client
Definition: Server.cpp:178
void runOnce(int timeout_ms)
Definition: Server.cpp:14
arg struct used to send a packet
Definition: Server.hpp:57
std::shared_ptr< IPacket > packet
shared pointer to the packet to send
Definition: Server.hpp:62
uint8_t flag
bitfield containing flags from the flags enum
Definition: Server.hpp:68