Ejemplo n.º 1
0
        /// <summary>
        /// 处理信息,管理接收到的数据
        /// </summary>
        private void DataProcessor()
        {
            //isConnect = client.Connected;
            //如果小于存储长度的数据长度,则返回
            if (start < sizeof(short))
            {
                return;
            }
            length = BitConverter.ToInt16(buffer, 0);

            //如果没接收完毕返回
            if (start < SF.SHORT_SIZE + length)
            {
                return;
            }

            ProtocolBase protocol = proto.Decode(buffer, SF.SHORT_SIZE, length);

            //Debug.Log();

            //todo this is to deal with protocol
            bool cor = false;

            //length
            if (length >= 144)
            {
                CRC16 crc16 = new CRC16(protocol.Encode(), true);
                crc16.CRC_16();
                cor = crc16.IsCorrect();
            }
            else
            {
                CRC8 crc8 = new CRC8(protocol.Encode(), CRC_op.Judge);
                //Debug.Log("CRC8");
                cor = crc8.IsCorrect();
            }

            if (cor)
            {
                //Add handler and handle.
                //Debug.Log(protocol.GetName() + " is cor");
                EventHandler.GetEventHandler().AddProtocol(protocol);
            }
            else
            {
                //Debug.Log("CRC failed");
            }

            //Operations for protocol
            int count = start - SF.SHORT_SIZE - length;

            Array.Copy(buffer, length + SF.SHORT_SIZE, buffer, 0, count);
            //Debug.Log(string.Format("start = {0}, count = {1}, length = {2}", start, count, length));
            start = count;
            if (count > 0)
            {
                DataProcessor();
            }
            return;
        }
Ejemplo n.º 2
0
        public static bool CRC16(byte[] data)
        {
            CRC16 j = new CRC16(data);

            return(j.IsCorrect());
        }