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