bigworld源码分析(3)——dbMgr分析
dbMgr主要是玩家数据的读取和保存的,例如在bigworld源码分析(3)中,玩家在认证的时候,loginApp需要通过dbMgr来验证玩家数据是否合法,这就是针对玩家的账号数据进行查询。本篇中,我们主要针对以下几个问题来分析dbMgr工作原理。
(1) dbMgr如何验证玩家的账号合法性
(2) dbMgr是如何读取玩家的游戏数据的
(3) dbMgr如何通知baseAppMgr创建entity
(4) dbMgr是如何保存玩家的游戏数据的
1. dbMgr验证玩家账号
dbMgr验证玩家账号的调用关系,入下图所示。

(1) DataBase在收到了logOn的消息之后,进行一些条件限制和判断之后,直接调用LoginHandler处理login
oid Database::logOn( const Mercury::Address & srcAddr,
Mercury::ReplyID replyID,
LogOnParamsPtr pParams,
const Mercury::Address & addrForProxy,
bool offChannel )
{
// 判断能否进行认证
// ....... LoginHandler * pHandler =
new LoginHandler( pParams, addrForProxy, srcAddr, offChannel, replyID ); pHandler->login();
}
(2) LoginHandler的login方法如下:
void LoginHandler::login()
{
// __glenc__ TODO: See if this needs to be converted to use params
Database::instance().getIDatabase().mapLoginToEntityDBKey(
pParams_->username(), pParams_->password(), *this ); // When mapLoginToEntityDBKey() completes, onMapLoginToEntityDBKeyComplete()
// will be called.
}
从代码中可以看出是通过getIDatabase方法返回的IDataBase接口,调用其mapLoginToEntityDBKey。 那么纠结是使用了哪个IDataBase实现类呢?在DataBase的init中我们可以看出来,主要可能是3中实现IDataBase接口的类。其类图关系如下。

主要是XMLDataBase,OracleDatabase和MySqlDatabase,也就是说bigworld提供了这3种数据存取方式。其实只有2中,XML和Mysql。我们这里主要看Mysql方式。
(3) (4)(5)MapLoginToEntityDBKeyTask调用typeMapping.getLogOnMapping
void MapLoginToEntityDBKeyTask::run()
{
bool retry;
do
{
retry = false;
MySqlThreadData& threadData = this->getThreadData();
try
{
MySqlTransaction transaction( threadData.connection );
std::string actualPassword;
bool entryExists = threadData.typeMapping.getLogOnMapping( transaction,
logOnName_, actualPassword, threadData.ekey.typeID,
threadData.ekey.name );
// .....
} while (retry);
}
(6) MySqlTypeMapping::getLogOnMapping
bool MySqlTypeMapping::getLogOnMapping( MySqlTransaction& t, const std::string& logOnName,
std::string& password, EntityTypeID& typeID, std::string& recordName )
{
boundLogOnName_.setString( logOnName );
t.execute( stmtGetLogOnMapping_ );
// ......
}
可以看出来,这里的execute就是真正执行sql语句的地方,那么这个stmtGetLogOnMapping_ 是什么呢,其实就是一个简单的sql语句的封装,
MySqlTypeMapping::MySqlTypeMapping( MySql& con, const EntityDefs& entityDefs,
const char * tableNamePrefix ) :
mappings_(),
// ....
stmtGetLogOnMapping_( con, "SELECT m.password, t.bigworldID, m.recordName "
"FROM bigworldLogOnMapping m, bigworldEntityTypes t "
"WHERE m.logOnName=? and m.typeID=t.typeID" ),
在MySqlTypeMapping的构造函数中,我们可以看出stmtGetLogOnMapping_具体是一个什么样的sql语句。其实这里就是简单的将mysql业务,和sql语句直接简单做了一个映射关系,也就可以理解MySqlTypeMapping中TypeMapping的意思了,就是根据业务类型做了映射而已。
到这里,我们基本就知道dbMgr是如何对账号进行认证的了。
在MapLoginToEntityDBKeyTask::run执行完之后,会执行MapLoginToEntityDBKeyTask::onRunComplete(),那么就会在最后回调LoginHandler的onMapLoginToEntityDBKeyComplete,那么整个认证就结束了。
2. dbMgr获取玩家数据
在验证完玩家登陆login信息之后,如果成功,则需要从数据库中将玩家数据获取出来。在验证玩家登陆信息的时候,同时也从bigworldLogOnMapping 表中,将玩家的id获取到了,这个是后面读取玩家数据需要的key。具体的读取流程如下:

(1) (2)(3)(4)LoginHandler::onMapLoginToEntityDBKeyComplete中直接一层一层调用getEntity接口,没有复杂逻辑。
(5) (6)在GetEnitityTask中调用GetLogOnRecord,然后真正获取玩家数据,如果获取成功,最后再回调LoginHandler的onGetEntityCompleted
3. dbMgr通知baseAppMgr创建entity
LoginHandler::onMapLoginToEntityDBKeyComplete中,在getEntity之前,DataBase中的接口就开始通知BaseAppMgr去创建Entity了,甚至这个时候还不知道Entity能否创建成功。
void LoginHandler::onMapLoginToEntityDBKeyComplete( DatabaseLoginStatus status,
const EntityDBKey& ekey )
{
bool shouldLoadEntity = false;
bool shouldCreateEntity = false; if (status == DatabaseLoginStatus::LOGGED_ON)
{
ekey_ = ekey;
shouldLoadEntity = true;
state_ = StateWaitingForLoad;
}
.......
// 可以load玩家数据
if (shouldLoadEntity)
{
// Start "create new base" message even though we're not sure entity
// exists. This is to take advantage of getEntity() streaming properties
// into the bundle directly.
// 这个接口里就直接通知BaseAppMgr去创建玩家的entity。
pStrmDbID_ = Database::prepareCreateEntityBundle( ekey_.typeID,
ekey_.dbID, clientAddr_, this, bundle_, pParams_ ); // Get entity data
pBaseRef_ = &baseRef_;
outRec_.provideBaseMB( pBaseRef_ ); // Get entity mailbox
outRec_.provideStrm( bundle_ ); // Get entity data into bundle Database::instance().getEntity( *this );
// When getEntity() completes, onGetEntityCompleted() is called.
}
....... }
Database::prepareCreateEntityBundle函数代码。
/*
* This method inserts the "header" info into the bundle for a
* BaseAppMgrInterface::createEntity message, up till the point
* where entity properties should begin.
*
* @return If dbID is 0, then this function returns the position in the
* bundle where you should put the DatabaseID.
*/
DatabaseID* Database::prepareCreateEntityBundle( EntityTypeID typeID,
DatabaseID dbID, const Mercury::Address& addrForProxy,
Mercury::ReplyMessageHandler* pHandler, Mercury::Bundle& bundle,
LogOnParamsPtr pParams )
{
bundle.startRequest( BaseAppMgrInterface::createEntity, pHandler, ,
Mercury::DEFAULT_REQUEST_TIMEOUT + ); // 1 second extra ......
}
从这个函数和其注释,我们可以看出来,这里并没有真正的向BaseAppMgr去请求createEntity,而只是准备让它创建,具体发送这个指令,需要entity的属性准备完毕。
最后LoginHandler::sendCreateEntityMsg调用DataBase的send接口,将createEntity的消息发送给BaseAppMgr。这个中间发生了哪些事情,最后调用了sendCreateEntityMsg呢?调用关系如下图:

从上面的调用关系图可以看出,onGetEntityCompleted中可能会发生3中情况
(1) 在玩家数据不存在的情况下,需要创建新的玩家,也就是createNewEntity
(2) 需要对登陆数据进行映射,也就是setLoginMapping
(3) 需要校验玩家数据checkOutEntity
不过他们最终都是会回调到LoginHandler::sendCreateEntityMsg中!!
4. dbMgr是如何保存玩家的游戏数据的
dbMgr还有一个重要的功能就是保持玩家数据,相对于获取玩家数据,保存玩家数据流程相对更加简单,其流程如下:

DataBase中writeEntity调用WriteEntityHandler的writeEntity接口,然后后面还是调用MysqlDatabase的putEntity接口,最后回调WriteEntityHandler的finalise接口,在这个接口里面判断,是否需要返回保存结果。其代码如下:
void WriteEntityHandler::finalise( bool isOK )
{
// 如果需要返回保存结果
if (shouldReply_)
{
Mercury::ChannelSender sender( Database::getChannel( srcAddr_ ) );
sender.bundle().startReply( replyID_ );
sender.bundle() << isOK << ekey_.dbID;
} if (isOK && (flags_ & WRITE_LOG_OFF))
{
// 如果是玩家下线保存
Database::instance().onEntityLogOff( ekey_.typeID, ekey_.dbID );
} delete this;
}
至此,dbMgr的登录验证,玩家数据获取,玩家数据保存,以及如何通知baseAppMgr创建entity这4个我们一开始列出来的点,都已经基本分析清楚了
bigworld源码分析(3)——dbMgr分析的更多相关文章
- bigworld源码分析(1)—— 研究bigworld的意义和目标
对于网络游戏服务器开发熟悉的人,基本都知道bigworld引擎,此引擎包括客户端部分和服务器部分,已经有很多知名的网络游戏通过bigworld来构建游戏.我主要关注bigworld的服务器部分,它是一 ...
- ArrayList源码和多线程安全问题分析
1.ArrayList源码和多线程安全问题分析 在分析ArrayList线程安全问题之前,我们线对此类的源码进行分析,找出可能出现线程安全问题的地方,然后代码进行验证和分析. 1.1 数据结构 Arr ...
- Okhttp3源码解析(3)-Call分析(整体流程)
### 前言 前面我们讲了 [Okhttp的基本用法](https://www.jianshu.com/p/8e404d9c160f) [Okhttp3源码解析(1)-OkHttpClient分析]( ...
- Okhttp3源码解析(2)-Request分析
### 前言 前面我们讲了 [Okhttp的基本用法](https://www.jianshu.com/p/8e404d9c160f) [Okhttp3源码解析(1)-OkHttpClient分析]( ...
- Spring mvc之源码 handlerMapping和handlerAdapter分析
Spring mvc之源码 handlerMapping和handlerAdapter分析 本篇并不是具体分析Spring mvc,所以好多细节都是一笔带过,主要是带大家梳理一下整个Spring mv ...
- HashMap的源码学习以及性能分析
HashMap的源码学习以及性能分析 一).Map接口的实现类 HashTable.HashMap.LinkedHashMap.TreeMap 二).HashMap和HashTable的区别 1).H ...
- ThreadLocal源码及相关问题分析
前言 在高并发的环境下,当我们使用一个公共的变量时如果不加锁会出现并发问题,例如SimpleDateFormat,但是加锁的话会影响性能,对于这种情况我们可以使用ThreadLocal.ThreadL ...
- 物联网防火墙himqtt源码之MQTT协议分析
物联网防火墙himqtt源码之MQTT协议分析 himqtt是首款完整源码的高性能MQTT物联网防火墙 - MQTT Application FireWall,C语言编写,采用epoll模式支持数十万 ...
- Netty 源码学习——客户端流程分析
Netty 源码学习--客户端流程分析 友情提醒: 需要观看者具备一些 NIO 的知识,否则看起来有的地方可能会不明白. 使用版本依赖 <dependency> <groupId&g ...
随机推荐
- sql基本操作
SQL功能 数据查询 SELECT 数据定义 CREATE, DROP, ALTER 数据操纵 INSERT, UPDATE, DELETE 数据控制 GRANT, REVOKE 创建 ...
- php 中的常量
1.__FINE__ 返回当前常量所在的行号. 2.__FILE__ 返回文件的完整路径和文件名. 3.__FUNCTION__ 返回函数名称. 4.__CLASS__ 返回类名称. 5.__METH ...
- 深入浅出设计模式——外观模式(Facade Pattern)
模式动机引入外观角色之后,用户只需要直接与外观角色交互,用户与子系统之间的复杂关系由外观角色来实现,从而降低了系统的耦合度. 模式定义外观模式(Facade Pattern):外部与一个子系统的通信必 ...
- jQuery核心之那些有效的方法
jQuery提供了一些很有效的方法,这些方法都在$命名空间之下,对常规的编码很有帮助,完整的api详见:utilities documentation on api.jquery.com $.trim ...
- 从欧几里得距离、向量、皮尔逊系数到http://guessthecorrelation.com/
一.欧几里得距离就是向量的距离公式 二.皮尔逊相关系数反应的就是线性相关 游戏http://guessthecorrelation.com/ 的秘诀也就是判断一组点的拟合线的斜率y/x ------- ...
- hdu3652 B-number
链接 题意求能够整除和包含13的数字. 这个比较简单,保留余数及1,然后标记前面是否出现过13就行. #include <iostream> #include<cstdio> ...
- 监控 Linux Unix Solaris AIX, swap page in / swap page out
vmstat 的 pi/po si/so --监控一天 vmstat 5 17280> vmstat.txt sar -W 1.得到数据 (linux 的 /var/log/sar/saX 自带 ...
- Java 实现word 中写入文字图片的解决方案
JAVA生成WORD文件的方法目前有以下两种方式: 一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案; 一种是poi但是他的excel处理很程序 ...
- telnet: connect to address xxxxxxx: No route to host
在要连接的服务上执行iptables -F
- Web前端入门了解
Web就是指万维网,网站,Web开发的方向包括Web前端,Web后台. Web前端又分为 静态Web 和 动态Web, 静态Web就是用HTML实现的. 动态Web的实现方式有多种. 动态Web的实 ...