Beispiel #1
0
        //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
        byte[] INetMessageWriter.MakeStream(int msgID, byte[] data)
        {
            //
            for (int i = 0; i < 8; i++)
            {
                m_SessionByte[i] = (byte)(PingManager.Instance.sessionId >> (8 * (7 - i)));
            }

            //Debug.Log("sessionId: " + PingManager.Instance.sessionId);

            m_NotUseByte[0] = mCounter;
            m_Buffer.Clear();
            int net_msgID = IPAddress.HostToNetworkOrder(msgID);

            byte[] net_MsgID_byte = BitConverter.GetBytes(net_msgID);
            short  net_data_size  = (short)(sizeof(short) + net_MsgID_byte.Length + m_NotUseByte.Length + m_SessionByte.Length + (data != null ? data.Length : 0));

            net_data_size = IPAddress.HostToNetworkOrder(net_data_size);
            byte[] net_Data_Size_byte = BitConverter.GetBytes(net_data_size);
            m_Buffer.Write(net_Data_Size_byte, 0, net_Data_Size_byte.Length);
            m_Buffer.Write(net_MsgID_byte, 0, net_MsgID_byte.Length);
            m_Buffer.Write(m_NotUseByte, 0, m_NotUseByte.Length);
            m_Buffer.Write(m_SessionByte, 0, m_SessionByte.Length);
            mCounter++;
            if (data != null)
            {
                m_Buffer.Write(data, 0, data.Length);
            }
            return(m_Buffer.ToArray());
        }
        //private static UInt32  m_cmdSequece = 0;
        //private byte[] m_NotUseByte = new byte[4]{0,0,0,0};

        //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
        //兼容新的golang服务器修改....
        byte[] INetMessageWriter.MakeStream(proto_header header, MemoryStream data)
        {
            m_Buffer.Clear();
            //m_cmdSequece ++;

            //先header初始化
            //header.uMsgContext = m_cmdSequece;
            header.iMsgBodyLen = proto_header.MODULE_HEAD_LENGTH + (data != null ?(int)data.Length : 0);
            int net_BodyLen = IPAddress.HostToNetworkOrder(header.iMsgBodyLen);

            byte[] net_BodyLen_byte = BitConverter.GetBytes(net_BodyLen);
            m_Buffer.Write(net_BodyLen_byte, 0, net_BodyLen_byte.Length);

            int net_msgID = IPAddress.HostToNetworkOrder(header.shMsgID);

            byte[] net_MsgID_byte = BitConverter.GetBytes(net_msgID);
            m_Buffer.Write(net_MsgID_byte, 0, net_MsgID_byte.Length);

            /*
             * int net_iSubCmdID = IPAddress.HostToNetworkOrder (header.iSubCmdID);
             * byte[] net_iSubCmdID_byte = BitConverter.GetBytes(net_iSubCmdID);
             *
             * int net_uMsgContext = IPAddress.HostToNetworkOrder ((int)header.uMsgContext);
             * byte[] net_uMsgContext_byte = BitConverter.GetBytes(net_uMsgContext);
             *
             * int net_iUin = IPAddress.HostToNetworkOrder ((int)header.iUin);
             * byte[] net_iUin_byte = BitConverter.GetBytes(net_iUin);
             *
             * int net_uHostTag = IPAddress.HostToNetworkOrder ((int)header.uHostTag);
             * byte[] net_uHostTag_byte = BitConverter.GetBytes(net_uHostTag);
             *
             * int net_uClientSeq = IPAddress.HostToNetworkOrder ((int)header.uClientSeq);
             * byte[] net_uClientSeq_byte = BitConverter.GetBytes(net_uClientSeq);*/

            //body
            //int net_data_size = proto_header.HEAD_LENGTH + (data != null ?(int)data.Length : 0);
            //byte[] net_Data_Size_byte = BitConverter.GetBytes(net_data_size);

            /*
             * m_Buffer.Write(net_iSubCmdID_byte,0,net_MsgID_byte.Length);
             * m_Buffer.Write(net_uMsgContext_byte,0,net_MsgID_byte.Length);
             * m_Buffer.Write(net_iUin_byte,0,net_MsgID_byte.Length);
             * m_Buffer.Write(net_uHostTag_byte,0,net_uHostTag_byte.Length);
             * m_Buffer.Write(net_uClientSeq_byte,0,net_uClientSeq_byte.Length);*/

            //body
            if (data != null)
            {
                m_Buffer.Write(data.GetBuffer(), 0, (int)data.Length);

                if (CNetWorkGlobal.Instance.IsDebug)
                {
                    Log.info(this, "[CNetStreamWriter][MakeStream] WritingHead , module head len : " + proto_header.MODULE_HEAD_LENGTH
                             + ",  stream len(not include length 4 bytes)  " + header.iMsgBodyLen);
                }
            }

            return(m_Buffer.ToArray());
        }
Beispiel #3
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;
                }
            }
        }
Beispiel #4
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);
        }
Beispiel #5
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;
         }
     }
 }
Beispiel #6
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;
                }
            }
        }