Browse Source

most services on server are now in global namespaces

Kajetan Johannes Hammerle 3 years ago
parent
commit
31df948133

+ 5 - 4
meson.build

@@ -10,12 +10,13 @@ src_common = [
 ]
 
 src_server = ['server/Main.cpp',
-    'server/GameServer.cpp',
-    'server/commands/ServerState.cpp',
-    'server/commands/CommandManager.cpp',
+    'server/Game.cpp',
+    'server/commands/Commands.cpp',
+    'server/commands/DefaultCommands.cpp',
+    'server/commands/SnuviCommands.cpp',
     'server/world/WorldGenerator.cpp',
     'server/snuviscript/Snuvi.cpp',
-    'server/PackageHandler.cpp',
+    'server/GameServer.cpp',
 ]
 
 src_client = ['client/Main.cpp',

+ 48 - 0
server/Game.cpp

@@ -0,0 +1,48 @@
+#include "server/Game.h"
+#include "commands/Commands.h"
+#include "memory/UniquePointer.h"
+#include "raw-terminal/RawReader.h"
+#include "server/world/WorldGenerator.h"
+#include "utils/List.h"
+
+static bool running = true;
+static RawReader<256, 10> reader{0, "> "};
+static List<UniquePointer<World>> worlds;
+
+Clock Game::ticksPerSecond;
+BlockRegistry Game::blocks;
+
+void Game::stop() {
+    running = false;
+}
+
+bool Game::isRunning() {
+    return running;
+}
+
+void Game::testWorld() {
+    worlds.add(new World(blocks));
+    WorldGenerator wg(blocks, *(worlds[0]));
+    wg.generate();
+}
+
+World& Game::getTestWorld() {
+    return *(worlds[0]);
+}
+
+static void handleCommands() {
+    if(!reader.canRead()) {
+        return;
+    }
+    const char* s = reader.readLine();
+    if(s == nullptr) {
+        return;
+    }
+    Commands::Raw raw(s);
+    Commands::execute(raw);
+}
+
+void Game::tick() {
+    ticksPerSecond.update();
+    handleCommands();
+}

+ 19 - 0
server/Game.h

@@ -0,0 +1,19 @@
+#ifndef GAMESERVER_H
+#define GAMESERVER_H
+
+#include "common/world/World.h"
+#include "utils/Clock.h"
+
+namespace Game {
+    extern Clock ticksPerSecond;
+    extern BlockRegistry blocks;
+
+    void stop();
+    bool isRunning();
+
+    void testWorld();
+    World& getTestWorld();
+    void tick();
+}
+
+#endif

+ 40 - 54
server/GameServer.cpp

@@ -1,67 +1,53 @@
-#include <iostream>
-
-#include "common/network/Packets.h"
 #include "server/GameServer.h"
-#include "server/commands/CommandManager.h"
-#include "server/commands/CommandTypes.h"
-#include "server/world/WorldGenerator.h"
-
-GameServer::GameServer(Server& server) : state(server), reader(0, "> ") {
-    worlds.add(new World(blocks));
-    WorldGenerator wg(blocks, *(worlds[0]));
-    wg.generate();
-}
-
-void GameServer::tick() {
-    tps.update();
-    handleCommands();
-}
+#include "common/network/Packets.h"
+#include "server/Game.h"
+
+static Server* server = nullptr;
+
+struct Receiver {
+    void onConnect(Server::Client& client) {
+        std::cout << "connected\n";
+
+        World& w = Game::getTestWorld();
+        OutPacket out = OutPacket::reliable(
+            w.getHeight() * w.getSize() * w.getSize() * sizeof(BlockId) + 2);
+        out.writeU16(ServerPacket::WORLD);
+        for(int x = 0; x < w.getSize(); x++) {
+            for(int y = 0; y < w.getHeight(); y++) {
+                for(int z = 0; z < w.getSize(); z++) {
+                    out.writeU16(w.getBlock(x, y, z).getId());
+                }
+            }
+        }
 
-void GameServer::handleCommands() {
-    if(!reader.canRead()) {
-        return;
-    }
-    const char* s = reader.readLine();
-    if(s == nullptr) {
-        return;
+        client.send(out);
     }
-    RawCommand rawCommand(s);
-    commandManager.execute(state, rawCommand);
-}
 
-void GameServer::onConnect(Server::Client& client) {
-    std::cout << "connected\n";
+    void onDisconnect(Server::Client& client) {
+        (void)client;
+        std::cout << "disconnected\n";
+    }
 
-    World& w = *(worlds[0]);
-    OutPacket out = OutPacket::reliable(
-        w.getHeight() * w.getSize() * w.getSize() * sizeof(BlockId) + 2);
-    out.writeU16(ServerPacket::WORLD);
-    for(int x = 0; x < w.getSize(); x++) {
-        for(int y = 0; y < w.getHeight(); y++) {
-            for(int z = 0; z < w.getSize(); z++) {
-                out.writeU16(w.getBlock(x, y, z).getId());
-            }
+    void onPacket(Server::Client& client, InPacket& in) {
+        (void)client;
+        StringBuffer<256> s;
+        int8 c;
+        while(in.readS8(c)) {
+            s.append(c);
         }
+        std::cout << "Packet: " << s << "\n";
     }
+};
 
-    client.send(out);
+void GameServer::init(Server* s) {
+    server = s;
 }
 
-void GameServer::onDisconnect(Server::Client& client) {
-    (void)client;
-    std::cout << "disconnected\n";
-}
-
-void GameServer::onPacket(Server::Client& client, InPacket& in) {
-    (void)client;
-    StringBuffer<256> s;
-    int8 c;
-    while(in.readS8(c)) {
-        s.append(c);
-    }
-    std::cout << "Packet: " << s << "\n";
+void GameServer::tick() {
+    Receiver r;
+    server->consumeEvents(r);
 }
 
-bool GameServer::isRunning() const {
-    return state.running;
+void GameServer::sendToAll(OutPacket& out) {
+    server->send(out);
 }

+ 7 - 31
server/GameServer.h

@@ -1,36 +1,12 @@
-#ifndef GAMESERVER_H
-#define GAMESERVER_H
+#ifndef GAME_SERVER_H
+#define GAME_SERVER_H
 
-#include "commands/CommandManager.h"
-#include "common/world/World.h"
-#include "memory/UniquePointer.h"
-#include "network/Packet.h"
-#include "raw-terminal/RawReader.h"
-#include "server/commands/ServerState.h"
-#include "utils/Clock.h"
-#include "utils/List.h"
-
-class GameServer final {
-    ServerState state;
-    Clock tps;
-    RawReader<256, 10> reader;
-    CommandManager commandManager;
-    BlockRegistry blocks;
-    List<UniquePointer<World>> worlds;
-
-public:
-    GameServer(Server& server);
+#include "network/Server.h"
 
+namespace GameServer {
+    void init(Server* s);
     void tick();
-
-    void onConnect(Server::Client& client);
-    void onDisconnect(Server::Client& client);
-    void onPacket(Server::Client& client, InPacket& in);
-
-    bool isRunning() const;
-
-private:
-    void handleCommands();
-};
+    void sendToAll(OutPacket& out);
+}
 
 #endif

+ 28 - 21
server/Main.cpp

@@ -1,39 +1,46 @@
-#include <iostream>
-
 #include "network/ENet.h"
-#include "network/Server.h"
+#include "server/Game.h"
 #include "server/GameServer.h"
-#include "server/PackageHandler.h"
+#include "server/commands/Commands.h"
 #include "server/snuviscript/Snuvi.h"
-#include "utils/Clock.h"
-
-int main() {
-    ENet enet;
-    if(enet.init()) {
-        std::cout << "cannot initialize enet\n";
-        return 0;
-    }
-    Server server(11196, 50);
-    if(server.hasError()) {
-        return 0;
-    }
-
-    GameServer gameServer(server);
 
+void loop() {
     Clock clock;
     constexpr Clock::Nanos NANOS_PER_TICK = 50000000;
     Clock::Nanos lag = 0;
-    while(gameServer.isRunning()) {
+    while(Game::isRunning()) {
         lag += clock.update();
         while(lag >= NANOS_PER_TICK) {
             lag -= NANOS_PER_TICK;
-            server.consumeEvents(gameServer);
-            gameServer.tick();
+            GameServer::tick();
+            Game::tick();
         }
         Clock::Nanos waitNanos = NANOS_PER_TICK - lag;
         if(waitNanos > 300000) {
             clock.wait(waitNanos);
         }
     }
+}
+
+void init(Server* server) {
+    Snuvi::init();
+    Commands::init();
+    GameServer::init(server);
+    Game::testWorld();
+}
+
+int main() {
+    ENet enet;
+    if(enet.init()) {
+        std::cout << "cannot initialize enet\n";
+        return 0;
+    }
+    Server server(11196, 50);
+    if(server.hasError()) {
+        std::cout << server.getError() << "\n";
+        return 0;
+    }
+    init(&server);
+    loop();
     return 0;
 }

+ 0 - 1
server/PackageHandler.cpp

@@ -1 +0,0 @@
-#include "server/PackageHandler.h"

+ 0 - 4
server/PackageHandler.h

@@ -1,4 +0,0 @@
-#ifndef PACKAGE_HANDLER_H
-#define PACKAGE_HANDLER_H
-
-#endif

+ 0 - 102
server/commands/CommandManager.cpp

@@ -1,102 +0,0 @@
-#include <cstring>
-#include <iostream>
-
-#include "common/network/Packets.h"
-#include "server/commands/CommandManager.h"
-#include "server/snuviscript/Snuvi.h"
-
-static void commandTest(ServerState&, const CommandArguments& args) {
-    std::cout << "test command\n";
-    for(int i = 0; i < args.getLength(); i++) {
-        std::cout << " - " << args[i] << '\n';
-    }
-}
-
-static void commandStop(ServerState& sc, const CommandArguments&) {
-    sc.running = false;
-}
-
-static void commandSay(ServerState& sc, const CommandArguments& args) {
-    StringBuffer<256> s;
-    for(int i = 1; i < args.getLength(); i++) {
-        s.append(args[i]);
-        s.append(' ');
-    }
-    OutPacket out = OutPacket::reliable(260);
-    out.writeU16(ServerPacket::CHAT);
-    out.writeString(s);
-    sc.server.send(out);
-    std::cout << s << '\n';
-}
-
-static void helpScript() {
-    std::cout << "/script start <script>\n";
-    std::cout << "/script term\n";
-}
-
-static void commandScript(ServerState&, const CommandArguments& args) {
-    if(args.getLength() < 2) {
-        helpScript();
-    } else if(strcmp(args[1], "start") == 0) {
-        if(args.getLength() < 3) {
-            std::cout << "/script start <script>\n";
-            return;
-        }
-        Snuvi::start(args[2]);
-    } else if(strcmp(args[1], "term") == 0) {
-        Snuvi::termAll();
-        std::cout << "all scripts were terminated\n";
-    } else {
-        helpScript();
-    }
-}
-
-static CommandArguments commandArguments = RawCommand();
-
-static void getArguments(Script* sc) {
-    int length = commandArguments.getLength();
-    Pointer p;
-    p.offset = 0;
-    p.array = asAllocate(&sc->arrays, sizeof(Pointer), length);
-    sPushPointer(sc, &p);
-    SnuviArray* array = asGet(&sc->arrays, p.array);
-    if(array == nullptr) {
-        sError(sc, "cannot allocate string memory");
-        return;
-    }
-    Pointer* texts = static_cast<Pointer*>(array->data);
-    for(int i = 0; i < length; i++) {
-        Pointer text = Snuvi::toString(sc, commandArguments[i]);
-        memcpy(texts + i, &text, sizeof(Pointer));
-    }
-}
-
-CommandManager::CommandManager() {
-    commands.add("test", commandTest);
-    commands.add("stop", commandStop);
-    commands.add("say", commandSay);
-    commands.add("script", commandScript);
-
-    DataType type = dtText();
-    dtDereference(&type);
-    Snuvi::initFunction("getArguments", type, getArguments);
-    Snuvi::addFunction();
-}
-
-void CommandManager::execute(ServerState& sc, const RawCommand& rawCommand) {
-    CommandArguments args(rawCommand);
-    if(args.getLength() == 0) {
-        std::cout << "Invalid command syntax: '" << rawCommand << "'\n";
-        return;
-    }
-
-    CommandName command(args[0]);
-    Command* c = commands.search(command);
-    if(c == nullptr) {
-        commandArguments = args;
-        Snuvi::callEvent(Snuvi::Event::COMMAND);
-        std::cout << "Unknown command: '" << command << "'\n";
-        return;
-    }
-    (*c)(sc, args);
-}

+ 0 - 18
server/commands/CommandManager.h

@@ -1,18 +0,0 @@
-#ifndef COMMANDMANAGER_H
-#define COMMANDMANAGER_H
-
-#include "server/commands/CommandTypes.h"
-#include "server/commands/ServerState.h"
-#include "utils/HashMap.h"
-
-class CommandManager {
-    typedef void (*Command)(ServerState&, const CommandArguments&);
-    HashMap<CommandName, Command> commands;
-
-public:
-    CommandManager();
-
-    void execute(ServerState& sc, const RawCommand& rawCommand);
-};
-
-#endif

+ 0 - 11
server/commands/CommandTypes.h

@@ -1,11 +0,0 @@
-#ifndef COMMANDTYPES_H
-#define COMMANDTYPES_H
-
-#include "utils/SplitString.h"
-#include "utils/StringBuffer.h"
-
-typedef StringBuffer<32> CommandName;
-typedef StringBuffer<256> RawCommand;
-typedef SplitString<256> CommandArguments;
-
-#endif

+ 30 - 0
server/commands/Commands.cpp

@@ -0,0 +1,30 @@
+#include "server/commands/Commands.h"
+#include "server/commands/DefaultCommands.h"
+#include "server/commands/SnuviCommands.h"
+#include "utils/HashMap.h"
+
+static HashMap<Commands::Name, Commands::Command> commands;
+
+void Commands::init() {
+    DefaultCommands::init();
+    SnuviCommands::init();
+}
+
+void Commands::add(const Name& name, Command command) {
+    commands.add(name, command);
+}
+
+void Commands::execute(const Raw& raw) {
+    Arguments args(raw);
+    if(args.getLength() == 0) {
+        std::cout << "Invalid command syntax: '" << raw << "'\n";
+        return;
+    }
+    Name command(args[0]);
+    Command* c = commands.search(command);
+    if(c == nullptr) {
+        SnuviCommands::callEvent(args);
+        return;
+    }
+    (*c)(args);
+}

+ 18 - 0
server/commands/Commands.h

@@ -0,0 +1,18 @@
+#ifndef COMMANDS_H
+#define COMMANDS_H
+
+#include "utils/SplitString.h"
+
+namespace Commands {
+    typedef StringBuffer<32> Name;
+    typedef StringBuffer<256> Raw;
+    typedef SplitString<256> Arguments;
+
+    typedef void (*Command)(const Arguments&);
+
+    void init();
+    void add(const Name& name, Command command);
+    void execute(const Raw& raw);
+}
+
+#endif

+ 27 - 0
server/commands/DefaultCommands.cpp

@@ -0,0 +1,27 @@
+#include "server/commands/DefaultCommands.h"
+#include "common/network/Packets.h"
+#include "server/Game.h"
+#include "server/GameServer.h"
+#include "server/commands/Commands.h"
+
+static void commandStop(const Commands::Arguments&) {
+    Game::stop();
+}
+
+static void commandSay(const Commands::Arguments& args) {
+    StringBuffer<256> s;
+    for(int i = 1; i < args.getLength(); i++) {
+        s.append(args[i]);
+        s.append(' ');
+    }
+    OutPacket out = OutPacket::reliable(260);
+    out.writeU16(ServerPacket::CHAT);
+    out.writeString(s);
+    GameServer::sendToAll(out);
+    std::cout << s << '\n';
+}
+
+void DefaultCommands::init() {
+    Commands::add("stop", commandStop);
+    Commands::add("say", commandSay);
+}

+ 8 - 0
server/commands/DefaultCommands.h

@@ -0,0 +1,8 @@
+#ifndef DEFAULT_COMMANDS_H
+#define DEFAULT_COMMANDS_H
+
+namespace DefaultCommands {
+    void init();
+}
+
+#endif

+ 0 - 4
server/commands/ServerState.cpp

@@ -1,4 +0,0 @@
-#include "server/commands/ServerState.h"
-
-ServerState::ServerState(Server& server) : server(server), running(true) {
-}

+ 0 - 13
server/commands/ServerState.h

@@ -1,13 +0,0 @@
-#ifndef SERVERSTATE_H
-#define SERVERSTATE_H
-
-#include "network/Server.h"
-
-struct ServerState final {
-    Server& server;
-    bool running;
-
-    ServerState(Server& server);
-};
-
-#endif

+ 58 - 0
server/commands/SnuviCommands.cpp

@@ -0,0 +1,58 @@
+#include "server/commands/SnuviCommands.h"
+#include "server/snuviscript/Snuvi.h"
+
+static Commands::Arguments commandArguments = Commands::Raw();
+
+static void helpScript() {
+    std::cout << "/script start <script>\n";
+    std::cout << "/script term\n";
+}
+
+static void commandScript(const Commands::Arguments& args) {
+    if(args.getLength() < 2) {
+        helpScript();
+    } else if(strcmp(args[1], "start") == 0) {
+        if(args.getLength() < 3) {
+            std::cout << "/script start <script>\n";
+            return;
+        }
+        Snuvi::start(args[2]);
+    } else if(strcmp(args[1], "term") == 0) {
+        Snuvi::termAll();
+        std::cout << "all scripts were terminated\n";
+    } else {
+        helpScript();
+    }
+}
+
+static void getArguments(Script* sc) {
+    int length = commandArguments.getLength();
+    Pointer p;
+    p.offset = 0;
+    p.array = asAllocate(&sc->arrays, sizeof(Pointer), length);
+    sPushPointer(sc, &p);
+    SnuviArray* array = asGet(&sc->arrays, p.array);
+    if(array == nullptr) {
+        sError(sc, "cannot allocate string memory");
+        return;
+    }
+    Pointer* texts = static_cast<Pointer*>(array->data);
+    for(int i = 0; i < length; i++) {
+        Pointer text = Snuvi::toString(sc, commandArguments[i]);
+        memcpy(texts + i, &text, sizeof(Pointer));
+    }
+}
+
+void SnuviCommands::init() {
+    Commands::add("script", commandScript);
+
+    DataType type = dtText();
+    dtDereference(&type);
+    Snuvi::initFunction("getArguments", type, getArguments);
+    Snuvi::addFunction();
+}
+
+void SnuviCommands::callEvent(Commands::Arguments& args) {
+    commandArguments = args;
+    Snuvi::callEvent(Snuvi::Event::COMMAND);
+}

+ 11 - 0
server/commands/SnuviCommands.h

@@ -0,0 +1,11 @@
+#ifndef SNUVI_COMMANDS_H
+#define SNUVI_COMMANDS_H
+
+#include "server/commands/Commands.h"
+
+namespace SnuviCommands {
+    void init();
+    void callEvent(Commands::Arguments& args);
+}
+
+#endif

+ 23 - 27
server/snuviscript/Snuvi.cpp

@@ -72,31 +72,27 @@ static void initPrinter() {
     Snuvi::addFunction();
 }
 
-struct Scripts {
-    int scriptId = 0;
-    HashMap<int, Script*> scripts;
-
-    Scripts() {
-        gfsInit();
-        gstsInit();
-        lTimeRegister();
-        lMathRegister();
-        initPrinter();
-        Snuvi::initFunction("wait", dtVoid(), wait);
-        Snuvi::addFunction();
-        Snuvi::initFunction("getEvent", dtInt32(), getEvent);
-        Snuvi::addFunction();
-    }
+static int scriptId = 0;
+static HashMap<int, Script*> scripts;
+static Function function;
+
+void Snuvi::init() {
+    gfsInit();
+    gstsInit();
+    lTimeRegister();
+    lMathRegister();
+    initPrinter();
+    Snuvi::initFunction("wait", dtVoid(), wait);
+    Snuvi::addFunction();
+    Snuvi::initFunction("getEvent", dtInt32(), getEvent);
+    Snuvi::addFunction();
 
-    ~Scripts() {
+    atexit([]() {
         gfsDelete();
         gstsDelete();
         Snuvi::termAll();
-    }
-};
-
-static Scripts scripts;
-static Function function;
+    });
+}
 
 void Snuvi::initFunction(const char* name, DataType returnType,
                          ScriptFunction sf) {
@@ -145,24 +141,24 @@ int Snuvi::start(const char* path) {
         sDelete(script);
         return -1;
     }
-    int id = scripts.scriptId++;
-    scripts.scripts.tryEmplace(id, script);
+    int id = scriptId++;
+    scripts.tryEmplace(id, script);
     return id;
 }
 
 void Snuvi::termAll() {
-    for(auto& entry : scripts.scripts) {
+    for(auto& entry : scripts) {
         sDelete(entry.value);
     }
-    scripts.scripts.clear();
+    scripts.clear();
 }
 
 void Snuvi::callEvent(Event e) {
     event = e;
-    for(auto& entry : scripts.scripts) {
+    for(auto& entry : scripts) {
         if(runScript(entry.value)) {
             sDelete(entry.value);
-            scripts.scripts.remove(entry.getKey());
+            scripts.remove(entry.getKey());
         }
     }
 }

+ 2 - 0
server/snuviscript/Snuvi.h

@@ -7,6 +7,8 @@
 namespace Snuvi {
     enum Event { NONE, COMMAND };
 
+    void init();
+
     void initFunction(const char* name, DataType returnType, ScriptFunction sf);
     void addArgument(DataType type);
     void addFunction();