|
Feb 26
|
Mangos源码总体结构
Mangos源码分析
Views: 6440 |
No Comments »
|
总体结构
类 World 是游戏世界的表示, World 中为每一个成功登录的用户维护一个 WorldSession. World 通过 WorldSocket 和客户端连接. 所以, 每一个用户对应一个 WorldSession, 一个 WorldSocket.
服务器主循环
1 2 3 4 5 6 7 8 | /// Heartbeat for the World void WorldRunnable::run(){ ///- While we have not World::m_stopEvent, update the world while (!World::m_stopEvent){ // 更新 World 中的所有 WorldSession, 也即更新角色的状态. sWorld.Update( diff ); } } |
服务器半同步半异步地处理客户端的指令(Packet)
从 WorldSocket 中读取的 Packet 并不被立即处理, 而是放入 WorldSocket 所对应的 WorldSession 的指令队列中, 在 World 的 Update 执行时才处理. 因为这种半同步半异步的方式, 指令队列要被加锁. Mangos 使用 ZThread::FastMutex 进行加锁.
不过, 从 WorldSocket 读出的两种指令会被立即处理: CMSG_PING 和 CMSG_AUTH_SESSION. 也就是 Ping 和 登录指令.
处理用户移动
WorldSession::HandleMovementOpcodes() 函数处理 opcode 为 MSG_MOVE_XXX 类的指令, 更新对应角色的状态, 并进行广播. 广播函数为 Map::MessageBroadcast().

Recent Comments