Kelsidavis-WoWee/include/network/world_socket.hpp
Kelsi 6bf3fa4ed4 Add Windows cross-platform support alongside Linux
Replace POSIX-specific socket and process APIs with portable
abstractions so the project builds on both Windows and Linux.

- Add include/network/net_platform.hpp: Winsock2/POSIX socket
  abstraction (socket types, non-blocking, error handling,
  WSAStartup lifecycle)
- Add include/platform/process.hpp: CreateProcess/fork+exec
  abstraction for spawning ffplay subprocesses
- Update network module (tcp_socket, world_socket) to use
  portable socket helpers instead of raw POSIX calls
- Update audio module (music_manager, footstep_manager,
  activity_sound_manager) to use portable process helpers
  instead of fork/exec/kill/waitpid
- Replace hardcoded /tmp/ paths with std::filesystem::temp_directory_path()
- Link ws2_32 and SDL2main on Windows in CMakeLists.txt
2026-02-03 22:25:41 -08:00

93 lines
2.4 KiB
C++

#pragma once
#include "network/socket.hpp"
#include "network/packet.hpp"
#include "network/net_platform.hpp"
#include "auth/rc4.hpp"
#include <functional>
#include <vector>
#include <cstdint>
namespace wowee {
namespace network {
/**
* World Server Socket
*
* Handles WoW 3.3.5a world server protocol with RC4 header encryption.
*
* Key Differences from Auth Server:
* - Outgoing: 6-byte header (2 bytes size + 4 bytes opcode, big-endian)
* - Incoming: 4-byte header (2 bytes size + 2 bytes opcode, big-endian)
* - Headers are RC4-encrypted after CMSG_AUTH_SESSION
* - Packet bodies remain unencrypted
* - Size field is payload size only (does NOT include header)
*/
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;
}
/**
* Initialize RC4 encryption for packet headers
* Must be called after CMSG_AUTH_SESSION before further communication
*
* @param sessionKey 40-byte session key from auth server
*/
void initEncryption(const std::vector<uint8_t>& sessionKey);
/**
* Check if header encryption is enabled
*/
bool isEncryptionEnabled() const { return encryptionEnabled; }
private:
/**
* Try to parse complete packets from receive buffer
*/
void tryParsePackets();
socket_t sockfd = INVALID_SOCK;
bool connected = false;
bool encryptionEnabled = false;
// RC4 ciphers for header encryption/decryption
auth::RC4 encryptCipher; // For outgoing headers
auth::RC4 decryptCipher; // For incoming headers
// Receive buffer
std::vector<uint8_t> receiveBuffer;
// Packet callback
std::function<void(const Packet&)> packetCallback;
};
} // namespace network
} // namespace wowee