1 #ifndef BLANK_SERVER_CLIENTCONNECTION_HPP_
2 #define BLANK_SERVER_CLIENTCONNECTION_HPP_
4 #include "ChunkTransmitter.hpp"
6 #include "../app/IntervalTimer.hpp"
7 #include "../ui/DirectInput.hpp"
8 #include "../net/Connection.hpp"
9 #include "../net/ConnectionHandler.hpp"
10 #include "../world/EntityState.hpp"
11 #include "../world/Player.hpp"
28 class ClientConnection
29 : public ConnectionHandler {
32 explicit ClientConnection(Server &, const IPaddress &);
35 bool Matches(const IPaddress &addr) const noexcept { return conn.Matches(addr); }
39 Connection &GetConnection() noexcept { return conn; }
40 bool Disconnected() const noexcept { return conn.Closed(); }
42 /// prepare a packet of given type
44 Type Prepare() const noexcept {
45 return Packet::Make<Type>(server.GetPacket());
47 /// send the previously prepared packet
49 /// send the previously prepared packet of non-default length
50 std::uint16_t Send(std::size_t len);
52 void AttachPlayer(Player &);
54 bool HasPlayer() const noexcept { return !!input; }
55 Entity &PlayerEntity() noexcept { return input->GetPlayer().GetEntity(); }
56 const Entity &PlayerEntity() const noexcept { return input->GetPlayer().GetEntity(); }
57 ChunkIndex &PlayerChunks() noexcept { return input->GetPlayer().GetChunks(); }
58 const ChunkIndex &PlayerChunks() const noexcept { return input->GetPlayer().GetChunks(); }
60 void SetPlayerModel(const CompositeModel &) noexcept;
61 bool HasPlayerModel() const noexcept;
62 const CompositeModel &GetPlayerModel() const noexcept;
64 bool ChunkInRange(const glm::ivec3 &) const noexcept;
68 // the entity in question
69 Entity *const entity = nullptr;
70 // sequence number of the spawn packet or -1 after it's been ack'd
71 std::int32_t spawn_pack = -1;
72 // sequence number of the despawn packet or -1 if no despawn has been sent
73 std::int32_t despawn_pack = -1;
75 explicit SpawnStatus(Entity &);
80 void OnPacketReceived(std::uint16_t) override;
81 void OnPacketLost(std::uint16_t) override;
83 void On(const Packet::Login &) override;
84 void On(const Packet::Part &) override;
85 void On(const Packet::PlayerUpdate &) override;
87 bool CanSpawn(const Entity &) const noexcept;
88 bool CanDespawn(const Entity &) const noexcept;
90 void SendSpawn(SpawnStatus &);
91 void SendDespawn(SpawnStatus &);
92 void QueueUpdate(SpawnStatus &);
95 void CheckPlayerFix();
97 void CheckChunkQueue();
102 std::unique_ptr<DirectInput> input;
103 const CompositeModel *player_model;
104 std::list<SpawnStatus> spawns;
105 unsigned int confirm_wait;
107 std::vector<SpawnStatus *> entity_updates;
109 EntityState player_update_state;
110 std::uint16_t player_update_pack;
111 IntervalTimer player_update_timer;
112 std::uint8_t old_actions;
114 ChunkTransmitter transmitter;
115 std::deque<glm::ivec3> chunk_queue;