My Project
Server.hpp
1 /*
2 ** EPITECH PROJECT, 2022
3 ** B-CPP-500-PAR-5-1-rtype-martin.vanaud
4 ** File description:
5 ** Server
6 */
7 
8 #ifndef SERVER_HPP_
9  #define SERVER_HPP_
10 
11  #include <iostream>
12  #include <thread>
13  #include <queue>
14  #include <map>
15  #include <functional>
16 
17  #include <asio/ip/udp.hpp>
18  #include <asio/error_code.hpp>
19  #include <asio/io_context.hpp>
20  #include <asio/io_service.hpp>
21  #include <asio/placeholders.hpp>
22 
23  #include "Registry.hpp"
24 
25  /* System */
26  #include "System/MoveSystem/MoveSystem.hpp"
27  #include "System/ReceiveSystem/ReceiveSystem.hpp"
28  #include "System/DirectionSystem/DirectionSystem.hpp"
29  #include "System/EndGameSystem/EndGameSystem.hpp"
30  #include "System/ShootSystem/ShootSystem.hpp"
31  // #include "System/NewPlayerSystem/NewPlayerSystem.hpp"
32  #include "System/SpawnEnemySystem/SpawnEnemySystem.hpp"
33  #include "System/HitboxSystem/HitboxSystem.hpp"
34  #include "System/DisconnectionSystem/DisconnectionSystem.hpp"
35  #include "System/NewClientSystem/NewClientSystem.hpp"
36  #include "System/JoinLobbySystem/JoinLobbySystem.hpp"
37  #include "System/StartGameSystem/StartGameSystem.hpp"
38 
39  // #include "Serialization.hpp"
40  #include "UdpCommunication.hpp"
41  #include "IUdpCommunication.hpp"
42 
46 class Server {
47  public:
53  Server(short const port);
54 
58  ~Server();
59 
63  void setUpEcs();
64 
68  void setUpComponents();
69 
70  protected:
71  private:
75  void ReceivePackets();
76 
83  void HandleReceive(asio::error_code const &e, std::size_t nbBytes);
84 
88  void HandleSendPacket();
89 
93  void threadLoop();
94 
101  std::vector<std::string> loadMap(std::string const &mapPath);
102 
110  std::vector<std::string> getFilesListFromDirectory(std::string const &directory, std::string const &suffix);
111 
118  std::vector<std::vector<std::string>> loadAllMaps(std::string const &directoryPath);
119 
120  asio::io_context _context;
121 
122  std::queue<std::function<void(void)>> _responseQueue;
123 
124  std::shared_ptr<IUdpCommunication> _com;
125 
126  std::vector<byte> _buffer_to_get;
127 
128  std::thread _thread;
129 
130  bool _isRunning;
131 
132  std::unordered_map<asio::ip::address, std::unordered_map<unsigned short, int>> _endpoints;
133  int client_id = 0;
134  Registry _registry;
135 
136  System::MoveSystem _moveSystem;
137  System::EndGameSystem _endGameSystem;
138  System::DirectionSystem _directionSystem;
139  System::ShootSystem _shootSystem;
140  System::SpawnEnemySystem _spawnEnemySystem;
141  System::ReceiveSystem _receiveSystem;
142  System::HitboxSystem _hitboxSystem;
143  System::DisconnectionSystem _disconnectionSystem;
144  System::NewClientSystem _newClientSystem;
145  System::JoinLobbySystem _joinLobbySystem;
146  System::StartGameSystem _startGameSystem;
147 
148  bool _serverIsRunning = true;
149 };
150 
151 #endif /* !SERVER_HPP_ */
Server::setUpComponents
void setUpComponents()
Set the Up Components object.
Definition: Server.cpp:136
Server::~Server
~Server()
Destroy the Server object.
Definition: Server.cpp:44
System::ReceiveSystem
System in charge of dispatch in specific queues sended request from queue.
Definition: ReceiveSystem.hpp:30
System::DirectionSystem
The Direction System class, it handles all packets related to movement by the clients.
Definition: DirectionSystem.hpp:28
System::StartGameSystem
System in charge of handle start of game.
Definition: StartGameSystem.hpp:31
System::JoinLobbySystem
The Join Lobby System class, it handles all packets related to lobby join by the clients.
Definition: JoinLobbySystem.hpp:27
System::ShootSystem
The Shoot System class, it handles all packets related to a mouse click by the user,...
Definition: ShootSystem.hpp:31
Registry
Class that handle ECS.
Definition: Registry.hpp:22
System::MoveSystem
The Move System class, it handles all packets related to movement by the clients.
Definition: MoveSystem.hpp:30
System::EndGameSystem
The End Game System class, it handles the end of the game of lobbies.
Definition: EndGameSystem.hpp:27
System::DisconnectionSystem
The Disconnection System class, it handles disconnection of a client.
Definition: DisconnectionSystem.hpp:28
Server::setUpEcs
void setUpEcs()
Set the Up Ecs object.
Definition: Server.cpp:98
System::SpawnEnemySystem
The SpawnEnemy System class, it handles all packets related to the spawn of an enemy in the game.
Definition: SpawnEnemySystem.hpp:37
System::HitboxSystem
The Hitbox System class, it handles everything related to collisions between entities.
Definition: HitboxSystem.hpp:31
Server::Server
Server(short const port)
Construct a new Server object.
Definition: Server.cpp:37
System::NewClientSystem
NewClientSystem class that handles the new client.
Definition: NewClientSystem.hpp:28
Server
Server class to handle Server.
Definition: Server.hpp:46