feat: c++ pragma 방식 => protocol buffer로 직렬화 수정 및 클라이언트 python gui 앱으로 변경

This commit is contained in:
bumpsoo 2026-02-08 14:59:16 +09:00
parent a88b22b177
commit cd192d4ec4
14 changed files with 305 additions and 371 deletions

View file

@ -1,11 +1,14 @@
#pragma once
#include "Packet.h"
#include <atomic>
#include <boost/asio.hpp>
#include <string>
#include <thread>
namespace google::protobuf {
class Message;
}
using boost::asio::ip::tcp;
class BaseClient {
@ -15,20 +18,10 @@ public:
virtual ~BaseClient() = default;
void SendPacket(PacketID id, const void *payload = nullptr,
size_t payloadSize = 0);
void SendPacket(PacketID id, const google::protobuf::Message *pkt = nullptr);
bool ReceiveHeader(PacketHeader &header);
template <typename T> bool ReceivePayload(T &payload) {
try {
boost::asio::read(socket_, boost::asio::buffer(&payload, sizeof(T)));
return true;
} catch (...) {
return false;
}
}
bool Login(const std::string &nickname);
// 비동기 수신 시작
@ -48,8 +41,8 @@ protected:
void ReceiveLoop(std::stop_token stopToken);
void HandlePacket(const PacketHeader &header);
void HandleUpgradeResult();
void HandleSellResult();
void HandleUpgradeResult(const PacketHeader &header);
void HandleSellResult(const PacketHeader &header);
};
class InteractiveClient : public BaseClient {

View file

@ -46,45 +46,3 @@ namespace GameConfig {
const uint32_t MAX_SWORD_LEVEL = 20;
const uint64_t INITIAL_GOLD = 10000;
} // namespace GameConfig
// 고정 크기 데이터 전송을 위한 구조체 정의
#pragma pack(push, 1)
struct PKT_CS_Login {
// 유저 닉네임 (최대 32자)
char nickname[32];
};
struct PKT_SC_LoginResult {
// 0: 이미 접속 중, 1: 성공
uint8_t result;
};
struct PKT_SC_UpgradeResult {
// 0: 파괴, 1: 성공, 2: 실패
uint8_t result;
// 현재 강화 레벨
uint32_t currentLevel;
// 현재 보유 골드
uint64_t currentGold;
};
struct PKT_SC_SellResult {
// 판매 후 획득 골드
uint64_t earnedGold;
// 현재 총 골드
uint64_t totalGold;
};
struct RankingEntry {
// 유저 이름 (최대 32자)
char username[32];
// 검 레벨
uint32_t swordLevel;
};
struct PKT_SC_RankingList {
// 랭킹 리스트 개수
uint32_t count;
// 이후 RankingEntry[count] 만큼 데이터가 따라옴
};
#pragma pack(pop)

View file

@ -16,14 +16,15 @@ public:
void Start();
void Send(std::span<const uint8_t> data);
template <typename T> void SendPacket(PacketID id, const T &payload) {
template <typename T> void SendPacket(PacketID id, const T &pkt) {
uint16_t size = static_cast<uint16_t>(pkt.ByteSizeLong());
PacketHeader header;
header.id = static_cast<uint16_t>(id);
header.size = sizeof(T);
header.size = size;
std::vector<uint8_t> buffer(sizeof(PacketHeader) + sizeof(T));
std::vector<uint8_t> buffer(sizeof(PacketHeader) + size);
std::memcpy(buffer.data(), &header, sizeof(PacketHeader));
std::memcpy(buffer.data() + sizeof(PacketHeader), &payload, sizeof(T));
pkt.SerializeToArray(buffer.data() + sizeof(PacketHeader), size);
Send(buffer);
}