Пример #1
0
        private void ReceiveCRC32(byte data)
        {
            if (_receivedDataCounter == 0)
            {
                _logger.Log($"Receiving CRC32...", LoggingLevel.Debug);
                _receiverDataIndex = 0;
                _receivedCRC32     = 0;
            }

            _receivedCRC32 = (_receivedCRC32 << 8) + data;

            _receiverDataIndex++;

            Console.Write($"\rReceived [{_receiverDataIndex} / 4] CRC32 bytes");

            if (_receiverDataIndex == 4)
            {
                Console.WriteLine();

                var calculatedCRC32 = CRC32.CalculateCRC32(_receiverDataBuffer);

                if (calculatedCRC32 == _receivedCRC32)
                {
                    _logger.Log($"CRC32 match. Local:{calculatedCRC32} Remote:{_receivedCRC32}", LoggingLevel.Debug);
                    _serialPort.BaseStream.WriteByte(Flags.CRC32Match);
                    _localDisk.WriteSectors(_receiverDataBuffer.Length, (int)_receivedSectorIndex, _receiverDataBuffer);
                    _state = ReceiverState.ReceiveStartMagic;
                }

                else
                {
                    _logger.Log($"CRC32 mismatch. Local:{calculatedCRC32} Remote:{_receivedCRC32}", LoggingLevel.Debug);
                    _serialPort.BaseStream.WriteByte(Flags.CRC32Mismatch);
                    _state = ReceiverState.ReceiveData;
                }

                _receivedDataCounter = -1;

                _logger.Log($"Receiver state: {_state}", LoggingLevel.Debug);
            }
        }
Пример #2
0
        private void SendData()
        {
            _logger.Log("Sending data...", LoggingLevel.Info);

            if (_receivedSectorCount == 1)
            {
                _logger.Log("Reading sector " + _receivedSectorIndex, LoggingLevel.Debug);
            }
            else
            {
                _logger.Log("Reading sectors " + _receivedSectorIndex + " - " + (_receivedSectorIndex + _receivedSectorCount - 1), LoggingLevel.Debug);
            }

            byte[] sendDataBuffer = _localDisk.ReadSectors(Convert.ToInt32(_receivedSectorIndex), Convert.ToInt32(_receivedSectorCount));

            UInt32 crc32Checksum = CRC32.CalculateCRC32(sendDataBuffer);

            SerialFlags serialFlags = SerialFlags.None;

            if (_compressionIsEnabled)
            {
                serialFlags |= SerialFlags.Compression;
            }

            _logger.Log($"Sending serial flags: {serialFlags}...", LoggingLevel.Debug);
            _serialPort.BaseStream.WriteByte(Convert.ToByte(serialFlags));

            var numUncompressedBytes = sendDataBuffer.Length;

            string sendingMessage = $"Sending {numUncompressedBytes} bytes";

            if (serialFlags.HasFlag(SerialFlags.Compression))
            {
                sendDataBuffer = Utilities.LZ4.CompressAsStandardLZ4Block(sendDataBuffer);

                sendingMessage = $"Sending {sendDataBuffer.Length} bytes";

                _transferStartDateTime = DateTime.Now;

                byte[] dataLenBuffer = new byte[4];
                dataLenBuffer[0] = (byte)((sendDataBuffer.Length >> 24) & 0xff);
                dataLenBuffer[1] = (byte)((sendDataBuffer.Length >> 16) & 0xff);
                dataLenBuffer[2] = (byte)((sendDataBuffer.Length >> 8) & 0xff);
                dataLenBuffer[3] = (byte)(sendDataBuffer.Length & 0xff);

                float percentageOfOriginalSize = (100 / (float)numUncompressedBytes) * sendDataBuffer.Length;

                _logger.Log($"Compression: { percentageOfOriginalSize.ToString("00.00")}% of { numUncompressedBytes} bytes", LoggingLevel.Debug);

                _serialPort.BaseStream.Write(dataLenBuffer, 0, dataLenBuffer.Length);
            }

            _logger.Log(sendingMessage, LoggingLevel.Info);

            for (int i = 0; i < sendDataBuffer.Length; i++)
            {
                _serialPort.BaseStream.WriteByte(sendDataBuffer[i]);
                string percentSent = ((Convert.ToDecimal(i + 1) / sendDataBuffer.Length) * 100).ToString("00.00");
                Console.Write($"\rSent [{(i + 1).ToString("D" + sendDataBuffer.Length.ToString().Length)} / {sendDataBuffer.Length} Bytes] {percentSent}% ");
            }

            Console.WriteLine();

            byte[] crc32Buffer = new byte[4];
            crc32Buffer[0] = (byte)((crc32Checksum >> 24) & 0xff);
            crc32Buffer[1] = (byte)((crc32Checksum >> 16) & 0xff);
            crc32Buffer[2] = (byte)((crc32Checksum >> 8) & 0xff);
            crc32Buffer[3] = (byte)(crc32Checksum & 0xff);

            _logger.Log("Sending CRC32...", LoggingLevel.Debug);

            _serialPort.BaseStream.Write(crc32Buffer, 0, crc32Buffer.Length);

            _state = ReceiverState.ReceiveStartMagic;

            _logger.Log($"Receiver state: {_state}", LoggingLevel.Debug);
        }
Пример #3
0
 public void CalculateCRC32ForBytes(byte[] dataBytes, uint expectedCrc32Checksum)
 {
     Assert.AreEqual(expectedCrc32Checksum, CRC32.CalculateCRC32(dataBytes));
 }
Пример #4
0
 public void CalculateCRC32ForString(string dataString, uint expectedCrc32Checksum)
 {
     byte[] dataBytes = Encoding.ASCII.GetBytes(dataString);
     Assert.AreEqual(expectedCrc32Checksum, CRC32.CalculateCRC32(dataBytes));
 }
Пример #5
0
        public void CalculateCRC32ForLargeSector()
        {
            uint crc32 = CRC32.CalculateCRC32(Enumerable.Repeat((byte)0xFF, 0x2000).ToArray());

            Assert.AreEqual(0x7A4A44C9, crc32);
        }