]> git.localhorst.tv Git - blank.git/commitdiff
server: distribute received messages to clients
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 21 Oct 2015 07:19:03 +0000 (09:19 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 21 Oct 2015 07:19:03 +0000 (09:19 +0200)
src/server/ClientConnection.hpp
src/server/Server.hpp
src/server/net.cpp

index 11670bcee6da3e8e4fc4c5117899e327bcd1d0e0..1a781c5dd6ad5dea7f88b033e4648bed6aee4550 100644 (file)
@@ -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;
index cef98beaeba764844059a5663179513f5fcd5ce6..683d36254c76523a6ba5afddd84c2607ae204292 100644 (file)
@@ -5,6 +5,7 @@
 #include "../world/World.hpp"
 #include "../world/WorldManipulator.hpp"
 
+#include <cstdint>
 #include <list>
 #include <SDL_net.h>
 
@@ -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;
index bb42979aed1d39f65d533c0cbdc20fab05b86cfa..6ceb186644d3a9f7d5d24526d3cfb2376492db8c 100644 (file)
@@ -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<Packet::Message>(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);
+       }
+}
+
 }
 }