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;
#include "../world/World.hpp"
#include "../world/WorldManipulator.hpp"
+#include <cstdint>
#include <list>
#include <SDL_net.h>
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;
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,
}
}
+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);
+ }
+}
+
}
}