游戏服务器之防加速器

加速器是网页类游戏常使用的通过修改前端帧频率来达到加速操作目的的工具,常用的有游戏浏览器等。所以前端的时间会变得很快或者很慢(跟后端的比较)。


防加速器设计上:

在网关服务器里限制连接发来(客户端发来)的移动系统里的角色移动消息,
根据消息的时间戳跟服务器的时间相比,时间相差较大就踢掉.前后端需要在一定时间内同步一次

时间上,每60秒同步一次后端时间到前端,设定为加速不超过9秒(9/60 ,即加速15%),减速不超过15秒(15/60,即减速25%,因为有网络延时,所以减速的会放宽些)。


本文代码内容如下:

网关连接处理客户端的移动消息

[cpp]  view plain  copy
  1. bool gateway_session::msgParse(const MSG::base_msg *ptNull, const unsigned int msglen)  
  2. {  
  3. ...  
  4. case MOVE_USERCMD://移动指令  
  5. {  
  6.     if (!testRunState(gateway_session::RunState_Play))  
  7.     {  
  8.         return true;  
  9.     }  
  10.     PRINT_MSG_LOG(ptrMsg, 0);  
  11.     switch (ptrMsg->second)  
  12.     {  
  13.     case USERMOVE_MOVE_USERCMD_PARA_C:  
  14.         {  
  15.       if(msglen != sizeof(stplayerMoveUpMoveplayerCmd))  
  16.       {  
  17.           g_log->error("消息长度有误:%u,%u",this->id,msglen);  
  18.           return false;  
  19.       }  
  20.             MSG::stplayerMoveUpMoveplayerCmd send = *(MSG::stplayerMoveUpMoveplayerCmd*) ptrMsg;  
  21.             int timeDiff = int((int)send.dwTimestamp - (int)main_logic_thread::currentTime.sec());  
  22.             if (timeDiff > 9 || timeDiff < -15)//相差时间过大则断开网关的客户端连接(单位秒)  
  23.             {  
  24.                 error_log("加速器:时间相差过大(%u,%llu),(帐号%s,角色(%u,%s),ip:%s)",  
  25.                         send.dwTimestamp,main_logic_thread::currentTime.sec(),  
  26.                         account,pplayer->id,pplayer->name,getIP());  
  27.                 TerminateWait();  
  28.                 return false;  
  29.             }  
  30.             return forwardScene(&send, sizeof(send));  
  31.         }  
  32.         break;  
  33.         ...  
  34.     }  
  35.     ...  
  36. }  
  37. ...  
  38. }  

前后端需要在一定时间内同步一次

[cpp]  view plain  copy
  1. //发送同步时间消息  
  2. struct gateway_session_time_sync: public callback<gateway_session>  
  3. {  
  4.     bool invoke(gateway_session *entry)  
  5.     {  
  6.         MSG::stGameTimeTimerplayerCmd cmd;  
  7.         cmd.qwGameTime = (uint32)(main_logic_thread::currentTime.sec());   
  8.         entry->sendmsg(&cmd,sizeof(cmd));  
  9.         return true;  
  10.     }  
  11. }timesync;  


主线程循环定时同步后端时间到所有连接的前端


[cpp]  view plain  copy
  1. void main_logic_thread::run()  
  2. {  
  3.     while(!isFinal())  
  4.     {  
  5.         setRunning();  
  6.         main_logic_thread::currentTime.now();  
  7.         thread_base::msleep(50);  
  8.         g_server.handle_msg();  
  9.         if(_one_sec(main_logic_thread::currentTime))    
  10.         {  
  11.             g_gateway_session_manager.invoke_all_task(timeout);  
  12.               
  13.             if(_one_min(main_logic_thread::currentTime))  
  14.       {  
  15.                 g_gateway_session_manager.invoke_all_task(timesync);  
  16.       }  
  17.      ...  
  18. }  

转自:http://blog.csdn.net/chenjiayi_yun/article/details/25000785

 plain

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值