Beispiel #1
0
        /// <summary>
        /// 分发消息,异步,会在协程BeginHandleReceiveMessageQueue的下一次检查中进行真正的消息分发
        /// </summary>
        /// <param name="iMessageType">消息类型</param>
        /// <param name="kParam">附加参数</param>
        public void DispatchMessageAsync(uint iMessageType, object kParam = null)
        {
            lock (_receiveMessageQueue)
            {
                MessageArgs args = new MessageArgs()
                {
                    iMessageType = iMessageType,
                    kParam       = kParam,
                };

                _receiveMessageQueue.Enqueue(args);
            }
        }
Beispiel #2
0
        private IEnumerator BeginHandleReceiveMessageQueue()
        {
            while (true)
            {
                yield return(0);

                lock (_receiveMessageQueue)
                {
                    while (_receiveMessageQueue.Count != 0)
                    {
                        MessageArgs args = _receiveMessageQueue.Dequeue();
                        DispatchMessage(args.iMessageType, args.kParam);
                    }
                }
            }
        }
        private void EndReceivePacket(IAsyncResult ar)
        {
            int bytesRead = -1;

            try
            {
                if (IsConncted)
                {
                    lock (_socket)
                    {
                        bytesRead = _socket.EndReceive(ar);
                    }
                }

                if (bytesRead == -1)
                {
                    CloseConnection();
                    return;
                }
            }
            catch (ObjectDisposedException)
            {
                Log4U.LogInfo("Receive Closed");
            }
            catch (Exception ex)
            {
                Log4U.LogError(ex.Message + "\n " + ex.StackTrace + "\n" + ex.Source);
            }


            // Begin Read //
            int position = 0;

            while (position < bytesRead)
            {
                int             bufferSize     = MiniConverter.BytesToInt(_receiveBuffer, position + HEAD_SIZE * 0);
                ENetworkMessage networkMessage = (ENetworkMessage)MiniConverter.BytesToInt(_receiveBuffer, position + HEAD_SIZE * 1);

                byte[] msgIDBytes = new byte[HEAD_SIZE];
                for (int i = 0; i < HEAD_SIZE; i++)
                {
                    msgIDBytes[i] = _receiveBuffer[position + HEAD_SIZE * 2 + i];
                }
                string msgID = BitConverter.ToString(msgIDBytes);

                if (networkMessage != ENetworkMessage.KEEP_ALIVE_SYNC)
                {
                    Log4U.LogInfo("networkMessage : " + networkMessage, "msgID : " + msgID, "bufferSize : " + bufferSize);
                }

                if (position + bufferSize > bytesRead)
                {
                    Log4U.LogError("Error receive packet, packet is too long : " + bufferSize);
                    break;
                }

                IExtensible rspPacket = UnPackTool.UnPack(networkMessage, position + HEAD_SIZE * HEAD_NUM, bufferSize - HEAD_NUM * HEAD_SIZE, _receiveBuffer);
                if (rspPacket == null)
                {
                    continue;
                }

                MessageArgs args = new MessageArgs
                {
                    iMessageType = (uint)networkMessage,
                    kParam       = rspPacket,
                };

                NetworkMessageParam networkParam = new NetworkMessageParam
                {
                    rsp   = rspPacket,
                    msgID = msgID,
                };

                lock (_msgIDDict)
                {
                    if (_msgIDDict.ContainsKey(msgID))
                    {
                        networkParam.req = _msgIDDict[msgID];
                    }

                    if (_needReqMessageType.Contains(networkMessage))
                    {
                        args.kParam = networkParam;
                    }

                    if (_forcePushMessageType.Contains(networkMessage) || _msgIDDict.ContainsKey(msgID))
                    {
                        MessageDispatcher.GetInstance().DispatchMessageAsync(args.iMessageType, args.kParam);
                    }

                    if (_msgIDDict.ContainsKey(msgID))
                    {
                        RemoveMsgID(msgID);
                    }
                }

                //if (_forcePushMessageType.Contains(networkMessage))
                //{
                //    DoBeginSendPacket(networkMessage, msgIDBytes);
                //}

                position += bufferSize;
            }

            Array.Clear(_receiveBuffer, 0, _socket.ReceiveBufferSize);

            BeginReceivePacket();
        }