222 lines
7.6 KiB
C++
222 lines
7.6 KiB
C++
#include "network/network_client.h"
|
|
#include <cstring>
|
|
|
|
namespace metin2::network {
|
|
|
|
NetworkClient::NetworkClient()
|
|
: m_phase_fsm(ConnectionPhase::Disconnected) {
|
|
}
|
|
|
|
void NetworkClient::Reset() {
|
|
m_phase_fsm.ForcePhase(ConnectionPhase::Disconnected);
|
|
m_cipher.SetEnabled(false);
|
|
m_recv_buffer.Clear();
|
|
m_send_buffer.clear();
|
|
}
|
|
|
|
void NetworkClient::PushReceivedData(const uint8_t* data, size_t size) {
|
|
if (!data || size == 0) return;
|
|
|
|
std::vector<uint8_t> decrypted(data, data + size);
|
|
if (m_cipher.IsEnabled()) {
|
|
m_cipher.DecryptRecv(decrypted.data(), decrypted.size());
|
|
}
|
|
|
|
m_recv_buffer.Append(decrypted);
|
|
|
|
auto packets = m_recv_buffer.ExtractPackets(m_registry);
|
|
for (const auto& pkt : packets) {
|
|
ProcessPacket(pkt);
|
|
}
|
|
}
|
|
|
|
std::vector<uint8_t> NetworkClient::PopSendData() {
|
|
std::vector<uint8_t> result;
|
|
if (!m_send_buffer.empty()) {
|
|
result = std::move(m_send_buffer);
|
|
m_send_buffer.clear();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void NetworkClient::SendRawPacket(uint8_t header, const void* payload, size_t payload_size) {
|
|
std::vector<uint8_t> raw;
|
|
raw.reserve(1 + payload_size);
|
|
raw.push_back(header);
|
|
if (payload && payload_size > 0) {
|
|
const uint8_t* bytes = static_cast<const uint8_t*>(payload);
|
|
raw.insert(raw.end(), bytes, bytes + payload_size);
|
|
}
|
|
|
|
if (m_cipher.IsEnabled()) {
|
|
m_cipher.EncryptSend(raw.data(), raw.size());
|
|
}
|
|
|
|
m_send_buffer.insert(m_send_buffer.end(), raw.begin(), raw.end());
|
|
}
|
|
|
|
void NetworkClient::SendDynamicPacket(uint8_t header, const void* payload, size_t payload_size) {
|
|
std::vector<uint8_t> raw;
|
|
raw.reserve(3 + payload_size);
|
|
raw.push_back(header);
|
|
|
|
uint16_t sz = static_cast<uint16_t>(payload_size);
|
|
raw.push_back(static_cast<uint8_t>(sz & 0xFF));
|
|
raw.push_back(static_cast<uint8_t>((sz >> 8) & 0xFF));
|
|
|
|
if (payload && payload_size > 0) {
|
|
const uint8_t* bytes = static_cast<const uint8_t*>(payload);
|
|
raw.insert(raw.end(), bytes, bytes + payload_size);
|
|
}
|
|
|
|
if (m_cipher.IsEnabled()) {
|
|
m_cipher.EncryptSend(raw.data(), raw.size());
|
|
}
|
|
|
|
m_send_buffer.insert(m_send_buffer.end(), raw.begin(), raw.end());
|
|
}
|
|
|
|
void NetworkClient::SendHandshakeAck(uint32_t client_time) {
|
|
SendRawPacket(HEADER_CG_HANDSHAKE, &client_time, sizeof(client_time));
|
|
}
|
|
|
|
void NetworkClient::SendLogin(const std::string& username, const std::string& password) {
|
|
char buf[64];
|
|
std::memset(buf, 0, sizeof(buf));
|
|
std::strncpy(buf, username.c_str(), 31);
|
|
std::strncpy(buf + 32, password.c_str(), 31);
|
|
SendRawPacket(HEADER_CG_LOGIN, buf, sizeof(buf));
|
|
}
|
|
|
|
void NetworkClient::SendSelectCharacter(uint8_t slot_index) {
|
|
SendRawPacket(HEADER_CG_CHARACTER_SELECT, &slot_index, 1);
|
|
}
|
|
|
|
void NetworkClient::SendEnterGame() {
|
|
SendRawPacket(HEADER_CG_ENTERGAME, nullptr, 0);
|
|
}
|
|
|
|
void NetworkClient::SendChat(uint8_t chat_type, const std::string& text) {
|
|
std::vector<uint8_t> payload;
|
|
payload.reserve(1 + text.size());
|
|
payload.push_back(chat_type);
|
|
payload.insert(payload.end(), text.begin(), text.end());
|
|
SendDynamicPacket(HEADER_CG_CHAT, payload.data(), payload.size());
|
|
}
|
|
|
|
void NetworkClient::SendMove(float x, float y, float z, float rotation, uint32_t move_type) {
|
|
struct MovePacketPayload {
|
|
float x, y, z, rot;
|
|
uint32_t type;
|
|
} p{x, y, z, rotation, move_type};
|
|
SendRawPacket(HEADER_CG_MOVE, &p, sizeof(p));
|
|
}
|
|
|
|
void NetworkClient::ProcessPacket(const Packet& packet) {
|
|
switch (packet.header) {
|
|
case HEADER_GC_HANDSHAKE: {
|
|
if (packet.payload.size() >= 4) {
|
|
uint32_t server_time = *reinterpret_cast<const uint32_t*>(packet.payload.data());
|
|
if (m_on_handshake) {
|
|
m_on_handshake(server_time);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case HEADER_GC_AUTH_SUCCESS: {
|
|
SetPhase(ConnectionPhase::CharacterSelect);
|
|
if (m_on_auth_result) {
|
|
m_on_auth_result(true, 0, "Authentication successful");
|
|
}
|
|
break;
|
|
}
|
|
case HEADER_GC_AUTH_FAILURE: {
|
|
uint32_t error_code = 0;
|
|
std::string msg;
|
|
if (packet.payload.size() >= 4) {
|
|
error_code = *reinterpret_cast<const uint32_t*>(packet.payload.data());
|
|
}
|
|
if (packet.payload.size() > 4) {
|
|
const char* str = reinterpret_cast<const char*>(packet.payload.data() + 4);
|
|
size_t str_len = strnlen(str, packet.payload.size() - 4);
|
|
msg.assign(str, str_len);
|
|
}
|
|
if (m_on_auth_result) {
|
|
m_on_auth_result(false, error_code, msg);
|
|
}
|
|
break;
|
|
}
|
|
case HEADER_GC_CHARACTER_LIST: {
|
|
// Parse character summary list
|
|
// Layout per character: id(4), job(1), level(1), name(24) = 30 bytes
|
|
std::vector<CharacterSummary> characters;
|
|
size_t offset = 0;
|
|
while (offset + 30 <= packet.payload.size()) {
|
|
CharacterSummary c;
|
|
c.id = *reinterpret_cast<const uint32_t*>(packet.payload.data() + offset);
|
|
c.job = packet.payload[offset + 4];
|
|
c.level = packet.payload[offset + 5];
|
|
const char* name_ptr = reinterpret_cast<const char*>(packet.payload.data() + offset + 6);
|
|
size_t nlen = strnlen(name_ptr, 24);
|
|
c.name.assign(name_ptr, nlen);
|
|
characters.push_back(c);
|
|
offset += 30;
|
|
}
|
|
if (m_on_character_list) {
|
|
m_on_character_list(characters);
|
|
}
|
|
break;
|
|
}
|
|
case HEADER_GC_GAME_START: {
|
|
SetPhase(ConnectionPhase::InGame);
|
|
PlayerInfo info;
|
|
if (packet.payload.size() >= 4 + 32 + 16) {
|
|
info.player_id = *reinterpret_cast<const uint32_t*>(packet.payload.data());
|
|
const char* name_ptr = reinterpret_cast<const char*>(packet.payload.data() + 4);
|
|
info.name.assign(name_ptr, strnlen(name_ptr, 32));
|
|
const float* coords = reinterpret_cast<const float*>(packet.payload.data() + 36);
|
|
info.x = coords[0];
|
|
info.y = coords[1];
|
|
info.z = coords[2];
|
|
info.rotation = coords[3];
|
|
}
|
|
if (m_on_game_start) {
|
|
m_on_game_start(info);
|
|
}
|
|
break;
|
|
}
|
|
case HEADER_GC_CHAT: {
|
|
if (!packet.payload.empty()) {
|
|
uint8_t chat_type = packet.payload[0];
|
|
std::string msg;
|
|
if (packet.payload.size() > 1) {
|
|
msg.assign(reinterpret_cast<const char*>(packet.payload.data() + 1), packet.payload.size() - 1);
|
|
}
|
|
if (m_on_chat) {
|
|
m_on_chat(0, chat_type, msg);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case HEADER_GC_ACTOR_MOVE: {
|
|
if (packet.payload.size() >= 4 + 16 + 1) {
|
|
uint32_t vid = *reinterpret_cast<const uint32_t*>(packet.payload.data());
|
|
const float* coords = reinterpret_cast<const float*>(packet.payload.data() + 4);
|
|
uint8_t move_type = packet.payload[20];
|
|
if (m_on_actor_move) {
|
|
m_on_actor_move(vid, coords[0], coords[1], coords[2], coords[3], move_type);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (m_on_generic_packet) {
|
|
m_on_generic_packet(packet);
|
|
}
|
|
}
|
|
|
|
} // namespace metin2::network
|