Files
yugioh-legacy-engine/server/gframe/duelclient.h
T
donnyandClaude Fable 5.1 da27435cdb server: import the duel server (nanahira/ygopro server at 52c868ce)
The server tree of https://code.moenext.com/nanahira/ygopro.git, branch
server, commit 52c868ce071ea0f2fcb0cf39bf2fb4151d12b90c, stock: gframe/,
premake5.lua, .ci/, LICENSE, and gframe/ygopro.rc + .ico (upstream keeps
them in resource/ and copies them in before building). No game assets, no
scripts, no ocgcore submodule: the core is ../core.

The third-party sources upstream's Windows CI downloads are vendored here
at the positions its premake expects, each with its premake fragment from
upstream's premake/ folder inside it: lua 5.4.8, sqlite 3.53.3, libevent
2.1.13-stable (tests, samples and autotools helpers left out; the files
its Windows prebuild copies into include/ left out), zlib 1.3.2 (contrib
and examples left out), xz 5.8.3 (src/liblzma + src/common only), and
irrlicht 1.9 from mycard at 20f86d25: include/ plus the source/Irrlicht
headers and the 11 .cpp files the server's zip reader project compiles.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-14 20:58:21 -07:00

137 lines
4.1 KiB
C++

#ifndef DUELCLIENT_H
#define DUELCLIENT_H
#include <cstdio>
#include <vector>
#include "bufferio.h"
#include "deck.h"
#include "network.h"
#ifndef _WIN32
#include <resolv.h>
#include <arpa/nameser.h>
#include <arpa/nameser_compat.h>
#endif
namespace ygo {
#define CONNECT_STATE_NONE 0x0
#define CONNECT_STATE_CONNECTING 0x1
#define CONNECT_STATE_CONNECTED 0x2
#define CONNECT_STATE_JOINED 0x4
#define CLIENT_CLOSE_REASON_NONE 0
#define CLIENT_CLOSE_REASON_STOP 1
#define CLIENT_CLOSE_REASON_EXIT 2
class HostResult {
public:
unsigned int host;
unsigned short port;
bool isValid() {
return host > 0 && port > 0;
}
HostResult() {
host = 0;
port = 0;
}
};
#ifndef _WIN32
class RetrivedSRVRecord {
public:
bool valid;
unsigned short priority;
unsigned short weight;
unsigned short port;
char host[100];
RetrivedSRVRecord(ns_msg nsMsg, int i) {
valid = false;
ns_rr rr;
if (ns_parserr(&nsMsg, ns_s_an, i, &rr) < 0 || ns_rr_type(rr) != T_SRV)
return;
priority = ns_get16(ns_rr_rdata(rr));
weight = ns_get16(ns_rr_rdata(rr) + NS_INT16SZ);
port = ns_get16(ns_rr_rdata(rr) + 2 * NS_INT16SZ);
if (dn_expand(ns_msg_base(nsMsg), ns_msg_end(nsMsg), ns_rr_rdata(rr) + 3 * NS_INT16SZ, host, sizeof(host)) < 0)
return;
valid = true;
}
};
#endif
class DuelClient {
private:
static bufferevent* client_bev;
static unsigned char duel_client_write[SIZE_NETWORK_BUFFER];
static int WriteBufferEvent(bufferevent* bufev, const void* data, size_t size);
public:
static unsigned int temp_ip;
static unsigned short temp_port;
static unsigned short temp_ver;
static bool try_needed;
static unsigned char selftype;
static bool StartClient(unsigned int ip, unsigned short port, bool create_game = true);
static void ConnectTimeout(EventSocket fd, short events, void* arg);
static void StopClient(unsigned reason = CLIENT_CLOSE_REASON_STOP);
static void ClientRead(bufferevent* bev, void* ctx);
static void ClientEvent(bufferevent* bev, short events, void* ctx);
static void ClientThread();
static void HandleSTOCPacketLan(unsigned char* data, size_t len);
static bool ClientAnalyze(unsigned char* msg, size_t len);
static void SwapField();
static void SetResponseI(int32_t respI);
static void SetResponseB(void* respB, size_t len);
static void SendResponse();
static void SendUpdateDeck(const Deck& deck);
static unsigned int LookupHost(char *host);
static bool LookupSRV(char *hostname, HostResult* result);
static bool CheckHostnameSplitter(char *hostname, HostResult *result);
static HostResult ParseHost(char *hostname);
static void SendPacketToServer(unsigned char proto) {
auto p = duel_client_write;
BufferIO::Write<uint16_t>(p, 1);
BufferIO::Write<uint8_t>(p, proto);
#ifdef YGOPRO_MESSAGE_DEBUG
printf("CTOS: %d\n", proto);
#endif
WriteBufferEvent(client_bev, duel_client_write, 3);
}
template<typename ST>
static void SendPacketToServer(unsigned char proto, const ST& st) {
auto p = duel_client_write;
static_assert(sizeof(ST) <= MAX_DATA_SIZE, "Packet size is too large.");
BufferIO::Write<uint16_t>(p, (uint16_t)(1 + sizeof(ST)));
BufferIO::Write<uint8_t>(p, proto);
std::memcpy(p, &st, sizeof(ST));
#ifdef YGOPRO_MESSAGE_DEBUG
printf("CTOS: %d Length: %ld\n", proto, sizeof(ST));
#endif
WriteBufferEvent(client_bev, duel_client_write, sizeof(ST) + 3);
}
static void SendBufferToServer(unsigned char proto, void* buffer, size_t len) {
auto p = duel_client_write;
if (len > MAX_DATA_SIZE)
len = MAX_DATA_SIZE;
BufferIO::Write<uint16_t>(p, (uint16_t)(1 + len));
BufferIO::Write<uint8_t>(p, proto);
std::memcpy(p, buffer, len);
#ifdef YGOPRO_MESSAGE_DEBUG
printf("CTOS: %d Length: %ld\n", proto, len);
#endif
WriteBufferEvent(client_bev, duel_client_write, len + 3);
}
static std::vector<std::wstring> hosts;
static std::vector<std::wstring> hosts_srvpro;
static bool is_srvpro;
static void BeginRefreshHost();
static int RefreshThread(event_base* broadev);
static void BroadcastReply(EventSocket fd, short events, void* arg);
static unsigned int ResolveHostName(const char* hostname, const char* port);
};
}
#endif //DUELCLIENT_H