]> git.localhorst.tv Git - blank.git/blob - src/server/Server.hpp
9b89ab1bbc94692a400c7a3a6eda0054ca98a161
[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 "../shared/CLI.hpp"
6 #include "../world/World.hpp"
7 #include "../world/WorldManipulator.hpp"
8
9 #include <cstdint>
10 #include <list>
11 #include <SDL_net.h>
12
13
14 namespace blank {
15
16 class ChunkIndex;
17 class CLIContext;
18 class Model;
19 class Player;
20 class WorldSave;
21
22 namespace server {
23
24 class ClientConnection;
25
26 class Server
27 : public WorldManipulator {
28
29 public:
30         Server(const Config::Network &, World &, const World::Config &, const WorldSave &);
31         ~Server();
32
33         // wait for data to arrive for at most dt milliseconds
34         void Wait(int dt) noexcept;
35         // true if there's data waiting to be handled
36         bool Ready() noexcept;
37         void Handle();
38
39         void Update(int dt);
40
41         UDPsocket &GetSocket() noexcept { return serv_sock; }
42         UDPpacket &GetPacket() noexcept { return serv_pack; }
43
44         World &GetWorld() noexcept { return world; }
45         const WorldSave &GetWorldSave() noexcept { return save; }
46
47         void SetPlayerModel(const Model &) noexcept;
48         bool HasPlayerModel() const noexcept;
49         const Model &GetPlayerModel() const noexcept;
50
51         Player *JoinPlayer(const std::string &name);
52
53         void SetBlock(Chunk &, int, const Block &) override;
54
55         /// for use by client connections when they receive a line from the player
56         void DispatchMessage(CLIContext &, const std::string &);
57
58         /// send message to all connected clients
59         void DistributeMessage(std::uint8_t type, std::uint32_t ref, const std::string &msg);
60
61 private:
62         void HandlePacket(const UDPpacket &);
63
64         ClientConnection &GetClient(const IPaddress &);
65
66         void SendAll();
67
68 private:
69         UDPsocket serv_sock;
70         UDPpacket serv_pack;
71         SDLNet_SocketSet serv_set;
72         std::list<ClientConnection> clients;
73
74         World &world;
75         ChunkIndex &spawn_index;
76         const WorldSave &save;
77         const Model *player_model;
78
79         CLI cli;
80
81 };
82
83 }
84 }
85
86 #endif