Exemple #1
0
        public bool ProcessPacket(ref byte serverMsgIndex)
        {
            if (m_LogicRecvBufQueue == null)
            {
                return(false);
            }

            //-- 判断是否有待处理的逻辑层消息缓冲数据
            LogicRecvBuffer logicRecvBuf = m_LogicRecvBufQueue.GetUsedBuf();

            if (logicRecvBuf == null)
            {
                return(true);
            }

            MemoryStream memStream = logicRecvBuf.GetMemoryStream();

            if (memStream == null)
            {
                return(false);
            }

            //-- test

            if (memStream.Length == memStream.Position)
            {
                //Console.WriteLine("memStream.Length == memStream.Position");
            }

            //-- 如果m_RemainingStream长度不为0,则之后处理的memStream的前面要加上m_RemainingStream。
            if (m_LogicRemainingBuf.Length != 0)
            {
                m_LogicRemainingBuf.Write(memStream.GetBuffer(), 0, (Int32)memStream.Length);
                Array.Clear(memStream.GetBuffer(), 0, (Int32)memStream.Length);
                memStream.Write(m_LogicRemainingBuf.GetBuffer(), 0, (Int32)m_LogicRemainingBuf.Length);
                memStream.Seek(0, SeekOrigin.Begin);
                Array.Clear(m_LogicRemainingBuf.GetBuffer(), 0, (Int32)m_LogicRemainingBuf.Length);
                m_LogicRemainingBuf.SetLength(0);
            }

            //-- memStream中的消息反序列化操作
            m_BinaryReader = new BinaryReader(memStream, System.Text.Encoding.ASCII);

            Int32      iTmpHead         = 0;
            Int32      iTmpTotalSize    = (Int32)memStream.Length;
            Int32      iTmpDeltaLenth   = 0;
            PacketID_t tmpPacketID      = (PacketID_t)PACKET_TYPE.PACKET_TYPE_NONE;
            Int16      iTmpPacketUInt32 = 0;

            while (true)
            {
                iTmpDeltaLenth = iTmpTotalSize;
                memStream.Seek(iTmpHead, SeekOrigin.Begin);
                iTmpDeltaLenth -= iTmpHead;
                UnityEngine.Debug.Log("the head is " + iTmpHead + " and the  deltaLength is " + iTmpDeltaLenth);
                //-- 如果剩余的消息长度小于Header,则放入m_RemainingStream中。等待下次帧循环来处理
                if (iTmpDeltaLenth < PacketUtil.PACKET_HEADER_SIZE)
                {
                    if (iTmpDeltaLenth > 0)
                    {
                        m_LogicRemainingBuf.Write(memStream.GetBuffer(), iTmpHead, iTmpDeltaLenth);
                    }
                    logicRecvBuf.SetUsedFlag(false);
                    break;
                }
                UnityEngine.Debug.Log("the temp delta length is " + iTmpDeltaLenth);

                iTmpPacketUInt32 = m_BinaryReader.ReadInt16();;
                //-- 消息没有接收完全。
                if (iTmpDeltaLenth < iTmpPacketUInt32)
                {
                    if (iTmpDeltaLenth > 0)
                    {
                        m_LogicRemainingBuf.Write(memStream.GetBuffer(), iTmpHead, iTmpDeltaLenth);
                    }
                    logicRecvBuf.SetUsedFlag(false);
                    Console.WriteLine("消息没有接收完全");
                    break;
                }

                try
                {
                    tmpPacketID = m_BinaryReader.ReadUInt16();
                    UnityEngine.Debug.Log("THE PACKET ID IS ====================" + tmpPacketID);
                }
                catch (System.Exception ex)
                {
                    CommonDebugLog.LogWarning("ex:=[" + ex.ToString() + "]");
                }


                Packet packet = PacketFacotry.GetInstance().CreatePacket(tmpPacketID);
                if (packet == null)
                {
                    //-- 抛出异常,根据PacketID创建Packet失败
                    //-- 略过这个出错的消息,并继续处理
                    iTmpHead += iTmpPacketUInt32;
                    CommonDebugLog.LogWarning(string.Format("CreatePacket Failed packetID = {0}", tmpPacketID));
                    //Console.WriteLine("packet == null");
                    continue;
                }

                try
                {
                    if (!packet.ReadPacketBody(m_BinaryReader, (Int16)(iTmpPacketUInt32 - 4)))
                    {
                        Console.WriteLine("packet.ReadPacketBody( m_BinaryReader) == false");
                        //-- log
                    }
                    UnityEngine.Debug.LogError("begin process packet ");
                    PacketFacotry.GetInstance().ProcessPacket(packet);

                    NetClient.GetInstance().RemoveWatingPackets((PACKET_TYPE)packet.GetPacketID());
                }
                catch (System.Exception ex)
                {
                    CommonDebugLog.LogWarning(ex.ToString());
                }

                //-- 执行成功,set iTmpHead
                iTmpHead += (Int32)(iTmpPacketUInt32);
            } //-- end_while (true)
            return(true);
        }
Exemple #2
0
        //-- 将从socket接收到的数据,拷贝到LogicRecvBuf中。
        private static void OnReceive(object sender, SocketAsyncEventArgs e)
        {
            if (e == null)
            {
                return;
            }

            if (e.LastOperation != SocketAsyncOperation.Receive)
            {
                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                return;
            }

            OnRecvObj recvData = e.UserToken as OnRecvObj;

            if (recvData == null)
            {
                return;
            }

            if (recvData.m_RecvBuffer == null)
            {
                return;
            }

            if (recvData.m_LogicRecvHandler == null)
            {
                return;
            }

            if (recvData.m_TSocket == null)
            {
                return;
            }

            LogicRecvBufQueue logicRecvBufQueue = recvData.m_LogicRecvHandler.GetLogicRecvBufQueue();

            if (logicRecvBufQueue == null)
            {
                //-- log
                return;
            }

            LogicRecvBuffer logicRecvBuf = logicRecvBufQueue.GetEmptyBuf();

            if (logicRecvBuf == null)
            {
                //-- log
                return;
            }

            MemoryStream memStream = logicRecvBuf.GetMemoryStream();

            if (memStream == null)
            {
                //-- log
                return;
            }

            if (e.BytesTransferred > 0)
            {
                //-- test
                //Console.WriteLine("e.BytesTransferred:=[" + e.BytesTransferred + "]");

                //-- 拷贝数据要从memStream的头写入。
                memStream.Seek(0, SeekOrigin.Begin);
                memStream.Write(e.Buffer, 0, e.BytesTransferred);
                memStream.Seek(0, SeekOrigin.Begin);
                logicRecvBuf.SetUsedFlag(true);
                //-- 释放recvData.m_RecvBuffer
                recvData.m_RecvBuffer.SetUsedFlag(false);
                //-- 继续接收
                recvData.m_TSocket.RecvFromSocketBuf(recvData.m_LogicRecvHandler);
            }
            else
            {
                recvData.m_RecvBuffer.SetUsedFlag(false);
                //-- log查看是否接收缓冲区满了
                CommonDebugLog.Log("Server DisConnect!!!");
                NetClient.GetInstance().Disconnect();
            }
            return;
        }