From: Daniel Karbach Date: Wed, 21 Oct 2015 07:19:03 +0000 (+0200) Subject: server: distribute received messages to clients X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=dbd214ac278019c20c883cbb76456b0b0fddc063;p=blank.git server: distribute received messages to clients --- diff --git a/src/server/ClientConnection.hpp b/src/server/ClientConnection.hpp index 11670bc..1a781c5 100644 --- a/src/server/ClientConnection.hpp +++ b/src/server/ClientConnection.hpp @@ -83,6 +83,7 @@ private: void On(const Packet::Login &) override; void On(const Packet::Part &) override; void On(const Packet::PlayerUpdate &) override; + void On(const Packet::Message &) override; bool CanSpawn(const Entity &) const noexcept; bool CanDespawn(const Entity &) const noexcept; diff --git a/src/server/Server.hpp b/src/server/Server.hpp index cef98be..683d362 100644 --- a/src/server/Server.hpp +++ b/src/server/Server.hpp @@ -5,6 +5,7 @@ #include "../world/World.hpp" #include "../world/WorldManipulator.hpp" +#include #include #include @@ -45,11 +46,16 @@ public: void SetBlock(Chunk &, int, const Block &) override; + /// send message to all connected clients + void DistributeMessage(std::uint8_t type, std::uint32_t ref, const std::string &msg); + private: void HandlePacket(const UDPpacket &); ClientConnection &GetClient(const IPaddress &); + void SendAll(); + private: UDPsocket serv_sock; UDPpacket serv_pack; diff --git a/src/server/net.cpp b/src/server/net.cpp index bb42979..6ceb186 100644 --- a/src/server/net.cpp +++ b/src/server/net.cpp @@ -582,6 +582,19 @@ bool ClientConnection::ChunkInRange(const glm::ivec3 &pos) const noexcept { return HasPlayer() && PlayerChunks().InRange(pos); } +void ClientConnection::On(const Packet::Message &pack) { + uint8_t type; + uint32_t ref; + string msg; + pack.ReadType(type); + pack.ReadReferral(ref); + pack.ReadMessage(msg); + + if (type == 1 && HasPlayer()) { + server.DistributeMessage(1, PlayerEntity().ID(), msg); + } +} + Server::Server( const Config::Network &conf, @@ -709,5 +722,20 @@ void Server::SetBlock(Chunk &chunk, int index, const Block &block) { } } +void Server::DistributeMessage(uint8_t type, uint32_t ref, const string &msg) { + auto pack = Packet::Make(serv_pack); + pack.WriteType(type); + pack.WriteReferral(ref); + pack.WriteMessage(msg); + serv_pack.len = sizeof(Packet::Header) + Packet::Message::GetSize(msg); + SendAll(); +} + +void Server::SendAll() { + for (ClientConnection &client : clients) { + client.GetConnection().Send(serv_pack, serv_sock); + } +} + } }