Files
WoW-Hybrid-gateway/native/quest_status_refresh.hpp
praxisandClaude Fable 5.2 8e02dccb86 In-client stack: transport, session lifecycle and engine bridge
175 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

62 lines
3.2 KiB
C++

// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "quest_translation.hpp"
#include <algorithm>
#include <deque>
#include <map>
#include <optional>
#include <vector>
// Tracks real Source status requests through actual send and binds each reply
// to the actor generation observed at send. All queues are session-local.
struct QuestStatusRefresh {
struct Request {uint64_t guid=0,generation=0;bool require_questgiver=true;uint64_t epoch=1;};
static constexpr size_t Limit=8192;
std::map<uint64_t,uint64_t> scheduled;
std::deque<Request> pending;
std::map<uint64_t,std::deque<Request>> inflight;
size_t inflight_count=0;
uint64_t epoch=1;
// Vanilla status replies contain only the GUID, not a request token. Keep
// every sent request in FIFO order across quest changes and SELF resets.
// Old replies must retire their old epoch before a fresh query is sent.
void invalidate(){if(epoch==UINT64_MAX)throw gateway::Error("quest refresh epoch overflow");++epoch;scheduled.clear();pending.clear();}
void reset_presentation(){invalidate();}
bool current(Request q,const NativeQuestTranslation::Bindings& b)const{
auto i=b.find(q.guid);return q.epoch==epoch && i!=b.end() && (!q.require_questgiver || i->second.questgiver) &&
(i->second.kind==3 || i->second.kind==5) && i->second.generation==q.generation;
}
void observe(const NativeQuestTranslation::Bindings& b){
for(auto i=scheduled.begin();i!=scheduled.end();)if(!current({i->first,i->second,true,epoch},b))i=scheduled.erase(i);else ++i;
for(auto& [guid,actor]:b){Request q{guid,actor.generation,true,epoch};
if(!current(q,b)||scheduled.contains(guid)||pending.size()>=Limit)continue;
pending.push_back(q);scheduled.emplace(guid,q.generation);
}
}
std::optional<Request> next(const NativeQuestTranslation::Bindings& b){
if(inflight_count>=Limit)return std::nullopt;
for(auto i=pending.begin();i!=pending.end();){
if(!current(*i,b)){i=pending.erase(i);continue;}
// Do not overlap an automatic refresh with tokenless replies still
// owed for this GUID. Other visible actors can refresh meanwhile.
if(!inflight.contains(i->guid))return *i;
++i;
}
return std::nullopt;
}
bool can_send(size_t count)const noexcept{return count<=Limit-inflight_count;}
void sent(Request q){q.epoch=epoch;inflight[q.guid].push_back(q);++inflight_count;}
void sent_pending(Request q){
auto i=std::find_if(pending.begin(),pending.end(),[&](const auto& p){return p.guid==q.guid&&p.generation==q.generation&&p.epoch==q.epoch;});
if(i==pending.end()||q.epoch!=epoch||inflight.contains(q.guid))throw gateway::Error("quest refresh pending mismatch");
sent(q);pending.erase(i);
}
// Unsolicited genuine Source statuses retain the preexisting behavior.
bool accept(uint64_t guid,const NativeQuestTranslation::Bindings& b){
auto it=inflight.find(guid);if(it==inflight.end())return true;
Request q=it->second.front();it->second.pop_front();--inflight_count;
if(it->second.empty())inflight.erase(it);
return current(q,b);
}
};