1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WardenPayloadMgr.h"
#include "Errors.h"
#include "Log.h"
#include "StringFormat.h"
WardenPayloadMgr::WardenPayloadMgr() { }
uint16 WardenPayloadMgr::GetFreePayloadId()
{
uint16 payloadId = WardenPayloadOffsetMin;
while (CachedChecks.find(payloadId) != CachedChecks.end())
{
payloadId++;
if (payloadId > WardenPayloadMgr::WardenPayloadOffsetMax)
{
LOG_ERROR("warden", "Max warden payload id of '{}' passed!", WardenPayloadMgr::WardenPayloadOffsetMax);
return 0;
}
}
return payloadId;
}
uint16 WardenPayloadMgr::RegisterPayload(const std::string& payload)
{
uint16 payloadId = GetFreePayloadId();
if (!payloadId || !RegisterPayload(payload, payloadId, false))
{
LOG_ERROR("warden", "Failed to register payload.");
return 0;
}
return payloadId;
}
bool WardenPayloadMgr::RegisterPayload(std::string const& payload, uint16 payloadId, bool replace)
{
//Payload id should be over or equal to the offset to prevent conflicts.
if (payloadId < WardenPayloadMgr::WardenPayloadOffsetMin)
{
LOG_ERROR("warden", "Tried to register payloadId lower than '{}'.", WardenPayloadMgr::WardenPayloadOffsetMin);
return false;
}
auto it = CachedChecks.find(payloadId);
if (it != CachedChecks.end() && !replace)
{
LOG_ERROR("warden", "Payload Id '{}' already exists in CachedChecks.", payloadId);
return false;
}
WardenCheck wCheck;
wCheck.Type = WardenPayloadMgr::WardenPayloadCheckType;
wCheck.Str = payload;
wCheck.CheckId = payloadId;
std::string idStr = Acore::StringFormat("%04u", payloadId);
ASSERT(idStr.size() == 4);
std::copy(idStr.begin(), idStr.end(), wCheck.IdStr.begin());
if (replace)
{
CachedChecks.erase(payloadId);
}
CachedChecks.emplace(payloadId, wCheck);
return true;
}
bool WardenPayloadMgr::UnregisterPayload(uint16 payloadId)
{
return CachedChecks.erase(payloadId);
}
WardenCheck* WardenPayloadMgr::GetPayloadById(uint16 payloadId)
{
auto it = CachedChecks.find(payloadId);
if (it != CachedChecks.end())
{
return &it->second;
}
return nullptr;
}
void WardenPayloadMgr::QueuePayload(uint16 payloadId, bool pushToFront)
{
auto it = CachedChecks.find(payloadId);
//Do not queue a payload if there is no payload matching the payloadId.
if (it == CachedChecks.end())
{
LOG_ERROR("warden", "Failed to queue payload id '{}' as it does not exist in CachedChecks.", payloadId);
return;
}
if (pushToFront)
{
QueuedPayloads.push_front(payloadId);
}
else
{
QueuedPayloads.push_back(payloadId);
}
}
bool WardenPayloadMgr::DequeuePayload(uint16 payloadId)
{
size_t const queueSize = QueuedPayloads.size();
QueuedPayloads.remove(payloadId);
return queueSize != QueuedPayloads.size();
}
void WardenPayloadMgr::ClearQueuedPayloads()
{
QueuedPayloads.clear();
}
uint32 WardenPayloadMgr::GetPayloadCountInQueue()
{
return QueuedPayloads.size();
}
std::list<uint16>* WardenPayloadMgr::GetPayloadsInQueue()
{
return &QueuedPayloads;
}
|