mirror of
https://github.com/mangosfour/server.git
synced 2025-12-27 10:37:02 +00:00
[10970] Implement mass mail send infrastructure.
It expected to be used in 2 case: some gameevent must send mails at start/end,
and this can be useful in game commands. Both case wil implemented in later commits.
* New MassMailMgr can accept tasks for send mass mails in safe way for map update threads context/etc.
* It work in way:
- By provided race mask or more generic SQL query string in async query selected affected characters
- At query result ready at next world tick update in safe common part of tick code some from mails
from queued mas mail tasks send.
- Amount mails limited MassMailer.SendPerTick confir option (10 by default). This done for prevent
high server load/lags at send too many mails in one tick (mail send all existed characters in DB
who match to seelction criteria)
- Manager not persistant for server shutdowns so any not send mails in queue lost at shutdown.
But with default setting 10K mail send in 20 secs (10000/50/10). Adding more safe execution
for this case will make related code lot more slow and req. many DB tables and code support.
This commit is contained in:
parent
231c6d77ce
commit
5f2aef756a
13 changed files with 331 additions and 2 deletions
134
src/game/MassMailMgr.cpp
Normal file
134
src/game/MassMailMgr.cpp
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup mailing
|
||||
* @{
|
||||
*
|
||||
* @file MassMailMgr.cpp
|
||||
* This file contains the the code needed for MaNGOS to handle mass mails send in safe and perfomence not affecting way.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "MassMailMgr.h"
|
||||
#include "Policies/SingletonImp.h"
|
||||
#include "Database/DatabaseEnv.h"
|
||||
#include "Database/DatabaseImpl.h"
|
||||
#include "SharedDefines.h"
|
||||
#include "World.h"
|
||||
#include "ObjectMgr.h"
|
||||
|
||||
INSTANTIATE_SINGLETON_1(MassMailMgr);
|
||||
|
||||
void MassMailMgr::AddMassMailTask(MailDraft* mailProto, MailSender sender, uint32 raceMask)
|
||||
{
|
||||
if (RACEMASK_ALL_PLAYABLE & ~raceMask) // have races not included in mask
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss << "SELECT guid FROM characters WHERE (1 << (race - 1)) & " << raceMask << " AND deleteDate IS NULL";
|
||||
AddMassMailTask(mailProto, sender, ss.str().c_str());
|
||||
}
|
||||
else
|
||||
AddMassMailTask(mailProto, sender, "SELECT guid FROM characters WHERE deleteDate IS NULL");
|
||||
}
|
||||
|
||||
struct MassMailerQueryHandler
|
||||
{
|
||||
void HandleQueryCallback(QueryResult * result, MailDraft* mailProto, MailSender sender)
|
||||
{
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
MassMailMgr::ReceiversList& recievers = sMassMailMgr.AddMassMailTask(mailProto, sender);
|
||||
|
||||
do
|
||||
{
|
||||
Field *fields = result->Fetch();
|
||||
recievers.insert(fields[0].GetUInt32());
|
||||
|
||||
} while (result->NextRow());
|
||||
delete result;
|
||||
}
|
||||
} massMailerQueryHandler;
|
||||
|
||||
void MassMailMgr::AddMassMailTask(MailDraft* mailProto, MailSender sender, char const* query)
|
||||
{
|
||||
CharacterDatabase.AsyncPQuery(&massMailerQueryHandler, &MassMailerQueryHandler::HandleQueryCallback, mailProto, sender, query);
|
||||
}
|
||||
|
||||
void MassMailMgr::Update()
|
||||
{
|
||||
if (m_massMails.empty())
|
||||
return;
|
||||
|
||||
uint32 maxcount = sWorld.getConfig(CONFIG_UINT32_MASS_MAILER_SEND_PER_TICK);
|
||||
|
||||
do
|
||||
{
|
||||
MassMail& task = m_massMails.front();
|
||||
|
||||
while (!task.m_recivers.empty() && maxcount > 0)
|
||||
{
|
||||
uint32 receiver_lowguid = *task.m_recivers.begin();
|
||||
task.m_recivers.erase(task.m_recivers.begin());
|
||||
|
||||
ObjectGuid receiver_guid = ObjectGuid(HIGHGUID_PLAYER, receiver_lowguid);
|
||||
Player *receiver = sObjectMgr.GetPlayer(receiver_guid);
|
||||
|
||||
// last case. can be just send
|
||||
if (task.m_recivers.empty())
|
||||
{
|
||||
// prevent mail return
|
||||
task.m_protoMail->SendMailTo(MailReceiver(receiver, receiver_guid), task.m_sender, MAIL_CHECK_MASK_RETURNED);
|
||||
|
||||
--maxcount;
|
||||
break;
|
||||
}
|
||||
|
||||
// need clone draft
|
||||
MailDraft draft;
|
||||
draft.CloneFrom(*task.m_protoMail);
|
||||
|
||||
// prevent mail return
|
||||
draft.SendMailTo(MailReceiver(receiver, receiver_guid), task.m_sender, MAIL_CHECK_MASK_RETURNED);
|
||||
|
||||
--maxcount;
|
||||
}
|
||||
|
||||
if (task.m_recivers.empty())
|
||||
m_massMails.pop_front();
|
||||
}
|
||||
while(!m_massMails.empty() && maxcount > 0);
|
||||
}
|
||||
|
||||
void MassMailMgr::GetStatistic(uint32& tasks, uint32& mails, uint32& needTime) const
|
||||
{
|
||||
tasks = m_massMails.size();
|
||||
|
||||
uint32 mailsCount = 0;
|
||||
for (MassMailList::const_iterator mailItr = m_massMails.begin(); mailItr != m_massMails.end(); ++mailItr)
|
||||
mailsCount += mailItr->m_recivers.size();
|
||||
|
||||
mails = mailsCount;
|
||||
|
||||
// 50 msecs is tick length
|
||||
needTime = 50 * mailsCount / sWorld.getConfig(CONFIG_UINT32_MASS_MAILER_SEND_PER_TICK) / IN_MILLISECONDS;
|
||||
}
|
||||
|
||||
|
||||
/*! @} */
|
||||
Loading…
Add table
Add a link
Reference in a new issue