2026-02-02 12:24:50 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "network/socket.hpp"
|
|
|
|
|
#include "network/packet.hpp"
|
2026-02-03 22:24:17 -08:00
|
|
|
#include "network/net_platform.hpp"
|
2026-02-02 12:24:50 -08:00
|
|
|
#include "auth/rc4.hpp"
|
2026-02-13 16:53:28 -08:00
|
|
|
#include "auth/vanilla_crypt.hpp"
|
2026-02-02 12:24:50 -08:00
|
|
|
#include <functional>
|
|
|
|
|
#include <vector>
|
2026-03-15 01:21:23 -07:00
|
|
|
#include <deque>
|
2026-02-02 12:24:50 -08:00
|
|
|
#include <cstdint>
|
2026-03-15 01:21:23 -07:00
|
|
|
#include <chrono>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <string>
|
2026-02-02 12:24:50 -08:00
|
|
|
|
|
|
|
|
namespace wowee {
|
|
|
|
|
namespace network {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* World Server Socket
|
|
|
|
|
*
|
2026-02-13 16:53:28 -08:00
|
|
|
* Handles WoW world server protocol with header encryption.
|
|
|
|
|
* Supports both vanilla/TBC (XOR+addition cipher) and WotLK (RC4).
|
2026-02-02 12:24:50 -08:00
|
|
|
*
|
|
|
|
|
* Key Differences from Auth Server:
|
|
|
|
|
* - Outgoing: 6-byte header (2 bytes size + 4 bytes opcode, big-endian)
|
2026-02-12 02:27:59 -08:00
|
|
|
* - Incoming: 4-byte header (2 bytes size + 2 bytes opcode)
|
2026-02-13 16:53:28 -08:00
|
|
|
* - Headers are encrypted after CMSG_AUTH_SESSION
|
2026-02-02 12:24:50 -08:00
|
|
|
* - Packet bodies remain unencrypted
|
2026-02-12 02:27:59 -08:00
|
|
|
* - Size field includes opcode bytes (payloadLen = size - 2)
|
2026-02-02 12:24:50 -08:00
|
|
|
*/
|
|
|
|
|
class WorldSocket : public Socket {
|
|
|
|
|
public:
|
|
|
|
|
WorldSocket();
|
|
|
|
|
~WorldSocket() override;
|
|
|
|
|
|
|
|
|
|
bool connect(const std::string& host, uint16_t port) override;
|
|
|
|
|
void disconnect() override;
|
|
|
|
|
bool isConnected() const override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a world packet
|
|
|
|
|
* Automatically encrypts 6-byte header if encryption is enabled
|
|
|
|
|
*
|
|
|
|
|
* @param packet Packet to send
|
|
|
|
|
*/
|
|
|
|
|
void send(const Packet& packet) override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update socket - receive data and parse packets
|
|
|
|
|
* Should be called regularly (e.g., each frame)
|
|
|
|
|
*/
|
|
|
|
|
void update();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set callback for complete packets
|
|
|
|
|
*
|
|
|
|
|
* @param callback Function to call when packet is received
|
|
|
|
|
*/
|
|
|
|
|
void setPacketCallback(std::function<void(const Packet&)> callback) {
|
|
|
|
|
packetCallback = callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-13 16:53:28 -08:00
|
|
|
* Initialize header encryption for packet headers
|
2026-02-02 12:24:50 -08:00
|
|
|
* Must be called after CMSG_AUTH_SESSION before further communication
|
|
|
|
|
*
|
|
|
|
|
* @param sessionKey 40-byte session key from auth server
|
2026-02-13 16:53:28 -08:00
|
|
|
* @param build Client build number (determines cipher: <= 8606 = XOR, > 8606 = RC4)
|
2026-02-02 12:24:50 -08:00
|
|
|
*/
|
2026-02-13 16:53:28 -08:00
|
|
|
void initEncryption(const std::vector<uint8_t>& sessionKey, uint32_t build = 12340);
|
2026-02-02 12:24:50 -08:00
|
|
|
|
2026-03-15 01:21:23 -07:00
|
|
|
void tracePacketsFor(std::chrono::milliseconds duration, const std::string& reason);
|
|
|
|
|
|
2026-02-02 12:24:50 -08:00
|
|
|
/**
|
|
|
|
|
* Check if header encryption is enabled
|
|
|
|
|
*/
|
|
|
|
|
bool isEncryptionEnabled() const { return encryptionEnabled; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* Try to parse complete packets from receive buffer
|
|
|
|
|
*/
|
|
|
|
|
void tryParsePackets();
|
2026-03-15 01:21:23 -07:00
|
|
|
void pumpNetworkIO();
|
|
|
|
|
void dispatchQueuedPackets();
|
|
|
|
|
void asyncPumpLoop();
|
|
|
|
|
void startAsyncPump();
|
|
|
|
|
void stopAsyncPump();
|
|
|
|
|
void closeSocketNoJoin();
|
2026-02-02 12:24:50 -08:00
|
|
|
|
2026-02-03 22:24:17 -08:00
|
|
|
socket_t sockfd = INVALID_SOCK;
|
2026-02-02 12:24:50 -08:00
|
|
|
bool connected = false;
|
|
|
|
|
bool encryptionEnabled = false;
|
2026-02-13 16:53:28 -08:00
|
|
|
bool useVanillaCrypt = false; // true = XOR cipher, false = RC4
|
2026-03-15 01:21:23 -07:00
|
|
|
bool useAsyncPump_ = true;
|
|
|
|
|
std::thread asyncPumpThread_;
|
|
|
|
|
std::atomic<bool> asyncPumpStop_{false};
|
|
|
|
|
std::atomic<bool> asyncPumpRunning_{false};
|
|
|
|
|
mutable std::mutex ioMutex_;
|
|
|
|
|
mutable std::mutex callbackMutex_;
|
2026-02-02 12:24:50 -08:00
|
|
|
|
2026-02-13 16:53:28 -08:00
|
|
|
// WotLK RC4 ciphers for header encryption/decryption
|
|
|
|
|
auth::RC4 encryptCipher;
|
|
|
|
|
auth::RC4 decryptCipher;
|
|
|
|
|
|
|
|
|
|
// Vanilla/TBC XOR+addition cipher
|
|
|
|
|
auth::VanillaCrypt vanillaCrypt;
|
2026-02-02 12:24:50 -08:00
|
|
|
|
|
|
|
|
// Receive buffer
|
|
|
|
|
std::vector<uint8_t> receiveBuffer;
|
2026-02-22 08:16:54 -08:00
|
|
|
size_t receiveReadOffset_ = 0;
|
2026-02-22 07:44:32 -08:00
|
|
|
// Optional reused packet queue (feature-gated) to reduce per-update allocations.
|
|
|
|
|
std::vector<Packet> parsedPacketsScratch_;
|
2026-03-15 01:21:23 -07:00
|
|
|
// Parsed packets waiting for callback dispatch; drained with a strict per-update budget.
|
|
|
|
|
std::deque<Packet> pendingPacketCallbacks_;
|
2026-02-22 07:44:32 -08:00
|
|
|
|
|
|
|
|
// Runtime-gated network optimization toggles (default off).
|
|
|
|
|
bool useFastRecvAppend_ = false;
|
|
|
|
|
bool useParseScratchQueue_ = false;
|
2026-02-02 12:24:50 -08:00
|
|
|
|
2026-02-05 21:55:52 -08:00
|
|
|
// Track how many header bytes have been decrypted (0-4)
|
|
|
|
|
// This prevents re-decrypting the same header when waiting for more data
|
|
|
|
|
size_t headerBytesDecrypted = 0;
|
|
|
|
|
|
2026-02-12 02:27:59 -08:00
|
|
|
// Debug-only tracing window for post-auth packet framing verification.
|
|
|
|
|
int headerTracePacketsLeft = 0;
|
2026-03-15 01:21:23 -07:00
|
|
|
std::chrono::steady_clock::time_point packetTraceStart_{};
|
|
|
|
|
std::chrono::steady_clock::time_point packetTraceUntil_{};
|
|
|
|
|
std::string packetTraceReason_;
|
2026-02-12 02:27:59 -08:00
|
|
|
|
2026-02-02 12:24:50 -08:00
|
|
|
// Packet callback
|
|
|
|
|
std::function<void(const Packet&)> packetCallback;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace network
|
|
|
|
|
} // namespace wowee
|