]> git.localhorst.tv Git - blank.git/blob - src/server/Server.hpp
server: distribute received messages to clients
[blank.git] / src / server / Server.hpp
1 #ifndef BLANK_SERVER_SERVER_HPP
2 #define BLANK_SERVER_SERVER_HPP
3
4 #include "../app/Config.hpp"
5 #include "../world/World.hpp"
6 #include "../world/WorldManipulator.hpp"
7
8 #include <cstdint>
9 #include <list>
10 #include <SDL_net.h>
11
12
13 namespace blank {
14
15 class ChunkIndex;
16 class Model;
17 class Player;
18 class WorldSave;
19
20 namespace server {
21
22 class ClientConnection;
23
24 class Server
25 : public WorldManipulator {
26
27 public:
28         Server(const Config::Network &, World &, const World::Config &, const WorldSave &);
29         ~Server();
30
31         void Handle();
32
33         void Update(int dt);
34
35         UDPsocket &GetSocket() noexcept { return serv_sock; }
36         UDPpacket &GetPacket() noexcept { return serv_pack; }
37
38         World &GetWorld() noexcept { return world; }
39         const WorldSave &GetWorldSave() noexcept { return save; }
40
41         void SetPlayerModel(const Model &) noexcept;
42         bool HasPlayerModel() const noexcept;
43         const Model &GetPlayerModel() const noexcept;
44
45         Player *JoinPlayer(const std::string &name);
46
47         void SetBlock(Chunk &, int, const Block &) override;
48
49         /// send message to all connected clients
50         void DistributeMessage(std::uint8_t type, std::uint32_t ref, const std::string &msg);
51
52 private:
53         void HandlePacket(const UDPpacket &);
54
55         ClientConnection &GetClient(const IPaddress &);
56
57         void SendAll();
58
59 private:
60         UDPsocket serv_sock;
61         UDPpacket serv_pack;
62         std::list<ClientConnection> clients;
63
64         World &world;
65         ChunkIndex &spawn_index;
66         const WorldSave &save;
67         const Model *player_model;
68
69 };
70
71 }
72 }
73
74 #endif