Browse Source

networking tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
00454cb77b

+ 34 - 2
client/Game.cpp

@@ -1,16 +1,28 @@
 #include "client/Game.h"
+#include "common/network/Packets.h"
 #include "gaming-core/utils/Utils.h"
 
 Game::Game(const Controller& controller, const Clock& fps, const Clock& tps,
-           RenderSettings& settings, const Size& size)
+           RenderSettings& settings, const Size& size, Client& client)
     : controller(controller), fps(fps), tps(tps), renderSettings(settings),
-      size(size), world(blockRegistry), worldRenderer(world) {
+      size(size), client(client), world(blockRegistry), worldRenderer(world),
+      connected(false) {
     pos = Vector3(16.0f, 30.0f, -10.0f);
     rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation;
     rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation;
 }
 
 void Game::tick() {
+    if(!connected && controller.down.wasReleased()) {
+        if(client.connect("127.0.0.1", 11196, 3000)) {
+            std::cout << client.getError() << '\n';
+        } else {
+            std::cout << "connected\n";
+        }
+    } else {
+        client.consumeEvents(*this);
+    }
+
     lastRotation = rotation;
     lastPos = pos;
 
@@ -72,4 +84,24 @@ void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) {
 
 bool Game::isRunning() const {
     return true;
+}
+
+void Game::onConnect() {
+}
+
+void Game::onDisconnect() {
+}
+
+void Game::onPacket(InPacket& in) {
+    uint16 id;
+    if(in.readU16(id)) {
+        return;
+    }
+    switch(id) {
+        case ServerPacket::CHAT:
+            StringBuffer<256> s;
+            in.readString(s);
+            std::cout << s << '\n';
+            break;
+    }
 }

+ 14 - 5
client/Game.h

@@ -2,18 +2,20 @@
 #define GAME_H
 
 #include "client/input/Controller.h"
-#include "gaming-core/utils/Clock.h"
+#include "client/rendering/FontRenderer.h"
 #include "client/rendering/RenderSettings.h"
 #include "client/rendering/Renderer.h"
-#include "client/rendering/FontRenderer.h"
-#include "common/world/World.h"
 #include "common/block/BlockRegistry.h"
-#include "rendering/renderer/WorldRenderer.h"
+#include "common/world/World.h"
+#include "gaming-core/network/Client.h"
+#include "gaming-core/utils/Clock.h"
 #include "gaming-core/utils/Size.h"
+#include "rendering/renderer/WorldRenderer.h"
 
 class Game final {
 public:
-    Game(const Controller& controller, const Clock& fps, const Clock& tps, RenderSettings& renderSettings, const Size& size);
+    Game(const Controller& controller, const Clock& fps, const Clock& tps,
+         RenderSettings& renderSettings, const Size& size, Client& client);
 
     void tick();
     void renderWorld(float lag, Renderer& renderer);
@@ -22,12 +24,17 @@ public:
 
     bool isRunning() const;
 
+    void onConnect();
+    void onDisconnect();
+    void onPacket(InPacket& in);
+
 private:
     const Controller& controller;
     const Clock& fps;
     const Clock& tps;
     RenderSettings& renderSettings;
     const Size& size;
+    Client& client;
 
     Vector3 lastPos;
     Vector3 pos;
@@ -38,6 +45,8 @@ private:
     World world;
 
     WorldRenderer worldRenderer;
+
+    bool connected;
 };
 
 #endif

+ 13 - 1
client/Main.cpp

@@ -3,6 +3,8 @@
 #include "client/rendering/Framebuffers.h"
 #include "client/rendering/RenderSettings.h"
 #include "client/rendering/Shaders.h"
+#include "gaming-core/network/Client.h"
+#include "gaming-core/network/ENet.h"
 #include "gaming-core/rendering/Window.h"
 #include "gaming-core/rendering/WindowOptions.h"
 #include "gaming-core/utils/Clock.h"
@@ -20,6 +22,16 @@ void updateSize(const Window& window, Size& size, Framebuffers& framebuffers) {
 }
 
 int main() {
+    ENet enet;
+    if(enet.init()) {
+        std::cout << "cannot initialize enet\n";
+        return 0;
+    }
+    Client client;
+    if(client.hasError()) {
+        std::cout << client.getError() << '\n';
+        return 0;
+    }
     if(GLFW::init()) {
         return 0;
     }
@@ -48,7 +60,7 @@ int main() {
     Controller controller(buttons);
     Clock fps;
     Clock tps;
-    Game game(controller, fps, tps, renderSettings, size);
+    Game game(controller, fps, tps, renderSettings, size, client);
 
     window.show();
 

+ 6 - 0
common/network/Packets.h

@@ -0,0 +1,6 @@
+#ifndef PACKETS_H
+#define PACKETS_H
+
+enum ServerPacket { CHAT };
+
+#endif

+ 0 - 10
common/world/World.cpp

@@ -4,16 +4,6 @@
 
 World::World(const BlockRegistry& blockRegistry)
     : blockRegistry(blockRegistry), blocks(7, 7) {
-    const Block& stone = blockRegistry.getBlock("stone");
-    HighMap map(blocks.getSize(), blocks.getHeight());
-    for(int x = 0; x < blocks.getSize(); x++) {
-        for(int z = 0; z < blocks.getSize(); z++) {
-            int height = map.getHeight(x, z);
-            for(int y = 0; y < height; y++) {
-                setBlock(x, y, z, stone);
-            }
-        }
-    }
 }
 
 void World::setBlock(int x, int y, int z, const Block& block) {

+ 3 - 1
meson.build

@@ -18,7 +18,8 @@ sourcesServer = ['server/Main.cpp',
     'server/GameServer.cpp',
     'server/commands/ServerState.cpp',
     'server/commands/CommandManager.cpp',
-    'gaming-core/network/Server.cpp']
+    'server/world/WorldGenerator.cpp',
+    'gaming-core/network/Server.cpp',]
 
 sourcesClient = ['client/Main.cpp',
     'gaming-core/utils/Size.cpp',
@@ -41,6 +42,7 @@ sourcesClient = ['client/Main.cpp',
     'gaming-core/utils/Buffer.cpp',
     'gaming-core/input/Button.cpp',
     'gaming-core/input/Buttons.cpp',
+    'gaming-core/network/Client.cpp',
     'client/rendering/Framebuffers.cpp',
     'client/rendering/Engine.cpp',
     'client/rendering/Shaders.cpp',

+ 4 - 1
server/GameServer.cpp

@@ -3,8 +3,11 @@
 #include "server/GameServer.h"
 #include "server/commands/CommandManager.h"
 #include "server/commands/CommandTypes.h"
+#include "server/world/WorldGenerator.h"
 
-GameServer::GameServer() : reader(0, "> ") {
+GameServer::GameServer(Server& server) : state(server), reader(0, "> ") {
+    worlds.add(new World(blocks));
+    WorldGenerator wg(blocks, *(worlds[0]));
 }
 
 void GameServer::tick() {

+ 6 - 2
server/GameServer.h

@@ -2,9 +2,11 @@
 #define GAMESERVER_H
 
 #include "commands/CommandManager.h"
+#include "common/world/World.h"
+#include "gaming-core/memory/UniquePointer.h"
 #include "gaming-core/network/Packet.h"
-#include "gaming-core/network/Server.h"
 #include "gaming-core/utils/Clock.h"
+#include "gaming-core/utils/List.h"
 #include "raw-terminal/RawReader.h"
 #include "server/commands/ServerState.h"
 
@@ -13,9 +15,11 @@ class GameServer final {
     Clock tps;
     RawReader<256, 10> reader;
     CommandManager commandManager;
+    BlockRegistry blocks;
+    List<UniquePointer<World>> worlds;
 
 public:
-    GameServer();
+    GameServer(Server& server);
 
     void tick();
 

+ 1 - 1
server/Main.cpp

@@ -16,7 +16,7 @@ int main() {
         return 0;
     }
 
-    GameServer gameServer;
+    GameServer gameServer(server);
 
     Clock clock;
     constexpr Clock::Nanos NANOS_PER_TICK = 50000000;

+ 15 - 0
server/commands/CommandManager.cpp

@@ -1,5 +1,6 @@
 #include <iostream>
 
+#include "common/network/Packets.h"
 #include "server/commands/CommandManager.h"
 
 static void commandTest(ServerState&, const CommandArguments& args) {
@@ -13,9 +14,23 @@ 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';
+}
+
 CommandManager::CommandManager() {
     commands.add("test", commandTest);
     commands.add("stop", commandStop);
+    commands.add("say", commandSay);
 }
 
 void CommandManager::execute(ServerState& sc, const RawCommand& rawCommand) {

+ 1 - 1
server/commands/ServerState.cpp

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

+ 5 - 2
server/commands/ServerState.h

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

+ 19 - 0
server/world/WorldGenerator.cpp

@@ -0,0 +1,19 @@
+#include "server/world/WorldGenerator.h"
+#include "common/world/HighMap.h"
+
+WorldGenerator::WorldGenerator(const BlockRegistry& blocks, World& world)
+    : blocks(blocks), world(world) {
+}
+
+void WorldGenerator::generate() {
+    const Block& stone = blocks.getBlock("stone");
+    HighMap map(world.getSize(), world.getHeight());
+    for(int x = 0; x < world.getSize(); x++) {
+        for(int z = 0; z < world.getSize(); z++) {
+            int height = map.getHeight(x, z);
+            for(int y = 0; y < height; y++) {
+                world.setBlock(x, y, z, stone);
+            }
+        }
+    }
+}

+ 15 - 0
server/world/WorldGenerator.h

@@ -0,0 +1,15 @@
+#ifndef WORLD_GENERATOR_H
+#define WORLD_GENERATOR_H
+
+#include "common/world/World.h"
+
+class WorldGenerator final {
+    const BlockRegistry& blocks;
+    World& world;
+
+public:
+    WorldGenerator(const BlockRegistry& blocks, World& world);
+    void generate();
+};
+
+#endif