コード例 #1
0
        /// <summary>
        /// 开始接收消息
        /// </summary>
        private void StartRecevie()
        {
            // 记录日志信息
            MsgLog.LOG.Info("开启消息接收线程");
            // 字节数组
            byte[] byteArr = new byte[2048];

            while (this._clientSocket != null && this._clientSocket.Connected)
            {
                try
                {
                    // 接收字节数组
                    int count = this._clientSocket.Receive(byteArr);

                    if (count <= 0)
                    {
                        // 如果接收数量 <= 0,
                        // 则直接跳过!
                        continue;
                    }

                    // 创建二进制读入流
                    BinaryReader br = new BinaryReader(new BufferedStream(new MemoryStream()));
                    // 写入字节数组
                    br.BaseStream.Write(byteArr, 0, byteArr.Length);
                    br.BaseStream.Position = 0;

                    // 先读掉消息长度
                    br.ReadInt16();
                    // 再获取消息序列化 Id
                    short msgSerialUId = br.ReadInt16();

                    // 创建 GC 消息对象
                    BaseGCMsg gcMSG = MsgServ.OBJ.NewMsgObj <BaseGCMsg>(msgSerialUId);

                    if (gcMSG == null)
                    {
                        // 如果消息对象为空,
                        // 则直接跳过!
                        MsgLog.LOG.ErrorFormat(
                            "接收到 MsgSerialUId = {0} 消息, 但消息对象为空",
                            msgSerialUId
                            );
                        continue;
                    }

                    // 记录日志信息
                    MsgLog.LOG.InfoFormat(
                        "接收到 {0} 消息", gcMSG.GetType().Name
                        );

                    br.BaseStream.Position = 0;
                    // 令 GC 消息读取二进制数据
                    gcMSG.ReadFrom(br);

                    // 令 GC 消息入队
                    this._gcMsgQ.Enqueue(gcMSG);
                    // 处理 GC 消息
                    this.ProcessGCMsg();
                }
                catch (Exception ex)
                {
                    // 记录错误日志
                    MsgLog.LOG.Error(ex);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 执行 GC 消息
        /// </summary>
        public void ProcessGCMsg()
        {
            for (int i = 0; i < 8 && this._gcMsgQ.Count > 0; i++)
            {
                // 获取 GC 消息
                BaseGCMsg gcMSG = this._gcMsgQ.Dequeue();

                if (gcMSG == null)
                {
                    // 如果 GC 消息为空,
                    // 则直接退出!
                    return;
                }

                // 获取消息类型
                Type msgT = gcMSG.GetType();
                // 获取内置字典
                IDictionary <Delegate, WrappedGCMsgH> innerDict = this._handlerDict.ContainsKey(msgT) ? this._handlerDict[msgT] : null;

                if (innerDict == null)
                {
                    // 如果内置字典为空,
                    // 则直接跳过!
                    continue;
                }

                // 关键字列表
                List <Delegate> keyFuncList = null;

                foreach (Delegate keyFunc in innerDict.Keys)
                {
                    // 获取消息处理器
                    WrappedGCMsgH wrappedH = innerDict[keyFunc];

                    if (wrappedH == null ||
                        wrappedH._hRef == null)
                    {
                        // 如果消息处理器为空,
                        // 则直接跳过!
                        continue;
                    }

                    // 记录调试信息
                    MsgLog.LOG.DebugFormat(
                        "执行消息 {0}", gcMSG.GetType().Name
                        );

                    // 处理 GC 消息
                    wrappedH._hRef.DynamicInvoke(gcMSG);

                    if (!wrappedH._reusable)
                    {
                        if (keyFuncList == null)
                        {
                            keyFuncList = new List <Delegate>();
                        }

                        keyFuncList.Add(keyFunc);
                    }
                }

                foreach (Delegate keyFunc in keyFuncList)
                {
                    // 从字典中移除关键字
                    innerDict.Remove(keyFunc);
                }

                if (innerDict.Count <= 0)
                {
                    // 如果内置字典为空,
                    // 则从根字典中移除消息类型
                    this._handlerDict.Remove(msgT);
                }
            }
        }