Core/Threading: Fixed possible data races after making auth queries async by moving their processing to boost::asio::io_service::run threads

This commit is contained in:
Shauren
2015-06-21 02:13:08 +02:00
parent 4093eee19b
commit f6bc87fe5d
5 changed files with 21 additions and 13 deletions

View File

@@ -17,7 +17,6 @@
#include "RBAC.h"
#include "AccountMgr.h"
#include "DatabaseEnv.h"
#include "Log.h"
namespace rbac

View File

@@ -40,7 +40,7 @@
#ifndef _RBAC_H
#define _RBAC_H
#include "Define.h"
#include "DatabaseEnv.h"
#include <string>
#include <set>
#include <map>

View File

@@ -77,8 +77,11 @@ void WorldSocket::Start()
stmt->setString(0, ip_address);
stmt->setUInt32(1, inet_addr(ip_address.c_str()));
_queryCallback = std::bind(&WorldSocket::CheckIpCallback, this, std::placeholders::_1);
_queryFuture = LoginDatabase.AsyncQuery(stmt);
{
std::lock_guard<std::mutex> guard(_queryLock);
_queryCallback = io_service().wrap(std::bind(&WorldSocket::CheckIpCallback, this, std::placeholders::_1));
_queryFuture = LoginDatabase.AsyncQuery(stmt);
}
}
void WorldSocket::CheckIpCallback(PreparedQueryResult result)
@@ -124,7 +127,7 @@ bool WorldSocket::Update()
return false;
{
std::lock_guard<std::mutex> lock(_queryLock);
std::lock_guard<std::mutex> guard(_queryLock);
if (_queryFuture.valid() && _queryFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
auto callback = std::move(_queryCallback);
@@ -584,14 +587,17 @@ struct AccountInfo
void WorldSocket::HandleAuthSession(std::shared_ptr<WorldPackets::Auth::AuthSession> authSession)
{
// Client switches packet headers after sending CMSG_AUTH_SESSION
_headerBuffer.Resize(SizeOfClientHeader[1][1]);
// Get the account information from the auth database
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_INFO_BY_NAME);
stmt->setInt32(0, int32(realmHandle.Index));
stmt->setString(1, authSession->Account);
{
std::lock_guard<std::mutex> lock(_queryLock);
_queryCallback = std::bind(&WorldSocket::HandleAuthSessionCallback, this, authSession, std::placeholders::_1);
std::lock_guard<std::mutex> guard(_queryLock);
_queryCallback = io_service().wrap(std::bind(&WorldSocket::HandleAuthSessionCallback, this, authSession, std::placeholders::_1));
_queryFuture = LoginDatabase.AsyncQuery(stmt);
}
}
@@ -622,7 +628,6 @@ void WorldSocket::HandleAuthSessionCallback(std::shared_ptr<WorldPackets::Auth::
// even if auth credentials are bad, try using the session key we have - client cannot read auth response error without it
_authCrypt.Init(&account.Game.SessionKey);
_headerBuffer.Resize(SizeOfClientHeader[1][1]);
// First reject the connection if packet contains invalid data or realm state doesn't allow logging in
if (sWorld->IsClosed())
@@ -759,7 +764,7 @@ void WorldSocket::HandleAuthSessionCallback(std::shared_ptr<WorldPackets::Auth::
if (wardenActive)
_worldSession->InitWarden(&account.Game.SessionKey, account.BattleNet.OS);
_queryCallback = std::bind(&WorldSocket::LoadSessionPermissionsCallback, this, std::placeholders::_1);
_queryCallback = io_service().wrap(std::bind(&WorldSocket::LoadSessionPermissionsCallback, this, std::placeholders::_1));
_queryFuture = _worldSession->LoadPermissionsAsync();
}
@@ -781,13 +786,16 @@ void WorldSocket::HandleAuthContinuedSession(std::shared_ptr<WorldPackets::Auth:
return;
}
// Client switches packet headers after sending CMSG_AUTH_CONTINUED_SESSION
_headerBuffer.Resize(SizeOfClientHeader[1][1]);
uint32 accountId = PAIR64_LOPART(authSession->Key);
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_INFO_CONTINUED_SESSION);
stmt->setUInt32(0, accountId);
{
std::lock_guard<std::mutex> lock(_queryLock);
_queryCallback = std::bind(&WorldSocket::HandleAuthContinuedSessionCallback, this, authSession, std::placeholders::_1);
std::lock_guard<std::mutex> guard(_queryLock);
_queryCallback = io_service().wrap(std::bind(&WorldSocket::HandleAuthContinuedSessionCallback, this, authSession, std::placeholders::_1));
_queryFuture = LoginDatabase.AsyncQuery(stmt);
}
}
@@ -808,7 +816,6 @@ void WorldSocket::HandleAuthContinuedSessionCallback(std::shared_ptr<WorldPacket
k.SetHexStr(fields[1].GetCString());
_authCrypt.Init(&k, _encryptSeed.AsByteArray().get(), _decryptSeed.AsByteArray().get());
_headerBuffer.Resize(SizeOfClientHeader[1][1]);
SHA1Hash sha;
sha.UpdateData(login);

View File

@@ -142,7 +142,7 @@ private:
std::mutex _queryLock;
PreparedQueryResultFuture _queryFuture;
std::function<void(PreparedQueryResult)> _queryCallback;
std::function<void(PreparedQueryResult&&)> _queryCallback;
std::string _ipCountry;
};

View File

@@ -169,6 +169,8 @@ protected:
MessageBuffer _writeBuffer;
#endif
boost::asio::io_service& io_service() { return _socket.get_io_service(); }
private:
void ReadHandlerInternal(boost::system::error_code error, size_t transferredBytes)
{