예제 #1
0
        /// <summary>
        /// 接收处理
        /// </summary>
        /// <param name="net"></param>
        /// <param name="session"></param>
        /// <param name="byData"></param>
        /// <param name="length"></param>
        /// <param name="offset"></param>
        protected void ReceiveHandle(ENet net, object session, byte[] byData, int length, int offset)
        {
            NetAdapter adapter = GetAdapter(net, session);
            if (adapter == null)
                return;

            // 这里处理接收消息,转化成Message
            if (byData == null || byData.Length < (length + offset) || length < sizeof(short) + sizeof(int))
                return;

            MemBlock block = NetAdapter.Alloc(length);
            Buffer.BlockCopy(byData, offset, block.GetBytes(), 0, length);

            MessageCache cache = new MessageCache();
            cache.AdapterID = adapter.AdapterID;
            cache.Block = block;

            PushMessageQueue(cache);
        }
예제 #2
0
        /// <summary>
        /// 读取消息
        /// </summary>
        /// <param name="msgCache"></param>
        /// <returns></returns>
        protected Message ReadMessage(MessageCache msgCache)
        {
            Message msg = null;
            try
            {
                int length = BitConverter.ToInt32(msgCache.Block.GetBytes(), 0);
                int msgId = BitConverter.ToInt32(msgCache.Block.GetBytes(), sizeof(int));
                msg = CreateMessage(msgId);
                OnCreateMessage(msg, msgCache.Block.UseSize);
                Buffer.BlockCopy(msgCache.Block.GetBytes(), 0, mReadMessage.GetBytes(), 0, msgCache.Block.UseSize);
                msg.SetByteArray(mReadMessage);
                msg.ParseObj();
            }
            catch(Exception ex)
            {
                Logger.GetLog("FrameWork").Error(ex.ToString());
            }

            return msg;
        }
예제 #3
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="msgCache"></param>
        protected void ProcessMessage(MessageCache msgCache)
        {
            Message msg = ReadMessage(msgCache);
            if (msg == null)
                return;

            try
            {
                TerminalFunction.LogMessage(msg.MsgId);
                DispatchMessage(msgCache.AdapterID, msg);
                OnDispatchMessage(msg);
                OnFreeMessage(msg);
            }
            catch(Exception ex)
            {
                Logger.GetLog("FrameWork").Error(ex.ToString());
            }
        }
예제 #4
0
        /// <summary>
        /// 添加消息队列
        /// </summary>
        /// <param name="msgCache"></param>
        protected void PushMessageQueue(MessageCache msgCache)
        {
            #if !UNITY_IPHONE
            while (true) {
                if(Interlocked.CompareExchange(ref lockMark,1,0) == 0)
                {
                    mMsgQueue.Enqueue(msgCache);
                    Interlocked.Decrement(ref lockMark);
                    break;
                }else
                {
                    Thread.Sleep(50);
                }

            }
            #else
            lock (mMsgQueueLock)
            {
                mMsgQueue.Enqueue(msgCache);
            }
            #endif
        }
예제 #5
0
 /// <summary>
 /// 释放消息
 /// </summary>
 /// <param name="msgCache"></param>
 protected void FreeMessage(MessageCache msgCache)
 {
     //NetAdapter.Free(msgCache.Block);
 }