]> git.localhorst.tv Git - blank.git/blob - src/net/ClientConnection.hpp
fixed old packet acks
[blank.git] / src / net / ClientConnection.hpp
1 #ifndef BLANK_NET_CLIENTCONNECTION_HPP_
2 #define BLANK_NET_CLIENTCONNECTION_HPP_
3
4 #include "Connection.hpp"
5 #include "ConnectionHandler.hpp"
6 #include "../app/IntervalTimer.hpp"
7 #include "../world/EntityState.hpp"
8
9 #include <list>
10 #include <SDL_net.h>
11
12
13 namespace blank {
14
15 class Entity;
16 class Server;
17
18 class ClientConnection
19 : public ConnectionHandler {
20
21 public:
22         explicit ClientConnection(Server &, const IPaddress &);
23         ~ClientConnection();
24
25         bool Matches(const IPaddress &addr) const noexcept { return conn.Matches(addr); }
26
27         void Update(int dt);
28
29         Connection &GetConnection() noexcept { return conn; }
30         bool Disconnected() const noexcept { return conn.Closed(); }
31
32         void AttachPlayer(Entity &);
33         void DetachPlayer();
34         bool HasPlayer() const noexcept { return player; }
35         Entity &Player() noexcept { return *player; }
36         const Entity &Player() const noexcept { return *player; }
37
38 private:
39         struct SpawnStatus {
40                 // the entity in question
41                 Entity *const entity = nullptr;
42                 // sequence number of the spawn packet or -1 after it's been ack'd
43                 std::int32_t spawn_pack = -1;
44                 // sequence number of the despawn packet or -1 if no despawn has been sent
45                 std::int32_t despawn_pack = -1;
46
47                 explicit SpawnStatus(Entity &);
48                 ~SpawnStatus();
49         };
50
51 private:
52         void OnPacketReceived(std::uint16_t) override;
53         void OnPacketLost(std::uint16_t) override;
54
55         void On(const Packet::Login &) override;
56         void On(const Packet::Part &) override;
57         void On(const Packet::PlayerUpdate &) override;
58
59         bool CanSpawn(const Entity &) const noexcept;
60         bool CanDespawn(const Entity &) const noexcept;
61
62         void SendSpawn(SpawnStatus &);
63         void SendDespawn(SpawnStatus &);
64         void SendUpdate(SpawnStatus &);
65
66         void CheckPlayerFix();
67
68 private:
69         Server &server;
70         Connection conn;
71         Entity *player;
72         std::list<SpawnStatus> spawns;
73         unsigned int confirm_wait;
74         EntityState player_update_state;
75         std::uint16_t player_update_pack;
76         IntervalTimer player_update_timer;
77
78 };
79
80 }
81
82 #endif