mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(protocol): add packet serialization and login handling
Introduce packet header and ID mapping for protobuf messages. Refactor session and server_world to use new packet wrapper. Fix missing semicolons in proto files.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "Cubed/gameplay/server_world.hpp"
|
||||
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/gameplay/packet.hpp"
|
||||
#include "Cubed/gameplay/session.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
@@ -357,17 +359,14 @@ void ServerWorld::sync_player_pos(const std::string& name, float x, float y,
|
||||
it->second.update_pos(x, y, z);
|
||||
}
|
||||
|
||||
void ServerWorld::handle_player_request(const PlayerRequest& request) {
|
||||
if (request.join()) {
|
||||
player_join(request.name());
|
||||
}
|
||||
if (request.exit()) {
|
||||
player_exit(request.name());
|
||||
}
|
||||
if (request.chunk_send()) {
|
||||
m_request_gen_name = request.name();
|
||||
need_gen();
|
||||
}
|
||||
void ServerWorld::handle_player_login(const std::string& name,
|
||||
std::shared_ptr<Session> session) {
|
||||
player_join(name);
|
||||
m_player_session.emplace(name, session);
|
||||
LoginRsp rsp;
|
||||
rsp.set_success(true);
|
||||
rsp.set_uuid(name);
|
||||
session->send(make_packet(name));
|
||||
}
|
||||
|
||||
void ServerWorld::player_join(const std::string& name) {
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
#include "Cubed/gameplay/server_world.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
#include "Cubed/tools/uuid.hpp"
|
||||
#include "proto/player_pos.pb.h"
|
||||
#include "proto/player_request.pb.h"
|
||||
using asio::ip::tcp;
|
||||
|
||||
namespace Cubed {
|
||||
@@ -61,20 +59,8 @@ asio::awaitable<void> Session::read_loop() {
|
||||
}
|
||||
|
||||
if (cmd_id == 1001) {
|
||||
PlayerRequest player_request;
|
||||
if (player_request.ParseFromArray(body_data.data(),
|
||||
body_data.size())) {
|
||||
}
|
||||
}
|
||||
if (cmd_id == 1002) {
|
||||
PlayerPos player_pos;
|
||||
if (player_pos.ParseFromArray(body_data.data(),
|
||||
body_data.size())) {
|
||||
m_server_world.sync_player_pos(
|
||||
player_pos.name(), player_pos.x(), player_pos.y(),
|
||||
player_pos.z());
|
||||
} else {
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const asio::system_error& e) {
|
||||
|
||||
11
src/proto/packet.proto
Normal file
11
src/proto/packet.proto
Normal file
@@ -0,0 +1,11 @@
|
||||
syntax = "proto3";
|
||||
|
||||
import "common/chunk_pos.proto";
|
||||
import "common/error.proto";
|
||||
import "common/player_info.proto";
|
||||
import "common/vector3.proto";
|
||||
import "player/player.proto";
|
||||
import "auth/auth.proto";
|
||||
import "system/ping.proto";
|
||||
import "system/pong.proto";
|
||||
import "world/chunk_data.proto";
|
||||
@@ -1,6 +1,6 @@
|
||||
syntax = "proto3";
|
||||
|
||||
import "common/vector3.proto"
|
||||
import "common/vector3.proto";
|
||||
|
||||
message PlayerPos {
|
||||
string uuid = 1;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
syntax = "proto3";
|
||||
|
||||
import "common/chunk_pos.proto"
|
||||
import "common/chunk_pos.proto";
|
||||
|
||||
message ChunkData {
|
||||
ChunkPos pos = 1;
|
||||
|
||||
Reference in New Issue
Block a user