1 #ifndef BLANK_NET_CLIENTCONNECTION_HPP_
2 #define BLANK_NET_CLIENTCONNECTION_HPP_
4 #include "ChunkTransmitter.hpp"
5 #include "Connection.hpp"
6 #include "ConnectionHandler.hpp"
8 #include "../app/IntervalTimer.hpp"
9 #include "../world/EntityState.hpp"
10 #include "../world/Player.hpp"
21 class ClientConnection
22 : public ConnectionHandler {
25 explicit ClientConnection(Server &, const IPaddress &);
28 bool Matches(const IPaddress &addr) const noexcept { return conn.Matches(addr); }
32 Connection &GetConnection() noexcept { return conn; }
33 bool Disconnected() const noexcept { return conn.Closed(); }
35 /// prepare a packet of given type
37 Type Prepare() const noexcept {
38 return Packet::Make<Type>(server.GetPacket());
40 /// send the previously prepared packet
42 /// send the previously prepared packet of non-default length
43 std::uint16_t Send(std::size_t len);
45 void AttachPlayer(const Player &);
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; }
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;
62 explicit SpawnStatus(Entity &);
67 void OnPacketReceived(std::uint16_t) override;
68 void OnPacketLost(std::uint16_t) override;
70 void On(const Packet::Login &) override;
71 void On(const Packet::Part &) override;
72 void On(const Packet::PlayerUpdate &) override;
74 bool CanSpawn(const Entity &) const noexcept;
75 bool CanDespawn(const Entity &) const noexcept;
77 void SendSpawn(SpawnStatus &);
78 void SendDespawn(SpawnStatus &);
79 void SendUpdate(SpawnStatus &);
81 void CheckPlayerFix();
83 void CheckChunkQueue();
89 std::list<SpawnStatus> spawns;
90 unsigned int confirm_wait;
92 EntityState player_update_state;
93 std::uint16_t player_update_pack;
94 IntervalTimer player_update_timer;
96 ChunkTransmitter transmitter;
97 std::deque<glm::ivec3> chunk_queue;