Skip to content

Commit ddce834

Browse files
committed
Update Antispam for ACE branch.
1 parent e7143a6 commit ddce834

2 files changed

Lines changed: 36 additions & 29 deletions

File tree

src/game/Anticheat/Antispam/Antispam.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,9 @@ static inline void ReplaceAllW(std::wstring &str, const std::wstring& from, cons
3030
}
3131
}
3232

33-
void AntispamAsyncWorker(Antispam *antispam)
34-
{
35-
using namespace std::chrono_literals;
36-
LoginDatabase.ThreadStart();
37-
LogsDatabase.ThreadStart();
38-
auto prevNow = Clock::now();
39-
while (!sWorld.IsStopped())
40-
{
41-
auto currNow = Clock::now();
42-
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(currNow - prevNow).count();
43-
antispam->processMessages(diff);
44-
prevNow = currNow;
45-
std::this_thread::sleep_for(50ms);
46-
}
47-
LogsDatabase.ThreadEnd();
48-
LoginDatabase.ThreadEnd();
49-
}
50-
5133
Antispam::Antispam()
5234
: m_enabled(false), m_restrictionLevel(0), m_originalNormalizeMask(0), m_fullyNormalizeMask(0),
53-
m_threshold(0), m_mutetime(0), m_chatMask(0), m_worker(), m_banEnabled(false), m_detectThreshold(3),
35+
m_threshold(0), m_mutetime(0), m_chatMask(0), m_worker(nullptr), m_banEnabled(false), m_detectThreshold(3),
5436
m_messageBlockSize(5), m_updateTimer(60000), m_messageRepeatCount(5), m_frequencyCount(5.0f), m_frequencyTime(6.0f),
5537
m_mergeAllWhispers(false)
5638
{
@@ -169,14 +151,14 @@ void Antispam::loadConfig()
169151
m_frequencyCount = sWorld.getConfig(CONFIG_UINT32_AC_ANTISPAM_FREQUENCY_COUNT);
170152
m_frequencyCoeff = m_frequencyCount / m_frequencyTime;
171153

172-
if (!m_worker.joinable())
173-
m_worker = std::thread(AntispamAsyncWorker, this);
154+
if (!m_worker)
155+
m_worker = new ACE_Based::Thread(new AntispamAsyncWorker(this));
174156
}
175157

176158
void Antispam::PushToMessageQueue(MessageBlock const& messageBlock)
177159
{
178160
#ifdef USE_STANDARD_MALLOC
179-
std::lock_guard<std::mutex> lock(m_messageMutex);
161+
ACE_Guard<ACE_Thread_Mutex> guard(m_messageMutex);
180162
m_messageQueue.push(messageBlock);
181163
#else
182164
m_messageQueue.push(messageBlock);
@@ -186,7 +168,7 @@ void Antispam::PushToMessageQueue(MessageBlock const& messageBlock)
186168
bool Antispam::TryPopFromMessageQueue(MessageBlock& messageBlock)
187169
{
188170
#ifdef USE_STANDARD_MALLOC
189-
std::lock_guard<std::mutex> lock(m_messageMutex);
171+
ACE_Guard<ACE_Thread_Mutex> guard(m_messageMutex);
190172
if (m_messageQueue.empty())
191173
return false;
192174
messageBlock = m_messageQueue.front();

src/game/Anticheat/Antispam/Antispam.h

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
#include <string>
55
#include <chrono>
6-
#include <thread>
76

87
#ifdef USE_STANDARD_MALLOC
9-
#include <mutex>
8+
#include "ace/Thread_Mutex.h"
109
#else
1110
#include <tbb/concurrent_queue.h>
1211
#endif
@@ -106,8 +105,8 @@ class Antispam : public AntispamInterface
106105
Antispam();
107106
~Antispam()
108107
{
109-
if (m_worker.joinable())
110-
m_worker.join();
108+
if (m_worker)
109+
m_worker->wait();
111110
}
112111

113112
void loadFromDB();
@@ -170,7 +169,7 @@ class Antispam : public AntispamInterface
170169
MutedAccountsSet m_mutedAccounts;
171170

172171
#ifdef USE_STANDARD_MALLOC
173-
std::mutex m_messageMutex;
172+
ACE_Thread_Mutex m_messageMutex;
174173
#endif
175174

176175
void PushToMessageQueue(MessageBlock const& messageBlock);
@@ -181,7 +180,33 @@ class Antispam : public AntispamInterface
181180
MessageCounters m_messageCounters[A_CHAT_TYPE_MAX];
182181
MessageRepeats m_messageRepeats[A_CHAT_TYPE_MAX];
183182

184-
std::thread m_worker;
183+
ACE_Based::Thread *m_worker;
184+
};
185+
186+
class AntispamAsyncWorker : public ACE_Based::Runnable
187+
{
188+
public:
189+
AntispamAsyncWorker(Antispam *antispam) : m_antispam(antispam)
190+
{
191+
}
192+
193+
virtual void run()
194+
{
195+
LoginDatabase.ThreadStart();
196+
LogsDatabase.ThreadStart();
197+
auto prevNow = Clock::now();
198+
while (!sWorld.IsStopped())
199+
{
200+
auto currNow = Clock::now();
201+
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(currNow - prevNow).count();
202+
m_antispam->processMessages(diff);
203+
prevNow = currNow;
204+
ACE_Based::Thread::Sleep(50);
205+
}
206+
LogsDatabase.ThreadEnd();
207+
LoginDatabase.ThreadEnd();
208+
}
209+
Antispam* m_antispam;
185210
};
186211

187212
#define sAntispam ACE_Singleton<Antispam, ACE_Null_Mutex>::instance()

0 commit comments

Comments
 (0)