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"
23 class ClientConnection
24 : public ConnectionHandler {
27 explicit ClientConnection(Server &, const IPaddress &);
30 bool Matches(const IPaddress &addr) const noexcept { return conn.Matches(addr); }
34 Connection &GetConnection() noexcept { return conn; }
35 bool Disconnected() const noexcept { return conn.Closed(); }
37 /// prepare a packet of given type
39 Type Prepare() const noexcept {
40 return Packet::Make<Type>(server.GetPacket());
42 /// send the previously prepared packet
44 /// send the previously prepared packet of non-default length
45 std::uint16_t Send(std::size_t len);
47 void AttachPlayer(const Player &);
49 bool HasPlayer() const noexcept { return player.entity; }
50 Entity &PlayerEntity() noexcept { return *player.entity; }
51 const Entity &PlayerEntity() const noexcept { return *player.entity; }
52 ChunkIndex &PlayerChunks() noexcept { return *player.chunks; }
53 const ChunkIndex &PlayerChunks() const noexcept { return *player.chunks; }
57 // the entity in question
58 Entity *const entity = nullptr;
59 // sequence number of the spawn packet or -1 after it's been ack'd
60 std::int32_t spawn_pack = -1;
61 // sequence number of the despawn packet or -1 if no despawn has been sent
62 std::int32_t despawn_pack = -1;
64 explicit SpawnStatus(Entity &);
69 void OnPacketReceived(std::uint16_t) override;
70 void OnPacketLost(std::uint16_t) override;
72 void On(const Packet::Login &) override;
73 void On(const Packet::Part &) override;
74 void On(const Packet::PlayerUpdate &) override;
76 bool CanSpawn(const Entity &) const noexcept;
77 bool CanDespawn(const Entity &) const noexcept;
79 void SendSpawn(SpawnStatus &);
80 void SendDespawn(SpawnStatus &);
81 void QueueUpdate(SpawnStatus &);
84 void CheckPlayerFix();
86 void CheckChunkQueue();
92 std::list<SpawnStatus> spawns;
93 unsigned int confirm_wait;
95 std::vector<SpawnStatus *> entity_updates;
97 EntityState player_update_state;
98 std::uint16_t player_update_pack;
99 IntervalTimer player_update_timer;
101 ChunkTransmitter transmitter;
102 std::deque<glm::ivec3> chunk_queue;