본문 바로가기
프로그래밍

C#에서의 패킷 디코딩 요령

by 건우아빠유리남편 2010. 6. 9.
반응형


//패킷 디코딩
        public static bool DecodePacket(byte[] btBuf, out uint uCommand, out object oData, out uint uLen)
        {
            uCommand = 0;
            oData = 0;
            uLen = 0;
            ASCIIEncoding encoding = new ASCIIEncoding();
            int nOffset = 0;
            Object oResult;
            //StartCode
            AssignByteAsLength(out oResult, ref nOffset, btBuf, sizeof(uint));
            if ((uint)oResult != STARTCODE)
                return false;
            //Session No
            AssignByteAsLength(out oResult, ref nOffset, btBuf, sizeof(uint));
           
            //Length => CMD + DATA
            AssignByteAsLength(out oResult, ref nOffset, btBuf, sizeof(uint));
            uLen = (uint)oResult;
            //CMD
            AssignByteAsLength(out oResult, ref nOffset, btBuf, sizeof(uint));
            uCommand = (uint)oResult;
            if (0 < uLen - sizeof(uint)) //Length에서 CMD 길이를 뺀게 0보다 크면 -> 데이터가 남았음
            {
                //Data
                AssignByteAsLength(out oResult, ref nOffset, btBuf, uLen - sizeof(uint));
                oData = oResult;
            }
            //EndCode
            AssignByteAsLength(out oResult, ref nOffset, btBuf, sizeof(uint));
            if ((uint)oResult == ENDCODE)
                return true;
            return false;
        }

반응형

댓글