88 lines
3.7 KiB
C++
88 lines
3.7 KiB
C++
#pragma once
|
|
|
|
#include "types.h"
|
|
#include "connection_phase.h"
|
|
#include "cipher.h"
|
|
#include "packet.h"
|
|
#include <functional>
|
|
#include <string>
|
|
|
|
namespace metin2::network {
|
|
|
|
class NetworkClient {
|
|
public:
|
|
using HandshakeCallback = std::function<void(uint32_t server_time)>;
|
|
using AuthResultCallback = std::function<void(bool success, uint32_t error_code, const std::string& message)>;
|
|
using CharacterListCallback = std::function<void(const std::vector<CharacterSummary>& characters)>;
|
|
using GameStartCallback = std::function<void(const PlayerInfo& player)>;
|
|
using ChatCallback = std::function<void(uint32_t sender_id, uint8_t chat_type, const std::string& text)>;
|
|
using ActorMoveCallback = std::function<void(uint32_t actor_id, float x, float y, float z, float rot, uint32_t move_type)>;
|
|
using GenericPacketCallback = std::function<void(const Packet& packet)>;
|
|
|
|
NetworkClient();
|
|
|
|
// Transport Interface (zero socket binding)
|
|
void PushReceivedData(const uint8_t* data, size_t size);
|
|
std::vector<uint8_t> PopSendData();
|
|
bool HasPendingSendData() const { return !m_send_buffer.empty(); }
|
|
|
|
// Phase and State
|
|
ConnectionPhase GetPhase() const { return m_phase_fsm.GetCurrentPhase(); }
|
|
const char* GetPhaseName() const { return m_phase_fsm.GetPhaseName(); }
|
|
bool SetPhase(ConnectionPhase phase) { return m_phase_fsm.TransitionTo(phase); }
|
|
void ForcePhase(ConnectionPhase phase) { m_phase_fsm.ForcePhase(phase); }
|
|
bool IsConnected() const { return m_phase_fsm.IsConnected(); }
|
|
bool IsInGame() const { return m_phase_fsm.IsInGame(); }
|
|
|
|
// Cipher Control
|
|
CipherContext& GetCipher() { return m_cipher; }
|
|
void EnableCipher(bool enable) { m_cipher.SetEnabled(enable); }
|
|
void SetEncryptionKeys(const TeaKey& tea_key, uint32_t xor_seed) {
|
|
m_cipher.InitializeKeys(tea_key, xor_seed);
|
|
}
|
|
|
|
// Packet Registry
|
|
PacketRegistry& GetRegistry() { return m_registry; }
|
|
|
|
// Callback Setup
|
|
void SetPhaseCallback(PhaseStateMachine::PhaseChangeCallback cb) { m_phase_fsm.SetPhaseChangeCallback(std::move(cb)); }
|
|
void SetOnHandshake(HandshakeCallback cb) { m_on_handshake = std::move(cb); }
|
|
void SetOnAuthResult(AuthResultCallback cb) { m_on_auth_result = std::move(cb); }
|
|
void SetOnCharacterList(CharacterListCallback cb) { m_on_character_list = std::move(cb); }
|
|
void SetOnGameStart(GameStartCallback cb) { m_on_game_start = std::move(cb); }
|
|
void SetOnChat(ChatCallback cb) { m_on_chat = std::move(cb); }
|
|
void SetOnActorMove(ActorMoveCallback cb) { m_on_actor_move = std::move(cb); }
|
|
void SetOnGenericPacket(GenericPacketCallback cb) { m_on_generic_packet = std::move(cb); }
|
|
|
|
// High-Level Sending Methods
|
|
void SendHandshakeAck(uint32_t client_time);
|
|
void SendLogin(const std::string& username, const std::string& password);
|
|
void SendSelectCharacter(uint8_t slot_index);
|
|
void SendEnterGame();
|
|
void SendChat(uint8_t chat_type, const std::string& text);
|
|
void SendMove(float x, float y, float z, float rotation, uint32_t move_type);
|
|
void SendRawPacket(uint8_t header, const void* payload, size_t payload_size);
|
|
void SendDynamicPacket(uint8_t header, const void* payload, size_t payload_size);
|
|
|
|
void Reset();
|
|
|
|
private:
|
|
void ProcessPacket(const Packet& packet);
|
|
|
|
PhaseStateMachine m_phase_fsm;
|
|
CipherContext m_cipher;
|
|
PacketRegistry m_registry;
|
|
PacketBuffer m_recv_buffer;
|
|
std::vector<uint8_t> m_send_buffer;
|
|
|
|
HandshakeCallback m_on_handshake;
|
|
AuthResultCallback m_on_auth_result;
|
|
CharacterListCallback m_on_character_list;
|
|
GameStartCallback m_on_game_start;
|
|
ChatCallback m_on_chat;
|
|
ActorMoveCallback m_on_actor_move;
|
|
GenericPacketCallback m_on_generic_packet;
|
|
};
|
|
|
|
} // namespace metin2::network
|