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