Jetpack documentation 1.0.0
Loading...
Searching...
No Matches
Server.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** jetpack
4** File description:
5** The Server.hpp
6*/
13#ifndef SERVER_HPP_
14 #define SERVER_HPP_
15
16 #include "utils/Jetpack.hpp"
17
18namespace Jetpack {
19
20 constexpr int START_FD = 4;
21 constexpr double GRAVITY = 9.81;
22 constexpr double JETPACK_SPEED = 10.0;
23 constexpr double PLAYER_SPEED = 5.0;
24 constexpr char INVALID_PSEUDO_CHARACTERS[] = ":| \t\n\r";
25
30 class Server {
31 public:
32 /* Constructor and destructor */
33
40 Server(int ac, char **av);
44 ~Server();
49 Server(const Server &) = delete;
55 Server &operator=(const Server &) = delete;
56
57
58
59 /* Protocol functions */
60
68 static void sendResponse(int client_fd, CmdID cmd_id, int cmd_code, const std::string &message);
69
70
71
72 /* Server functions */
73
79 void run();
80
81
82
83 class Client; /* Forward declaration of Client class */
84
85
86
87 private:
88 /* Initialization functions */
89
97 void _checkArgs(int ac, char **av);
102 void _initMap();
106 void _initCommands();
111 void _initServer();
112
113
114
115 /* Server game functions */
116
124 bool _isPlayerOn(const double &x, const double &y, const char c) const;
130 void _analyseClientOnCoin(Client &client);
138 void _updatePlayer(Client &client, double &delta_time, bool &reset_coins);
143 void _updateGame();
144
145
146
147 /* Server sub-functions */
148
153 void _addNewClient();
158 void _disconnectClient(std::size_t poll_index);
164 void _executeCommand(std::size_t poll_index, const std::string &command);
169 void _writeClientAction(std::size_t poll_index);
175 void _readClientAction(std::size_t poll_index);
180 void _analysePoll(std::size_t poll_index);
181
182
183
184 protected:
185 /* Command functions */
186
187 std::unordered_map<std::string, std::function<void(int client_fd, std::vector<std::string> &command)>> _commands; /* The map of commands (command name, function) */
188
194 void executeId(int client_fd, std::vector<std::string> &command);
200 void executeMap(int client_fd, std::vector<std::string> &command);
206 void executePseudo(int client_fd, std::vector<std::string> &command);
212 void executeMove(int client_fd, std::vector<std::string> &command);
218 void executeClientsNb(int client_fd, std::vector<std::string> &command);
224 void executeClientsStats(int client_fd, std::vector<std::string> &command);
230 void executeClientsPositions(int client_fd, std::vector<std::string> &command);
231
232
233
234 private:
235 #define MAX_HEIGHT (_map.size() - 1)
236 #define MIN_HEIGHT 0
237
238 std::string _port; /* The port of the server */
239 std::string _map_path; /* The path of the map */
240 bool _infinite_mode = false; /* The infinite mode of the game */
241 std::vector<std::string> _map; /* The map of the game */
242 struct sockaddr_in _server; /* The address of the server */
243 std::unique_ptr<Socket> _server_socket; /* The socket of the server */
244 std::vector<pollfd> _poll_list; /* The list of clients */
245 std::map<std::size_t, std::unique_ptr<Client>> _clients; /* The map of clients (client fd, client object) */
246 std::chrono::time_point<std::chrono::high_resolution_clock> _last_update; /* The last update time */
247 double _map_x_pos = 0; /* The x position of the map */
248 };
249}
250
251#endif /* SERVER_HPP_ */
The Jetpack.hpp.
A class that represents a client connected to the server.
Definition ServerClient.hpp:23
A class that represents the server.
Definition Server.hpp:30
void _readClientAction(std::size_t poll_index)
Reads a command from the client.
Definition Server.cpp:213
void _analyseClientOnCoin(Client &client)
Analyse if the client is on a coin and add it to the client coin list.
Definition Game.cpp:37
void run()
Runs the server.
Definition Server.cpp:258
void executePseudo(int client_fd, std::vector< std::string > &command)
Executes the PSEUDO command.
Definition Pseudo.cpp:21
Server & operator=(const Server &)=delete
Deleted assignment operator.
void executeId(int client_fd, std::vector< std::string > &command)
Executes the ID command.
Definition Id.cpp:21
bool _isPlayerOn(const double &x, const double &y, const char c) const
Checks if the player is on the map.
Definition Game.cpp:23
void executeMap(int client_fd, std::vector< std::string > &command)
Executes the MAP command.
Definition Map.cpp:21
void executeClientsPositions(int client_fd, std::vector< std::string > &command)
Executes the CLIENTS_POSITIONS command.
Definition ClientsPositions.cpp:21
void executeClientsNb(int client_fd, std::vector< std::string > &command)
Executes the CLIENTS_NB command.
Definition ClientsNb.cpp:21
void _initServer()
Initializes the server.
Definition Server.cpp:99
void _initCommands()
Initializes the commands of the server.
Definition Server.cpp:83
Server(int ac, char **av)
Constructor for Server class.
Definition Server.cpp:121
void _addNewClient()
Adds a new client to the server.
Definition Server.cpp:143
void _updateGame()
Updates the game.
Definition Game.cpp:98
void executeMove(int client_fd, std::vector< std::string > &command)
Executes the MOVE command.
Definition Move.cpp:21
void _executeCommand(std::size_t poll_index, const std::string &command)
Executes a command.
Definition Server.cpp:173
~Server()
Destructor for Server class.
Definition Server.cpp:134
void _initMap()
Initializes the map of the game.
Definition Server.cpp:62
Server(const Server &)=delete
Deleted copy constructor.
void executeClientsStats(int client_fd, std::vector< std::string > &command)
Executes the CLIENTS_STATS command.
Definition ClientsStats.cpp:21
static void sendResponse(int client_fd, CmdID cmd_id, int cmd_code, const std::string &message)
Sends a response to the client.
Definition Server.cpp:279
void _writeClientAction(std::size_t poll_index)
Writes a response to the client.
Definition Server.cpp:193
void _disconnectClient(std::size_t poll_index)
Disconnects a client from the server.
Definition Server.cpp:161
void _checkArgs(int ac, char **av)
Checks the command line arguments.
Definition Server.cpp:23
void _analysePoll(std::size_t poll_index)
Analyzes the poll events.
Definition Server.cpp:239
void _updatePlayer(Client &client, double &delta_time, bool &reset_coins)
Updates a client (player)
Definition Game.cpp:62