]> git.localhorst.tv Git - blank.git/blob - src/server/ClientConnection.hpp
transmit player input from client to server
[blank.git] / src / server / ClientConnection.hpp
1 #ifndef BLANK_SERVER_CLIENTCONNECTION_HPP_
2 #define BLANK_SERVER_CLIENTCONNECTION_HPP_
3
4 #include "ChunkTransmitter.hpp"
5 #include "Server.hpp"
6 #include "../app/IntervalTimer.hpp"
7 #include "../ui/DirectInput.hpp"
8 #include "../net/Connection.hpp"
9 #include "../net/ConnectionHandler.hpp"
10 #include "../world/EntityState.hpp"
11 #include "../world/Player.hpp"
12
13 #include <deque>
14 #include <list>
15 #include <memory>
16 #include <SDL_net.h>
17 #include <vector>
18
19
20 namespace blank {
21
22 class CompositeModel;
23
24 namespace server {
25
26 class Server;
27
28 class ClientConnection
29 : public ConnectionHandler {
30
31 public:
32         explicit ClientConnection(Server &, const IPaddress &);
33         ~ClientConnection();
34
35         bool Matches(const IPaddress &addr) const noexcept { return conn.Matches(addr); }
36
37         void Update(int dt);
38
39         Connection &GetConnection() noexcept { return conn; }
40         bool Disconnected() const noexcept { return conn.Closed(); }
41
42         /// prepare a packet of given type
43         template<class Type>
44         Type Prepare() const noexcept {
45                 return Packet::Make<Type>(server.GetPacket());
46         }
47         /// send the previously prepared packet
48         std::uint16_t Send();
49         /// send the previously prepared packet of non-default length
50         std::uint16_t Send(std::size_t len);
51
52         void AttachPlayer(Player &);
53         void DetachPlayer();
54         bool HasPlayer() const noexcept { return !!input; }
55         Entity &PlayerEntity() noexcept { return input->GetPlayer().GetEntity(); }
56         const Entity &PlayerEntity() const noexcept { return input->GetPlayer().GetEntity(); }
57         ChunkIndex &PlayerChunks() noexcept { return input->GetPlayer().GetChunks(); }
58         const ChunkIndex &PlayerChunks() const noexcept { return input->GetPlayer().GetChunks(); }
59
60         void SetPlayerModel(const CompositeModel &) noexcept;
61         bool HasPlayerModel() const noexcept;
62         const CompositeModel &GetPlayerModel() const noexcept;
63
64 private:
65         struct SpawnStatus {
66                 // the entity in question
67                 Entity *const entity = nullptr;
68                 // sequence number of the spawn packet or -1 after it's been ack'd
69                 std::int32_t spawn_pack = -1;
70                 // sequence number of the despawn packet or -1 if no despawn has been sent
71                 std::int32_t despawn_pack = -1;
72
73                 explicit SpawnStatus(Entity &);
74                 ~SpawnStatus();
75         };
76
77 private:
78         void OnPacketReceived(std::uint16_t) override;
79         void OnPacketLost(std::uint16_t) override;
80
81         void On(const Packet::Login &) override;
82         void On(const Packet::Part &) override;
83         void On(const Packet::PlayerUpdate &) override;
84
85         bool CanSpawn(const Entity &) const noexcept;
86         bool CanDespawn(const Entity &) const noexcept;
87
88         void SendSpawn(SpawnStatus &);
89         void SendDespawn(SpawnStatus &);
90         void QueueUpdate(SpawnStatus &);
91         void SendUpdates();
92
93         void CheckPlayerFix();
94
95         void CheckChunkQueue();
96
97 private:
98         Server &server;
99         Connection conn;
100         std::unique_ptr<DirectInput> input;
101         const CompositeModel *player_model;
102         std::list<SpawnStatus> spawns;
103         unsigned int confirm_wait;
104
105         std::vector<SpawnStatus *> entity_updates;
106
107         EntityState player_update_state;
108         std::uint16_t player_update_pack;
109         IntervalTimer player_update_timer;
110         std::uint8_t old_actions;
111
112         ChunkTransmitter transmitter;
113         std::deque<glm::ivec3> chunk_queue;
114         glm::ivec3 old_base;
115
116 };
117
118 }
119 }
120
121 #endif