X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnet%2FClientConnection.hpp;h=075fdcec190655e3aff8534b40a359c36db8e70e;hb=ae5a7e7d8517fac406a88e9bf98fd3d5bb1728b9;hp=8c5306c07945d4f80d4af15c44400d160508126e;hpb=c6ca9d21e45af5ea7caeec722a9b59fdf3aa3b24;p=blank.git diff --git a/src/net/ClientConnection.hpp b/src/net/ClientConnection.hpp index 8c5306c..075fdce 100644 --- a/src/net/ClientConnection.hpp +++ b/src/net/ClientConnection.hpp @@ -1,15 +1,21 @@ #ifndef BLANK_NET_CLIENTCONNECTION_HPP_ #define BLANK_NET_CLIENTCONNECTION_HPP_ +#include "ChunkTransmitter.hpp" #include "Connection.hpp" #include "ConnectionHandler.hpp" +#include "Server.hpp" +#include "../app/IntervalTimer.hpp" +#include "../world/EntityState.hpp" +#include "../world/Player.hpp" +#include +#include #include namespace blank { -class Entity; class Server; class ClientConnection @@ -26,19 +32,70 @@ public: Connection &GetConnection() noexcept { return conn; } bool Disconnected() const noexcept { return conn.Closed(); } - void AttachPlayer(Entity &); + /// prepare a packet of given type + template + Type Prepare() const noexcept { + return Packet::Make(server.GetPacket()); + } + /// send the previously prepared packet + std::uint16_t Send(); + /// send the previously prepared packet of non-default length + std::uint16_t Send(std::size_t len); + + void AttachPlayer(const Player &); void DetachPlayer(); - bool HasPlayer() const noexcept { return player; } - Entity &Player() noexcept { return *player; } + bool HasPlayer() const noexcept { return player.entity; } + Entity &PlayerEntity() noexcept { return *player.entity; } + const Entity &PlayerEntity() const noexcept { return *player.entity; } + ChunkIndex &PlayerChunks() noexcept { return *player.chunks; } + const ChunkIndex &PlayerChunks() const noexcept { return *player.chunks; } + +private: + struct SpawnStatus { + // the entity in question + Entity *const entity = nullptr; + // sequence number of the spawn packet or -1 after it's been ack'd + std::int32_t spawn_pack = -1; + // sequence number of the despawn packet or -1 if no despawn has been sent + std::int32_t despawn_pack = -1; + + explicit SpawnStatus(Entity &); + ~SpawnStatus(); + }; + +private: + void OnPacketReceived(std::uint16_t) override; + void OnPacketLost(std::uint16_t) override; void On(const Packet::Login &) override; void On(const Packet::Part &) override; void On(const Packet::PlayerUpdate &) override; + bool CanSpawn(const Entity &) const noexcept; + bool CanDespawn(const Entity &) const noexcept; + + void SendSpawn(SpawnStatus &); + void SendDespawn(SpawnStatus &); + void SendUpdate(SpawnStatus &); + + void CheckPlayerFix(); + + void CheckChunkQueue(); + private: Server &server; Connection conn; - Entity *player; + Player player; + std::list spawns; + unsigned int confirm_wait; + + EntityState player_update_state; + std::uint16_t player_update_pack; + IntervalTimer player_update_timer; + + ChunkTransmitter transmitter; + std::deque chunk_queue; + glm::ivec3 old_base; };