mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
refactor(server_world): use thread pool for chunk request handling
This commit is contained in:
@@ -516,57 +516,61 @@ glm::vec3 ServerWorld::get_player_pos(const std::string& uuid) const {
|
|||||||
|
|
||||||
void ServerWorld::handle_chunk_req(const std::string& uuid, ChunkPos pos) {
|
void ServerWorld::handle_chunk_req(const std::string& uuid, ChunkPos pos) {
|
||||||
|
|
||||||
Arena arean;
|
auto pool = m_gen_thread_pool.load();
|
||||||
ChunkDataRsp* rsp = Arena::Create<ChunkDataRsp>(&arean);
|
pool->enqueue([uuid, pos, this]() {
|
||||||
auto* rsq_pos = rsp->mutable_pos();
|
Arena arean;
|
||||||
rsq_pos->set_x(pos.x);
|
ChunkDataRsp* rsp = Arena::Create<ChunkDataRsp>(&arean);
|
||||||
rsq_pos->set_z(pos.z);
|
auto* rsq_pos = rsp->mutable_pos();
|
||||||
{
|
rsq_pos->set_x(pos.x);
|
||||||
std::shared_lock lock(m_chunks_mutex);
|
rsq_pos->set_z(pos.z);
|
||||||
auto it = m_chunks.find(pos);
|
{
|
||||||
if (it == m_chunks.end()) {
|
std::shared_lock lock(m_chunks_mutex);
|
||||||
|
auto it = m_chunks.find(pos);
|
||||||
|
if (it == m_chunks.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
rsp->set_chunk_seed(it->second.seed());
|
||||||
|
rsp->set_biome_type(std::to_underlying(it->second.biome()));
|
||||||
|
auto* blocks = rsp->mutable_chunk_blocks();
|
||||||
|
auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||||
|
blocks->Assign(chunk_blocks.begin(), chunk_blocks.end());
|
||||||
|
auto& neighbor_blocks = it->second.get_neightbor_blocks();
|
||||||
|
auto assign =
|
||||||
|
[](auto* nb,
|
||||||
|
const std::optional<std::vector<BlockType>>& blocks) {
|
||||||
|
if (!blocks) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!nb) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
nb->Assign(blocks->begin(), blocks->end());
|
||||||
|
};
|
||||||
|
auto* nb1 = rsp->mutable_neighbor_blocks_1();
|
||||||
|
auto* nb2 = rsp->mutable_neighbor_blocks_2();
|
||||||
|
auto* nb3 = rsp->mutable_neighbor_blocks_3();
|
||||||
|
auto* nb4 = rsp->mutable_neighbor_blocks_4();
|
||||||
|
assign(nb1, neighbor_blocks[0]);
|
||||||
|
assign(nb2, neighbor_blocks[1]);
|
||||||
|
assign(nb3, neighbor_blocks[2]);
|
||||||
|
assign(nb4, neighbor_blocks[3]);
|
||||||
|
}
|
||||||
|
std::shared_ptr<Session> s;
|
||||||
|
{
|
||||||
|
std::shared_lock lock(m_player_mutex);
|
||||||
|
auto it = m_players.find(uuid);
|
||||||
|
if (it != m_players.end()) {
|
||||||
|
s = it->second.get_session();
|
||||||
|
it->second.update_sync_gametick(m_game_ticks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!s) {
|
||||||
|
Logger::error("Player {} session not exist", uuid);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rsp->set_chunk_seed(it->second.seed());
|
|
||||||
rsp->set_biome_type(std::to_underlying(it->second.biome()));
|
|
||||||
auto* blocks = rsp->mutable_chunk_blocks();
|
|
||||||
auto& chunk_blocks = it->second.get_chunk_blocks();
|
|
||||||
blocks->Assign(chunk_blocks.begin(), chunk_blocks.end());
|
|
||||||
auto& neighbor_blocks = it->second.get_neightbor_blocks();
|
|
||||||
auto assign = [](auto* nb,
|
|
||||||
const std::optional<std::vector<BlockType>>& blocks) {
|
|
||||||
if (!blocks) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!nb) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
nb->Assign(blocks->begin(), blocks->end());
|
|
||||||
};
|
|
||||||
auto* nb1 = rsp->mutable_neighbor_blocks_1();
|
|
||||||
auto* nb2 = rsp->mutable_neighbor_blocks_2();
|
|
||||||
auto* nb3 = rsp->mutable_neighbor_blocks_3();
|
|
||||||
auto* nb4 = rsp->mutable_neighbor_blocks_4();
|
|
||||||
assign(nb1, neighbor_blocks[0]);
|
|
||||||
assign(nb2, neighbor_blocks[1]);
|
|
||||||
assign(nb3, neighbor_blocks[2]);
|
|
||||||
assign(nb4, neighbor_blocks[3]);
|
|
||||||
}
|
|
||||||
std::shared_ptr<Session> s;
|
|
||||||
{
|
|
||||||
std::shared_lock lock(m_player_mutex);
|
|
||||||
auto it = m_players.find(uuid);
|
|
||||||
if (it != m_players.end()) {
|
|
||||||
s = it->second.get_session();
|
|
||||||
it->second.update_sync_gametick(m_game_ticks);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!s) {
|
|
||||||
Logger::error("Player {} session not exist", uuid);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->send(make_packet(*rsp));
|
s->send(make_packet(*rsp));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerWorld::handle_block_change(const BlockChangeReq& req) {
|
void ServerWorld::handle_block_change(const BlockChangeReq& req) {
|
||||||
|
|||||||
Reference in New Issue
Block a user