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