feat(server): add block change packet handling and increase reserved threads

This commit is contained in:
2026-06-24 17:02:21 +08:00
parent 9d33b240c6
commit 85341651b3
9 changed files with 74 additions and 21 deletions

View File

@@ -25,7 +25,7 @@ constexpr float DEFAULT_G = 22.5f;
constexpr int SIZE_X = CHUNK_SIZE;
constexpr int SIZE_Y = WORLD_SIZE_Y;
constexpr int SIZE_Z = CHUNK_SIZE;
constexpr int RESERVED_THREADS = 3;
constexpr int RESERVED_THREADS = 5;
constexpr float DEFAULT_CAVE_PROBABILITY = 0.035f;

View File

@@ -11,16 +11,21 @@ public:
NetworkServer(int port = 25530);
~NetworkServer();
void stop();
void run();
// Run in another thread after initialization is complete
void start_server();
int port() const;
std::unordered_map<std::string, std::shared_ptr<Session>> m_session;
private:
asio::io_context m_io;
std::thread m_server;
std::thread m_net_thread;
int m_port = 25530;
std::atomic<bool> m_stopped{false};
ServerWorld m_world;
asio::awaitable<void> listen();
void net_run();
};
} // namespace Cubed

View File

@@ -15,6 +15,8 @@ enum class PacketEnum {
PLAYER_POS = 2002,
CHUNK_DATA_REQ = 3001,
CHUNK_DATA_RSP = 3002,
BLOCK_CHANGE_REQ = 3003,
BLOCK_CHANGE_RSP = 3004,
PING = 9001,
PONG = 9002
@@ -39,6 +41,10 @@ template <typename T> constexpr uint16_t get_packet_id() {
return to_num(CHUNK_DATA_REQ);
} else if constexpr (is_same_v<U, ChunkDataRsp>) {
return to_num(CHUNK_DATA_RSP);
} else if constexpr (is_same_v<U, BlockChangeReq>) {
return to_num(BLOCK_CHANGE_REQ);
} else if constexpr (is_same_v<U, BlockChangeRsp>) {
return to_num(BLOCK_CHANGE_RSP);
} else if constexpr (is_same_v<U, Ping>) {
return to_num(PING);
} else if (is_same_v<U, Pong>) {

View File

@@ -7,6 +7,7 @@
#include "Cubed/gameplay/server_chunk.hpp"
#include "Cubed/gameplay/server_player.hpp"
#include "Cubed/tools/thread_pool.hpp"
#include "world/block_change.pb.h"
#include <future>
#include <shared_mutex>
@@ -63,7 +64,7 @@ public:
int chunk_load_style() const;
void set_chunk_load_style(int id);
void set_block(const glm::ivec3& block_pos, unsigned id);
bool set_block(const glm::ivec3& block_pos, unsigned id);
void sync_player_pos(const std::string& uuid, float x, float y, float z);
void handle_player_login(const std::string& player_name,
@@ -71,6 +72,7 @@ public:
glm::vec3 get_player_pos(const std::string& uuid) const;
void handle_chunk_req(const std::string& uuid, ChunkPos pos);
void handle_block_change(const BlockChangeReq& req);
private:
enum class ChunkLoadStyle { RANDOM, CENTER };