]> git.localhorst.tv Git - blank.git/blobdiff - src/server/net.cpp
server: distribute received messages to clients
[blank.git] / src / server / net.cpp
index 1e60ff6ead06d3dfd7905946866d4d4920ead0fd..6ceb186644d3a9f7d5d24526d3cfb2376492db8c 100644 (file)
@@ -4,11 +4,12 @@
 
 #include "../app/init.hpp"
 #include "../io/WorldSave.hpp"
-#include "../model/CompositeModel.hpp"
+#include "../model/Model.hpp"
 #include "../world/ChunkIndex.hpp"
 #include "../world/Entity.hpp"
 #include "../world/World.hpp"
 
+#include <algorithm>
 #include <iostream>
 #include <zlib.h>
 #include <glm/gtx/io.hpp>
@@ -352,6 +353,23 @@ void ClientConnection::CheckPlayerFix() {
        }
 }
 
+namespace {
+
+struct QueueCompare {
+       explicit QueueCompare(const glm::ivec3 &base)
+       : base(base) { }
+       bool operator ()(const glm::ivec3 &left, const glm::ivec3 &right) const noexcept {
+               const glm::ivec3 ld(left - base);
+               const glm::ivec3 rd(right - base);
+               return
+                       ld.x * ld.x + ld.y * ld.y + ld.z * ld.z <
+                       rd.x * rd.x + rd.y * rd.y + rd.z * rd.z;
+       }
+       const glm::ivec3 &base;
+};
+
+}
+
 void ClientConnection::CheckChunkQueue() {
        if (PlayerChunks().Base() != old_base) {
                Chunk::Pos begin = PlayerChunks().CoordsBegin();
@@ -366,6 +384,7 @@ void ClientConnection::CheckChunkQueue() {
                        }
                }
                old_base = PlayerChunks().Base();
+               sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base));
        }
        if (transmitter.Transmitting()) {
                transmitter.Transmit();
@@ -406,6 +425,7 @@ void ClientConnection::AttachPlayer(Player &player) {
                        }
                }
        }
+       sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base));
        // TODO: should the server do this?
        if (HasPlayerModel()) {
                GetPlayerModel().Instantiate(PlayerEntity().GetModel());
@@ -426,7 +446,7 @@ void ClientConnection::DetachPlayer() {
        old_actions = 0;
 }
 
-void ClientConnection::SetPlayerModel(const CompositeModel &m) noexcept {
+void ClientConnection::SetPlayerModel(const Model &m) noexcept {
        player_model = &m;
        if (HasPlayer()) {
                m.Instantiate(PlayerEntity().GetModel());
@@ -437,7 +457,7 @@ bool ClientConnection::HasPlayerModel() const noexcept {
        return player_model;
 }
 
-const CompositeModel &ClientConnection::GetPlayerModel() const noexcept {
+const Model &ClientConnection::GetPlayerModel() const noexcept {
        return *player_model;
 }
 
@@ -562,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,
@@ -642,7 +675,7 @@ void Server::Update(int dt) {
        }
 }
 
-void Server::SetPlayerModel(const CompositeModel &m) noexcept {
+void Server::SetPlayerModel(const Model &m) noexcept {
        player_model = &m;
        for (ClientConnection &client : clients) {
                client.SetPlayerModel(m);
@@ -653,7 +686,7 @@ bool Server::HasPlayerModel() const noexcept {
        return player_model;
 }
 
-const CompositeModel &Server::GetPlayerModel() const noexcept {
+const Model &Server::GetPlayerModel() const noexcept {
        return *player_model;
 }
 
@@ -689,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);
+       }
+}
+
 }
 }