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"
26 class ClientConnection
27 : public ConnectionHandler {
30 explicit ClientConnection(Server &, const IPaddress &);
33 bool Matches(const IPaddress &addr) const noexcept { return conn.Matches(addr); }
37 Connection &GetConnection() noexcept { return conn; }
38 bool Disconnected() const noexcept { return conn.Closed(); }
40 /// prepare a packet of given type
42 Type Prepare() const noexcept {
43 return Packet::Make<Type>(server.GetPacket());
45 /// send the previously prepared packet
47 /// send the previously prepared packet of non-default length
48 std::uint16_t Send(std::size_t len);
50 void AttachPlayer(const Player &);
52 bool HasPlayer() const noexcept { return player.entity; }
53 Entity &PlayerEntity() noexcept { return *player.entity; }
54 const Entity &PlayerEntity() const noexcept { return *player.entity; }
55 ChunkIndex &PlayerChunks() noexcept { return *player.chunks; }
56 const ChunkIndex &PlayerChunks() const noexcept { return *player.chunks; }
58 void SetPlayerModel(const CompositeModel &) noexcept;
59 bool HasPlayerModel() const noexcept;
60 const CompositeModel &GetPlayerModel() const noexcept;
64 // the entity in question
65 Entity *const entity = nullptr;
66 // sequence number of the spawn packet or -1 after it's been ack'd
67 std::int32_t spawn_pack = -1;
68 // sequence number of the despawn packet or -1 if no despawn has been sent
69 std::int32_t despawn_pack = -1;
71 explicit SpawnStatus(Entity &);
76 void OnPacketReceived(std::uint16_t) override;
77 void OnPacketLost(std::uint16_t) override;
79 void On(const Packet::Login &) override;
80 void On(const Packet::Part &) override;
81 void On(const Packet::PlayerUpdate &) override;
83 bool CanSpawn(const Entity &) const noexcept;
84 bool CanDespawn(const Entity &) const noexcept;
86 void SendSpawn(SpawnStatus &);
87 void SendDespawn(SpawnStatus &);
88 void QueueUpdate(SpawnStatus &);
91 void CheckPlayerFix();
93 void CheckChunkQueue();
99 const CompositeModel *player_model;
100 std::list<SpawnStatus> spawns;
101 unsigned int confirm_wait;
103 std::vector<SpawnStatus *> entity_updates;
105 EntityState player_update_state;
106 std::uint16_t player_update_pack;
107 IntervalTimer player_update_timer;
109 ChunkTransmitter transmitter;
110 std::deque<glm::ivec3> chunk_queue;