init
This commit is contained in:
commit
b887f15662
21 changed files with 1038 additions and 0 deletions
96
tests/Client.cpp
Normal file
96
tests/Client.cpp
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
#include "Packet.h"
|
||||
#include <boost/asio.hpp>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
void SendPacket(tcp::socket &socket, PacketID id, const void *payload = nullptr,
|
||||
size_t payloadSize = 0) {
|
||||
PacketHeader header;
|
||||
header.id = static_cast<uint16_t>(id);
|
||||
header.size = static_cast<uint16_t>(payloadSize);
|
||||
|
||||
std::vector<uint8_t> buffer(sizeof(PacketHeader) + payloadSize);
|
||||
std::memcpy(buffer.data(), &header, sizeof(PacketHeader));
|
||||
if (payload && payloadSize > 0) {
|
||||
std::memcpy(buffer.data() + sizeof(PacketHeader), payload, payloadSize);
|
||||
}
|
||||
|
||||
boost::asio::write(socket, boost::asio::buffer(buffer));
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cout << "사용법: " << argv[0] << " <닉네임>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string nickname = argv[1];
|
||||
|
||||
try {
|
||||
boost::asio::io_context io_context;
|
||||
tcp::socket socket(io_context);
|
||||
tcp::resolver resolver(io_context);
|
||||
boost::asio::connect(socket, resolver.resolve("127.0.0.1", "30000"));
|
||||
|
||||
std::cout << "서버에 연결되었습니다! (닉네임: " << nickname << ")"
|
||||
<< std::endl;
|
||||
|
||||
// 1. 로그인 패킷 전송
|
||||
PKT_CS_Login loginPkt;
|
||||
std::memset(loginPkt.nickname, 0, sizeof(loginPkt.nickname));
|
||||
std::strncpy(loginPkt.nickname, nickname.c_str(),
|
||||
sizeof(loginPkt.nickname) - 1);
|
||||
SendPacket(socket, PacketID::Login, &loginPkt, sizeof(loginPkt));
|
||||
std::cout << "로그인 요청 보냄" << std::endl;
|
||||
|
||||
// 2. 강화 테스트 (5번 시도)
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
SendPacket(socket, PacketID::CS_UpgradeSword);
|
||||
std::cout << i + 1 << "번째 강화 시도..." << std::endl;
|
||||
|
||||
// 결과 수신
|
||||
PacketHeader resHeader;
|
||||
boost::asio::read(socket,
|
||||
boost::asio::buffer(&resHeader, sizeof(resHeader)));
|
||||
|
||||
if (resHeader.id == static_cast<uint16_t>(PacketID::SC_UpgradeResult)) {
|
||||
PKT_SC_UpgradeResult res;
|
||||
boost::asio::read(socket, boost::asio::buffer(&res, sizeof(res)));
|
||||
|
||||
std::string resultStr =
|
||||
(res.result == 1) ? "성공" : (res.result == 0 ? "파괴!!!" : "실패");
|
||||
std::cout << ">> 강화 결과: " << resultStr
|
||||
<< " (현재레벨: " << res.currentLevel
|
||||
<< ", 남은골드: " << res.currentGold << ")" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 판매 테스트
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
SendPacket(socket, PacketID::CS_SellSword);
|
||||
std::cout << "검 판매 시도..." << std::endl;
|
||||
|
||||
// 결과 수신
|
||||
PacketHeader sellResHeader;
|
||||
boost::asio::read(
|
||||
socket, boost::asio::buffer(&sellResHeader, sizeof(sellResHeader)));
|
||||
if (sellResHeader.id == static_cast<uint16_t>(PacketID::SC_SellResult)) {
|
||||
PKT_SC_SellResult res;
|
||||
boost::asio::read(socket, boost::asio::buffer(&res, sizeof(res)));
|
||||
std::cout << ">> 판매 결과: " << res.earnedGold
|
||||
<< " 골드 획득! (총 골드: " << res.totalGold << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
|
||||
} catch (std::exception &e) {
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue