Ejemplo n.º 1
0
        /// <summary>
        /// Palyer Timer工作
        /// </summary>
        /// <param name="cancelToken">控制取消对象</param>
        /// <returns></returns>
        private async Task PlayerCtxTimerWorkerProc(CancellationToken cancelToken)
        {
            // 检查工作状态
            if (1 != m_state)
            {
                return;
            }
            try
            {
                //Int64 currentTimeTick = 0;

                // 没有收到取消请求时
                while (!cancelToken.IsCancellationRequested)
                {
                    //// 获取开始时间毫秒
                    //currentTimeTick = DateTime.Now.Ticks;

                    //// 用当前时间秒查询循环容器
                    //IEnumerable<KeyValuePair<Int64, PlayerTimerNode>> query =
                    //    m_playerCtxTimers.Where(timer => timer.Value.m_nextCallTime <= currentTimeTick);

                    // 有满足条件的作timer call处理以及删除
                    foreach (KeyValuePair <Int64, PlayerTimerNode> timerPair in GetTriggerableTimerNodeList())
                    {
                        // 先删除
                        PlayerTimerNode playerTimer = null;
                        m_playerCtxTimers.TryRemove(timerPair.Key, out playerTimer);

                        if (playerTimer == null)
                        {
                            continue;
                        }

                        // 查找目标playerctx
                        IManagedContext playerCtx = m_playerCtxManager.FindPlayerContextBySid(playerTimer.m_playerSid);
                        if (playerCtx == null)
                        {
                            continue;
                        }

                        // 发送msg
                        PlayerTimerMessage message = new PlayerTimerMessage(playerTimer.m_int32Data, playerTimer.m_int64Data, playerTimer.m_objData);
                        playerCtx.PostLocalMessage(message);
                    }

                    // 每500毫秒轮询一次
                    await Task.Delay(500);
                }
                Log.Debug("TimerManager::PlayerCtxTimerWorkerProc stop");
            }
            catch (Exception ex)
            {
                Log.Debug("TimerManager::PlayerCtxTimerWorkerProc catch Execption: " + ex.ToString());
            }
        }
 /// <summary>
 /// 玩家现场timer回调
 /// </summary>
 /// <param name="msg"></param>
 /// <returns></returns>
 protected virtual Task OnPlayerContextTimer(PlayerTimerMessage msg)
 {
     if (msg == null)
     {
         Log.Error("PlayerContextBase::OnPlayerContextTimer, but lmsg checked failed");
         return(Task.CompletedTask);
     }
     Log.Debug("PlayerContextBase::OnPlayerContextTimer, MessageId = " +
               msg.MessageId.ToString());
     return(Task.CompletedTask);
 }
        /// <summary>
        /// 消息都走这里,从网络来的消息 或者是内部产生的消息 都走这里
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public virtual async Task OnMessage(ILocalMessage msg)
        {
            //在这里将处理一系列消息

            switch (msg.MessageId)
            {
            //AsyncAction消息的回调
            case ServerBaseLocalMesssageIDDef.LocalMsgAsyncActionResult:
                ContextAsyncAction contextAsyncAction = msg as ContextAsyncAction;
                contextAsyncAction.OnResultInternal();    //执行回调

                break;

            case ServerBaseLocalMesssageIDDef.NetMessage:                    //如何是网络消息就通过分发器进行分发
                NetClientMessage message = msg as NetClientMessage;
                MessageDispather.Instance.Handle(this, message.MessageInfo); //在这里将消息进行分发  直接调用逻辑handler
                break;

            case ServerBaseLocalMesssageIDDef.RpcNetMessage:
                RpcNetClientMessage rpcmessage = msg as RpcNetClientMessage;



                var response = (IResponse)rpcmessage.MessageInfo.Message;
                if (response == null)
                {
                    throw new Exception($"flag is response, but message is not! {rpcmessage.MessageInfo.Opcode}");
                }
                Action <IResponse> action;
                if (!m_requestCallback.TryGetValue(response.RpcId, out action))
                {
                    return;
                }
                m_requestCallback.Remove(response.RpcId);

                action(response);    //这里处理逻辑 返回await


                break;

            case ServerBaseLocalMesssageIDDef.LocalMsgPlayCtxTimer:    //现场计时任务回调感知
            {
                PlayerTimerMessage timerMsg = msg as PlayerTimerMessage;
                await OnPlayerContextTimer(timerMsg);

                return;
            }

            case ServerBaseLocalMesssageIDDef.SystemSendNetMessage:

                var snm = (SystemSendNetMessage)msg;
                if (snm.Message == null)
                {
                    Log.Debug("要发送的网络消息为空");
                    return;
                }
                //Log.Info("系统向玩家发送消息 " + snm.PlayerId + "  " + snm.Message.GetType());
                //S2C_SyncLevelStateBattleMessage syncLevelStateBattleMessage = snm.Message as S2C_SyncLevelStateBattleMessage;
                //if (syncLevelStateBattleMessage != null)
                //{
                //    Log.Info("服务器转发延迟 = "+(DateTime.Now.Ticks - syncLevelStateBattleMessage.Time)/10000);
                //}
                Send(snm.Message);


                break;

            default: break;
            }


            //本地消息和网络消息分开

            //收到的如果是网络消息就通过分发器进行分发 分发器由ServerBase维护

            //收到的是本地消息就在本地进行处理


            return;
        }