Code: 0
Payload: none
+Length: 0
Login
Payload:
0 player name, max 32 byte UTF-8 string,
shorter names should be zero terminated
+Length: 0-32
Join
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
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
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));
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<class PayloadType>
PayloadType As() {
#include <cstring>
#include <iostream>
+#include <glm/gtx/io.hpp>
using namespace std;
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 {
return conn.Send(client_pack, client_sock);
}
+uint16_t Client::SendPlayerUpdate(const Entity &player) {
+ auto pack = Packet::Make<Packet::PlayerUpdate>(client_pack);
+ pack.WritePlayer(player);
+ return conn.Send(client_pack, client_sock);
+}
+
ClientConnection::ClientConnection(Server &server, const IPaddress &addr)
: server(server)
conn.Close();
}
+void ClientConnection::On(const Packet::PlayerUpdate &pack) {
+ if (!HasPlayer()) return;
+ pack.ReadPlayer(Player());
+}
+
Connection::Connection(const IPaddress &addr)
: handler(nullptr)
return "Join";
case Part::TYPE:
return "Part";
+ case PlayerUpdate::TYPE:
+ return "PlayerUpdate";
default:
return "Unknown";
}
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<const Packet *>(udp_pack.data);
case Packet::Part::TYPE:
On(Packet::As<Packet::Part>(udp_pack));
break;
+ case Packet::PlayerUpdate::TYPE:
+ On(Packet::As<Packet::PlayerUpdate>(udp_pack));
+ break;
default:
// drop unknown or unhandled packets
break;