/// <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; }
/// <summary> /// Append crc code. If length > 144 use crc-16 else use crc-8. /// </summary> public override void AppendCrc() { if (bytes.Length > 144) { CRC16 crc = new CRC16(bytes, false); crc.CRC_16(); bytes = crc.AppendCRC(); } else { CRC8 crc = new CRC8(bytes, false); crc.CRC_8(); bytes = crc.AppendCRC(); } }
/// <summary> /// Append crc code. If length > 144 use crc-16 else use crc-8. /// </summary> public override void AppendCrc() { if (bytes.Length > 144) { CRC16 crc = new CRC16(bytes, false); if (ClientLauncher.Instance.EnableCrc16) { crc.CRC_16(); } bytes = crc.AppendCRC(); } else { CRC8 crc = new CRC8(bytes, false); if (ClientLauncher.Instance.EnableCrc8) { crc.CRC_8(); } bytes = crc.AppendCRC(); } }
///// <summary> ///// 向指定的Player发送协议 ///// </summary> ///// <param name="player"></param> ///// <param name="protocolBase"></param> //public static void SendToPlayer(Player player, ProtocolBase protocolBase) //{ // if (ServNet.instance != null) // ServNet.instance.SendToPlayer(player, protocolBase); // else NullServNet(); // return; //} #region CRC public static bool CRC8(byte[] data) { CRC8 j = new CRC8(data, CRC_op.Judge); return(!j.IsError()); }