public void FetchLoop()
{
try {
DateTime since = DateTime.MinValue;
int requests = 1;
while (fIsEnabled) {
String name = null;
bool isTagRequest = true;
int n = 0;
lock (fPriorityFetchQ) {
while (fPriorityFetchQ.Count == 0) {
Monitor.Wait(fPriorityFetchQ);
if (!fIsEnabled) return;
}
/*
Tag requests have priority over stats requests.
Exhaust the tag queue before taking from the stats queue.
*/
if (fPriorityFetchQ.TagQueue.Count > 0) {
name = fPriorityFetchQ.TagQueue.Dequeue();
} else if (fPriorityFetchQ.StatsQueue.Count > 0) {
name = fPriorityFetchQ.StatsQueue.Dequeue();
isTagRequest = false;
}
n = fPriorityFetchQ.Count;
}
if (since == DateTime.MinValue) since = DateTime.Now;
String msg = n.ToString() + " request" + ((n > 1) ? "s" : "") + " in Battlelog request queue";
if (n == 0) {
msg = "no more requests in Battlelog request queue";
DebugFetch("^0" + msg, 4);
} else {
DebugFetch("^0" + msg, 3);
}
PlayerModel player = GetPlayer(name);
if (player == null) continue;
if (!EnableBattlelogRequests) {
player.TagFetchStatus.State = FetchState.Aborted; // drain the fetch queue
player.StatsFetchStatus.State = FetchState.Aborted; // drain the fetch queue
}
if (player.TagFetchStatus.State == FetchState.Aborted || player.StatsFetchStatus.State == FetchState.Aborted) {
if (DebugLevel >= 8) ConsoleDebug("FetchLoop: fetch for ^b" + name + "^n was aborted!");
continue;
}
if (++requests > MaximumRequestRate) {
// Wait remainder of 20 seconds before continuing
int delay = 20 - Convert.ToInt32(DateTime.Now.Subtract(since).TotalSeconds);
if (delay > 0) {
DebugFetch("Sleeping remaining " + delay + " seconds before sending next request");
while (delay > 0) {
Thread.Sleep(1000);
if (!fIsEnabled) return;
if (!EnableBattlelogRequests) break;
--delay;
}
}
requests = 1; // reset
since = DateTime.Now;
}
String requestType = (isTagRequest) ? "clanTag" : "overview";
if (fIsCacheEnabled) {
SendCacheRequest(name, requestType);
} else {
switch (fGameVersion) {
case GameVersion.BFH:
SendBattlelogRequestBFH(name, requestType, null);
break;
case GameVersion.BF3:
SendBattlelogRequest(name, requestType, null);
break;
case GameVersion.BF4:
default:
SendBattlelogRequestBF4(name, requestType, null);
break;
}
PlayerModel pm = GetPlayer(name);
if (isTagRequest) {
if (pm.TagFetchStatus.State != FetchState.Succeeded) pm.TagVerified = true;
} else {
if (pm.StatsFetchStatus.State != FetchState.Succeeded) pm.StatsVerified = true;
}
}
}
} catch (ThreadAbortException) {
fAborted = true;
return;
} catch (Exception e) {
ConsoleException(e);
} finally {
if (!fAborted) ConsoleWrite("^bFetchLoop^n thread stopped", 0);
}
}