Exemplo n.º 1
0
        //这个在主线程调用.
        public void Update()
        {
            //TODO @zjm 每一帧都lock,可能存在性能问题.
            lock (m_ComunicationMem)
            {
                if (m_ComunicationMem.Length > 0)
                {
                    if (CNetWorkGlobal.Instance.IsDebug)
                    {
                        Log.info(this, "ClientNetWork : Update m_ComunicationMem.Length: " + m_ComunicationMem.Length);
                    }
                    if (m_Reader != null)
                    {
                        m_Reader.DidReadData(m_ComunicationMem.GetBuffer(), (int)(m_ComunicationMem.Length));
                    }
                    m_ComunicationMem.Clear();
                }
            }

            lock (m_networkState)
            {
                CheckSocketState();
                if (this.isStateChanged == true)
                {
                    CallBackNetState(m_networkState);
                    this.isStateChanged = false;
                }
            }
        }
Exemplo n.º 2
0
 public void Update()
 {
     lock (m_ComunicationMem)
     {
         if (m_ComunicationMem.Length > 0)
         {
             if (m_Reader != null)
             {
                 m_Reader.DidReadData(m_ComunicationMem.GetBuffer(), (int)(m_ComunicationMem.Length));
             }
             m_ComunicationMem.Clear();
         }
     }
     lock (m_eNetWorkState)
     {
         EClientNetWorkState eState = (EClientNetWorkState)m_eNetWorkState;
         if (eState > EClientNetWorkState.E_CNWS_NORMAL)
         {
             if (m_ClientSocket != null)
             {
                 ReleaseSocket();
                 CallBackNetState(eState);
                 eState = EClientNetWorkState.E_CNWS_NOT_UNABLE;
             }
         }
         else if (connectState == EClientConnectState.E_NOT_CONNECT)
         {
             connect_timeout += Time.deltaTime;
             if (connect_timeout > CONNECT_TIME_OUT)
             {
                 connect_timeout = 0;
                 DidDisconnect();
             }
         }
         else if (connectState == EClientConnectState.E_CONNECT)
         {
             // µ÷ÓÃlua½Ó¿Ú
             CallBackNetState(EClientNetWorkState.E_CNWS_NORMAL);
             connectState = EClientConnectState.E_NOTICE_CONNECT;
         }
     }
 }
Exemplo n.º 3
0
        public MemoryStreamEx MoveStream(int index)
        {
            MemoryStreamEx oldStream = (MemoryStreamEx)m_streamList[m_nActivedStreamPosition];

            if (index > 0)
            {
                if (index < oldStream.Length)
                {
                    m_nActivedStreamPosition = (m_nActivedStreamPosition + 1) % m_nMaxStreamCount;
                    MemoryStreamEx newStream = (MemoryStreamEx)m_streamList[m_nActivedStreamPosition];
                    newStream.Clear();
                    newStream.Write(oldStream.GetBuffer(), (int)index, (int)(oldStream.Length - index));
                    oldStream.Clear();
                    return(newStream);
                }
                else
                {
                    oldStream.Clear();
                }
            }
            return(oldStream);
        }
Exemplo n.º 4
0
        //-=-=-=-=-=-=-=-=-=Message Stream Format:Size(2),MsgID(4),NOTUSE(4),PB(0~)=-=-=-=-=-=-=-=-=-=-=-=-=
        void INetMessageReader.DidReadData(byte[] data, int size)
        {
            MemoryStream activedStream = m_NetBuffer.GetActivedStream();

            //Get Current Active Stream Buffer
            activedStream.Write(data, 0, size);
            byte[] nowData = activedStream.GetBuffer();

            /*for (int i = 0; i < size; ++i)
             * {
             *  Debug.Log("recv byte " + i + " : " + (int)nowData[i]);
             * }*/

            long nowStreamLength = activedStream.Length;

            //Debug.Log("nowStreamLength: " + nowStreamLength);
            while (true)
            {
                try {
                    if (m_nProgress != 4)
                    {
                        while (m_nProgress < 3)
                        {
                            int tmpvalue = 0;
                            if (nowStreamLength < (sizeof(short) + sizeof(int)))
                            //throw new Exception ("Data Not Enough");
                            {
                                if (m_nProgress != 4)
                                {
                                    m_nProgress = 0;
                                }
                                return;
                            }

                            switch (m_nProgress)
                            {
                            case 0:
                            {
                                m_nStreamSize    = BitConverter.ToInt16(nowData, 0);
                                m_nStreamSize    = IPAddress.NetworkToHostOrder(m_nStreamSize);
                                nowStreamLength -= sizeof(short);
                                //Debug.Log("size: " + m_nStreamSize);
                                break;
                            }

                            case 1:
                            {
                                tmpvalue         = BitConverter.ToInt32(nowData, 2);
                                tmpvalue         = IPAddress.NetworkToHostOrder(tmpvalue);
                                m_nMsgID         = tmpvalue;
                                nowStreamLength -= sizeof(int);
                                //Debug.Log("m_nMsgID: " + m_nMsgID);
                                break;
                            }

                            case 2:
                            {
                                tmpvalue         = BitConverter.ToInt32(nowData, 6);
                                tmpvalue         = IPAddress.NetworkToHostOrder(tmpvalue);
                                m_nDataType      = tmpvalue;
                                nowStreamLength -= sizeof(int);
                                //Debug.Log("m_nDataType: " + m_nDataType);
                                break;
                            }
                            }
                            m_nProgress++;
                        }
                        if (CheckHead())
                        {
                            m_nProgress = 4;
                        }
                        else
                        {
                            activedStream.SetLength(0);
                            //clearAll
                            return;
                        }
                    }
                    int nDataSize = m_nStreamSize - 10;
                    //Must >= 0
                    bool bReturn = false;
                    m_MsgDataBody.Clear();
                    //ClearData
                    if (nDataSize > 0)
                    {
                        //Debug.Log("nowStreamLength: " + nowStreamLength + ", nDataSize: " + nDataSize);
                        if (nowStreamLength >= nDataSize)
                        {
                            m_MsgDataBody.Write(nowData, (int)(activedStream.Length - nowStreamLength), nDataSize);
                            nowStreamLength -= nDataSize;
                        }
                        else
                        {
                            bReturn = true;
                            //Can't Process any more.
                        }
                    }
                    activedStream = m_NetBuffer.MoveStream((int)(activedStream.Length - nowStreamLength));
                    if (bReturn)
                    {
                        return;
                    }
                    else
                    {
                        try {
                            if (m_nMsgID >= 1 && m_nMsgID < 1024)
                            {
                                m_Logic.ClientHandleMessage(m_nMsgID, m_MsgDataBody);
                            }
                            else
                            {
                                /*for (int i = 0; i < m_MsgDataBody.Length; ++i)
                                 * {
                                 *  Debug.Log("msgData " + i + " : " + (int)(m_MsgDataBody.GetBuffer()[i]));
                                 * }*/

                                lua_.RawGetI(LuaAPI.LUA_REGISTRYINDEX, func_handle_ref_);
                                lua_.PushInteger(m_nMsgID);
                                userdata_ = lua_.NewUnManageMem(m_MsgDataBody.GetBuffer());
                                lua_.PushNumber((double)m_MsgDataBody.Length);
#if DEBUG
                                if (m_MsgDataBody.Length != nDataSize)
                                {
                                    Debug.LogWarning("msg length is 0" + m_nMsgID);
                                }
#endif
                                lua_.PCall(3, 0, 0);
                                userdata_.Free();
                            }
                        } catch (Exception e) {
                            Debug.LogError(e + "\nMsg " + m_nMsgID + " has error!");
                        }
                    }
                    nowData         = activedStream.GetBuffer();
                    nowStreamLength = activedStream.Length;
                    m_nProgress     = 0;
                } catch {
                    if (m_nProgress != 4)
                    {
                        m_nProgress = 0;
                    }
                    break;
                }
            }
        }