【分析总结框架记录】异步操作

对于游戏开发而言,经常使用到异步相关操作,因此在使用moduleframework之前应该提供一个异步操作的类。 以后相关的异步操作均继承这个类

AsyncOperation:

先贴出代码:

  1. #ifndef __ASYNC_OPERATION_H__  
  2. #define __ASYNC_OPERATION_H__  
  3. #include <boost/function.hpp>  
  4.   
  5. class AsyncOperation  
  6. {  
  7. public:  
  8.     typedef boost::function(void<AsyncOperation*>) TypeOnExecute;  
  9.     typedef boost::function(void<AsyncOperation*>) TypeOnComplete;  
  10.     typedef boost::function(void<AsyncOperation*>) TypeOnAborted;  
  11.   
  12.     // 委托执行函数  
  13.     explicit AsyncOperation(TypeOnExecute onExecute)  
  14.         :_onExecute(onExecute)  
  15.     {  
  16.         if(!_onExecute) throw "onExecute is NULL";  
  17.     }  
  18.   
  19.     virtual AsyncOperation(){}  
  20.   
  21.     void Execute(TypeOnComplete onComplete, TypeOnAborted onAorted = NULL)  
  22.     {  
  23.         if (!_onComplete) throw "onComplete is NULL"  
  24.         _onComplete = onComplete;  
  25.         _onAborted = onAorted;  
  26.         _onExecute(this);  
  27.     }  
  28.   
  29.     virtual void Abort()  
  30.     {  
  31.   
  32.     }  
  33.   
  34.     void Complete()  
  35.     {  
  36.         _onComplete(this);  
  37.     }  
  38.   
  39. private:  
  40.     TypeOnExecute _onExecute;  
  41.     TypeOnComplete _onComplete;  
  42.     TypeOnAborted _onAborted;  
  43.   
  44. };  
  45. #endif  

真正的执行函数是Execute 另外提供了一个完成 和终止的接口。

这个类应该比较简单。

在此类的基础上构建一个异步队列。  AsyncQueue继承该类

  1. #ifndef __ASYNC_QUEUE_H__  
  2. #define __ASYNC_QUEUE_H__  
  3.   
  4. #include <glog/logging.h>  
  5. #include "AsyncOpertaion.hpp"  
  6. #include <boost/bind.hpp>  
  7. #include <deque>  
  8.   
  9. using namespace std;  
  10.   
  11. class AsyncQueue : public AsyncQueue  
  12. {  
  13. public:  
  14.     // 构造函数默认调用ExecuteNextChild来启动整个异步进程  
  15.     AsyncQueue(): AsyncOperation(boost::bind(&AsyncQueue::ExecuteNextChild, this, _1))  
  16.         , _abortFlag(false)  
  17.     {  
  18.   
  19.     }  
  20.     ~AsyncQueue()  
  21.     {  
  22.         TypeOptQueue::iterator it = _queue.begin();  
  23.         for (; it != _queue.end(); ++it)  
  24.         {  
  25.             delete *it;  
  26.         }  
  27.     }  
  28.   
  29.     // 加入队列  
  30.     void Push(AsyncOperation* operation)  
  31.     {  
  32.         _queue.push_back(operation);  
  33.     }  
  34.   
  35.     // 终止执行  
  36.     void Abort()  
  37.     {  
  38.         if(_queue.empty()) return;  
  39.         _abortFlag = true;  
  40.         AsyncOperation* front = _queue.front();  
  41.         front->Abort(); // 终止队列首位的执行  
  42.     }  
  43.   
  44. private:  
  45.     void ExecuteNextChild(AsyncOperation* operation)  
  46.     {  
  47.         if(_queue.empty())  
  48.         {  
  49.             LOG(ERROR) << "Async Queue is Empty!";  
  50.             return;  
  51.         }  
  52.         // 迭代执行_queue中的操作  
  53.         AsyncOperation* front = _queue.front();  
  54.         front->Execute(boost::bind(&AsyncQueue::OnChildComplete, this, _1));  
  55.     }  
  56.   
  57.     void OnChildComplete(AsyncOperation* operation)  
  58.     {  
  59.         // 移除已执行的异步操作  
  60.         _queue.pop_front();  
  61.         delete operation;  
  62.         if (_queue.empty())  
  63.         {  
  64.             // 通知队列执行完成  
  65.             Complete();  
  66.             return;  
  67.         }  
  68.   
  69.         if (_abortFlag && _onAborted)  
  70.         {  
  71.             _onAborted(this);  
  72.         }else  
  73.         {  
  74.             // 迭代执行  
  75.             ExecuteNextChild(0);  
  76.         }  
  77.     }  
  78.   
  79. private:  
  80.     typedef deque<AsyncOperation*> TypeOptQueue; // 使用队列来包装  
  81.     TypeOptQueue _queue;  
  82.     bool _abortFlag;   
  83. };  
  84.   
  85. #endif  

这里一个巧妙指出在于 如果迭代执行整个异步队列, 注意看 构造函数和OnExecuteNextChild 这两点 。  


至此我们构建了一个初步的异步队列。以后其他各个模块均继承他们。 比如模块的初始化等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值