byte[] ReadSectorAt(int sector) { disk.SeekSector(sector); return(disk.ReadSectors(1)); }
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); }