From c6ca9d21e45af5ea7caeec722a9b59fdf3aa3b24 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sat, 5 Sep 2015 16:16:45 +0200 Subject: [PATCH] send player updates from client to server --- doc/protocol | 16 +++++++++++++ src/client/client.cpp | 2 ++ src/net/Client.hpp | 1 + src/net/ClientConnection.hpp | 3 +++ src/net/ConnectionHandler.hpp | 1 + src/net/Packet.hpp | 8 +++++++ src/net/net.cpp | 45 +++++++++++++++++++++++++++++++++++ 7 files changed, 76 insertions(+) diff --git a/doc/protocol b/doc/protocol index a20d8d6..43c14da 100644 --- a/doc/protocol +++ b/doc/protocol @@ -30,6 +30,7 @@ a while. Code: 0 Payload: none +Length: 0 Login @@ -43,6 +44,7 @@ Code: 1 Payload: 0 player name, max 32 byte UTF-8 string, shorter names should be zero terminated +Length: 0-32 Join @@ -58,6 +60,7 @@ Payload: 16 pos/vel/rot/ang of the player, 13x 32bit float 68 name of the world the server's currently running max 32 byte UTF-8 string +Length: 68-100 Part @@ -69,3 +72,16 @@ Optionally sent by the client on disconnect. Code: 3 Payload: none +Length: 0 + + +Player Update +------------- + +Sent by clients to notify the server of their changes to the player. + +Code: 4 +Payload: + 0 chunk coords of the player, 3x 32bit signed int + 12 pos/vel/rot/ang of the player, 13x 32bit float +Length: 64 diff --git a/src/client/client.cpp b/src/client/client.cpp index 8dd575a..9c87549 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -99,6 +99,8 @@ void InteractiveState::Update(int dt) { chunk_renderer.Rebase(interface.Player().ChunkCoords()); chunk_renderer.Update(dt); + master.GetClient().SendPlayerUpdate(interface.Player()); + glm::mat4 trans = interface.Player().Transform(interface.Player().ChunkCoords()); glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)); glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)); diff --git a/src/net/Client.hpp b/src/net/Client.hpp index bd29c85..5c0a9c2 100644 --- a/src/net/Client.hpp +++ b/src/net/Client.hpp @@ -32,6 +32,7 @@ public: std::uint16_t SendPing(); std::uint16_t SendLogin(const std::string &); + std::uint16_t SendPlayerUpdate(const Entity &); private: void HandlePacket(const UDPpacket &); diff --git a/src/net/ClientConnection.hpp b/src/net/ClientConnection.hpp index acbee6e..8c5306c 100644 --- a/src/net/ClientConnection.hpp +++ b/src/net/ClientConnection.hpp @@ -28,9 +28,12 @@ public: void AttachPlayer(Entity &); void DetachPlayer(); + bool HasPlayer() const noexcept { return player; } + Entity &Player() noexcept { return *player; } void On(const Packet::Login &) override; void On(const Packet::Part &) override; + void On(const Packet::PlayerUpdate &) override; private: Server &server; diff --git a/src/net/ConnectionHandler.hpp b/src/net/ConnectionHandler.hpp index 3eb8f55..29595e5 100644 --- a/src/net/ConnectionHandler.hpp +++ b/src/net/ConnectionHandler.hpp @@ -22,6 +22,7 @@ private: virtual void On(const Packet::Login &) { } virtual void On(const Packet::Join &) { } virtual void On(const Packet::Part &) { } + virtual void On(const Packet::PlayerUpdate &) { } }; diff --git a/src/net/Packet.hpp b/src/net/Packet.hpp index 5e9751c..0dab949 100644 --- a/src/net/Packet.hpp +++ b/src/net/Packet.hpp @@ -86,6 +86,14 @@ struct Packet { static constexpr std::size_t MAX_LEN = 0; }; + struct PlayerUpdate : public Payload { + static constexpr std::uint8_t TYPE = 4; + static constexpr std::size_t MAX_LEN = 64; + + void WritePlayer(const Entity &) noexcept; + void ReadPlayer(Entity &) const noexcept; + }; + template PayloadType As() { diff --git a/src/net/net.cpp b/src/net/net.cpp index 7f12e63..837a7fb 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -11,6 +11,7 @@ #include #include +#include using namespace std; @@ -21,6 +22,7 @@ constexpr size_t Packet::Ping::MAX_LEN; constexpr size_t Packet::Login::MAX_LEN; constexpr size_t Packet::Join::MAX_LEN; constexpr size_t Packet::Part::MAX_LEN; +constexpr size_t Packet::PlayerUpdate::MAX_LEN; namespace { @@ -101,6 +103,12 @@ uint16_t Client::SendLogin(const string &name) { return conn.Send(client_pack, client_sock); } +uint16_t Client::SendPlayerUpdate(const Entity &player) { + auto pack = Packet::Make(client_pack); + pack.WritePlayer(player); + return conn.Send(client_pack, client_sock); +} + ClientConnection::ClientConnection(Server &server, const IPaddress &addr) : server(server) @@ -162,6 +170,11 @@ void ClientConnection::On(const Packet::Part &) { conn.Close(); } +void ClientConnection::On(const Packet::PlayerUpdate &pack) { + if (!HasPlayer()) return; + pack.ReadPlayer(Player()); +} + Connection::Connection(const IPaddress &addr) : handler(nullptr) @@ -291,6 +304,8 @@ const char *Packet::Type2String(uint8_t t) noexcept { return "Join"; case Part::TYPE: return "Part"; + case PlayerUpdate::TYPE: + return "PlayerUpdate"; default: return "Unknown"; } @@ -383,6 +398,33 @@ void Packet::Join::ReadWorldName(string &name) const noexcept { ReadString(name, 68, 32); } +void Packet::PlayerUpdate::WritePlayer(const Entity &player) noexcept { + Write(player.ChunkCoords(), 0); + Write(player.Position(), 12); + Write(player.Velocity(), 24); + Write(player.Orientation(), 36); + Write(player.AngularVelocity(), 52); +} + +void Packet::PlayerUpdate::ReadPlayer(Entity &player) const noexcept { + glm::ivec3 chunk_coords(0); + glm::vec3 pos; + glm::vec3 vel; + glm::quat rot; + glm::vec3 ang; + + Read(chunk_coords, 0); + Read(pos, 12); + Read(vel, 24); + Read(rot, 36); + Read(ang, 52); + + player.Position(chunk_coords, pos); + player.Velocity(vel); + player.Orientation(rot); + player.AngularVelocity(ang); +} + void ConnectionHandler::Handle(const UDPpacket &udp_pack) { const Packet &pack = *reinterpret_cast(udp_pack.data); @@ -399,6 +441,9 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) { case Packet::Part::TYPE: On(Packet::As(udp_pack)); break; + case Packet::PlayerUpdate::TYPE: + On(Packet::As(udp_pack)); + break; default: // drop unknown or unhandled packets break; -- 2.39.2