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