1 #ifndef BLANK_SERVER_CLIENTCONNECTION_HPP_
2 #define BLANK_SERVER_CLIENTCONNECTION_HPP_
4 #include "ChunkTransmitter.hpp"
6 #include "../app/IntervalTimer.hpp"
7 #include "../net/Connection.hpp"
8 #include "../net/ConnectionHandler.hpp"
9 #include "../world/EntityState.hpp"
10 #include "../world/Player.hpp"
22 class ClientConnection
23 : public ConnectionHandler {
26 explicit ClientConnection(Server &, const IPaddress &);
29 bool Matches(const IPaddress &addr) const noexcept { return conn.Matches(addr); }
33 Connection &GetConnection() noexcept { return conn; }
34 bool Disconnected() const noexcept { return conn.Closed(); }
36 /// prepare a packet of given type
38 Type Prepare() const noexcept {
39 return Packet::Make<Type>(server.GetPacket());
41 /// send the previously prepared packet
43 /// send the previously prepared packet of non-default length
44 std::uint16_t Send(std::size_t len);
46 void AttachPlayer(const Player &);
48 bool HasPlayer() const noexcept { return player.entity; }
49 Entity &PlayerEntity() noexcept { return *player.entity; }
50 const Entity &PlayerEntity() const noexcept { return *player.entity; }
51 ChunkIndex &PlayerChunks() noexcept { return *player.chunks; }
52 const ChunkIndex &PlayerChunks() const noexcept { return *player.chunks; }
56 // the entity in question
57 Entity *const entity = nullptr;
58 // sequence number of the spawn packet or -1 after it's been ack'd
59 std::int32_t spawn_pack = -1;
60 // sequence number of the despawn packet or -1 if no despawn has been sent
61 std::int32_t despawn_pack = -1;
63 explicit SpawnStatus(Entity &);
68 void OnPacketReceived(std::uint16_t) override;
69 void OnPacketLost(std::uint16_t) override;
71 void On(const Packet::Login &) override;
72 void On(const Packet::Part &) override;
73 void On(const Packet::PlayerUpdate &) override;
75 bool CanSpawn(const Entity &) const noexcept;
76 bool CanDespawn(const Entity &) const noexcept;
78 void SendSpawn(SpawnStatus &);
79 void SendDespawn(SpawnStatus &);
80 void SendUpdate(SpawnStatus &);
82 void CheckPlayerFix();
84 void CheckChunkQueue();
90 std::list<SpawnStatus> spawns;
91 unsigned int confirm_wait;
93 EntityState player_update_state;
94 std::uint16_t player_update_pack;
95 IntervalTimer player_update_timer;
97 ChunkTransmitter transmitter;
98 std::deque<glm::ivec3> chunk_queue;