예제 #1
0
        /// <summary>
        /// 从消息中心获取消息的回调函数
        /// </summary>
        protected virtual void DispatchMessage(Protos.MsgID msgID, IMessage message)
        {
            MessageHandler handler = this._messageCenter.GetHandler(msgID);

            if (handler != null)
            {
                ErrorCode errorCode = handler.Invoke(this, message);
                if (errorCode != ErrorCode.Success)
                {
                    Logger.Warn(errorCode);
                }
            }
            else
            {
                Logger.Warn($"unhandle msg:{msgID}.");
            }
        }
예제 #2
0
 internal MessageHandler GetHandler(Protos.MsgID msgID)
 {
     this._handlers.TryGetValue(msgID, out MessageHandler handler);
     return(handler);
 }
예제 #3
0
 internal void Unregister(Protos.MsgID msgID) => this._handlers.Remove(msgID);
예제 #4
0
 internal void Register(Protos.MsgID msgID, MessageHandler handler) => this._handlers[msgID] = handler;
예제 #5
0
 protected MessageHandler GetMsgHandler(Protos.MsgID msgID) => this._messageCenter.GetHandler(msgID);
예제 #6
0
 protected void RegMsgHandler(Protos.MsgID msgID, MessageHandler handler) => this._messageCenter.Register(msgID, handler);
예제 #7
0
 /// <summary>
 /// 是否需要阻断该消息
 /// 子类可重写该方法以判断该消息是否合法
 /// </summary>
 protected virtual bool ShouldBlockMsg(Protos.MsgID msgID) => false;
예제 #8
0
        /// <summary>
        /// 接收消息后的处理函数
        /// </summary>
        /// <param name="data">接收的数据</param>
        /// <param name="offset">数据偏移量</param>
        /// <param name="size">数据长度</param>
        protected override void OnRecv(byte[] data, int offset, int size)
        {
            int len = data.Length;

            if (len - offset < sizeof(int))
            {
                Logger.Warn("invalid msg.");
                return;
            }
            //剥离消息ID
            Protos.MsgID msgID = (Protos.MsgID)ByteUtils.Decode32i(data, offset);
            //判断是否需要阻断该消息
            if (this.ShouldBlockMsg(msgID))
            {
                return;
            }
            offset += sizeof(int);
            size   -= offset;

            //解码消息
            IMessage message;

            try
            {
                message = ProtoCreator.DecodeMsg(msgID, data, offset, size);
            }
            catch (Exception e)
            {
                Logger.Warn($"decode message failed:{e}");
                return;
            }
            //检查第一个字段是否Protos.MsgOpts类型
            Protos.MsgOpts opts = message.GetMsgOpts();
            System.Diagnostics.Debug.Assert(opts != null, "invalid msg options");

            if ((opts.Flag & (1 << ( int )Protos.MsgOpts.Types.Flag.Trans)) > 0)                   //这是一条转发消息
            {
                //去掉转发标记
                //opts.Flag &= ~( uint ) Protos.MsgOpts.Types.Flag.Trans;

                //后2位为转发目标
                Protos.MsgOpts.Types.TransTarget transTarget = (Protos.MsgOpts.Types.TransTarget)((opts.Flag >> 4) & 0xf);
                //如果不是转发的目标
                if (!this.owner.IsTransTarget(transTarget))
                {
                    this.TransMsg(transTarget, opts.Transid, message);
                    return;
                }
            }
            //是否RPC消息
            if ((opts.Flag & (1 << ( int )Protos.MsgOpts.Types.Flag.Resp)) > 0)
            {
                if (this._pidToRPCEntries.TryGetValue(opts.Rpid, out RPCEntry rpcEntry))
                {
                    this._pidToRPCEntries.Remove(opts.Rpid);
                    this._rpcEntries.Remove(rpcEntry);
                    rpcEntry.handler(this, message, rpcEntry.args);                      //调用回调函数
                    RPCEntry.Push(rpcEntry);
                }
                else
                {
                    Logger.Warn($"RPC handler not found with message:{msgID}");
                }
            }
            else
            {
                this.DispatchMessage(msgID, message);
            }
        }