feat(net): wire up polling loop for realm connections

This commit is contained in:
fallenoak 2023-02-25 15:59:30 -06:00
parent cf6bc34657
commit 5e2c1e7769
No known key found for this signature in database
GPG Key ID: 7628F8E61AEA070D
8 changed files with 178 additions and 5 deletions

View File

@ -8,6 +8,7 @@
#include "gx/Screen.hpp"
#include "gx/Texture.hpp"
#include "model/Model2.hpp"
#include "net/Poll.hpp"
#include "ui/FrameScript.hpp"
#include "ui/FrameXML.hpp"
#include "util/BlizzardCore.hpp"
@ -436,5 +437,6 @@ void WowClientInit() {
// TODO
// CGlueMgr::m_pendingTimerAlert = dword_B2F9D8;
// sub_7FC5A0();
// EventRegister(EVENT_ID_POLL, &PollNet);
EventRegister(EVENT_ID_POLL, &PollNet);
}

View File

@ -33,9 +33,9 @@ enum EVENTID {
EVENT_ID_PAINT = 23,
EVENT_ID_NET_DATA = 24,
EVENT_ID_NET_CONNECT = 25,
EVENT_ID_26 = 26,
EVENT_ID_27 = 27,
EVENT_ID_28 = 28,
EVENT_ID_NET_DISCONNECT = 26,
EVENT_ID_NET_CANTCONNECT = 27,
EVENT_ID_NET_DESTROY = 28,
EVENT_ID_NET_AUTH_CHALLENGE = 29,
EVENT_ID_30 = 30,
EVENT_ID_31 = 31,

10
src/net/Poll.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "net/Poll.hpp"
#include "net/connection/RealmConnection.hpp"
int32_t PollNet(const void* a1, void* a2) {
RealmConnection::PollNet();
// TODO
return 1;
}

8
src/net/Poll.hpp Normal file
View File

@ -0,0 +1,8 @@
#ifndef NET_POLL_HPP
#define NET_POLL_HPP
#include <cstdint>
int32_t PollNet(const void* a1, void* a2);
#endif

View File

@ -38,11 +38,71 @@ void NETEVENTQUEUE::AddEvent(EVENTID eventId, void* conn, NetClient* client, con
node->m_timeReceived = OsGetAsyncTimeMsPrecise();
// TODO SInterlockedIncremement(client->uint2E44);
client->AddRef();
this->m_critSect.Leave();
}
void NETEVENTQUEUE::Poll() {
this->m_critSect.Enter();
auto deleted = false;
auto client = this->m_client;
client->AddRef();
for (auto node = this->m_eventQueue.Head(); node; node = this->m_eventQueue.Next(node)) {
if (!client->GetDelete()) {
switch (node->m_eventId) {
case EVENT_ID_NET_DATA:
client->HandleData(node->m_timeReceived, node->m_data, node->m_dataSize);
break;
case EVENT_ID_NET_CONNECT:
client->HandleConnect();
break;
case EVENT_ID_NET_DISCONNECT:
client->HandleDisconnect();
break;
case EVENT_ID_NET_CANTCONNECT:
client->HandleCantConnect();
break;
case EVENT_ID_NET_DESTROY:
client->SetDelete();
deleted = true;
break;
case EVENT_ID_NET_AUTH_CHALLENGE:
client->HandleAuthChallenge(static_cast<AuthenticationChallenge*>(node->m_data));
break;
default:
break;
}
}
// Matching 1:1 with the ref added by NETEVENTQUEUE::AddEvent
client->DelRef();
}
if (!deleted) {
client->HandleIdle();
}
client->DelRef();
this->m_eventQueue.DeleteAll();
this->m_critSect.Leave();
}
void NetClient::AddRef() {
SInterlockedIncrement(&this->m_refCount);
}
void NetClient::AuthChallengeHandler(WowConnection* conn, CDataStore* msg) {
auto challenge = static_cast<AuthenticationChallenge*>(SMemAlloc(sizeof(AuthenticationChallenge), __FILE__, __LINE__, 0x0));
@ -98,6 +158,42 @@ int32_t NetClient::ConnectInternal(const char* host, uint16_t port) {
return 1;
}
void NetClient::DelRef() {
auto refCount = SInterlockedDecrement(&this->m_refCount);
if (refCount == 0 && this->m_deleteMe) {
delete this;
}
}
bool NetClient::GetDelete() {
return this->m_deleteMe;
}
int32_t NetClient::HandleCantConnect() {
// TODO
return 1;
}
int32_t NetClient::HandleConnect() {
// TODO
return 1;
}
int32_t NetClient::HandleData(uint32_t timeReceived, void* data, int32_t size) {
// TODO
return 1;
}
int32_t NetClient::HandleDisconnect() {
// TODO
return 1;
}
void NetClient::HandleIdle() {
// TODO;
}
int32_t NetClient::Initialize() {
STORM_ASSERT(this->m_netState == NS_UNINITIALIZED);
@ -127,10 +223,18 @@ int32_t NetClient::Initialize() {
return 1;
}
void NetClient::PollEventQueue() {
this->m_netEventQueue->Poll();
}
void NetClient::PongHandler(WowConnection* conn, CDataStore* msg) {
// TODO
}
void NetClient::SetDelete() {
this->m_deleteMe = true;
}
void NetClient::SetLoginData(LoginData* loginData) {
memcpy(&this->m_loginData, loginData, sizeof(this->m_loginData));
}

View File

@ -4,6 +4,7 @@
#include "net/connection/WowConnectionResponse.hpp"
#include "event/Event.hpp"
#include "net/Types.hpp"
#include <storm/Atomic.hpp>
#include <storm/List.hpp>
#include <storm/Thread.hpp>
#include <cstdint>
@ -41,6 +42,7 @@ class NETEVENTQUEUE {
: m_client(client)
{};
void AddEvent(EVENTID eventId, void* conn, NetClient* client, const void* data, uint32_t bytes);
void Poll();
};
class NetClient : public WowConnectionResponse {
@ -57,6 +59,8 @@ class NetClient : public WowConnectionResponse {
NETEVENTQUEUE* m_netEventQueue = nullptr;
WowConnection* m_serverConnection = nullptr;
WowConnection* m_redirectConnection = nullptr;
ATOMIC32 m_refCount = 0;
bool m_deleteMe = false;
uint32_t m_pingSent = 0;
uint32_t m_pingSequence = 0;
uint32_t m_latency[16];
@ -69,13 +73,24 @@ class NetClient : public WowConnectionResponse {
virtual void WCConnected(WowConnection* conn, WowConnection* inbound, uint32_t timeStamp, const NETCONNADDR* addr);
virtual void WCCantConnect(WowConnection* conn, uint32_t timeStamp, NETCONNADDR* addr);
virtual void WCDisconnected(WowConnection* conn, uint32_t timeStamp, NETCONNADDR* addr);
virtual int32_t HandleData(uint32_t timeReceived, void* data, int32_t size);
virtual int32_t HandleAuthChallenge(AuthenticationChallenge* challenge) = 0;
virtual int32_t HandleConnect();
virtual int32_t HandleDisconnect();
virtual int32_t HandleCantConnect();
// Member functions
void AddRef();
void AuthChallengeHandler(WowConnection* conn, CDataStore* msg);
void Connect(const char* addrStr);
int32_t ConnectInternal(const char* host, uint16_t port);
void DelRef();
bool GetDelete();
void HandleIdle();
int32_t Initialize();
void PollEventQueue();
void PongHandler(WowConnection* conn, CDataStore* msg);
void SetDelete();
void SetLoginData(LoginData* loginData);
void SetMessageHandler(NETMESSAGE msgId, MESSAGE_HANDLER handler, void* param);
};

View File

@ -1,11 +1,24 @@
#include "net/connection/RealmConnection.hpp"
#include "net/Types.hpp"
SCritSect RealmConnection::s_AllRealmConnectionsCrit;
STORM_LIST(RealmConnection::REALMCONNECTIONNODE) RealmConnection::s_AllRealmConnections;
int32_t RealmConnection::MessageHandler(void* param, NETMESSAGE msgId, uint32_t time, CDataStore* msg) {
// TODO
return 0;
}
void RealmConnection::PollNet() {
RealmConnection::s_AllRealmConnectionsCrit.Enter();
for (auto node = RealmConnection::s_AllRealmConnections.Head(); node; node = RealmConnection::s_AllRealmConnections.Next(node)) {
node->connection->PollEventQueue();
}
RealmConnection::s_AllRealmConnectionsCrit.Leave();
}
RealmConnection::RealmConnection(RealmResponse* realmResponse) {
this->m_realmResponse = realmResponse;
@ -25,7 +38,15 @@ RealmConnection::RealmConnection(RealmResponse* realmResponse) {
this->SetMessageHandler(SMSG_DELETE_CHAR, &RealmConnection::MessageHandler, this);
this->SetMessageHandler(SMSG_CACHE_VERSION, &RealmConnection::MessageHandler, this);
RealmConnection::s_AllRealmConnectionsCrit.Enter();
auto node = RealmConnection::s_AllRealmConnections.NewNode(2, 0, 0x0);
node->connection = this;
RealmConnection::s_AllRealmConnectionsCrit.Leave();
}
int32_t RealmConnection::HandleAuthChallenge(AuthenticationChallenge* challenge) {
// TODO
return 1;
}
void RealmConnection::SetSelectedRealm(uint32_t a2, uint32_t a3, uint32_t a4) {

View File

@ -9,12 +9,25 @@ class RealmResponse;
class RealmConnection : public NetClient {
public:
// Types
struct REALMCONNECTIONNODE : TSLinkedNode<REALMCONNECTIONNODE> {
RealmConnection* connection;
};
// Static variables
SCritSect static s_AllRealmConnectionsCrit;
STORM_LIST(REALMCONNECTIONNODE) static s_AllRealmConnections;
// Static functions
int32_t static MessageHandler(void* param, NETMESSAGE msgId, uint32_t time, CDataStore* msg);
void static PollNet();
// Member variables
RealmResponse* m_realmResponse;
// Virtual member functions
virtual int32_t HandleAuthChallenge(AuthenticationChallenge* challenge);
// Member functions
RealmConnection(RealmResponse* realmResponse);
void SetSelectedRealm(uint32_t a2, uint32_t a3, uint32_t a4);