tmp
This commit is contained in:
parent
a88b22b177
commit
263bb3e3af
10 changed files with 374 additions and 117 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue