Browse Source

removed circular references (previously solved by interfaces), atomic
running bool in server

Kajetan Johannes Hammerle 4 years ago
parent
commit
428a3c6bd2

+ 1 - 1
meson.build

@@ -4,7 +4,7 @@ project('cubes plus plus', 'cpp')
 
 sourcesCommon = ['common/stream/Stream.cpp', 'common/block/BlockRegistry.cpp','common/block/Block.cpp', 'common/utils/DataVector.cpp']
 
-sourcesServer = ['server/Main.cpp', 'server/GameServer.cpp', 'server/network/Server.cpp', 'server/commands/CommandManager.cpp', 'server/commands/CommandUtils.cpp', 'server/commands/GeneralCommands.cpp']
+sourcesServer = ['server/Main.cpp', 'server/GameServer.cpp', 'server/network/Server.cpp', 'server/commands/CommandManager.cpp', 'server/commands/CommandUtils.cpp', 'server/commands/GeneralCommands.cpp', 'server/commands/ServerCommands.cpp']
 
 #sourcesClient = ['client/Main.cpp', 'client/engine/Clock.cpp', 'client/engine/DirectRenderer.cpp', 'client/engine/KeyManager.cpp', 'client/engine/Mesh.cpp', 'client/engine/MouseManager.cpp', 'client/engine/Shader.cpp', 'client/engine/Texture.cpp', 'client/engine/Utils.cpp', 'client/engine/Wrapper.cpp', 'client/engine/shader/ShaderProgram.cpp', 'client/engine/shader/WorldShader.cpp', 'client/engine/shader/FramebufferRectangle.cpp', 'client/engine/shader/SSAOShader.cpp', 'client/engine/shader/SSAOBlurShader.cpp', 'client/engine/shader/WorldPostShader.cpp', 'client/engine/shader/OverlayShader.cpp', 'client/GameClient.cpp', 'client/rendering/ChunkRenderer.cpp', 'client/rendering/ClientChunkProvider.cpp', 'client/rendering/block/BlockRenderer.cpp', 'client/rendering/block/BlockRenderers.cpp', 'client/rendering/entity/EntityRenderer.cpp', 'client/rendering/gui/GUI.cpp', 'client/rendering/gui/StartMenu.cpp', 'client/math/Matrix3D.cpp', 'client/math/Matrix3DStack.cpp', 'client/math/StackOverflow.cpp', 'client/math/StackUnderflow.cpp', 'client/math/Vector3D.cpp', 'client/math/Plane3D.cpp', 'client/math/Camera3D.cpp', 'client/network/Client.cpp', 'client/network/ClientListener.cpp']
 

+ 4 - 10
server/GameServer.cpp

@@ -3,10 +3,10 @@
 #include "server/GameServer.h"
 #include "server/network/Server.h"
 
-void GameServer::stop()
+/*void GameServer::stop()
 {
     isRunning = false;
-}
+}*/
 
 void GameServer::start(uint16_t port, uint16_t maxClients)
 {
@@ -17,8 +17,7 @@ void GameServer::start(uint16_t port, uint16_t maxClients)
         return;
     }
     
-    isRunning = true;
-    while(isRunning)
+    while(serverCommands.isRunning())
     {
         std::cout << "> ";
         std::string command;
@@ -27,7 +26,7 @@ void GameServer::start(uint16_t port, uint16_t maxClients)
         {
             break;
         }
-        commandManager.execute(*this, *this, command);
+        commandManager.execute(serverCommands, command);
     }
 }
 
@@ -69,9 +68,4 @@ void GameServer::onClientDisconnect(int socket) const
     Stream answer;
     answer.write("Bye.");
     answer.sendToSocket(socket);
-}
-
-bool GameServer::isServer() const
-{
-    return true;
 }

+ 3 - 7
server/GameServer.h

@@ -2,11 +2,10 @@
 #define GAMESERVER_H
 
 #include "server/network/IServerListener.h"
-#include "server/commands/ICommandSource.h"
 #include "server/commands/CommandManager.h"
-#include "server/IGameServer.h"
+#include "commands/ServerCommands.h"
 
-class GameServer : public IServerListener, public ICommandSource, public IGameServer
+class GameServer : public IServerListener
 {
 public:
     void start(uint16_t port, uint16_t maxClients);
@@ -16,12 +15,9 @@ public:
     void onClientPackage(int socket, Stream& in) const override;
     void onClientDisconnect(int socket) const override;
     
-    void stop() override;
-    bool isServer() const override;
-    
 private:
-    bool isRunning = false;
     CommandManager commandManager;
+    ServerCommands serverCommands;
 };
 
 #endif

+ 0 - 13
server/IGameServer.h

@@ -1,13 +0,0 @@
-#ifndef IGAMESERVER_H
-#define IGAMESERVER_H
-
-class IGameServer
-{
-public:
-    virtual ~IGameServer() = default;
-    
-    virtual void stop() = 0;
-};
-
-#endif
-

+ 3 - 3
server/commands/CommandManager.cpp

@@ -10,12 +10,12 @@ CommandManager::CommandManager()
     registerCommand("stop", GeneralCommands::stop);
 }
 
-void CommandManager::registerCommand(const std::string& name, void (*command) (IGameServer& gs, ICommandSource&, const std::vector<std::string>&))
+void CommandManager::registerCommand(const std::string& name, Command command)
 {
     commands[name] = command;
 }
 
-void CommandManager::execute(IGameServer& gs, ICommandSource& cs, const std::string& rawCommand) const
+void CommandManager::execute(ServerCommands& sc, const std::string& rawCommand) const
 {
     std::vector<std::string> args;
     
@@ -32,5 +32,5 @@ void CommandManager::execute(IGameServer& gs, ICommandSource& cs, const std::str
         std::cout << "Unknown command: '" << command << "'" << std::endl;
         return;
     }
-    iter->second(gs, cs, args);
+    iter->second(sc, args);
 }

+ 3 - 3
server/commands/CommandManager.h

@@ -4,16 +4,16 @@
 #include <unordered_map>
 #include <vector>
 
-#include "server/commands/ICommandSource.h"
+#include "ServerCommands.h"
 
 class CommandManager
 {
 public:
-    typedef void (*Command) (IGameServer&, ICommandSource&, const std::vector<std::string>&);
+    typedef void (*Command) (ServerCommands&, const std::vector<std::string>&);
     
     CommandManager();
     
-    void execute(IGameServer& gs, ICommandSource& cs, const std::string& rawCommand) const;
+    void execute(ServerCommands& sc, const std::string& rawCommand) const;
 
 private:
     std::unordered_map<std::string, Command> commands;

+ 4 - 4
server/commands/GeneralCommands.cpp

@@ -2,7 +2,7 @@
 
 #include "server/commands/GeneralCommands.h"
 
-void GeneralCommands::test(IGameServer& gs, ICommandSource& cs, const std::vector<std::string>& args)
+void GeneralCommands::test(ServerCommands& sc, const std::vector<std::string>& args)
 {
     std::cout << "test command" << std::endl;
     for(unsigned long i = 0; i < args.size(); i++)
@@ -11,8 +11,8 @@ void GeneralCommands::test(IGameServer& gs, ICommandSource& cs, const std::vecto
     }
 }
 
-void GeneralCommands::stop(IGameServer& gs, ICommandSource& cs, const std::vector<std::string>& args)
+void GeneralCommands::stop(ServerCommands& sc, const std::vector<std::string>& args)
 {
-    std::cout << cs.isServer() << std::endl;
-    gs.stop();
+    //std::cout << cs.isServer() << std::endl;
+    //gs.stop();
 }

+ 3 - 4
server/commands/GeneralCommands.h

@@ -3,13 +3,12 @@
 
 #include <vector>
 #include <string>
-
-#include "server/commands/ICommandSource.h"
+#include "server/commands/ServerCommands.h"
 
 namespace GeneralCommands
 {
-    void test(IGameServer& gs, ICommandSource& cs, const std::vector<std::string>& args);
-    void stop(IGameServer& gs, ICommandSource& cs, const std::vector<std::string>& args);
+    void test(ServerCommands& sc, const std::vector<std::string>& args);
+    void stop(ServerCommands& sc, const std::vector<std::string>& args);
 }
 
 #endif

+ 0 - 18
server/commands/ICommandSource.h

@@ -1,18 +0,0 @@
-#ifndef ICOMMANDSOURCE_H
-#define ICOMMANDSOURCE_H
-
-#include "server/IGameServer.h"
-
-class ICommandSource
-{
-public:
-    virtual ~ICommandSource() = default;
-    
-    virtual bool isServer() const
-    {
-        return false;
-    }
-};
-
-#endif
-

+ 15 - 0
server/commands/ServerCommands.cpp

@@ -0,0 +1,15 @@
+#include "ServerCommands.h"
+
+ServerCommands::ServerCommands() : running(true)
+{
+}
+
+bool ServerCommands::isRunning() const
+{
+    return running;
+}
+
+void ServerCommands::stop()
+{
+    running = false;
+}

+ 17 - 0
server/commands/ServerCommands.h

@@ -0,0 +1,17 @@
+#ifndef SERVERCOMMANDS_H
+#define SERVERCOMMANDS_H
+
+class ServerCommands
+{
+public:
+    ServerCommands();
+    
+    bool isRunning() const;
+    void stop();
+    
+private:
+    bool running;
+};
+
+#endif
+

+ 2 - 1
server/network/Server.h

@@ -3,6 +3,7 @@
 
 #include <thread>
 #include <mutex>
+#include <atomic>
 
 #include "server/network/IServerListener.h"
 
@@ -28,7 +29,7 @@ private:
     bool addClient(int clientSocket);
     void listenOnClient(ConnectedClient& cc);
     
-    volatile bool shouldRun;
+    std::atomic_bool shouldRun;
     
     uint16_t port;
     uint16_t maxClients;