Exemple #1
0
 private void UpdateMsg()
 {
     while (true)
     {
         using (MsgDynamic msg = Network.GetMsg())
         {
             if (null != msg)
             {
                 Network.UpdateRecvMsgTime();
                 try
                 {
                     MsgManager.Instance.ProcessMsg(msg);
                 }
                 catch (Exception e)
                 {
                     string error = string.Format("Client UpdateMsg Exception: {0}", e.ToString());
                     UnityEngine.Debug.LogError(error);
                     //m_Log.Error(error);
                 }
             }
             else
             {
                 break;
             }
         }
     }
 }
Exemple #2
0
        public bool Push(MsgDynamic msg)
        {
            int nNextIdx = (m_WriteIndex + 1) % MaxMsgNum;

            if (nNextIdx == m_ReadIndex)
            {
                return(false);
            }

            m_MsgNode[m_WriteIndex] = msg;
            m_WriteIndex            = nNextIdx;
            return(true);
        }
        public void ProcessMsg(MsgDynamic msgSrc)
        {
            RecvMsgPacket.Instance.Init(msgSrc);
            RecvMsgPacket          packet     = RecvMsgPacket.Instance;
            Action <RecvMsgPacket> msgHandler = null;

            if (m_MsgHandlers.TryGetValue(msgSrc.GetMsgType(), out msgHandler))
            {
                msgHandler(packet);
                return;
            }
            else
            {
                //m_Log.InfoFormat("Receive unregister msg, id:{0} ?", msgSrc.GetMsgType());
            }

            //m_Log.DebugFormat("End of ProcessMsg Msg: Type: {0}(Base{1}/Sub{2})",
            //  msgSrc.GetMsgType(), msgSrc.GetBaseType(), msgSrc.GetSubType());
        }
Exemple #4
0
        //private int m_MsgLargeCount = 0;
        void ReceiveCallback(IAsyncResult asyncReceive)
        {
            if (m_Socket == null)
            {
                //m_Log.InfoFormat("{0}", "Network is reading data. Socket == null");
                StartReconnect();
                return;
            }
            if (m_Socket.Connected == false)
            {
                //m_Log.InfoFormat("{0}", "Network is reading data. But NOT Connected");
                StartReconnect();
                return;
            }

            try
            {
                SocketError errorCode;
                int         bytesReceived = m_Socket.EndReceive(asyncReceive, out errorCode);
                if (bytesReceived > 0)
                {
                    m_msgBuffer.RecvBytes += bytesReceived;
                    while (m_msgBuffer.RecvBytes >= sizeof(Int32))
                    {
                        m_msgBuffer.MsgLength = BitConverter.ToInt32(m_msgBuffer.Buffer, m_msgBuffer.StartPos);
                        if (m_msgBuffer.MsgLength < MsgHeader.GetHeaderLength() ||
                            m_msgBuffer.MsgLength > MsgRecvBuffer.MaxMsgLength)
                        {
                            //m_Log.ErrorFormat("ReceiveCallback: Invalid message, MsgLength = {0}", m_msgBuffer.MsgLength);
                            CloseSocket();
                            return;
                        }

                        //m_Log.DebugFormat("ReceiveCallback: Loop RecvBytes = {0} StartPos = {1} MsgLength = {2}", m_msgBuffer.RecvBytes, m_msgBuffer.StartPos, m_msgBuffer.MsgLength);

                        if (m_msgBuffer.RecvBytes >= m_msgBuffer.MsgLength)
                        {
                            MsgDynamic msg = new MsgDynamic();
                            msg.SetLength(m_msgBuffer.MsgLength);
                            ushort uType = BitConverter.ToUInt16(m_msgBuffer.Buffer, m_msgBuffer.StartPos + 4);
                            msg.SetMsgType(uType);
                            int bufferLength = (int)(msg.GetLength() - msg.GetHeaderLength());
                            if (bufferLength > 0)
                            {
                                if (bufferLength > BufferPool.BufferLength)
                                {
                                    msg.Buffer = new byte[bufferLength];
                                    //m_Log.DebugFormat("Count {0}: Network received a too long msg type {1}({2}/{3}), length {4}", ++m_MsgLargeCount, uType, msg.GetBaseType(), msg.GetSubType(), msg.GetLength());
                                }
                                else
                                {
                                    msg.Buffer = BufferPool.GetBuffer();
                                }
                                Array.Copy(m_msgBuffer.Buffer, m_msgBuffer.StartPos + msg.GetHeaderLength(), msg.Buffer, 0, bufferLength);
                            }
                            else if (bufferLength < 0)
                            {
                                //m_Log.ErrorFormat("ReceiveCallback: Invalid message, DataLength = {0}", bufferLength);
                                CloseSocket();
                                return;
                            }
                            else
                            {
                                // bufferLength == 0
                            }
                            m_msgQueue.Push(msg);

                            //ushort clientMsgType;
                            //if (m_RPCMsgs.TryGetValue(msg.GetMsgType(), out clientMsgType))
                            //{
                            //    if (m_WaitForAnswer.ContainsKey(clientMsgType))
                            //    {
                            //        m_WaitForAnswer[clientMsgType] = 0;
                            //    }
                            //}

                            m_msgBuffer.RecvBytes -= m_msgBuffer.MsgLength;
                            m_msgBuffer.StartPos  += m_msgBuffer.MsgLength;

                            if (m_msgBuffer.RecvBytes >= sizeof(Int32))
                            {
                                // 继续解析MsgLength
                                continue;
                            }
                            else if (m_msgBuffer.RecvBytes > 0)
                            {
                                m_msgBuffer.MsgLength = 0;
                                break;
                            }
                            else if (m_msgBuffer.RecvBytes == 0)
                            {
                                // 数据刚好读完,可以重新设置StartPos
                                m_msgBuffer.StartPos  = 0;
                                m_msgBuffer.MsgLength = 0;
                                break;
                            }
                            else
                            {
                                // (m_msgBuffer.RecvBytes < 0) 数据异常
                                //m_Log.ErrorFormat("ReceiveCallback: Buffer recv size error, RecvBytes = {0}, StartPos = {1}",
                                //    m_msgBuffer.RecvBytes, m_msgBuffer.StartPos);

                                CloseSocket();
                                return;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    //m_Log.DebugFormat("ReceiveCallback: Endl RecvBytes = {0} StartPos = {1} MsgLength = {2}", m_msgBuffer.RecvBytes, m_msgBuffer.StartPos, m_msgBuffer.MsgLength);

                    if (m_msgBuffer.MsgLength > MsgRecvBuffer.RecvBufferLength - m_msgBuffer.StartPos)
                    {
                        //m_Log.ErrorFormat("Debug Info: ReceiveCallback: Not enough buffer size: MsgLength {0} > (buffer total size {1} - buffer StartPos {2})",
                        //    m_msgBuffer.MsgLength, MsgRecvBuffer.RecvBufferLength, m_msgBuffer.StartPos);

                        MoveBuffer();
                    }

                    //m_Log.DebugFormat("{0}: {1}", "Network received data. Length is", bytesReceived);
                }
                else
                {
                    //m_Log.InfoFormat("Network recv byte: {0}, errorCode:{1}", bytesReceived, errorCode);
                }

                ReadData();
            }
            catch (Exception ex) // Never called, EXCEPTION WHERE ARE YOU???
            {
                m_NetworkFailState = (int)NetworkFailState.Receive;
                CloseSocket();
                //m_Log.WarnFormat("Network ReceiveCallback Exception: {0} / {1}", ex.ToString(), ex.Message);
            }
        }