Пример #1
0
        private void CheckInput()
        {
            if (!input.HasBytes(12))
            {
                return;
            }

            int packetLength = (int)input.ReadInt32();

            if (packetLength < 12)
            {
                logger.error("invalid packet length: {0}", packetLength);
                return;
            }

            while (input.HasBytes(packetLength))
            {
                uint   len           = input.GetInt32();
                uint   seq           = input.GetInt32();
                byte[] packet        = input.GetBytes(packetLength - 12);
                int    checksum      = (int)input.GetInt32();
                int    validChecksum = input.Crc32(-packetLength, packetLength - 4);

                if (checksum != validChecksum)
                {
                    logger.warning("invalid checksum! skip");
                    continue;
                }

                try {
                    OnInputEvent(this, packet);
                } catch (Exception ex) {
                    logger.error("OnReceive error {0}", ex);
                }

                if (input.HasBytes(4))
                {
                    packetLength = (int)input.ReadInt32();
                }
                else
                {
                    break;
                }
            }
        }
Пример #2
0
        private void CheckInput()
        {
            if (!input.HasBytes(12))
            {
                return;
            }

            int packetLength = (int)input.ReadInt32();

            if (packetLength < 12)
            {
                logger.error("invalid packet length: {0}", packetLength);
                return;
            }

            while (input.HasBytes(packetLength))
            {
                uint   len           = input.GetInt32();
                uint   seq           = input.GetInt32();
                byte[] packet        = input.GetBytes(packetLength - 12);
                int    checksum      = (int)input.GetInt32();
                int    validChecksum = input.Crc32(-packetLength, packetLength - 4);

                if (checksum != validChecksum)
                {
                    logger.warning("invalid checksum! skip");
                    continue;
                }

                try {
                    OnReceive(packet);
                } catch (Exception ex) {
                    logger.error("OnReceive error {0}", ex);
                }

                if (input.HasBytes(4))
                {
                    packetLength = (int)input.ReadInt32();
                }
                else
                {
                    break;
                }
            }

            /*
             * //logger.debug("check input started");
             * if (inputStream.Length < 12) {
             *  //logger.debug("input too short");
             *  return;
             * }
             *
             * inputStream.Seek(0, SeekOrigin.Begin);
             * BinaryReader binaryReader = new BinaryReader(inputStream);
             * byte[] buffer = inputStream.GetBuffer();
             *
             * int packetLength = ReadInt(buffer, inputStream.Position);
             * //logger.debug("readed packet length: {0}, buffer size: {1}", packetLength, inputStream.Length - inputStream.Position);
             *
             * while (inputStream.Length - inputStream.Position >= packetLength) {
             *  //logger.info("new packet success");
             *  int len = binaryReader.ReadInt32();
             *  int seq = binaryReader.ReadInt32();
             *  byte[] packet = binaryReader.ReadBytes(packetLength - 12);
             *  byte[] checksum = binaryReader.ReadBytes(4);
             *
             *
             *  Crc32 crc32 = new Crc32();
             *  byte[] validChecksum = crc32.ComputeHash(inputStream.GetBuffer(), (int)inputStream.Position - 4 - packet.Length - 8, 8 + packet.Length).Reverse().ToArray();
             *
             *  //logger.debug("readed new network packet: len {0}, seq {1}, packet data size {2}, checksum {3}, valid checksum: {4}", len, seq, packet.Length, BitConverter.ToString(checksum).Replace("-",""), BitConverter.ToString(validChecksum).Replace("-",""));
             *  //logger.debug("response data: {0}", BitConverter.ToString(packet));
             *
             *  if(!checksum.SequenceEqual(validChecksum)) {
             *      logger.warning("invalid checksum! skip");
             *      continue;
             *  }
             *
             *  try {
             *      OnReceive(packet);
             *  }
             *  catch (Exception ex) {
             *      logger.error("OnReceive error {0}", ex);
             *  }
             *
             *  if (inputStream.Length - inputStream.Position < 12) {
             *      break;
             *  }
             *
             *  packetLength = ReadInt(buffer, inputStream.Position);
             * }
             *
             * long remaining = inputStream.Length - inputStream.Position;
             * if (remaining == 0) {
             *  inputStream.Seek(0, SeekOrigin.Begin);
             *  inputStream.SetLength(0);
             * } else {
             *  //Array.Copy(buffer, (int) inputStream.Position, buffer, 0,(int) remaining);
             *
             *  for (int i = 0; i < remaining; i++) {
             *      buffer[i] = buffer[inputStream.Position + i];
             *  }
             *
             *
             *  inputStream.SetLength(remaining);
             *  inputStream.Seek(0, SeekOrigin.End);
             * }*/
        }