mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(server): add block change packet handling and increase reserved threads
This commit is contained in:
@@ -20,8 +20,8 @@ void NetworkServer::stop() {
|
||||
|
||||
m_io.stop();
|
||||
m_session.clear();
|
||||
if (m_server.joinable()) {
|
||||
m_server.join();
|
||||
if (m_net_thread.joinable()) {
|
||||
m_net_thread.join();
|
||||
}
|
||||
Logger::info("Server Stopped!");
|
||||
}
|
||||
@@ -42,14 +42,19 @@ asio::awaitable<void> NetworkServer::listen() {
|
||||
co_return;
|
||||
}
|
||||
|
||||
void NetworkServer::run() {
|
||||
m_server = std::thread([this]() {
|
||||
void NetworkServer::net_run() {
|
||||
m_net_thread = std::thread([this]() {
|
||||
asio::co_spawn(m_io, listen(), asio::detached);
|
||||
m_io.run();
|
||||
});
|
||||
Logger::info("Server Started!");
|
||||
}
|
||||
|
||||
void NetworkServer::start_server() {
|
||||
m_world.init_world();
|
||||
net_run();
|
||||
}
|
||||
|
||||
int NetworkServer::port() const { return m_port; }
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -47,17 +47,9 @@ void ServerWorld::init_world() {
|
||||
Logger::info("Chunk Block Init Finish, Time Consuming: {}", d);
|
||||
|
||||
start_server_thread();
|
||||
|
||||
Logger::info("TestPlayer Create Finish");
|
||||
}
|
||||
|
||||
void ServerWorld::init_chunks() {
|
||||
hot_reload();
|
||||
while (!m_chunk_gen_finished) {
|
||||
// Logger::info("World Spawn: {:.2f}%", m_chunk_gen_fraction.load());
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(200));
|
||||
}
|
||||
}
|
||||
void ServerWorld::init_chunks() { hot_reload(); }
|
||||
|
||||
void ServerWorld::gen_chunks_internal(std::optional<std::string> uuid) {
|
||||
// Logger::info("gen_chunks_internal");
|
||||
@@ -304,7 +296,7 @@ void ServerWorld::need_gen(std::optional<std::string> uuid) {
|
||||
m_gen_cv.notify_one();
|
||||
}
|
||||
|
||||
void ServerWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||
bool ServerWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||
|
||||
int world_x, world_y, world_z;
|
||||
world_x = block_pos.x;
|
||||
@@ -316,17 +308,18 @@ void ServerWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||
|
||||
if (it == m_chunks.end()) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto [x, y, z] = ServerChunk::world_to_block(world_x, world_y, world_z,
|
||||
chunk_x, chunk_z);
|
||||
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||
z >= CHUNK_SIZE) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
it->second.set_chunk_block(ServerChunk::index(x, y, z), id);
|
||||
return true;
|
||||
}
|
||||
|
||||
void ServerWorld::hot_reload() {
|
||||
@@ -456,6 +449,27 @@ void ServerWorld::handle_chunk_req(const std::string& uuid, ChunkPos pos) {
|
||||
s->send(make_packet(rsq));
|
||||
}
|
||||
|
||||
void ServerWorld::handle_block_change(const BlockChangeReq& req) {
|
||||
float x = req.pos().x();
|
||||
float y = req.pos().y();
|
||||
float z = req.pos().z();
|
||||
if (!set_block(glm::ivec3(x, y, z), req.block())) {
|
||||
return;
|
||||
}
|
||||
BlockChangeRsp rsp;
|
||||
auto* pos = rsp.mutable_pos();
|
||||
pos->set_x(x);
|
||||
pos->set_y(y);
|
||||
pos->set_z(z);
|
||||
rsp.set_block(req.block());
|
||||
|
||||
for (auto& [uuid, session] : m_player_session) {
|
||||
if (session) {
|
||||
session->send(make_packet(rsp));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ServerWorld::rendering_distance() const {
|
||||
return m_rendering_distance.load();
|
||||
}
|
||||
|
||||
@@ -80,6 +80,12 @@ asio::awaitable<void> Session::read_loop() {
|
||||
req.uuid(), ChunkPos(req.pos().x(), req.pos().z()));
|
||||
}
|
||||
}
|
||||
if (cmd_id == to_num(PacketEnum::BLOCK_CHANGE_REQ)) {
|
||||
BlockChangeReq req;
|
||||
if (req.ParseFromArray(body_data.data(), body_data.size())) {
|
||||
m_server_world.handle_block_change(req);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const asio::system_error& e) {
|
||||
Logger::warn("Catch Asio Error {}", e.what());
|
||||
|
||||
@@ -8,4 +8,5 @@ import "player/player.proto";
|
||||
import "auth/auth.proto";
|
||||
import "system/ping.proto";
|
||||
import "system/pong.proto";
|
||||
import "world/chunk_data.proto";
|
||||
import "world/chunk_data.proto";
|
||||
import "world/block_change.proto";
|
||||
14
src/proto/world/block_change.proto
Normal file
14
src/proto/world/block_change.proto
Normal file
@@ -0,0 +1,14 @@
|
||||
syntax = "proto3";
|
||||
|
||||
import "common/vector3.proto";
|
||||
|
||||
message BlockChangeReq {
|
||||
string uuid = 1;
|
||||
Vec3 pos = 2;
|
||||
uint32 block = 3;
|
||||
}
|
||||
|
||||
message BlockChangeRsp {
|
||||
Vec3 pos = 1;
|
||||
uint32 block = 2;
|
||||
}
|
||||
Reference in New Issue
Block a user