]> git.localhorst.tv Git - blank.git/blobdiff - src/server/net.cpp
move common exceptions to app/error
[blank.git] / src / server / net.cpp
index e210fef4ff1e7db6cf4a0a2e51f8b0a29f514bb6..b5590ee1b41dfe353d0ba8f12cc9594f572d3d8b 100644 (file)
@@ -2,10 +2,11 @@
 #include "ChunkTransmitter.hpp"
 #include "Server.hpp"
 
-#include "../app/init.hpp"
+#include "../app/error.hpp"
 #include "../geometry/distance.hpp"
 #include "../io/WorldSave.hpp"
 #include "../model/Model.hpp"
+#include "../shared/CommandService.hpp"
 #include "../world/ChunkIndex.hpp"
 #include "../world/Entity.hpp"
 #include "../world/World.hpp"
@@ -647,7 +648,7 @@ uint16_t ClientConnection::SendMessage(uint8_t type, uint32_t from, const string
 
 
 NetworkCLIFeedback::NetworkCLIFeedback(Player &p, ClientConnection &c)
-: CLIContext(p)
+: CLIContext(&p)
 , conn(c) {
 
 }
@@ -665,6 +666,10 @@ void NetworkCLIFeedback::Broadcast(const string &msg) {
 }
 
 
+// relying on {} zero intitialization for UDPpacket, because
+// the type and number of fields is not well defined
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
 Server::Server(
        const Config::Network &conf,
        World &world,
@@ -678,7 +683,9 @@ Server::Server(
 , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
 , save(save)
 , player_model(nullptr)
-, cli(world) {
+, cli(world)
+, cmd_srv() {
+#pragma GCC diagnostic pop
        if (!serv_set) {
                throw NetError("SDLNet_AllocSocketSet");
        }
@@ -697,6 +704,10 @@ Server::Server(
 
        serv_pack.data = new Uint8[sizeof(Packet)];
        serv_pack.maxlen = sizeof(Packet);
+
+       if (conf.cmd_port) {
+               cmd_srv.reset(new CommandService(cli, conf.cmd_port));
+       }
 }
 
 Server::~Server() {
@@ -714,10 +725,16 @@ Server::~Server() {
 
 void Server::Wait(int dt) noexcept {
        SDLNet_CheckSockets(serv_set, dt);
+       if (cmd_srv) {
+               cmd_srv->Wait(0);
+       }
 }
 
 bool Server::Ready() noexcept {
-       return SDLNet_CheckSockets(serv_set, 0) > 0;
+       if (SDLNet_CheckSockets(serv_set, 0) > 0) {
+               return true;
+       }
+       return cmd_srv && cmd_srv->Ready();
 }
 
 void Server::Handle() {
@@ -730,6 +747,9 @@ void Server::Handle() {
                // a boo boo happened
                throw NetError("SDLNet_UDP_Recv");
        }
+       if (cmd_srv) {
+               cmd_srv->Handle();
+       }
 }
 
 void Server::HandlePacket(const UDPpacket &udp_pack) {
@@ -769,6 +789,9 @@ void Server::Update(int dt) {
                        ++client;
                }
        }
+       if (cmd_srv) {
+               cmd_srv->Send();
+       }
 }
 
 void Server::SetPlayerModel(const Model &m) noexcept {