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分析的更多相关文章

  1. bigworld源码分析(1)—— 研究bigworld的意义和目标

    对于网络游戏服务器开发熟悉的人,基本都知道bigworld引擎,此引擎包括客户端部分和服务器部分,已经有很多知名的网络游戏通过bigworld来构建游戏.我主要关注bigworld的服务器部分,它是一 ...

  2. ArrayList源码和多线程安全问题分析

    1.ArrayList源码和多线程安全问题分析 在分析ArrayList线程安全问题之前,我们线对此类的源码进行分析,找出可能出现线程安全问题的地方,然后代码进行验证和分析. 1.1 数据结构 Arr ...

  3. Okhttp3源码解析(3)-Call分析(整体流程)

    ### 前言 前面我们讲了 [Okhttp的基本用法](https://www.jianshu.com/p/8e404d9c160f) [Okhttp3源码解析(1)-OkHttpClient分析]( ...

  4. Okhttp3源码解析(2)-Request分析

    ### 前言 前面我们讲了 [Okhttp的基本用法](https://www.jianshu.com/p/8e404d9c160f) [Okhttp3源码解析(1)-OkHttpClient分析]( ...

  5. Spring mvc之源码 handlerMapping和handlerAdapter分析

    Spring mvc之源码 handlerMapping和handlerAdapter分析 本篇并不是具体分析Spring mvc,所以好多细节都是一笔带过,主要是带大家梳理一下整个Spring mv ...

  6. HashMap的源码学习以及性能分析

    HashMap的源码学习以及性能分析 一).Map接口的实现类 HashTable.HashMap.LinkedHashMap.TreeMap 二).HashMap和HashTable的区别 1).H ...

  7. ThreadLocal源码及相关问题分析

    前言 在高并发的环境下,当我们使用一个公共的变量时如果不加锁会出现并发问题,例如SimpleDateFormat,但是加锁的话会影响性能,对于这种情况我们可以使用ThreadLocal.ThreadL ...

  8. 物联网防火墙himqtt源码之MQTT协议分析

    物联网防火墙himqtt源码之MQTT协议分析 himqtt是首款完整源码的高性能MQTT物联网防火墙 - MQTT Application FireWall,C语言编写,采用epoll模式支持数十万 ...

  9. Netty 源码学习——客户端流程分析

    Netty 源码学习--客户端流程分析 友情提醒: 需要观看者具备一些 NIO 的知识,否则看起来有的地方可能会不明白. 使用版本依赖 <dependency> <groupId&g ...

随机推荐

  1. 16-underscore库(上)

    第16课 underscore库 一.介绍 Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象.他弥补了 ...

  2. 用JavaBean实现数据库的连接和关闭,在jsp页面输出数据库中student表中学生的信息

    package com.hanqi.test; import java.sql.*; public class XveSheng { Connection conn; Statement st; Re ...

  3. 【Unity3D游戏开发】NGUI之多分辨率下完美分布式协同开发 (五)

    NGUI多分辨率下完美分布式协同开发:不同分辨率下相对于屏幕坐标的Perfab数据不再丢失 NGUI多分辨率下完美分布式协同开发不同分辨率下相对于屏幕坐标的Perfab数据不再丢失 开发问题 原因分析 ...

  4. 【原创】Capture CIS利用Access数据库建立封装库说明

    1.在服务器端建立新空间,方便封装库以及数据库的归档存放 服务器路径:\\192.168.1.234\Share\STG_LIB,文件夹内容如下,其中Datesheet存放物料数据手册,Pcb_Lib ...

  5. JAVA 列表排序

    以前都通过Comparator进行排序,今天看到个例子,通过apache-common工具包进行排序,实现例子参考下面界面 http://blog.csdn.net/lizeyang/article/ ...

  6. jQuery学习笔记整理

    一.子元素选择器.:nth-child:匹配父元素下的第N个子或者奇偶元素.注意:序号是从1开始的,而eq是从0开始计数的!它匹配的是前方选择器选择到的元素的父元素下面的第几个元素.例如:ul li: ...

  7. C语言语法之运算符及优先级

    注:该内容整理自以下链接. http://blog.csdn.net/huangblog/article/details/8271791 表:C语言运算符及优先级 优先级 运算符 名称或含义 使用形式 ...

  8. MFC编程入门之十一(对话框:模态对话框及其弹出过程)

    加法计算器对话框程序大家照着做一遍后,相信对基于对话框的程序有了些解了,有个好的开始对于以后的学习大有裨益.趁热打铁,这一节讲讲什么是对话框和非模态对话框,以及模态对话框怎样弹出. 一.模态对话框和非 ...

  9. 【转】 void与void*详解

    void关键字的使用规则: 1. 如果函数没有返回值,那么应声明为void类型: 2. 如果函数无参数,那么应声明其参数为void: 3. 如果函数的参数可以是任意类型指针,那么应声明其参数为void ...

  10. Maven内置变量说明

    Maven内置变量说明: ${basedir} 项目根目录 ${project.build.directory} 构建目录,缺省为target ${project.build.outputDirect ...